1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.location;
import android.annotation.SystemApi;
import android.annotation.UnsupportedAppUsage;
import android.location.Location;
import android.os.Build;
import android.os.RemoteException;
import java.lang.ref.WeakReference;
import java.util.HashMap;
/**
* This class handles geofences managed by various hardware subsystems. It contains
* the public APIs that is needed to accomplish the task.
*
* <p>The APIs should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* <p> There are 3 states associated with a Geofence: Inside, Outside, Unknown.
* There are 3 transitions: {@link #GEOFENCE_ENTERED}, {@link #GEOFENCE_EXITED},
* {@link #GEOFENCE_UNCERTAIN}. The APIs only expose the transitions.
*
* <p> Inside state: The hardware subsystem is reasonably confident that the user is inside
* the geofence. Outside state: The hardware subsystem is reasonably confident that the user
* is outside the geofence Unknown state: Unknown state can be interpreted as a state in which the
* monitoring subsystem isn't confident enough that the user is either inside or
* outside the Geofence. If the accuracy does not improve for a sufficient period of time,
* the {@link #GEOFENCE_UNCERTAIN} transition would be triggered. If the accuracy improves later,
* an appropriate transition would be triggered. The "reasonably confident" parameter
* depends on the hardware system and the positioning algorithms used.
* For instance, {@link #MONITORING_TYPE_GPS_HARDWARE} uses 95% as a confidence level.
*
* @hide
*/
@SystemApi
public final class GeofenceHardware {
private IGeofenceHardware mService;
// Hardware systems that do geofence monitoring.
static final int NUM_MONITORS = 2;
/**
* Constant for geofence monitoring done by the GPS hardware.
*/
public static final int MONITORING_TYPE_GPS_HARDWARE = 0;
/**
* Constant for geofence monitoring done by the Fused hardware.
*/
public static final int MONITORING_TYPE_FUSED_HARDWARE = 1;
/**
* Constant to indicate that the monitoring system is currently
* available for monitoring geofences.
*/
public static final int MONITOR_CURRENTLY_AVAILABLE = 0;
/**
* Constant to indicate that the monitoring system is currently
* unavailable for monitoring geofences.
*/
public static final int MONITOR_CURRENTLY_UNAVAILABLE = 1;
/**
* Constant to indicate that the monitoring system is unsupported
* for hardware geofence monitoring.
*/
public static final int MONITOR_UNSUPPORTED = 2;
// The following constants need to match geofence flags in gps.h and fused_location.h
/**
* The constant to indicate that the user has entered the geofence.
*/
public static final int GEOFENCE_ENTERED = 1<<0L;
/**
* The constant to indicate that the user has exited the geofence.
*/
public static final int GEOFENCE_EXITED = 1<<1L;
/**
* The constant to indicate that the user is uncertain with respect to a
* geofence.
*/
public static final int GEOFENCE_UNCERTAIN = 1<<2L;
/**
* The constant used to indicate success of the particular geofence call
*/
public static final int GEOFENCE_SUCCESS = 0;
/**
* The constant used to indicate that too many geofences have been registered.
*/
public static final int GEOFENCE_ERROR_TOO_MANY_GEOFENCES = 1;
/**
* The constant used to indicate that the geofence id already exists.
*/
public static final int GEOFENCE_ERROR_ID_EXISTS = 2;
/**
* The constant used to indicate that the geofence id is unknown.
*/
public static final int GEOFENCE_ERROR_ID_UNKNOWN = 3;
/**
* The constant used to indicate that the transition requested for the geofence is invalid.
*/
public static final int GEOFENCE_ERROR_INVALID_TRANSITION = 4;
/**
* The constant used to indicate that the geofence operation has failed.
*/
public static final int GEOFENCE_FAILURE = 5;
/**
* The constant used to indicate that the operation failed due to insufficient memory.
*/
public static final int GEOFENCE_ERROR_INSUFFICIENT_MEMORY = 6;
// the following values must match the definitions in fused_location.h
/**
* The constant used to indicate that the monitoring system supports GNSS.
*/
public static final int SOURCE_TECHNOLOGY_GNSS = (1<<0);
/**
* The constant used to indicate that the monitoring system supports WiFi.
*/
public static final int SOURCE_TECHNOLOGY_WIFI = (1<<1);
/**
* The constant used to indicate that the monitoring system supports Sensors.
*/
public static final int SOURCE_TECHNOLOGY_SENSORS = (1<<2);
/**
* The constant used to indicate that the monitoring system supports Cell.
*/
public static final int SOURCE_TECHNOLOGY_CELL = (1<<3);
/**
* The constant used to indicate that the monitoring system supports Bluetooth.
*/
public static final int SOURCE_TECHNOLOGY_BLUETOOTH = (1<<4);
private HashMap<GeofenceHardwareCallback, GeofenceHardwareCallbackWrapper>
mCallbacks = new HashMap<GeofenceHardwareCallback, GeofenceHardwareCallbackWrapper>();
private HashMap<GeofenceHardwareMonitorCallback, GeofenceHardwareMonitorCallbackWrapper>
mMonitorCallbacks = new HashMap<GeofenceHardwareMonitorCallback,
GeofenceHardwareMonitorCallbackWrapper>();
/** @hide */
@UnsupportedAppUsage
public GeofenceHardware(IGeofenceHardware service) {
mService = service;
}
/**
* Returns all the hardware geofence monitoring systems which are supported
*
* <p> Call {@link #getStatusOfMonitoringType(int)} to know the current state
* of a monitoring system.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* @return An array of all the monitoring types.
* An array of length 0 is returned in case of errors.
*/
public int[] getMonitoringTypes() {
try {
return mService.getMonitoringTypes();
} catch (RemoteException e) {
}
return new int[0];
}
/**
* Returns current status of a hardware geofence monitoring system.
*
* <p>Status can be one of {@link #MONITOR_CURRENTLY_AVAILABLE},
* {@link #MONITOR_CURRENTLY_UNAVAILABLE} or {@link #MONITOR_UNSUPPORTED}
*
* <p> Some supported hardware monitoring systems might not be available
* for monitoring geofences in certain scenarios. For example, when a user
* enters a building, the GPS hardware subsystem might not be able monitor
* geofences and will change from {@link #MONITOR_CURRENTLY_AVAILABLE} to
* {@link #MONITOR_CURRENTLY_UNAVAILABLE}.
*
* @param monitoringType
* @return Current status of the monitoring type.
*/
public int getStatusOfMonitoringType(int monitoringType) {
try {
return mService.getStatusOfMonitoringType(monitoringType);
} catch (RemoteException e) {
return MONITOR_UNSUPPORTED;
}
}
/**
* Creates a circular geofence which is monitored by subsystems in the hardware.
*
* <p> When the device detects that is has entered, exited or is uncertain
* about the area specified by the geofence, the given callback will be called.
*
* <p> If this call returns true, it means that the geofence has been sent to the hardware.
* {@link GeofenceHardwareCallback#onGeofenceAdd} will be called with the result of the
* add call from the hardware. The {@link GeofenceHardwareCallback#onGeofenceAdd} will be
* called with the following parameters when a transition event occurs.
* <ul>
* <li> The geofence Id
* <li> The location object indicating the last known location.
* <li> The transition associated with the geofence. One of
* {@link #GEOFENCE_ENTERED}, {@link #GEOFENCE_EXITED}, {@link #GEOFENCE_UNCERTAIN}
* <li> The timestamp when the geofence transition occured.
* <li> The monitoring type ({@link #MONITORING_TYPE_GPS_HARDWARE} is one such example)
* that was used.
* </ul>
*
* <p> The geofence will be monitored by the subsystem specified by monitoring_type parameter.
* The application does not need to hold a wakelock when the monitoring
* is being done by the underlying hardware subsystem. If the same geofence Id is being
* monitored by two different monitoring systems, the same id can be used for both calls, as
* long as the same callback object is used.
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* <p> Create a geofence request object using the methods in {@link GeofenceHardwareRequest} to
* set all the characteristics of the geofence. Use the created GeofenceHardwareRequest object
* in this call.
*
* @param geofenceId The id associated with the geofence.
* @param monitoringType The type of the hardware subsystem that should be used
* to monitor the geofence.
* @param geofenceRequest The {@link GeofenceHardwareRequest} object associated with the
* geofence.
* @param callback {@link GeofenceHardwareCallback} that will be use to notify the
* transition.
* @return true when the geofence is successfully sent to the hardware for addition.
* @throws IllegalArgumentException when the geofence request type is not supported.
*/
public boolean addGeofence(int geofenceId, int monitoringType, GeofenceHardwareRequest
geofenceRequest, GeofenceHardwareCallback callback) {
try {
if (geofenceRequest.getType() == GeofenceHardwareRequest.GEOFENCE_TYPE_CIRCLE) {
return mService.addCircularFence(
monitoringType,
new GeofenceHardwareRequestParcelable(geofenceId, geofenceRequest),
getCallbackWrapper(callback));
} else {
throw new IllegalArgumentException("Geofence Request type not supported");
}
} catch (RemoteException e) {
}
return false;
}
/**
* Removes a geofence added by {@link #addGeofence} call.
*
* <p> If this call returns true, it means that the geofence has been sent to the hardware.
* {@link GeofenceHardwareCallback#onGeofenceRemove} will be called with the result of the
* remove call from the hardware.
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* @param geofenceId The id of the geofence.
* @param monitoringType The type of the hardware subsystem that should be used
* to monitor the geofence.
* @return true when the geofence is successfully sent to the hardware for removal. .
*/
public boolean removeGeofence(int geofenceId, int monitoringType) {
try {
return mService.removeGeofence(geofenceId, monitoringType);
} catch (RemoteException e) {
}
return false;
}
/**
* Pauses the monitoring of a geofence added by {@link #addGeofence} call.
*
* <p> If this call returns true, it means that the geofence has been sent to the hardware.
* {@link GeofenceHardwareCallback#onGeofencePause} will be called with the result of the
* pause call from the hardware.
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* @param geofenceId The id of the geofence.
* @param monitoringType The type of the hardware subsystem that should be used
* to monitor the geofence.
* @return true when the geofence is successfully sent to the hardware for pausing.
*/
public boolean pauseGeofence(int geofenceId, int monitoringType) {
try {
return mService.pauseGeofence(geofenceId, monitoringType);
} catch (RemoteException e) {
}
return false;
}
/**
* Resumes the monitoring of a geofence added by {@link #pauseGeofence} call.
*
* <p> If this call returns true, it means that the geofence has been sent to the hardware.
* {@link GeofenceHardwareCallback#onGeofenceResume} will be called with the result of the
* resume call from the hardware.
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* @param geofenceId The id of the geofence.
* @param monitoringType The type of the hardware subsystem that should be used
* to monitor the geofence.
* @param monitorTransition Bitwise OR of {@link #GEOFENCE_ENTERED},
* {@link #GEOFENCE_EXITED}, {@link #GEOFENCE_UNCERTAIN}
* @return true when the geofence is successfully sent to the hardware for resumption.
*/
public boolean resumeGeofence(int geofenceId, int monitoringType, int monitorTransition) {
try {
return mService.resumeGeofence(geofenceId, monitoringType, monitorTransition);
} catch (RemoteException e) {
}
return false;
}
/**
* Register the callback to be notified when the state of a hardware geofence
* monitoring system changes. For instance, it can change from
* {@link #MONITOR_CURRENTLY_AVAILABLE} to {@link #MONITOR_CURRENTLY_UNAVAILABLE}
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* <p> The same callback object can be used to be informed of geofence transitions
* and state changes of the underlying hardware subsystem.
*
* @param monitoringType Type of the monitor
* @param callback Callback that will be called.
* @return true on success
*/
public boolean registerForMonitorStateChangeCallback(int monitoringType,
GeofenceHardwareMonitorCallback callback) {
try {
return mService.registerForMonitorStateChangeCallback(monitoringType,
getMonitorCallbackWrapper(callback));
} catch (RemoteException e) {
}
return false;
}
/**
* Unregister the callback that was used with {@link #registerForMonitorStateChangeCallback}
* to notify when the state of the hardware geofence monitoring system changes.
*
* <p> Requires {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission when
* {@link #MONITORING_TYPE_GPS_HARDWARE} is used.
*
* <p> Requires {@link android.Manifest.permission#LOCATION_HARDWARE} permission to access
* geofencing in hardware.
*
* <p>This API should not be called directly by the app developers. A higher level api
* which abstracts the hardware should be used instead. All the checks are done by the higher
* level public API. Any needed locking should be handled by the higher level API.
*
* @param monitoringType Type of the monitor
* @param callback Callback that will be called.
* @return true on success
*/
public boolean unregisterForMonitorStateChangeCallback(int monitoringType,
GeofenceHardwareMonitorCallback callback) {
boolean result = false;
try {
result = mService.unregisterForMonitorStateChangeCallback(monitoringType,
getMonitorCallbackWrapper(callback));
if (result) removeMonitorCallback(callback);
} catch (RemoteException e) {
}
return result;
}
private void removeCallback(GeofenceHardwareCallback callback) {
synchronized (mCallbacks) {
mCallbacks.remove(callback);
}
}
private GeofenceHardwareCallbackWrapper getCallbackWrapper(GeofenceHardwareCallback callback) {
synchronized (mCallbacks) {
GeofenceHardwareCallbackWrapper wrapper = mCallbacks.get(callback);
if (wrapper == null) {
wrapper = new GeofenceHardwareCallbackWrapper(callback);
mCallbacks.put(callback, wrapper);
}
return wrapper;
}
}
private void removeMonitorCallback(GeofenceHardwareMonitorCallback callback) {
synchronized (mMonitorCallbacks) {
mMonitorCallbacks.remove(callback);
}
}
private GeofenceHardwareMonitorCallbackWrapper getMonitorCallbackWrapper(
GeofenceHardwareMonitorCallback callback) {
synchronized (mMonitorCallbacks) {
GeofenceHardwareMonitorCallbackWrapper wrapper = mMonitorCallbacks.get(callback);
if (wrapper == null) {
wrapper = new GeofenceHardwareMonitorCallbackWrapper(callback);
mMonitorCallbacks.put(callback, wrapper);
}
return wrapper;
}
}
class GeofenceHardwareMonitorCallbackWrapper extends IGeofenceHardwareMonitorCallback.Stub {
private WeakReference<GeofenceHardwareMonitorCallback> mCallback;
GeofenceHardwareMonitorCallbackWrapper(GeofenceHardwareMonitorCallback c) {
mCallback = new WeakReference<GeofenceHardwareMonitorCallback>(c);
}
public void onMonitoringSystemChange(GeofenceHardwareMonitorEvent event) {
GeofenceHardwareMonitorCallback c = mCallback.get();
if (c == null) return;
// report the legacy event first, so older clients are not broken
c.onMonitoringSystemChange(
event.getMonitoringType(),
event.getMonitoringStatus() == GeofenceHardware.MONITOR_CURRENTLY_AVAILABLE,
event.getLocation());
// and only call the updated callback on on L and above, this complies with the
// documentation of GeofenceHardwareMonitorCallback
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
c.onMonitoringSystemChange(event);
}
}
}
class GeofenceHardwareCallbackWrapper extends IGeofenceHardwareCallback.Stub {
private WeakReference<GeofenceHardwareCallback> mCallback;
GeofenceHardwareCallbackWrapper(GeofenceHardwareCallback c) {
mCallback = new WeakReference<GeofenceHardwareCallback>(c);
}
public void onGeofenceTransition(int geofenceId, int transition, Location location,
long timestamp, int monitoringType) {
GeofenceHardwareCallback c = mCallback.get();
if (c != null) {
c.onGeofenceTransition(geofenceId, transition, location, timestamp,
monitoringType);
}
}
public void onGeofenceAdd(int geofenceId, int status) {
GeofenceHardwareCallback c = mCallback.get();
if (c != null) c.onGeofenceAdd(geofenceId, status);
}
public void onGeofenceRemove(int geofenceId, int status) {
GeofenceHardwareCallback c = mCallback.get();
if (c != null) {
c.onGeofenceRemove(geofenceId, status);
removeCallback(c);
}
}
public void onGeofencePause(int geofenceId, int status) {
GeofenceHardwareCallback c = mCallback.get();
if (c != null) {
c.onGeofencePause(geofenceId, status);
}
}
public void onGeofenceResume(int geofenceId, int status) {
GeofenceHardwareCallback c = mCallback.get();
if (c != null) c.onGeofenceResume(geofenceId, status);
}
}
}
|