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
17package com.android.settings.notification;
18
19import android.content.ContentResolver;
20import android.content.Context;
21
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Handler;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27
28import com.android.internal.annotations.VisibleForTesting;
29import com.android.settings.SettingsPreferenceFragment;
30import com.android.settings.core.PreferenceController;
31import com.android.settings.core.lifecycle.Lifecycle;
32import com.android.settings.core.lifecycle.LifecycleObserver;
33import com.android.settings.core.lifecycle.events.OnPause;
34import com.android.settings.core.lifecycle.events.OnResume;
35import java.util.List;
36
37public abstract class SettingPrefController extends PreferenceController implements
38    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
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
56    public void displayPreference(PreferenceScreen screen) {
57        mPreference.init(mParent);
58        if (isAvailable()) {
59            mSettingsObserver = new SettingsObserver();
60        }
61    }
62
63    @Override
64    public String getPreferenceKey() {
65        return mPreference.getKey();
66    }
67
68    @Override
69    public boolean isAvailable() {
70        return mPreference.isApplicable(mContext);
71    }
72
73    @Override
74    public void updateState(Preference preference) {
75        mPreference.update(mContext);
76    }
77
78    @Override
79    public void onResume() {
80        if (mSettingsObserver != null) {
81            mSettingsObserver.register(true /* register */);
82        }
83    }
84
85    @Override
86    public void onPause() {
87        if (mSettingsObserver != null) {
88            mSettingsObserver.register(false /* register */);
89        }
90    }
91
92    @VisibleForTesting
93    final class SettingsObserver extends ContentObserver {
94        public SettingsObserver() {
95            super(new Handler());
96        }
97
98        public void register(boolean register) {
99            final ContentResolver cr = mContext.getContentResolver();
100            if (register) {
101                cr.registerContentObserver(mPreference.getUri(), false, this);
102            } else {
103                cr.unregisterContentObserver(this);
104            }
105        }
106
107        @Override
108        public void onChange(boolean selfChange, Uri uri) {
109            super.onChange(selfChange, uri);
110            if (mPreference.getUri().equals(uri)) {
111                mPreference.update(mContext);
112            }
113        }
114    }
115
116}
117