Home | History | Annotate | Download | only in location
      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
     38     public void onCreate() {
     39         mContext = this;
     40         mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext);
     41     }
     42 
     43     @Override
     44     public IBinder onBind(Intent intent) {
     45         return mBinder;
     46     }
     47 
     48     @Override
     49     public boolean onUnbind(Intent intent) {
     50         return false;
     51     }
     52 
     53     @Override
     54     public void onDestroy() {
     55         mGeofenceHardwareImpl = null;
     56     }
     57 
     58 
     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         public void setGpsGeofenceHardware(IGpsGeofenceHardware service) {
     69             mGeofenceHardwareImpl.setGpsHardwareGeofence(service);
     70         }
     71 
     72         public void setFusedGeofenceHardware(IFusedGeofenceHardware service) {
     73             mGeofenceHardwareImpl.setFusedGeofenceHardware(service);
     74         }
     75 
     76         public int[] getMonitoringTypes() {
     77             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
     78                     "Location Hardware permission not granted to access hardware geofence");
     79 
     80             return mGeofenceHardwareImpl.getMonitoringTypes();
     81         }
     82 
     83         public int getStatusOfMonitoringType(int monitoringType) {
     84             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
     85                     "Location Hardware permission not granted to access hardware geofence");
     86 
     87             return mGeofenceHardwareImpl.getStatusOfMonitoringType(monitoringType);
     88         }
     89         public boolean addCircularFence(int id, int monitoringType, double lat, double longitude,
     90                 double radius, int lastTransition, int monitorTransitions, int
     91                 notificationResponsiveness, int unknownTimer, IGeofenceHardwareCallback callback) {
     92             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
     93                     "Location Hardware permission not granted to access hardware geofence");
     94             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
     95             return mGeofenceHardwareImpl.addCircularFence(id, monitoringType, lat, longitude,
     96                     radius, lastTransition, monitorTransitions, notificationResponsiveness,
     97                     unknownTimer, callback);
     98         }
     99 
    100         public boolean removeGeofence(int id, int monitoringType) {
    101             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
    102                     "Location Hardware permission not granted to access hardware geofence");
    103 
    104             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
    105             return mGeofenceHardwareImpl.removeGeofence(id, monitoringType);
    106         }
    107 
    108         public boolean pauseGeofence(int id, int monitoringType) {
    109             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
    110                     "Location Hardware permission not granted to access hardware geofence");
    111 
    112             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
    113             return mGeofenceHardwareImpl.pauseGeofence(id, monitoringType);
    114         }
    115 
    116         public boolean resumeGeofence(int id, int monitoringType, int monitorTransitions) {
    117             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
    118                     "Location Hardware permission not granted to access hardware geofence");
    119 
    120             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
    121             return mGeofenceHardwareImpl.resumeGeofence(id, monitoringType, monitorTransitions);
    122         }
    123 
    124         public boolean registerForMonitorStateChangeCallback(int monitoringType,
    125                 IGeofenceHardwareMonitorCallback 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.registerForMonitorStateChangeCallback(monitoringType,
    131                     callback);
    132         }
    133 
    134         public boolean unregisterForMonitorStateChangeCallback(int monitoringType,
    135                 IGeofenceHardwareMonitorCallback callback) {
    136             mContext.enforceCallingPermission(Manifest.permission.LOCATION_HARDWARE,
    137                     "Location Hardware permission not granted to access hardware geofence");
    138 
    139             checkPermission(Binder.getCallingPid(), Binder.getCallingUid(), monitoringType);
    140             return mGeofenceHardwareImpl.unregisterForMonitorStateChangeCallback(monitoringType,
    141                     callback);
    142         }
    143     };
    144 }
    145