TestAppBlockingPolicyService.java revision e54ac276796c6535558f8444d882adecd19ce2bd
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 com.android.car.test;
17
18import android.car.content.pm.AppBlockingPackageInfo;
19import android.car.content.pm.CarAppBlockingPolicy;
20import android.car.content.pm.CarAppBlockingPolicyService;
21import android.content.pm.PackageManager;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.content.pm.Signature;
24import android.util.Log;
25
26public class TestAppBlockingPolicyService extends CarAppBlockingPolicyService {
27    private static final String TAG = TestAppBlockingPolicyService.class.getSimpleName();
28
29    private static TestAppBlockingPolicyService sInstance;
30    private static boolean sSetPolicy = true;
31
32    public static synchronized TestAppBlockingPolicyService getInstance() {
33        return sInstance;
34    }
35
36    public static synchronized void controlPolicySettingFromService(boolean setPolicy) {
37        Log.i(TAG, "controlPolicySettingFromService:" + setPolicy);
38        sSetPolicy = setPolicy;
39    }
40
41    @Override
42    protected CarAppBlockingPolicy getAppBlockingPolicy() {
43        synchronized (TestAppBlockingPolicyService.class) {
44            sInstance = this;
45            if (sSetPolicy == false) {
46                Log.i(TAG, "getAppBlockingPolicy returning null");
47                return null;
48            }
49        }
50        PackageManager pm = getPackageManager();
51        String packageName = getPackageName();
52        Signature[] signatures;
53        try {
54            signatures = pm.getPackageInfo(packageName,
55                    PackageManager.GET_SIGNATURES).signatures;
56        } catch (NameNotFoundException e) {
57            return null;
58        }
59        AppBlockingPackageInfo selfInfo = new AppBlockingPackageInfo(packageName, 0, 0, 0,
60                signatures, null);
61        AppBlockingPackageInfo[] whitelists = new AppBlockingPackageInfo[] { selfInfo };
62        CarAppBlockingPolicy policy = new CarAppBlockingPolicy(whitelists, null);
63        Log.i(TAG, "getAppBlockingPolicy, passing policy:" + policy);
64        return policy;
65    }
66}
67