GeofenceHardwareService.java revision 8ce470dd4ba0608abb6b5eae117cefca927af96b
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[] getMonitoringTypesAndStatus() {
72            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
73                    "Location Hardware permission not granted to access hardware geofence");
74
75            return mGeofenceHardwareImpl.getMonitoringTypesAndStatus();
76        }
77
78        public boolean addCircularFence(int id, double lat, double longitude, double radius,
79                int lastTransition, int monitorTransitions, int
80                notificationResponsiveness, int unknownTimer, int monitoringType,
81                IGeofenceHardwareCallback callback) {
82            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
83                    "Location Hardware permission not granted to access hardware geofence");
84            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
85            return mGeofenceHardwareImpl.addCircularFence(id, lat, longitude, radius,
86                    lastTransition, monitorTransitions, notificationResponsiveness, unknownTimer,
87                    monitoringType, callback);
88        }
89
90        public boolean removeGeofence(int id, int monitoringType) {
91            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
92                    "Location Hardware permission not granted to access hardware geofence");
93
94            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
95            return mGeofenceHardwareImpl.removeGeofence(id, monitoringType);
96        }
97
98        public boolean pauseGeofence(int id, int monitoringType) {
99            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
100                    "Location Hardware permission not granted to access hardware geofence");
101
102            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
103            return mGeofenceHardwareImpl.pauseGeofence(id, monitoringType);
104        }
105
106        public boolean resumeGeofence(int id, int monitorTransitions, int monitoringType) {
107            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
108                    "Location Hardware permission not granted to access hardware geofence");
109
110            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
111            return mGeofenceHardwareImpl.resumeGeofence(id, monitorTransitions, monitoringType);
112        }
113
114        public boolean registerForMonitorStateChangeCallback(int monitoringType,
115                IGeofenceHardwareCallback callback) {
116            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
117                    "Location Hardware permission not granted to access hardware geofence");
118
119            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
120            return mGeofenceHardwareImpl.registerForMonitorStateChangeCallback(monitoringType,
121                    callback);
122        }
123
124        public boolean unregisterForMonitorStateChangeCallback(int monitoringType,
125                IGeofenceHardwareCallback callback) {
126            mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
127                    "Location Hardware permission not granted to access hardware geofence");
128
129            checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
130            return mGeofenceHardwareImpl.unregisterForMonitorStateChangeCallback(monitoringType,
131                    callback);
132        }
133    };
134}
135