destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.location; |
| 18 | |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.hardware.location.ActivityRecognitionHardware; |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 21 | import android.hardware.location.IActivityRecognitionHardwareClient; |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 22 | import android.hardware.location.IActivityRecognitionHardwareWatcher; |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 23 | import android.os.IBinder; |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 24 | import android.os.RemoteException; |
| 25 | import android.util.Log; |
| 26 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 27 | import com.android.internal.os.BackgroundThread; |
| 28 | import com.android.server.ServiceWatcher; |
| 29 | |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Proxy class to bind GmsCore to the ActivityRecognitionHardware. |
| 32 | * |
| 33 | * @hide |
| 34 | */ |
| 35 | public class ActivityRecognitionProxy { |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 36 | |
destradaa | 3b0224d | 2014-07-16 14:28:06 -0700 | [diff] [blame] | 37 | private static final String TAG = "ActivityRecognitionProxy"; |
| 38 | |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 39 | /** |
| 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, |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 46 | boolean activityRecognitionHardwareIsSupported, |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 47 | ActivityRecognitionHardware activityRecognitionHardware, |
| 48 | int overlaySwitchResId, |
| 49 | int defaultServicePackageNameResId, |
| 50 | int initialPackageNameResId) { |
| 51 | ActivityRecognitionProxy activityRecognitionProxy = new ActivityRecognitionProxy( |
| 52 | context, |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 53 | activityRecognitionHardwareIsSupported, |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 54 | activityRecognitionHardware, |
| 55 | overlaySwitchResId, |
| 56 | defaultServicePackageNameResId, |
| 57 | initialPackageNameResId); |
| 58 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 59 | if (activityRecognitionProxy.mServiceWatcher.start()) { |
| 60 | return activityRecognitionProxy; |
| 61 | } else { |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 62 | return null; |
| 63 | } |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 66 | private final ServiceWatcher mServiceWatcher; |
| 67 | private final boolean mIsSupported; |
| 68 | private final ActivityRecognitionHardware mInstance; |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 69 | |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 70 | 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); |
destradaa | 6e2fe75 | 2015-06-23 17:25:53 -0700 | [diff] [blame] | 91 | } |
Soonil Nagarkar | e731ca8 | 2018-11-02 13:55:51 -0700 | [diff] [blame] | 92 | }; |
| 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); |
destradaa | a4fa3b5 | 2014-07-09 10:46:39 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |