1 /* 2 * Copyright (C) 2022 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 android.safetycenter.cts.testing 18 19 import android.Manifest.permission.WRITE_DEVICE_CONFIG 20 import android.content.Context 21 import android.content.res.Resources 22 import android.provider.DeviceConfig 23 import com.android.compatibility.common.util.SystemUtil.callWithShellPermissionIdentity 24 25 /** A class that facilitates working with Safety Center flags. */ 26 object SafetyCenterFlags { 27 28 /** Name of the flag that determines whether SafetyCenter is enabled. */ 29 private const val PROPERTY_SAFETY_CENTER_ENABLED = "safety_center_is_enabled" 30 31 /** Returns whether the device supports Safety Center. */ Contextnull32 fun Context.deviceSupportsSafetyCenter() = 33 resources.getBoolean( 34 Resources.getSystem().getIdentifier("config_enableSafetyCenter", "bool", "android")) 35 36 /** Sets the Safety Center device config flag to the given boolean [value]. */ 37 fun setSafetyCenterEnabled(value: Boolean) { 38 callWithShellPermissionIdentity( 39 { 40 val valueWasSet = 41 DeviceConfig.setProperty( 42 DeviceConfig.NAMESPACE_PRIVACY, 43 PROPERTY_SAFETY_CENTER_ENABLED, 44 /* value = */ value.toString(), 45 /* makeDefault = */ false) 46 if (!valueWasSet) { 47 throw IllegalStateException("Could not set Safety Center flag value to: $value") 48 } 49 }, 50 WRITE_DEVICE_CONFIG) 51 } 52 } 53