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