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.privacy;
18 
19 import android.content.ClipboardManager;
20 import android.content.Context;
21 import android.provider.DeviceConfig;
22 import android.provider.Settings;
23 
24 import androidx.lifecycle.Lifecycle;
25 import androidx.lifecycle.LifecycleObserver;
26 import androidx.lifecycle.OnLifecycleEvent;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.TogglePreferenceController;
32 
33 /**
34  * Controller for preference to toggle whether clipboard access notifications should be shown.
35  */
36 public class ShowClipAccessNotificationPreferenceController
37         extends TogglePreferenceController implements LifecycleObserver {
38 
39     private static final String KEY_SHOW_CLIP_ACCESS_NOTIFICATION = "show_clip_access_notification";
40 
41     private final DeviceConfig.OnPropertiesChangedListener mDeviceConfigListener =
42             properties -> updateConfig();
43     private boolean mDefault;
44     private Preference mPreference;
45 
ShowClipAccessNotificationPreferenceController(Context context)46     public ShowClipAccessNotificationPreferenceController(Context context) {
47         super(context, KEY_SHOW_CLIP_ACCESS_NOTIFICATION);
48         updateConfig();
49     }
50 
51     @Override
isChecked()52     public boolean isChecked() {
53         return Settings.Secure.getInt(mContext.getContentResolver(),
54                 Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, (mDefault ? 1 : 0)) != 0;
55     }
56 
57     @Override
setChecked(boolean isChecked)58     public boolean setChecked(boolean isChecked) {
59         Settings.Secure.putInt(mContext.getContentResolver(),
60                 Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, (isChecked ? 1 : 0));
61         return true;
62     }
63 
64     @Override
getAvailabilityStatus()65     public int getAvailabilityStatus() {
66         return AVAILABLE;
67     }
68 
69     @Override
displayPreference(PreferenceScreen screen)70     public void displayPreference(PreferenceScreen screen) {
71         super.displayPreference(screen);
72 
73         mPreference = screen.findPreference(getPreferenceKey());
74     }
75 
76     @Override
getSliceHighlightMenuRes()77     public int getSliceHighlightMenuRes() {
78         return R.string.menu_key_privacy;
79     }
80 
81     /**
82      * Registers a DeviceConfig listener on start.
83      */
84     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()85     public void onStart() {
86         DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_CLIPBOARD,
87                 mContext.getMainExecutor(), mDeviceConfigListener);
88     }
89 
90     /**
91      * Removes the DeviceConfig listener on stop.
92      */
93     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()94     public void onStop() {
95         DeviceConfig.removeOnPropertiesChangedListener(mDeviceConfigListener);
96     }
97 
updateConfig()98     private void updateConfig() {
99         mDefault = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_CLIPBOARD,
100                 ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS,
101                 ClipboardManager.DEVICE_CONFIG_DEFAULT_SHOW_ACCESS_NOTIFICATIONS);
102         updateState(mPreference);
103     }
104 
105 }
106