1 /*
2  * Copyright (C) 2023 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 
17 package com.android.ondevicepersonalization.services;
18 
19 import static com.android.ondevicepersonalization.services.PhFlags.KEY_CALLER_APP_ALLOW_LIST;
20 import static com.android.ondevicepersonalization.services.PhFlags.KEY_ENABLE_PERSONALIZATION_STATUS_OVERRIDE;
21 import static com.android.ondevicepersonalization.services.PhFlags.KEY_GLOBAL_KILL_SWITCH;
22 import static com.android.ondevicepersonalization.services.PhFlags.KEY_ISOLATED_SERVICE_ALLOW_LIST;
23 import static com.android.ondevicepersonalization.services.PhFlags.KEY_ODP_SPE_PILOT_JOB_ENABLED;
24 import static com.android.ondevicepersonalization.services.PhFlags.KEY_OUTPUT_DATA_ALLOW_LIST;
25 import static com.android.ondevicepersonalization.services.PhFlags.KEY_SHARED_ISOLATED_PROCESS_FEATURE_ENABLED;
26 
27 import android.provider.DeviceConfig;
28 
29 import androidx.test.InstrumentationRegistry;
30 
31 public class PhFlagsTestUtil {
32     private static final String WRITE_DEVICE_CONFIG_PERMISSION =
33             "android.permission.WRITE_DEVICE_CONFIG";
34 
35     private static final String READ_DEVICE_CONFIG_PERMISSION =
36             "android.permission.READ_DEVICE_CONFIG";
37 
38     private static final String MONITOR_DEVICE_CONFIG_ACCESS =
39             "android.permission.MONITOR_DEVICE_CONFIG_ACCESS";
40 
41     /**
42      * Get necessary permissions to access Setting.Config API and set up context
43      */
setUpDeviceConfigPermissions()44     public static void setUpDeviceConfigPermissions() throws Exception {
45         InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
46                 WRITE_DEVICE_CONFIG_PERMISSION, READ_DEVICE_CONFIG_PERMISSION,
47                 MONITOR_DEVICE_CONFIG_ACCESS);
48     }
49 
enableGlobalKillSwitch()50     public static void enableGlobalKillSwitch() {
51         // Override the global_kill_switch to test other flag values.
52         DeviceConfig.setProperty(
53                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
54                 KEY_GLOBAL_KILL_SWITCH,
55                 Boolean.toString(true),
56                 /* makeDefault */ false);
57     }
58 
disableGlobalKillSwitch()59     public static void disableGlobalKillSwitch() {
60         // Override the global_kill_switch to test other flag values.
61         DeviceConfig.setProperty(
62                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
63                 KEY_GLOBAL_KILL_SWITCH,
64                 Boolean.toString(false),
65                 /* makeDefault */ false);
66     }
67 
68     /**
69      * Disable the enable_personalization_status_override to test personalization-related features.
70      */
disablePersonalizationStatusOverride()71     public static void disablePersonalizationStatusOverride() {
72         DeviceConfig.setProperty(
73                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
74                 KEY_ENABLE_PERSONALIZATION_STATUS_OVERRIDE,
75                 Boolean.toString(false),
76                 /* makeDefault */ false);
77     }
78 
79     /**
80      * Set up caller app allow list in device config
81      */
setCallerAppAllowList(final String callerAppAllowList)82     public static void setCallerAppAllowList(final String callerAppAllowList) {
83         DeviceConfig.setProperty(
84                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
85                 KEY_CALLER_APP_ALLOW_LIST,
86                 callerAppAllowList,
87                 /* makeDefault */ false);
88     }
89 
90     /**
91      * Set up isolated service allow list in device config
92      */
setIsolatedServiceAllowList(final String isolatedServiceAllowList)93     public static void setIsolatedServiceAllowList(final String isolatedServiceAllowList) {
94         DeviceConfig.setProperty(
95                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
96                 KEY_ISOLATED_SERVICE_ALLOW_LIST,
97                 isolatedServiceAllowList,
98                 /* makeDefault */ false);
99     }
100 
101     /**
102      * Set up output data allow list in device config
103      */
setOutputDataAllowList(final String outputDataAllowList)104     public static void setOutputDataAllowList(final String outputDataAllowList) {
105         DeviceConfig.setProperty(
106                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
107                 KEY_OUTPUT_DATA_ALLOW_LIST,
108                 outputDataAllowList,
109                 /* makeDefault */ false);
110     }
111 
112     /** Set up output data allow list in device config */
setSharedIsolatedProcessFeatureEnabled(boolean enabled)113     public static void setSharedIsolatedProcessFeatureEnabled(boolean enabled) {
114         DeviceConfig.setProperty(
115                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
116                 KEY_SHARED_ISOLATED_PROCESS_FEATURE_ENABLED,
117                 Boolean.toString(enabled),
118                 /* makeDefault */ false);
119     }
120 
121     /** Sets up if SPE is enabled for pilot jobs. */
setSpePilotJobEnabled(boolean enabled)122     public static void setSpePilotJobEnabled(boolean enabled) {
123         DeviceConfig.setProperty(
124                 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION,
125                 KEY_ODP_SPE_PILOT_JOB_ENABLED,
126                 Boolean.toString(enabled),
127                 /* makeDefault */ false);
128     }
129 }
130