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
17package com.android.server.location;
18
19import com.android.server.ServiceWatcher;
20
21import android.content.Context;
22import android.hardware.location.ActivityRecognitionHardware;
23import android.hardware.location.IActivityRecognitionHardwareWatcher;
24import android.os.Handler;
25import android.os.RemoteException;
26import android.util.Log;
27
28/**
29 * Proxy class to bind GmsCore to the ActivityRecognitionHardware.
30 *
31 * @hide
32 */
33public class ActivityRecognitionProxy {
34    private static final String TAG = "ActivityRecognitionProxy";
35
36    private final ServiceWatcher mServiceWatcher;
37    private final ActivityRecognitionHardware mActivityRecognitionHardware;
38
39    private ActivityRecognitionProxy(
40            Context context,
41            Handler handler,
42            ActivityRecognitionHardware activityRecognitionHardware,
43            int overlaySwitchResId,
44            int defaultServicePackageNameResId,
45            int initialPackageNameResId) {
46        mActivityRecognitionHardware = activityRecognitionHardware;
47
48        Runnable newServiceWork = new Runnable() {
49            @Override
50            public void run() {
51                bindProvider(mActivityRecognitionHardware);
52            }
53        };
54
55        // prepare the connection to the provider
56        mServiceWatcher = new ServiceWatcher(
57                context,
58                TAG,
59                "com.android.location.service.ActivityRecognitionProvider",
60                overlaySwitchResId,
61                defaultServicePackageNameResId,
62                initialPackageNameResId,
63                newServiceWork,
64                handler);
65    }
66
67    /**
68     * Creates an instance of the proxy and binds it to the appropriate FusedProvider.
69     *
70     * @return An instance of the proxy if it could be bound, null otherwise.
71     */
72    public static ActivityRecognitionProxy createAndBind(
73            Context context,
74            Handler handler,
75            ActivityRecognitionHardware activityRecognitionHardware,
76            int overlaySwitchResId,
77            int defaultServicePackageNameResId,
78            int initialPackageNameResId) {
79        ActivityRecognitionProxy activityRecognitionProxy = new ActivityRecognitionProxy(
80                context,
81                handler,
82                activityRecognitionHardware,
83                overlaySwitchResId,
84                defaultServicePackageNameResId,
85                initialPackageNameResId);
86
87        // try to bind the provider
88        if (!activityRecognitionProxy.mServiceWatcher.start()) {
89            Log.e(TAG, "ServiceWatcher could not start.");
90            return null;
91        }
92
93        return activityRecognitionProxy;
94    }
95
96    /**
97     * Helper function to bind the FusedLocationHardware to the appropriate FusedProvider instance.
98     */
99    private void bindProvider(ActivityRecognitionHardware activityRecognitionHardware) {
100        IActivityRecognitionHardwareWatcher watcher =
101                IActivityRecognitionHardwareWatcher.Stub.asInterface(mServiceWatcher.getBinder());
102        if (watcher == null) {
103            Log.e(TAG, "No provider instance found on connection.");
104            return;
105        }
106
107        try {
108            watcher.onInstanceChanged(mActivityRecognitionHardware);
109        } catch (RemoteException e) {
110            Log.e(TAG, "Error delivering hardware interface.", e);
111        }
112    }
113}
114