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 com.android.car;
17 
18 import android.car.content.pm.AppBlockingPackageInfo;
19 import android.car.content.pm.CarAppBlockingPolicy;
20 import android.car.content.pm.CarAppBlockingPolicyService;
21 import android.content.pm.PackageManager;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.content.pm.Signature;
24 import android.util.Log;
25 
26 public 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 
getInstance()32     public static synchronized TestAppBlockingPolicyService getInstance() {
33         return sInstance;
34     }
35 
controlPolicySettingFromService(boolean setPolicy)36     public static synchronized void controlPolicySettingFromService(boolean setPolicy) {
37         Log.i(TAG, "controlPolicySettingFromService:" + setPolicy);
38         sSetPolicy = setPolicy;
39     }
40 
41     @Override
getAppBlockingPolicy()42     protected CarAppBlockingPolicy getAppBlockingPolicy() {
43         synchronized (this) {
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 
60         AppBlockingPackageInfo selfInfo = new AppBlockingPackageInfo(packageName, 0, 0, 0,
61                 signatures, null);
62         AppBlockingPackageInfo[] whitelists = new AppBlockingPackageInfo[] { selfInfo };
63         CarAppBlockingPolicy policy = new CarAppBlockingPolicy(whitelists, null);
64         Log.i(TAG, "getAppBlockingPolicy, passing policy:" + policy);
65         return policy;
66     }
67 }
68