Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.hardware.location; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.app.Service; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.location.IGpsGeofenceHardware; |
| 24 | import android.os.Binder; |
| 25 | import android.os.IBinder; |
| 26 | |
| 27 | /** |
| 28 | * Service that handles hardware geofencing. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public class GeofenceHardwareService extends Service { |
| 33 | private GeofenceHardwareImpl mGeofenceHardwareImpl; |
| 34 | private Context mContext; |
| 35 | |
| 36 | @Override |
| 37 | public void onCreate() { |
| 38 | mContext = this; |
| 39 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public IBinder onBind(Intent intent) { |
| 44 | return mBinder; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public boolean onUnbind(Intent intent) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void onDestroy() { |
| 54 | mGeofenceHardwareImpl = null; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | private void checkPermission(int pid, int uid, int monitoringType) { |
| 59 | if (mGeofenceHardwareImpl.getAllowedResolutionLevel(pid, uid) < |
| 60 | mGeofenceHardwareImpl.getMonitoringResolutionLevel(monitoringType)) { |
| 61 | throw new SecurityException("Insufficient permissions to access hardware geofence for" |
| 62 | + " type: " + monitoringType); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private IBinder mBinder = new IGeofenceHardware.Stub() { |
| 67 | public void setGpsGeofenceHardware(IGpsGeofenceHardware service) { |
| 68 | mGeofenceHardwareImpl.setGpsHardwareGeofence(service); |
| 69 | } |
| 70 | |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 71 | public int[] getMonitoringTypes() { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 72 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 73 | "Location Hardware permission not granted to access hardware geofence"); |
| 74 | |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 75 | return mGeofenceHardwareImpl.getMonitoringTypes(); |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 78 | public int getStatusOfMonitoringType(int monitoringType) { |
| 79 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 80 | "Location Hardware permission not granted to access hardware geofence"); |
| 81 | |
| 82 | return mGeofenceHardwareImpl.getStatusOfMonitoringType(monitoringType); |
| 83 | } |
| 84 | public boolean addCircularFence(int id, int monitoringType, double lat, double longitude, |
| 85 | double radius, int lastTransition, int monitorTransitions, int |
| 86 | notificationResponsiveness, int unknownTimer, IGeofenceHardwareCallback callback) { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 87 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 88 | "Location Hardware permission not granted to access hardware geofence"); |
| 89 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 90 | return mGeofenceHardwareImpl.addCircularFence(id, monitoringType, lat, longitude, |
| 91 | radius, lastTransition, monitorTransitions, notificationResponsiveness, |
| 92 | unknownTimer, callback); |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | public boolean removeGeofence(int id, int monitoringType) { |
| 96 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 97 | "Location Hardware permission not granted to access hardware geofence"); |
| 98 | |
| 99 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
| 100 | return mGeofenceHardwareImpl.removeGeofence(id, monitoringType); |
| 101 | } |
| 102 | |
| 103 | public boolean pauseGeofence(int id, int monitoringType) { |
| 104 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 105 | "Location Hardware permission not granted to access hardware geofence"); |
| 106 | |
| 107 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
| 108 | return mGeofenceHardwareImpl.pauseGeofence(id, monitoringType); |
| 109 | } |
| 110 | |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 111 | public boolean resumeGeofence(int id, int monitoringType, int monitorTransitions) { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 112 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 113 | "Location Hardware permission not granted to access hardware geofence"); |
| 114 | |
| 115 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 116 | return mGeofenceHardwareImpl.resumeGeofence(id, monitoringType, monitorTransitions); |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public boolean registerForMonitorStateChangeCallback(int monitoringType, |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 120 | IGeofenceHardwareMonitorCallback callback) { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 121 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 122 | "Location Hardware permission not granted to access hardware geofence"); |
| 123 | |
| 124 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
| 125 | return mGeofenceHardwareImpl.registerForMonitorStateChangeCallback(monitoringType, |
| 126 | callback); |
| 127 | } |
| 128 | |
| 129 | public boolean unregisterForMonitorStateChangeCallback(int monitoringType, |
Jaikumar Ganesh | da65089 | 2013-04-17 12:19:10 -0700 | [diff] [blame^] | 130 | IGeofenceHardwareMonitorCallback callback) { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 131 | mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE, |
| 132 | "Location Hardware permission not granted to access hardware geofence"); |
| 133 | |
| 134 | checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType); |
| 135 | return mGeofenceHardwareImpl.unregisterForMonitorStateChangeCallback(monitoringType, |
| 136 | callback); |
| 137 | } |
| 138 | }; |
| 139 | } |