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.anyString;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.pm.ActivityInfo;
26 import android.content.pm.ApplicationInfo;
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 /** Utilities class to enable or disable the Active Unlock flag in tests. */
36 public final class ActiveUnlockTestUtils {
37 
38     public static final String TARGET = "com.active.unlock.target";
39     public static final String PROVIDER = "com.active.unlock.provider";
40     public static final String TARGET_SETTING = "active_unlock_target";
41     public static final String PROVIDER_SETTING = "active_unlock_provider";
42 
enable(Context context)43     public static void enable(Context context) {
44         ActiveUnlockTestUtils.enable(context, ActiveUnlockStatusUtils.UNLOCK_INTENT_LAYOUT);
45     }
46 
enable(Context context, String flagValue)47     public static void enable(Context context, String flagValue) {
48         Settings.Secure.putString(
49                 context.getContentResolver(), TARGET_SETTING, TARGET);
50         Settings.Secure.putString(
51                 context.getContentResolver(), PROVIDER_SETTING, PROVIDER);
52 
53         PackageManager packageManager = context.getPackageManager();
54         ApplicationInfo applicationInfo = new ApplicationInfo();
55         applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
56 
57         ResolveInfo resolveInfo = new ResolveInfo();
58         resolveInfo.activityInfo = new ActivityInfo();
59         resolveInfo.activityInfo.applicationInfo = applicationInfo;
60         when(packageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
61 
62         ProviderInfo providerInfo = new ProviderInfo();
63         providerInfo.authority = PROVIDER;
64         providerInfo.applicationInfo = applicationInfo;
65         when(packageManager.resolveContentProvider(anyString(), any())).thenReturn(providerInfo);
66 
67         DeviceConfig.setProperty(
68                 DeviceConfig.NAMESPACE_REMOTE_AUTH,
69                 ActiveUnlockStatusUtils.CONFIG_FLAG_NAME,
70                 flagValue,
71                 false /* makeDefault */);
72     }
73 
disable(Context context)74     public static void disable(Context context) {
75         DeviceConfig.setProperty(
76                 DeviceConfig.NAMESPACE_REMOTE_AUTH,
77                 ActiveUnlockStatusUtils.CONFIG_FLAG_NAME,
78                 null /* value */,
79                 false /* makeDefault */);
80         Settings.Secure.putString(context.getContentResolver(), TARGET_SETTING, null);
81         Settings.Secure.putString(context.getContentResolver(), PROVIDER_SETTING, null);
82     }
83 }
84