• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 package com.android.server.location.geofence;
17 
18 import android.annotation.Nullable;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ServiceConnection;
23 import android.hardware.location.GeofenceHardwareService;
24 import android.hardware.location.IGeofenceHardware;
25 import android.location.IGeofenceProvider;
26 import android.location.IGpsGeofenceHardware;
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 import android.os.UserHandle;
30 import android.util.Log;
31 
32 import com.android.server.servicewatcher.CurrentUserServiceSupplier;
33 import com.android.server.servicewatcher.CurrentUserServiceSupplier.BoundServiceInfo;
34 import com.android.server.servicewatcher.ServiceWatcher;
35 import com.android.server.servicewatcher.ServiceWatcher.ServiceListener;
36 
37 import java.util.Objects;
38 
39 /**
40  * @hide
41  */
42 public final class GeofenceProxy implements ServiceListener<BoundServiceInfo> {
43 
44     private static final String TAG = "GeofenceProxy";
45     private static final String SERVICE_ACTION = "com.android.location.service.GeofenceProvider";
46 
47     /**
48      * Creates and registers this proxy. If no suitable service is available for the proxy, returns
49      * null.
50      */
51     @Nullable
createAndBind(Context context, IGpsGeofenceHardware gpsGeofence)52     public static GeofenceProxy createAndBind(Context context, IGpsGeofenceHardware gpsGeofence) {
53         GeofenceProxy proxy = new GeofenceProxy(context, gpsGeofence);
54         if (proxy.register(context)) {
55             return proxy;
56         } else {
57             return null;
58         }
59     }
60 
61     final IGpsGeofenceHardware mGpsGeofenceHardware;
62     final ServiceWatcher mServiceWatcher;
63 
64     volatile IGeofenceHardware mGeofenceHardware;
65 
GeofenceProxy(Context context, IGpsGeofenceHardware gpsGeofence)66     private GeofenceProxy(Context context, IGpsGeofenceHardware gpsGeofence) {
67         mGpsGeofenceHardware = Objects.requireNonNull(gpsGeofence);
68         mServiceWatcher = ServiceWatcher.create(context,
69                 "GeofenceProxy",
70                 new CurrentUserServiceSupplier(context, SERVICE_ACTION,
71                         com.android.internal.R.bool.config_enableGeofenceOverlay,
72                         com.android.internal.R.string.config_geofenceProviderPackageName),
73                 this);
74 
75         mGeofenceHardware = null;
76     }
77 
updateGeofenceHardware(IBinder binder)78     void updateGeofenceHardware(IBinder binder) throws RemoteException {
79         IGeofenceProvider.Stub.asInterface(binder).setGeofenceHardware(mGeofenceHardware);
80     }
81 
register(Context context)82     private boolean register(Context context) {
83         boolean resolves = mServiceWatcher.checkServiceResolves();
84         if (resolves) {
85             mServiceWatcher.register();
86             context.bindServiceAsUser(
87                     new Intent(context, GeofenceHardwareService.class),
88                     new GeofenceProxyServiceConnection(),
89                     Context.BIND_AUTO_CREATE,
90                     UserHandle.SYSTEM);
91         }
92         return resolves;
93     }
94 
95     @Override
onBind(IBinder binder, BoundServiceInfo boundServiceInfo)96     public void onBind(IBinder binder, BoundServiceInfo boundServiceInfo) throws RemoteException {
97         updateGeofenceHardware(binder);
98     }
99 
100     @Override
onUnbind()101     public void onUnbind() {}
102 
103     private class GeofenceProxyServiceConnection implements ServiceConnection {
104 
GeofenceProxyServiceConnection()105         GeofenceProxyServiceConnection() {}
106 
107         @Override
onServiceConnected(ComponentName name, IBinder service)108         public void onServiceConnected(ComponentName name, IBinder service) {
109             IGeofenceHardware geofenceHardware = IGeofenceHardware.Stub.asInterface(service);
110 
111             try {
112                 geofenceHardware.setGpsGeofenceHardware(mGpsGeofenceHardware);
113                 mGeofenceHardware = geofenceHardware;
114                 mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware);
115             } catch (RemoteException e) {
116                 Log.w(TAG, "unable to initialize geofence hardware", e);
117             }
118         }
119 
120         @Override
onServiceDisconnected(ComponentName name)121         public void onServiceDisconnected(ComponentName name) {
122             mGeofenceHardware = null;
123             mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware);
124         }
125     }
126 }
127