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.IFusedGeofenceHardware;
24 import android.location.IGpsGeofenceHardware;
25 import android.os.Binder;
26 import android.os.IBinder;
27 
28 /**
29  * Service that handles hardware geofencing.
30  *
31  * @hide
32  */
33 public class GeofenceHardwareService extends Service {
34     private GeofenceHardwareImpl mGeofenceHardwareImpl;
35     private Context mContext;
36 
37     @Override
onCreate()38     public void onCreate() {
39         mContext = this;
40         mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext);
41     }
42 
43     @Override
onBind(Intent intent)44     public IBinder onBind(Intent intent) {
45         return mBinder;
46     }
47 
48     @Override
onUnbind(Intent intent)49     public boolean onUnbind(Intent intent) {
50         return false;
51     }
52 
53     @Override
onDestroy()54     public void onDestroy() {
55         mGeofenceHardwareImpl = null;
56     }
57 
58 
checkPermission(int pid, int uid, int monitoringType)59     private void checkPermission(int pid, int uid, int monitoringType) {
60         if (mGeofenceHardwareImpl.getAllowedResolutionLevel(pid, uid) <
61                 mGeofenceHardwareImpl.getMonitoringResolutionLevel(monitoringType)) {
62             throw new SecurityException("Insufficient permissions to access hardware geofence for"
63                     + " type: " + monitoringType);
64         }
65     }
66 
67     private IBinder mBinder = new IGeofenceHardware.Stub() {
68         @Override
69         public void setGpsGeofenceHardware(IGpsGeofenceHardware service) {
70             mGeofenceHardwareImpl.setGpsHardwareGeofence(service);
71         }
72 
73         @Override
74         public void setFusedGeofenceHardware(IFusedGeofenceHardware service) {
75             mGeofenceHardwareImpl.setFusedGeofenceHardware(service);
76         }
77 
78         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
79         @Override
80         public int[] getMonitoringTypes() {
81 
82             super.getMonitoringTypes_enforcePermission();
83 
84             return mGeofenceHardwareImpl.getMonitoringTypes();
85         }
86 
87         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
88         @Override
89         public int getStatusOfMonitoringType(int monitoringType) {
90 
91             super.getStatusOfMonitoringType_enforcePermission();
92 
93             return mGeofenceHardwareImpl.getStatusOfMonitoringType(monitoringType);
94         }
95 
96         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
97         @Override
98         public boolean addCircularFence(
99                 int monitoringType,
100                 GeofenceHardwareRequestParcelable request,
101                 IGeofenceHardwareCallback callback) {
102             super.addCircularFence_enforcePermission();
103 
104             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
105             return mGeofenceHardwareImpl.addCircularFence(monitoringType, request, callback);
106         }
107 
108         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
109         @Override
110         public boolean removeGeofence(int id, int monitoringType) {
111 
112             super.removeGeofence_enforcePermission();
113 
114             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
115             return mGeofenceHardwareImpl.removeGeofence(id, monitoringType);
116         }
117 
118         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
119         @Override
120         public boolean pauseGeofence(int id, int monitoringType) {
121 
122             super.pauseGeofence_enforcePermission();
123 
124             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
125             return mGeofenceHardwareImpl.pauseGeofence(id, monitoringType);
126         }
127 
128         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
129         @Override
130         public boolean resumeGeofence(int id, int monitoringType, int monitorTransitions) {
131 
132             super.resumeGeofence_enforcePermission();
133 
134             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
135             return mGeofenceHardwareImpl.resumeGeofence(id, monitoringType, monitorTransitions);
136         }
137 
138         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
139         @Override
140         public boolean registerForMonitorStateChangeCallback(int monitoringType,
141                 IGeofenceHardwareMonitorCallback callback) {
142 
143             super.registerForMonitorStateChangeCallback_enforcePermission();
144 
145             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
146             return mGeofenceHardwareImpl.registerForMonitorStateChangeCallback(monitoringType,
147                     callback);
148         }
149 
150         @android.annotation.EnforcePermission(android.Manifest.permission.LOCATION_HARDWARE)
151         @Override
152         public boolean unregisterForMonitorStateChangeCallback(int monitoringType,
153                 IGeofenceHardwareMonitorCallback callback) {
154 
155             super.unregisterForMonitorStateChangeCallback_enforcePermission();
156 
157             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
158             return mGeofenceHardwareImpl.unregisterForMonitorStateChangeCallback(monitoringType,
159                     callback);
160         }
161     };
162 }
163