blob: 9d9852ba5da3a5160b52e112734c960e9ef3ca62 [file] [log] [blame]
Soonil Nagarkar6d21a4d2020-01-22 16:21:00 -08001/*
2 * Copyright (C) 2014 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 com.android.server.location;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.hardware.location.ActivityRecognitionHardware;
22import android.hardware.location.IActivityRecognitionHardwareClient;
23import android.hardware.location.IActivityRecognitionHardwareWatcher;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.util.Log;
27
28import com.android.server.FgThread;
29import com.android.server.ServiceWatcher;
30
31/**
32 * Proxy class to bind GmsCore to the ActivityRecognitionHardware.
33 *
34 * @hide
35 */
36public class HardwareActivityRecognitionProxy {
37
38 private static final String TAG = "ARProxy";
39 private static final String SERVICE_ACTION =
40 "com.android.location.service.ActivityRecognitionProvider";
41
42 /**
43 * Creates and registers this proxy. If no suitable service is available for the proxy, returns
44 * null.
45 */
46 @Nullable
47 public static HardwareActivityRecognitionProxy createAndRegister(Context context) {
48 HardwareActivityRecognitionProxy arProxy = new HardwareActivityRecognitionProxy(context);
49 if (arProxy.register()) {
50 return arProxy;
51 } else {
52 return null;
53 }
54 }
55
56 private final boolean mIsSupported;
57 private final ActivityRecognitionHardware mInstance;
58
59 private final ServiceWatcher mServiceWatcher;
60
61 private HardwareActivityRecognitionProxy(Context context) {
62 mIsSupported = ActivityRecognitionHardware.isSupported();
63 if (mIsSupported) {
64 mInstance = ActivityRecognitionHardware.getInstance(context);
65 } else {
66 mInstance = null;
67 }
68
69 mServiceWatcher = new ServiceWatcher(context,
70 FgThread.getHandler(),
71 SERVICE_ACTION,
72 this::onBind,
73 null,
74 com.android.internal.R.bool.config_enableActivityRecognitionHardwareOverlay,
75 com.android.internal.R.string.config_activityRecognitionHardwarePackageName);
76 }
77
78 private boolean register() {
79 return mServiceWatcher.register();
80 }
81
82 private void onBind(IBinder binder) throws RemoteException {
83 String descriptor = binder.getInterfaceDescriptor();
84
85 if (IActivityRecognitionHardwareWatcher.class.getCanonicalName().equals(descriptor)) {
86 IActivityRecognitionHardwareWatcher watcher =
87 IActivityRecognitionHardwareWatcher.Stub.asInterface(binder);
88 if (mInstance != null) {
89 watcher.onInstanceChanged(mInstance);
90 }
91 } else if (IActivityRecognitionHardwareClient.class.getCanonicalName().equals(descriptor)) {
92 IActivityRecognitionHardwareClient client =
93 IActivityRecognitionHardwareClient.Stub.asInterface(binder);
94 client.onAvailabilityChanged(mIsSupported, mInstance);
95 } else {
96 Log.e(TAG, "Unknown descriptor: " + descriptor);
97 }
98 }
99}