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 static android.provider.Settings.Secure.SHOW_QR_CODE_SCANNER_SETTING;
20 
21 import static androidx.lifecycle.Lifecycle.Event.ON_START;
22 import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.database.ContentObserver;
27 import android.net.Uri;
28 import android.provider.Settings;
29 
30 import androidx.lifecycle.OnLifecycleEvent;
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settings.R;
35 import com.android.settings.core.TogglePreferenceController;
36 
37 /**
38  * Preference controller for enabling/disabling QR code scanner button on lock screen.
39  */
40 public class QRCodeScannerPreferenceController extends TogglePreferenceController {
41     private static final String SETTING_KEY = Settings.Secure.LOCK_SCREEN_SHOW_QR_CODE_SCANNER;
42     private final ContentObserver mSettingsObserver;
43     private final ContentResolver mContentResolver;
44     private Preference mPreference;
45 
QRCodeScannerPreferenceController(Context context, String preferenceKey)46     public QRCodeScannerPreferenceController(Context context, String preferenceKey) {
47         super(context, preferenceKey);
48         mContentResolver = context.getContentResolver();
49         mSettingsObserver = new ContentObserver(null) {
50             @Override
51             public void onChange(boolean selfChange, Uri uri) {
52                 updateState(mPreference);
53             }
54         };
55     }
56 
57     @Override
displayPreference(PreferenceScreen screen)58     public void displayPreference(PreferenceScreen screen) {
59         super.displayPreference(screen);
60         mPreference = screen.findPreference(getPreferenceKey());
61     }
62 
63     /** Called when activity starts being displayed to user. */
64     @OnLifecycleEvent(ON_START)
onStart()65     public void onStart() {
66         mContentResolver.registerContentObserver(
67                 Settings.Global.getUriFor(SHOW_QR_CODE_SCANNER_SETTING), false,
68                 mSettingsObserver);
69     }
70 
71     /** Called when activity stops being displayed to user. */
72     @OnLifecycleEvent(ON_STOP)
onStop()73     public void onStop() {
74         mContentResolver.unregisterContentObserver(mSettingsObserver);
75     }
76 
77     @Override
isChecked()78     public boolean isChecked() {
79         return Settings.Secure.getInt(mContext.getContentResolver(), SETTING_KEY, 0) != 0;
80     }
81 
82     @Override
setChecked(boolean isChecked)83     public boolean setChecked(boolean isChecked) {
84         return Settings.Secure.putInt(mContext.getContentResolver(), SETTING_KEY,
85                 isChecked ? 1 : 0);
86     }
87 
88     @Override
getAvailabilityStatus()89     public int getAvailabilityStatus() {
90         if (CustomizableLockScreenUtils.isFeatureEnabled(mContext)) {
91             return UNSUPPORTED_ON_DEVICE;
92         }
93 
94         return isScannerActivityAvailable() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
95     }
96 
97     @Override
updateState(Preference preference)98     public void updateState(Preference preference) {
99         super.updateState(preference);
100         refreshSummary(preference);
101     }
102 
103     @Override
getSliceHighlightMenuRes()104     public int getSliceHighlightMenuRes() {
105         return R.string.menu_key_display;
106     }
107 
isScannerActivityAvailable()108     private boolean isScannerActivityAvailable() {
109         return Settings.Secure.getString(mContext.getContentResolver(),
110                 SHOW_QR_CODE_SCANNER_SETTING) != null;
111     }
112 }
113