• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.notification;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.SettingsPreferenceFragment;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
34 import com.android.settingslib.core.lifecycle.events.OnPause;
35 import com.android.settingslib.core.lifecycle.events.OnResume;
36 
37 public abstract class SettingPrefController extends AbstractPreferenceController
38         implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
39 
40     protected static final int DEFAULT_ON = 1;
41 
42     private SettingsPreferenceFragment mParent;
43     protected SettingsObserver mSettingsObserver;
44     protected SettingPref mPreference;
45 
SettingPrefController(Context context, SettingsPreferenceFragment parent, Lifecycle lifecycle)46     public SettingPrefController(Context context, SettingsPreferenceFragment parent,
47             Lifecycle lifecycle) {
48         super(context);
49         mParent = parent;
50         if (lifecycle != null) {
51             lifecycle.addObserver(this);
52         }
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         mPreference.init(mParent);
58         super.displayPreference(screen);
59         if (isAvailable()) {
60             mSettingsObserver = new SettingsObserver();
61         }
62     }
63 
64     @Override
getPreferenceKey()65     public String getPreferenceKey() {
66         return mPreference.getKey();
67     }
68 
69     @Override
isAvailable()70     public boolean isAvailable() {
71         return mPreference.isApplicable(mContext);
72     }
73 
74     @Override
updateState(Preference preference)75     public void updateState(Preference preference) {
76         mPreference.update(mContext);
77     }
78 
79     @Override
onResume()80     public void onResume() {
81         if (mSettingsObserver != null) {
82             mSettingsObserver.register(true /* register */);
83         }
84     }
85 
86     @Override
onPause()87     public void onPause() {
88         if (mSettingsObserver != null) {
89             mSettingsObserver.register(false /* register */);
90         }
91     }
92 
93     @VisibleForTesting
94     final class SettingsObserver extends ContentObserver {
SettingsObserver()95         public SettingsObserver() {
96             super(new Handler());
97         }
98 
register(boolean register)99         public void register(boolean register) {
100             final ContentResolver cr = mContext.getContentResolver();
101             if (register) {
102                 cr.registerContentObserver(mPreference.getUri(), false, this);
103             } else {
104                 cr.unregisterContentObserver(this);
105             }
106         }
107 
108         @Override
onChange(boolean selfChange, Uri uri)109         public void onChange(boolean selfChange, Uri uri) {
110             super.onChange(selfChange, uri);
111             if (mPreference.getUri().equals(uri)) {
112                 mPreference.update(mContext);
113             }
114         }
115     }
116 
117 }
118