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