blob: f006fb177382c0c9014c8f45617bd6dc5a5ca6c5 [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 */
16package com.android.server.location;
17
Soonil Nagarkare731ca82018-11-02 13:55:51 -070018import android.annotation.Nullable;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070019import android.content.ComponentName;
Soonil Nagarkare731ca82018-11-02 13:55:51 -070020import android.content.Context;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070021import android.content.Intent;
22import android.content.ServiceConnection;
23import android.hardware.location.GeofenceHardwareService;
24import android.hardware.location.IGeofenceHardware;
25import android.location.IGeofenceProvider;
26import android.location.IGpsGeofenceHardware;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070027import android.os.IBinder;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070028import android.os.RemoteException;
29import android.os.UserHandle;
30import android.util.Log;
Soonil Nagarkare731ca82018-11-02 13:55:51 -070031
Soonil Nagarkara620d342019-01-30 10:02:40 -080032import com.android.server.FgThread;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070033import com.android.server.ServiceWatcher;
34
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080035import java.util.Objects;
36
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070037/**
38 * @hide
39 */
40public final class GeofenceProxy {
Soonil Nagarkare731ca82018-11-02 13:55:51 -070041
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070042 private static final String TAG = "GeofenceProxy";
Soonil Nagarkare731ca82018-11-02 13:55:51 -070043 private static final String SERVICE_ACTION = "com.android.location.service.GeofenceProvider";
44
Soonil Nagarkare731ca82018-11-02 13:55:51 -070045 @Nullable
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080046 public static GeofenceProxy createAndBind(Context context, IGpsGeofenceHardware gpsGeofence) {
47 GeofenceProxy proxy = new GeofenceProxy(context, gpsGeofence);
48 if (proxy.register(context)) {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070049 return proxy;
50 } else {
51 return null;
52 }
53 }
54
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080055 private final IGpsGeofenceHardware mGpsGeofenceHardware;
56 private final ServiceWatcher mServiceWatcher;
Soonil Nagarkare731ca82018-11-02 13:55:51 -070057
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080058 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 Nagarkare731ca82018-11-02 13:55:51 -070066
67 mGeofenceHardware = null;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070068 }
69
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080070 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 Nagarkare731ca82018-11-02 13:55:51 -070080 UserHandle.SYSTEM);
81 return true;
82 }
83
84 return false;
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070085 }
86
Soonil Nagarkare731ca82018-11-02 13:55:51 -070087 private class GeofenceProxyServiceConnection implements ServiceConnection {
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070088
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -070089 @Override
90 public void onServiceConnected(ComponentName name, IBinder service) {
Soonil Nagarkare731ca82018-11-02 13:55:51 -070091 IGeofenceHardware geofenceHardware = IGeofenceHardware.Stub.asInterface(service);
92
93 try {
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080094 geofenceHardware.setGpsGeofenceHardware(mGpsGeofenceHardware);
Soonil Nagarkare731ca82018-11-02 13:55:51 -070095 mGeofenceHardware = geofenceHardware;
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -080096 mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware);
97 } catch (RemoteException e) {
98 Log.w(TAG, "unable to initialize geofence hardware", e);
Zhentao Sund535ead2013-08-29 14:43:35 -070099 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700100 }
101
102 @Override
103 public void onServiceDisconnected(ComponentName name) {
Soonil Nagarkare731ca82018-11-02 13:55:51 -0700104 mGeofenceHardware = null;
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -0800105 mServiceWatcher.runOnBinder(GeofenceProxy.this::updateGeofenceHardware);
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700106 }
107 }
Jaikumar Ganesh8ce470d2013-04-03 12:22:18 -0700108}