Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 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 | package com.android.server.location; |
| 17 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 18 | import android.annotation.Nullable; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 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; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.UserHandle; |
| 30 | import android.util.Log; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 31 | |
Soonil Nagarkar | a620d34 | 2019-01-30 10:02:40 -0800 | [diff] [blame] | 32 | import com.android.server.FgThread; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 33 | import com.android.server.ServiceWatcher; |
| 34 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 35 | import java.util.Objects; |
| 36 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 37 | /** |
| 38 | * @hide |
| 39 | */ |
| 40 | public final class GeofenceProxy { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 41 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 42 | private static final String TAG = "GeofenceProxy"; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 43 | private static final String SERVICE_ACTION = "com.android.location.service.GeofenceProvider"; |
| 44 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 45 | @Nullable |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 46 | public static GeofenceProxy createAndBind(Context context, IGpsGeofenceHardware gpsGeofence) { |
| 47 | GeofenceProxy proxy = new GeofenceProxy(context, gpsGeofence); |
| 48 | if (proxy.register(context)) { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 49 | return proxy; |
| 50 | } else { |
| 51 | return null; |
| 52 | } |
| 53 | } |
| 54 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 55 | private final IGpsGeofenceHardware mGpsGeofenceHardware; |
| 56 | private final ServiceWatcher mServiceWatcher; |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 57 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 58 | private volatile IGeofenceHardware mGeofenceHardware; |
| 59 | |
| 60 | private GeofenceProxy(Context context, IGpsGeofenceHardware gpsGeofence) { |
| 61 | mGpsGeofenceHardware = Objects.requireNonNull(gpsGeofence); |
| 62 | mServiceWatcher = new ServiceWatcher(context, FgThread.getHandler(), SERVICE_ACTION, |
| 63 | this::updateGeofenceHardware, null, |
| 64 | com.android.internal.R.bool.config_enableGeofenceOverlay, |
| 65 | com.android.internal.R.string.config_geofenceProviderPackageName); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 66 | |
| 67 | mGeofenceHardware = null; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 70 | private void updateGeofenceHardware(IBinder binder) throws RemoteException { |
| 71 | IGeofenceProvider.Stub.asInterface(binder).setGeofenceHardware(mGeofenceHardware); |
| 72 | } |
| 73 | |
| 74 | private boolean register(Context context) { |
| 75 | if (mServiceWatcher.register()) { |
| 76 | context.bindServiceAsUser( |
| 77 | new Intent(context, GeofenceHardwareService.class), |
| 78 | new GeofenceProxyServiceConnection(), |
| 79 | Context.BIND_AUTO_CREATE, |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 80 | UserHandle.SYSTEM); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | return false; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 87 | private class GeofenceProxyServiceConnection implements ServiceConnection { |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 88 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void onServiceConnected(ComponentName name, IBinder service) { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 91 | IGeofenceHardware geofenceHardware = IGeofenceHardware.Stub.asInterface(service); |
| 92 | |
| 93 | try { |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 94 | geofenceHardware.setGpsGeofenceHardware(mGpsGeofenceHardware); |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 95 | mGeofenceHardware = geofenceHardware; |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 96 | mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware); |
| 97 | } catch (RemoteException e) { |
| 98 | Log.w(TAG, "unable to initialize geofence hardware", e); |
Zhentao Sun | d535ead | 2013-08-29 14:43:35 -0700 | [diff] [blame] | 99 | } |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void onServiceDisconnected(ComponentName name) { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 104 | mGeofenceHardware = null; |
Soonil Nagarkar | 6d21a4d | 2020-01-22 16:21:00 -0800 | [diff] [blame] | 105 | mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware); |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 108 | } |