blob: 3bc70eed749ffaf986a57215ee49d0dc466a902b [file] [log] [blame]
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -07001/*
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
Jaikumar Ganeshda650892013-04-17 12:19:10 -070071 public int[] getMonitoringTypes() {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070072 mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
73 "Location Hardware permission not granted to access hardware geofence");
74
Jaikumar Ganeshda650892013-04-17 12:19:10 -070075 return mGeofenceHardwareImpl.getMonitoringTypes();
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070076 }
77
Jaikumar Ganeshda650892013-04-17 12:19:10 -070078 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 Ganesh8ce470d2013-04-03 12:22:18 -070087 mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
88 "Location Hardware permission not granted to access hardware geofence");
89 checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
Jaikumar Ganeshda650892013-04-17 12:19:10 -070090 return mGeofenceHardwareImpl.addCircularFence(id, monitoringType, lat, longitude,
91 radius, lastTransition, monitorTransitions, notificationResponsiveness,
92 unknownTimer, callback);
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070093 }
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 Ganeshda650892013-04-17 12:19:10 -0700111 public boolean resumeGeofence(int id, int monitoringType, int monitorTransitions) {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700112 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 Ganeshda650892013-04-17 12:19:10 -0700116 return mGeofenceHardwareImpl.resumeGeofence(id, monitoringType, monitorTransitions);
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700117 }
118
119 public boolean registerForMonitorStateChangeCallback(int monitoringType,
Jaikumar Ganeshda650892013-04-17 12:19:10 -0700120 IGeofenceHardwareMonitorCallback callback) {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700121 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 Ganeshda650892013-04-17 12:19:10 -0700130 IGeofenceHardwareMonitorCallback callback) {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700131 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}