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.location.provider;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.hardware.location.IActivityRecognitionHardware;
22import android.hardware.location.IActivityRecognitionHardwareWatcher;
23import android.os.Binder;
24import android.os.IBinder;
25import android.os.Process;
26import android.os.RemoteException;
27import android.util.Log;
28
29/**
30 * A watcher class for Activity-Recognition instances.
31 */
32public class ActivityRecognitionProviderWatcher {
33    private static final String TAG = "ActivityRecognitionProviderWatcher";
34
35    private static ActivityRecognitionProviderWatcher sWatcher;
36    private static final Object sWatcherLock = new Object();
37
38    private ActivityRecognitionProvider mActivityRecognitionProvider;
39
40    private ActivityRecognitionProviderWatcher() {}
41
42    public static ActivityRecognitionProviderWatcher getInstance() {
43        synchronized (sWatcherLock) {
44            if (sWatcher == null) {
45                sWatcher = new ActivityRecognitionProviderWatcher();
46            }
47            return sWatcher;
48        }
49    }
50
51    private IActivityRecognitionHardwareWatcher.Stub mWatcherStub =
52            new IActivityRecognitionHardwareWatcher.Stub() {
53        @Override
54        public void onInstanceChanged(IActivityRecognitionHardware instance) {
55            int callingUid = Binder.getCallingUid();
56            if (callingUid != Process.SYSTEM_UID) {
57                Log.d(TAG, "Ignoring calls from non-system server. Uid: " + callingUid);
58                return;
59            }
60
61            try {
62                mActivityRecognitionProvider = new ActivityRecognitionProvider(instance);
63            } catch (RemoteException e) {
64                Log.e(TAG, "Error creating Hardware Activity-Recognition", e);
65            }
66        }
67    };
68
69    /**
70     * Gets the binder needed to interact with proxy provider in the platform.
71     */
72    @NonNull
73    public IBinder getBinder() {
74        return mWatcherStub;
75    }
76
77    /**
78     * Gets an object that supports the functionality of {@link ActivityRecognitionProvider}.
79     *
80     * @return Non-null value if the functionality is supported by the platform, false otherwise.
81     */
82    @Nullable
83    public ActivityRecognitionProvider getActivityRecognitionProvider() {
84        return mActivityRecognitionProvider;
85    }
86}
87