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.settings.testutils; 18 19 import static org.mockito.Mockito.any; 20 import static org.mockito.Mockito.anyInt; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.content.pm.ActivityInfo; 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.PackageInfo; 27 import android.content.pm.PackageManager; 28 import android.content.pm.ProviderInfo; 29 import android.content.pm.ResolveInfo; 30 import android.provider.DeviceConfig; 31 import android.provider.Settings; 32 33 import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils; 34 35 import java.util.ArrayList; 36 37 /** Utilities class to enable or disable the Active Unlock flag in tests. */ 38 public final class ActiveUnlockTestUtils { 39 40 public static final String TARGET = "com.active.unlock.target"; 41 public static final String PROVIDER = "com.active.unlock.provider"; 42 public static final String TARGET_SETTING = "active_unlock_target"; 43 public static final String PROVIDER_SETTING = "active_unlock_provider"; 44 enable(Context context)45 public static void enable(Context context) { 46 ActiveUnlockTestUtils.enable(context, ActiveUnlockStatusUtils.UNLOCK_INTENT_LAYOUT); 47 } 48 enable(Context context, String flagValue)49 public static void enable(Context context, String flagValue) { 50 Settings.Secure.putString( 51 context.getContentResolver(), TARGET_SETTING, TARGET); 52 Settings.Secure.putString( 53 context.getContentResolver(), PROVIDER_SETTING, PROVIDER); 54 55 PackageManager packageManager = context.getPackageManager(); 56 ApplicationInfo applicationInfo = new ApplicationInfo(); 57 applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; 58 59 ResolveInfo resolveInfo = new ResolveInfo(); 60 resolveInfo.activityInfo = new ActivityInfo(); 61 resolveInfo.activityInfo.applicationInfo = applicationInfo; 62 when(packageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo); 63 64 PackageInfo packageInfo = new PackageInfo(); 65 packageInfo.applicationInfo = applicationInfo; 66 ProviderInfo providerInfo = new ProviderInfo(); 67 providerInfo.authority = PROVIDER; 68 providerInfo.applicationInfo = applicationInfo; 69 packageInfo.providers = new ProviderInfo[] { providerInfo }; 70 ArrayList<PackageInfo> packageInfos = new ArrayList<>(); 71 packageInfos.add(packageInfo); 72 when(packageManager.getInstalledPackages(any())).thenReturn(packageInfos); 73 74 DeviceConfig.setProperty( 75 DeviceConfig.NAMESPACE_REMOTE_AUTH, 76 ActiveUnlockStatusUtils.CONFIG_FLAG_NAME, 77 flagValue, 78 false /* makeDefault */); 79 } 80 disable(Context context)81 public static void disable(Context context) { 82 DeviceConfig.setProperty( 83 DeviceConfig.NAMESPACE_REMOTE_AUTH, 84 ActiveUnlockStatusUtils.CONFIG_FLAG_NAME, 85 null /* value */, 86 false /* makeDefault */); 87 Settings.Secure.putString(context.getContentResolver(), TARGET_SETTING, null); 88 Settings.Secure.putString(context.getContentResolver(), PROVIDER_SETTING, null); 89 } 90 } 91