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