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