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 package android.car.content.pm;
17 
18 import android.annotation.SystemApi;
19 import android.app.Service;
20 import android.car.Car;
21 import android.content.Intent;
22 import android.os.IBinder;
23 import android.os.RemoteException;
24 import android.util.Slog;
25 
26 /**
27  * Service to be implemented by Service which wants to control app blocking policy.
28  * App should require android.car.permission.CONTROL_APP_BLOCKING to launch Service
29  * implementation. Additionally the APK should have the permission to be launched by Car Service.
30  * The implementing service should declare {@link #SERVICE_INTERFACE} in its intent filter as
31  * action.
32  * @hide
33  */
34 @SystemApi
35 public abstract class CarAppBlockingPolicyService extends Service {
36 
37     private static final String TAG = CarAppBlockingPolicyService.class.getSimpleName();
38 
39     public static final String SERVICE_INTERFACE =
40             "android.car.content.pm.CarAppBlockingPolicyService";
41 
42     private final ICarAppBlockingPolicyImpl mBinder = new ICarAppBlockingPolicyImpl();
43 
44     /**
45      * Return the app blocking policy. This is called from binder thread.
46      * @return
47      */
getAppBlockingPolicy()48     protected abstract CarAppBlockingPolicy getAppBlockingPolicy();
49 
50     @Override
onStartCommand(Intent intent, int flags, int startId)51     public int onStartCommand(Intent intent, int flags, int startId) {
52         return START_STICKY;
53     }
54 
55     @Override
onBind(Intent intent)56     public IBinder onBind(Intent intent) {
57         Slog.i(TAG, "onBind");
58         return mBinder;
59     }
60 
61     @Override
onUnbind(Intent intent)62     public boolean onUnbind(Intent intent) {
63         Slog.i(TAG, "onUnbind");
64         stopSelf();
65         return false;
66     }
67 
68 
69     private class ICarAppBlockingPolicyImpl extends ICarAppBlockingPolicy.Stub {
70 
71         @Override
setAppBlockingPolicySetter(ICarAppBlockingPolicySetter setter)72         public void setAppBlockingPolicySetter(ICarAppBlockingPolicySetter setter) {
73             Slog.i(TAG, "setAppBlockingPolicySetter will set policy");
74             CarAppBlockingPolicy policy = CarAppBlockingPolicyService.this.getAppBlockingPolicy();
75             try {
76                 setter.setAppBlockingPolicy(policy);
77             } catch (RemoteException e) {
78                 Car.handleRemoteExceptionFromCarService(CarAppBlockingPolicyService.this, e);
79             }
80         }
81     }
82 }
83