GeofenceHardwareService.java revision da6508954a492f3dd4397e70e4fa08ee54bd2741
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
17package android.hardware.location;
18
19import android.Manifest;
20import android.app.Service;
21import android.content.Context;
22import android.content.Intent;
23import android.location.IGpsGeofenceHardware;
24import android.os.Binder;
25import android.os.IBinder;
26
27/**
28 * Service that handles hardware geofencing.
29 *
30 * @hide
31 */
32public 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
71        public int[] getMonitoringTypes() {
72            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
73                    "Location Hardware permission not granted to access hardware geofence");
74
75            return mGeofenceHardwareImpl.getMonitoringTypes();
76        }
77
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) {
87            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
88                    "Location Hardware permission not granted to access hardware geofence");
89            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
90            return mGeofenceHardwareImpl.addCircularFence(id, monitoringType, lat, longitude,
91                    radius, lastTransition, monitorTransitions, notificationResponsiveness,
92                    unknownTimer, callback);
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
111        public boolean resumeGeofence(int id, int monitoringType, int monitorTransitions) {
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);
116            return mGeofenceHardwareImpl.resumeGeofence(id, monitoringType, monitorTransitions);
117        }
118
119        public boolean registerForMonitorStateChangeCallback(int monitoringType,
120                IGeofenceHardwareMonitorCallback callback) {
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,
130                IGeofenceHardwareMonitorCallback callback) {
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}
140