1 /*
2  * Copyright (C) 2021 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.display;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.provider.Settings;
22 import android.service.quickaccesswallet.QuickAccessWalletClient;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.internal.widget.LockPatternUtils;
28 import com.android.settings.R;
29 import com.android.settings.core.TogglePreferenceController;
30 import com.android.settings.overlay.FeatureFactory;
31 
32 /**
33  * Preference for showing/hiding sensitive wallet content while the device is locked.
34  */
35 public class WalletPrivacyPreferenceController extends TogglePreferenceController {
36 
37     private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_WALLET;
38     private final QuickAccessWalletClient mClient;
39 
WalletPrivacyPreferenceController(Context context, String preferenceKey)40     public WalletPrivacyPreferenceController(Context context, String preferenceKey) {
41         super(context, preferenceKey);
42         mClient = initWalletClient();
43     }
44 
45     @Override
isChecked()46     public boolean isChecked() {
47         return Settings.Secure.getInt(mContext.getContentResolver(), SETTING_KEY, 0) != 0;
48     }
49 
50     @Override
setChecked(boolean isChecked)51     public boolean setChecked(boolean isChecked) {
52         return Settings.Secure.putInt(mContext.getContentResolver(), SETTING_KEY,
53                 isChecked ? 1 : 0);
54     }
55 
56     @Override
getSummary()57     public CharSequence getSummary() {
58         final int res = isSecure() ? R.string.lockscreen_privacy_wallet_summary :
59                 R.string.lockscreen_privacy_not_secure;
60         return mContext.getText(res);
61     }
62 
63     @Override
getAvailabilityStatus()64     public int getAvailabilityStatus() {
65         if (CustomizableLockScreenUtils.isFeatureEnabled(mContext)) {
66             return UNSUPPORTED_ON_DEVICE;
67         } else  if (!isEnabled()) {
68             return UNSUPPORTED_ON_DEVICE;
69         } else if (!isSecure()) {
70             return DISABLED_DEPENDENT_SETTING;
71         }
72         return AVAILABLE;
73     }
74 
75     @Override
updateState(Preference preference)76     public void updateState(Preference preference) {
77         super.updateState(preference);
78         preference.setEnabled(getAvailabilityStatus() == AVAILABLE);
79         refreshSummary(preference);
80     }
81 
82     @Override
getSliceHighlightMenuRes()83     public int getSliceHighlightMenuRes() {
84         return R.string.menu_key_display;
85     }
86 
isEnabled()87     private boolean isEnabled() {
88         return mClient.isWalletServiceAvailable();
89     }
90 
isSecure()91     private boolean isSecure() {
92         final LockPatternUtils utils = FeatureFactory.getFeatureFactory()
93                 .getSecurityFeatureProvider()
94                 .getLockPatternUtils(mContext);
95         int userId = UserHandle.myUserId();
96         return utils.isSecure(userId);
97     }
98 
99     @VisibleForTesting
initWalletClient()100     QuickAccessWalletClient initWalletClient() {
101         return QuickAccessWalletClient.create(mContext);
102     }
103 }
104