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