Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2015 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.location.provider;
     18 
     19 import android.annotation.NonNull;
     20 import android.hardware.location.IActivityRecognitionHardware;
     21 import android.hardware.location.IActivityRecognitionHardwareClient;
     22 import android.os.Binder;
     23 import android.os.IBinder;
     24 import android.os.Process;
     25 import android.os.RemoteException;
     26 import android.util.Log;
     27 
     28 /**
     29  * A client class for interaction with an Activity-Recognition provider.
     30  */
     31 public abstract class ActivityRecognitionProviderClient {
     32     private static final String TAG = "ArProviderClient";
     33 
     34     protected ActivityRecognitionProviderClient() {}
     35 
     36     private IActivityRecognitionHardwareClient.Stub mClient =
     37             new IActivityRecognitionHardwareClient.Stub() {
     38                 @Override
     39                 public void onAvailabilityChanged(
     40                         boolean isSupported,
     41                         IActivityRecognitionHardware instance) {
     42                     int callingUid = Binder.getCallingUid();
     43                     if (callingUid != Process.SYSTEM_UID) {
     44                         Log.d(TAG, "Ignoring calls from non-system server. Uid: " + callingUid);
     45                         return;
     46                     }
     47                     ActivityRecognitionProvider provider;
     48                     try {
     49                         provider = isSupported ? new ActivityRecognitionProvider(instance) : null;
     50                     } catch (RemoteException e) {
     51                         Log.e(TAG, "Error creating Hardware Activity-Recognition Provider.", e);
     52                         return;
     53                     }
     54                     onProviderChanged(isSupported, provider);
     55                 }
     56             };
     57 
     58     /**
     59      * Gets the binder needed to interact with proxy provider in the platform.
     60      */
     61     @NonNull
     62     public IBinder getBinder() {
     63         return mClient;
     64     }
     65 
     66     /**
     67      * Called when a change in the availability of {@link ActivityRecognitionProvider} is detected.
     68      *
     69      * @param isSupported whether the platform supports the provider natively
     70      * @param instance the available provider's instance
     71      */
     72     public abstract void onProviderChanged(
     73             boolean isSupported,
     74             ActivityRecognitionProvider instance);
     75 }
     76