Criteria.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.location;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * A class indicating the application criteria for selecting a
24 * location provider.  Providers maybe ordered according to accuracy,
25 * power usage, ability to report altitude, speed,
26 * and bearing, and monetary cost.
27 */
28public class Criteria implements Parcelable {
29    /**
30     * A constant indicating that the application does not choose to
31     * place requirement on a particular feature.
32     */
33    public static final int NO_REQUIREMENT = 0;
34
35    /**
36     * A constant indicating a low power requirement.
37     */
38    public static final int POWER_LOW = 1;
39
40    /**
41     * A constant indicating a medium power requirement.
42     */
43    public static final int POWER_MEDIUM = 2;
44
45    /**
46     * A constant indicating a high power requirement.
47     */
48    public static final int POWER_HIGH = 3;
49
50    /**
51     * A constant indicating a finer location accuracy requirement
52     */
53    public static final int ACCURACY_FINE = 1;
54
55    /**
56     * A constant indicating an approximate accuracy requirement
57     */
58    public static final int ACCURACY_COARSE = 2;
59
60    private int mAccuracy              = NO_REQUIREMENT;
61    private int mPowerRequirement      = NO_REQUIREMENT;
62//    private int mPreferredResponseTime = NO_REQUIREMENT;
63    private boolean mAltitudeRequired  = false;
64    private boolean mBearingRequired   = false;
65    private boolean mSpeedRequired     = false;
66    private boolean mCostAllowed       = false;
67
68    /**
69     * Constructs a new Criteria object.  The new object will have no
70     * requirements on accuracy, power, or response time; will not
71     * require altitude, speed, or bearing; and will not allow monetary
72     * cost.
73     */
74    public Criteria() {}
75
76    /**
77     * Constructs a new Criteria object that is a copy of the given criteria.
78     */
79    public Criteria(Criteria criteria) {
80        mAccuracy = criteria.mAccuracy;
81        mPowerRequirement = criteria.mPowerRequirement;
82//        mPreferredResponseTime = criteria.mPreferredResponseTime;
83        mAltitudeRequired = criteria.mAltitudeRequired;
84        mBearingRequired = criteria.mBearingRequired;
85        mSpeedRequired = criteria.mSpeedRequired;
86        mCostAllowed = criteria.mCostAllowed;
87    }
88
89    /**
90     * Indicates the desired accuracy for latitude and longitude. Accuracy
91     * may be {@link #ACCURACY_FINE} if desired location
92     * is fine, else it can be {@link #ACCURACY_COARSE}.
93     * More accurate location usually consumes more power and may take
94     * longer.
95     *
96     * @throws IllegalArgumentException if accuracy is negative
97     */
98    public void setAccuracy(int accuracy) {
99        if (accuracy < NO_REQUIREMENT && accuracy > ACCURACY_COARSE) {
100            throw new IllegalArgumentException("accuracy=" + accuracy);
101        }
102        mAccuracy = accuracy;
103    }
104
105    /**
106     * Returns a constant indicating desired accuracy of location
107     * Accuracy may be {@link #ACCURACY_FINE} if desired location
108     * is fine, else it can be {@link #ACCURACY_COARSE}.
109     */
110    public int getAccuracy() {
111        return mAccuracy;
112    }
113
114    /**
115     * Indicates the desired maximum power level.  The level parameter
116     * must be one of NO_REQUIREMENT, POWER_LOW, POWER_MEDIUM, or
117     * POWER_HIGH.
118     */
119    public void setPowerRequirement(int level) {
120        if (level < NO_REQUIREMENT || level > POWER_HIGH) {
121            throw new IllegalArgumentException("level=" + level);
122        }
123        mPowerRequirement = level;
124    }
125
126    /**
127     * Returns a constant indicating the desired power requirement.  The
128     * returned
129     */
130    public int getPowerRequirement() {
131        return mPowerRequirement;
132    }
133
134//    /**
135//     * Indicates the preferred response time of the provider, in milliseconds.
136//     */
137//    public void setPreferredResponseTime(int time) {
138//        mPreferredResponseTime = time;
139//    }
140//
141//    /**
142//     * Returns the preferred response time of the provider, in milliseconds.
143//     */
144//    public int getPreferredResponseTime() {
145//        return mPreferredResponseTime;
146//    }
147
148    /**
149     * Indicates whether the provider is allowed to incur monetary cost.
150     */
151    public void setCostAllowed(boolean costAllowed) {
152        mCostAllowed = costAllowed;
153    }
154
155    /**
156     * Returns whether the provider is allowed to incur monetary cost.
157     */
158    public boolean isCostAllowed() {
159        return mCostAllowed;
160    }
161
162    /**
163     * Indicates whether the provider must provide altitude information.
164     * Not all fixes are guaranteed to contain such information.
165     */
166    public void setAltitudeRequired(boolean altitudeRequired) {
167        mAltitudeRequired = altitudeRequired;
168    }
169
170    /**
171     * Returns whether the provider must provide altitude information.
172     * Not all fixes are guaranteed to contain such information.
173     */
174    public boolean isAltitudeRequired() {
175        return mAltitudeRequired;
176    }
177
178    /**
179     * Indicates whether the provider must provide speed information.
180     * Not all fixes are guaranteed to contain such information.
181     */
182    public void setSpeedRequired(boolean speedRequired) {
183        mSpeedRequired = speedRequired;
184    }
185
186    /**
187     * Returns whether the provider must provide speed information.
188     * Not all fixes are guaranteed to contain such information.
189     */
190    public boolean isSpeedRequired() {
191        return mSpeedRequired;
192    }
193
194    /**
195     * Indicates whether the provider must provide bearing information.
196     * Not all fixes are guaranteed to contain such information.
197     */
198    public void setBearingRequired(boolean bearingRequired) {
199        mBearingRequired = bearingRequired;
200    }
201
202    /**
203     * Returns whether the provider must provide bearing information.
204     * Not all fixes are guaranteed to contain such information.
205     */
206    public boolean isBearingRequired() {
207        return mBearingRequired;
208    }
209
210    public static final Parcelable.Creator<Criteria> CREATOR =
211        new Parcelable.Creator<Criteria>() {
212        public Criteria createFromParcel(Parcel in) {
213            Criteria c = new Criteria();
214            c.mAccuracy = in.readInt();
215            c.mPowerRequirement = in.readInt();
216//            c.mPreferredResponseTime = in.readInt();
217            c.mAltitudeRequired = in.readInt() != 0;
218            c.mBearingRequired = in.readInt() != 0;
219            c.mSpeedRequired = in.readInt() != 0;
220            c.mCostAllowed = in.readInt() != 0;
221            return c;
222        }
223
224        public Criteria[] newArray(int size) {
225            return new Criteria[size];
226        }
227    };
228
229    public int describeContents() {
230        return 0;
231    }
232
233    public void writeToParcel(Parcel parcel, int flags) {
234        parcel.writeInt(mAccuracy);
235        parcel.writeInt(mPowerRequirement);
236//        parcel.writeInt(mPreferredResponseTime);
237        parcel.writeInt(mAltitudeRequired ? 1 : 0);
238        parcel.writeInt(mBearingRequired ? 1 : 0);
239        parcel.writeInt(mSpeedRequired ? 1 : 0);
240        parcel.writeInt(mCostAllowed ? 1 : 0);
241    }
242}
243