blob: 22fabb2cdd3e5de6b3bff29fdbbdad951f18abaf [file] [log] [blame]
destradaaa4fa3b52014-07-09 10:46:39 -07001/*
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
destradaaa4fa3b52014-07-09 10:46:39 -070019import android.content.Context;
20import android.hardware.location.ActivityRecognitionHardware;
destradaa6e2fe752015-06-23 17:25:53 -070021import android.hardware.location.IActivityRecognitionHardwareClient;
destradaaa4fa3b52014-07-09 10:46:39 -070022import android.hardware.location.IActivityRecognitionHardwareWatcher;
destradaa6e2fe752015-06-23 17:25:53 -070023import android.os.IBinder;
destradaaa4fa3b52014-07-09 10:46:39 -070024import android.os.RemoteException;
25import android.util.Log;
26
Soonil Nagarkare731ca82018-11-02 13:55:51 -070027import com.android.internal.os.BackgroundThread;
28import com.android.server.ServiceWatcher;
29
destradaaa4fa3b52014-07-09 10:46:39 -070030/**
31 * Proxy class to bind GmsCore to the ActivityRecognitionHardware.
32 *
33 * @hide
34 */
35public class ActivityRecognitionProxy {
Soonil Nagarkare731ca82018-11-02 13:55:51 -070036
destradaa3b0224d2014-07-16 14:28:06 -070037 private static final String TAG = "ActivityRecognitionProxy";
38
destradaaa4fa3b52014-07-09 10:46:39 -070039 /**
40 * Creates an instance of the proxy and binds it to the appropriate FusedProvider.
41 *
42 * @return An instance of the proxy if it could be bound, null otherwise.
43 */
44 public static ActivityRecognitionProxy createAndBind(
45 Context context,
destradaa6e2fe752015-06-23 17:25:53 -070046 boolean activityRecognitionHardwareIsSupported,
destradaaa4fa3b52014-07-09 10:46:39 -070047 ActivityRecognitionHardware activityRecognitionHardware,
48 int overlaySwitchResId,
49 int defaultServicePackageNameResId,
50 int initialPackageNameResId) {
51 ActivityRecognitionProxy activityRecognitionProxy = new ActivityRecognitionProxy(
52 context,
destradaa6e2fe752015-06-23 17:25:53 -070053 activityRecognitionHardwareIsSupported,
destradaaa4fa3b52014-07-09 10:46:39 -070054 activityRecognitionHardware,
55 overlaySwitchResId,
56 defaultServicePackageNameResId,
57 initialPackageNameResId);
58
Soonil Nagarkare731ca82018-11-02 13:55:51 -070059 if (activityRecognitionProxy.mServiceWatcher.start()) {
60 return activityRecognitionProxy;
61 } else {
destradaaa4fa3b52014-07-09 10:46:39 -070062 return null;
63 }
destradaaa4fa3b52014-07-09 10:46:39 -070064 }
65
Soonil Nagarkare731ca82018-11-02 13:55:51 -070066 private final ServiceWatcher mServiceWatcher;
67 private final boolean mIsSupported;
68 private final ActivityRecognitionHardware mInstance;
destradaaa4fa3b52014-07-09 10:46:39 -070069
Soonil Nagarkare731ca82018-11-02 13:55:51 -070070 private ActivityRecognitionProxy(
71 Context context,
72 boolean activityRecognitionHardwareIsSupported,
73 ActivityRecognitionHardware activityRecognitionHardware,
74 int overlaySwitchResId,
75 int defaultServicePackageNameResId,
76 int initialPackageNameResId) {
77 mIsSupported = activityRecognitionHardwareIsSupported;
78 mInstance = activityRecognitionHardware;
79
80 mServiceWatcher = new ServiceWatcher(
81 context,
82 TAG,
83 "com.android.location.service.ActivityRecognitionProvider",
84 overlaySwitchResId,
85 defaultServicePackageNameResId,
86 initialPackageNameResId,
87 BackgroundThread.getHandler()) {
88 @Override
89 protected void onBind() {
90 runOnBinder(ActivityRecognitionProxy.this::initializeService);
destradaa6e2fe752015-06-23 17:25:53 -070091 }
Soonil Nagarkare731ca82018-11-02 13:55:51 -070092 };
93 }
94
95 private void initializeService(IBinder binder) {
96 try {
97 String descriptor = binder.getInterfaceDescriptor();
98
99 if (IActivityRecognitionHardwareWatcher.class.getCanonicalName().equals(
100 descriptor)) {
101 IActivityRecognitionHardwareWatcher watcher =
102 IActivityRecognitionHardwareWatcher.Stub.asInterface(binder);
103 if (mInstance != null) {
104 watcher.onInstanceChanged(mInstance);
105 }
106 } else if (IActivityRecognitionHardwareClient.class.getCanonicalName()
107 .equals(descriptor)) {
108 IActivityRecognitionHardwareClient client =
109 IActivityRecognitionHardwareClient.Stub.asInterface(binder);
110 client.onAvailabilityChanged(mIsSupported, mInstance);
111 } else {
112 Log.e(TAG, "Invalid descriptor found on connection: " + descriptor);
113 }
114 } catch (RemoteException e) {
115 Log.w(TAG, e);
destradaaa4fa3b52014-07-09 10:46:39 -0700116 }
117 }
118}