• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.location;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * A class indicating the application criteria for selecting a location provider. Providers may be
31  * ordered according to accuracy, power usage, ability to report altitude, speed, bearing, and
32  * monetary cost.
33  *
34  * @deprecated Criteria based APIs are deprecated because they cannot fully capture the complexity
35  *             of location providers, and encourage bad development practices. Prefer to explicit
36  *             select the location provider of interest ({@link LocationManager#FUSED_PROVIDER},
37  *             {@link LocationManager#GPS_PROVIDER, etc}), rather than relying on Criteria.
38  */
39 @Deprecated
40 public class Criteria implements Parcelable {
41 
42     /** @hide */
43     @Retention(RetentionPolicy.SOURCE)
44     @IntDef({NO_REQUIREMENT, POWER_LOW, POWER_MEDIUM, POWER_HIGH})
45     public @interface PowerRequirement {
46     }
47 
48     /** @hide */
49     @Retention(RetentionPolicy.SOURCE)
50     @IntDef({NO_REQUIREMENT, ACCURACY_LOW, ACCURACY_MEDIUM, ACCURACY_HIGH})
51     public @interface AccuracyRequirement {
52     }
53 
54     /** @hide */
55     @Retention(RetentionPolicy.SOURCE)
56     @IntDef({NO_REQUIREMENT, ACCURACY_FINE, ACCURACY_COARSE})
57     public @interface LocationAccuracyRequirement {
58     }
59 
60     /**
61      * A constant indicating that the application does not choose to
62      * place requirement on a particular feature.
63      */
64     public static final int NO_REQUIREMENT = 0;
65 
66     /**
67      * A constant indicating a low power requirement.
68      */
69     public static final int POWER_LOW = 1;
70 
71     /**
72      * A constant indicating a medium power requirement.
73      */
74     public static final int POWER_MEDIUM = 2;
75 
76     /**
77      * A constant indicating a high power requirement.
78      */
79     public static final int POWER_HIGH = 3;
80 
81     /**
82      * A constant indicating a finer location accuracy requirement
83      */
84     public static final int ACCURACY_FINE = 1;
85 
86     /**
87      * A constant indicating an approximate accuracy requirement
88      */
89     public static final int ACCURACY_COARSE = 2;
90 
91     /**
92      * A constant indicating a low location accuracy requirement
93      * - may be used for horizontal, altitude, speed or bearing accuracy.
94      * For horizontal and vertical position this corresponds roughly to
95      * an accuracy of greater than 500 meters.
96      */
97     public static final int ACCURACY_LOW = 1;
98 
99     /**
100      * A constant indicating a medium accuracy requirement
101      * - currently used only for horizontal accuracy.
102      * For horizontal position this corresponds roughly to to an accuracy
103      * of between 100 and 500 meters.
104      */
105     public static final int ACCURACY_MEDIUM = 2;
106 
107     /**
108      * a constant indicating a high accuracy requirement
109      * - may be used for horizontal, altitude, speed or bearing accuracy.
110      * For horizontal and vertical position this corresponds roughly to
111      * an accuracy of less than 100 meters.
112      */
113     public static final int ACCURACY_HIGH = 3;
114 
115     private int mHorizontalAccuracy = NO_REQUIREMENT;
116     private int mVerticalAccuracy = NO_REQUIREMENT;
117     private int mSpeedAccuracy = NO_REQUIREMENT;
118     private int mBearingAccuracy = NO_REQUIREMENT;
119     private int mPowerRequirement = NO_REQUIREMENT;
120     private boolean mAltitudeRequired = false;
121     private boolean mBearingRequired = false;
122     private boolean mSpeedRequired = false;
123     private boolean mCostAllowed = false;
124 
125     /**
126      * Constructs a new Criteria object.  The new object will have no
127      * requirements on accuracy, power, or response time; will not
128      * require altitude, speed, or bearing; and will not allow monetary
129      * cost.
130      */
Criteria()131     public Criteria() {
132     }
133 
134     /**
135      * Constructs a new Criteria object that is a copy of the given criteria.
136      */
Criteria(Criteria criteria)137     public Criteria(Criteria criteria) {
138         mHorizontalAccuracy = criteria.mHorizontalAccuracy;
139         mVerticalAccuracy = criteria.mVerticalAccuracy;
140         mSpeedAccuracy = criteria.mSpeedAccuracy;
141         mBearingAccuracy = criteria.mBearingAccuracy;
142         mPowerRequirement = criteria.mPowerRequirement;
143         mAltitudeRequired = criteria.mAltitudeRequired;
144         mBearingRequired = criteria.mBearingRequired;
145         mSpeedRequired = criteria.mSpeedRequired;
146         mCostAllowed = criteria.mCostAllowed;
147     }
148 
149     /**
150      * Indicates the desired horizontal accuracy (latitude and longitude). Accuracy may be
151      * {@link #ACCURACY_LOW}, {@link #ACCURACY_MEDIUM}, {@link #ACCURACY_HIGH} or
152      * {@link #NO_REQUIREMENT}. More accurate location may consume more power and may take longer.
153      *
154      * @throws IllegalArgumentException if accuracy is not one of the supported constants
155      */
setHorizontalAccuracy(@ccuracyRequirement int accuracy)156     public void setHorizontalAccuracy(@AccuracyRequirement int accuracy) {
157         mHorizontalAccuracy = Preconditions.checkArgumentInRange(accuracy, NO_REQUIREMENT,
158                 ACCURACY_HIGH, "accuracy");
159     }
160 
161     /**
162      * Returns a constant indicating the desired horizontal accuracy (latitude and longitude).
163      *
164      * @see #setHorizontalAccuracy(int)
165      */
166     @AccuracyRequirement
getHorizontalAccuracy()167     public int getHorizontalAccuracy() {
168         return mHorizontalAccuracy;
169     }
170 
171     /**
172      * Indicates the desired vertical accuracy (altitude). Accuracy may be {@link #ACCURACY_LOW},
173      * {@link #ACCURACY_MEDIUM}, {@link #ACCURACY_HIGH} or {@link #NO_REQUIREMENT}. More accurate
174      * location may consume more power and may take longer.
175      *
176      * @throws IllegalArgumentException if accuracy is not one of the supported constants
177      */
setVerticalAccuracy(@ccuracyRequirement int accuracy)178     public void setVerticalAccuracy(@AccuracyRequirement int accuracy) {
179         mVerticalAccuracy = Preconditions.checkArgumentInRange(accuracy, NO_REQUIREMENT,
180                 ACCURACY_HIGH, "accuracy");
181     }
182 
183     /**
184      * Returns a constant indicating the desired vertical accuracy (altitude).
185      *
186      * @see #setVerticalAccuracy(int)
187      */
188     @AccuracyRequirement
getVerticalAccuracy()189     public int getVerticalAccuracy() {
190         return mVerticalAccuracy;
191     }
192 
193     /**
194      * Indicates the desired speed accuracy. Accuracy may be {@link #ACCURACY_LOW},
195      * {@link #ACCURACY_MEDIUM}, {@link #ACCURACY_HIGH}, or {@link #NO_REQUIREMENT}. More accurate
196      * location may consume more power and may take longer.
197      *
198      * @throws IllegalArgumentException if accuracy is not one of the supported constants
199      */
setSpeedAccuracy(@ccuracyRequirement int accuracy)200     public void setSpeedAccuracy(@AccuracyRequirement int accuracy) {
201         mSpeedAccuracy = Preconditions.checkArgumentInRange(accuracy, NO_REQUIREMENT, ACCURACY_HIGH,
202                 "accuracy");
203     }
204 
205     /**
206      * Returns a constant indicating the desired speed accuracy.
207      *
208      * @see #setSpeedAccuracy(int)
209      */
210     @AccuracyRequirement
getSpeedAccuracy()211     public int getSpeedAccuracy() {
212         return mSpeedAccuracy;
213     }
214 
215     /**
216      * Indicates the desired bearing accuracy. Accuracy may be {@link #ACCURACY_LOW},
217      * {@link #ACCURACY_MEDIUM}, {@link #ACCURACY_HIGH}, or {@link #NO_REQUIREMENT}. More accurate
218      * location may consume more power and may take longer.
219      *
220      * @throws IllegalArgumentException if accuracy is not one of the supported constants
221      */
setBearingAccuracy(@ccuracyRequirement int accuracy)222     public void setBearingAccuracy(@AccuracyRequirement int accuracy) {
223         mBearingAccuracy = Preconditions.checkArgumentInRange(accuracy, NO_REQUIREMENT,
224                 ACCURACY_HIGH, "accuracy");
225     }
226 
227     /**
228      * Returns a constant indicating the desired bearing accuracy.
229      *
230      * @see #setBearingAccuracy(int)
231      */
232     @AccuracyRequirement
getBearingAccuracy()233     public int getBearingAccuracy() {
234         return mBearingAccuracy;
235     }
236 
237     /**
238      * Indicates the desired accuracy for latitude and longitude. Accuracy may be
239      * {@link #ACCURACY_FINE} or {@link #ACCURACY_COARSE}. More accurate location may consume more
240      * power and may take longer.
241      *
242      * @throws IllegalArgumentException if accuracy is not one of the supported constants
243      */
setAccuracy(@ocationAccuracyRequirement int accuracy)244     public void setAccuracy(@LocationAccuracyRequirement int accuracy) {
245         Preconditions.checkArgumentInRange(accuracy, NO_REQUIREMENT, ACCURACY_COARSE, "accuracy");
246         switch (accuracy) {
247             case NO_REQUIREMENT:
248                 setHorizontalAccuracy(NO_REQUIREMENT);
249                 break;
250             case ACCURACY_FINE:
251                 setHorizontalAccuracy(ACCURACY_HIGH);
252                 break;
253             case ACCURACY_COARSE:
254                 setHorizontalAccuracy(ACCURACY_LOW);
255                 break;
256         }
257     }
258 
259     /**
260      * Returns a constant indicating desired accuracy of location.
261      *
262      * @see #setAccuracy(int)
263      */
264     @LocationAccuracyRequirement
getAccuracy()265     public int getAccuracy() {
266         if (mHorizontalAccuracy >= ACCURACY_HIGH) {
267             return ACCURACY_FINE;
268         } else {
269             return ACCURACY_COARSE;
270         }
271     }
272 
273     /**
274      * Indicates the desired maximum power requirement. The power requirement parameter may be
275      * {@link #NO_REQUIREMENT}, {@link #POWER_LOW}, {@link #POWER_MEDIUM}, or {@link #POWER_HIGH}.
276      */
setPowerRequirement(@owerRequirement int powerRequirement)277     public void setPowerRequirement(@PowerRequirement int powerRequirement) {
278         mPowerRequirement = Preconditions.checkArgumentInRange(powerRequirement, NO_REQUIREMENT,
279                 POWER_HIGH, "powerRequirement");
280     }
281 
282     /**
283      * Returns a constant indicating the desired maximum power requirement.
284      *
285      * @see #setPowerRequirement(int)
286      */
287     @PowerRequirement
getPowerRequirement()288     public int getPowerRequirement() {
289         return mPowerRequirement;
290     }
291 
292     /**
293      * Indicates whether the provider is allowed to incur monetary cost.
294      */
setCostAllowed(boolean costAllowed)295     public void setCostAllowed(boolean costAllowed) {
296         mCostAllowed = costAllowed;
297     }
298 
299     /**
300      * Returns whether the provider is allowed to incur monetary cost.
301      */
isCostAllowed()302     public boolean isCostAllowed() {
303         return mCostAllowed;
304     }
305 
306     /**
307      * Indicates whether the provider must provide altitude information. Not all fixes are
308      * guaranteed to contain such information.
309      */
setAltitudeRequired(boolean altitudeRequired)310     public void setAltitudeRequired(boolean altitudeRequired) {
311         mAltitudeRequired = altitudeRequired;
312     }
313 
314     /**
315      * Returns whether the provider must provide altitude information.
316      *
317      * @see #setAltitudeRequired(boolean)
318      */
isAltitudeRequired()319     public boolean isAltitudeRequired() {
320         return mAltitudeRequired;
321     }
322 
323     /**
324      * Indicates whether the provider must provide speed information. Not all fixes are guaranteed
325      * to contain such information.
326      */
setSpeedRequired(boolean speedRequired)327     public void setSpeedRequired(boolean speedRequired) {
328         mSpeedRequired = speedRequired;
329     }
330 
331     /**
332      * Returns whether the provider must provide speed information.
333      *
334      * @see #setSpeedRequired(boolean)
335      */
isSpeedRequired()336     public boolean isSpeedRequired() {
337         return mSpeedRequired;
338     }
339 
340     /**
341      * Indicates whether the provider must provide bearing information. Not all fixes are guaranteed
342      * to contain such information.
343      */
setBearingRequired(boolean bearingRequired)344     public void setBearingRequired(boolean bearingRequired) {
345         mBearingRequired = bearingRequired;
346     }
347 
348     /**
349      * Returns whether the provider must provide bearing information.
350      *
351      * @see #setBearingRequired(boolean)
352      */
isBearingRequired()353     public boolean isBearingRequired() {
354         return mBearingRequired;
355     }
356 
357     @NonNull
358     public static final Parcelable.Creator<Criteria> CREATOR =
359             new Parcelable.Creator<Criteria>() {
360                 @Override
361                 public Criteria createFromParcel(Parcel in) {
362                     Criteria c = new Criteria();
363                     c.mHorizontalAccuracy = in.readInt();
364                     c.mVerticalAccuracy = in.readInt();
365                     c.mSpeedAccuracy = in.readInt();
366                     c.mBearingAccuracy = in.readInt();
367                     c.mPowerRequirement = in.readInt();
368                     c.mAltitudeRequired = in.readInt() != 0;
369                     c.mBearingRequired = in.readInt() != 0;
370                     c.mSpeedRequired = in.readInt() != 0;
371                     c.mCostAllowed = in.readInt() != 0;
372                     return c;
373                 }
374 
375                 @Override
376                 public Criteria[] newArray(int size) {
377                     return new Criteria[size];
378                 }
379             };
380 
381     @Override
describeContents()382     public int describeContents() {
383         return 0;
384     }
385 
386     @Override
writeToParcel(Parcel parcel, int flags)387     public void writeToParcel(Parcel parcel, int flags) {
388         parcel.writeInt(mHorizontalAccuracy);
389         parcel.writeInt(mVerticalAccuracy);
390         parcel.writeInt(mSpeedAccuracy);
391         parcel.writeInt(mBearingAccuracy);
392         parcel.writeInt(mPowerRequirement);
393         parcel.writeInt(mAltitudeRequired ? 1 : 0);
394         parcel.writeInt(mBearingRequired ? 1 : 0);
395         parcel.writeInt(mSpeedRequired ? 1 : 0);
396         parcel.writeInt(mCostAllowed ? 1 : 0);
397     }
398 
399     @Override
toString()400     public String toString() {
401         StringBuilder s = new StringBuilder();
402         s.append("Criteria[");
403         s.append("power=").append(requirementToString(mPowerRequirement)).append(", ");
404         s.append("accuracy=").append(requirementToString(mHorizontalAccuracy));
405         if (mVerticalAccuracy != NO_REQUIREMENT) {
406             s.append(", verticalAccuracy=").append(requirementToString(mVerticalAccuracy));
407         }
408         if (mSpeedAccuracy != NO_REQUIREMENT) {
409             s.append(", speedAccuracy=").append(requirementToString(mSpeedAccuracy));
410         }
411         if (mBearingAccuracy != NO_REQUIREMENT) {
412             s.append(", bearingAccuracy=").append(requirementToString(mBearingAccuracy));
413         }
414         if (mAltitudeRequired || mBearingRequired || mSpeedRequired) {
415             s.append(", required=[");
416             if (mAltitudeRequired) {
417                 s.append("altitude, ");
418             }
419             if (mBearingRequired) {
420                 s.append("bearing, ");
421             }
422             if (mSpeedRequired) {
423                 s.append("speed, ");
424             }
425             s.setLength(s.length() - 2);
426             s.append("]");
427         }
428         if (mCostAllowed) {
429             s.append(", costAllowed");
430         }
431         s.append(']');
432         return s.toString();
433     }
434 
requirementToString(int power)435     private static String requirementToString(int power) {
436         switch (power) {
437             case NO_REQUIREMENT:
438                 return "None";
439             //case ACCURACY_LOW:
440             case POWER_LOW:
441                 return "Low";
442             //case ACCURACY_MEDIUM:
443             case POWER_MEDIUM:
444                 return "Medium";
445             //case ACCURACY_HIGH:
446             case POWER_HIGH:
447                 return "High";
448             default:
449                 return "???";
450         }
451     }
452 }
453