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