1/*
2 * Copyright (C) 2016 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;
21import android.database.ContentObserver;
22import android.net.Uri;
23import android.os.Handler;
24import android.provider.Settings;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27import android.support.v7.preference.TwoStatePreference;
28
29import com.android.settings.Utils;
30import com.android.settings.core.PreferenceControllerMixin;
31import com.android.settingslib.core.AbstractPreferenceController;
32import com.android.settingslib.core.lifecycle.LifecycleObserver;
33import com.android.settingslib.core.lifecycle.events.OnPause;
34import com.android.settingslib.core.lifecycle.events.OnResume;
35
36import static android.provider.Settings.System.VIBRATE_WHEN_RINGING;
37
38public class VibrateWhenRingPreferenceController extends AbstractPreferenceController
39        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
40        LifecycleObserver, OnResume, OnPause {
41
42    private static final String KEY_VIBRATE_WHEN_RINGING = "vibrate_when_ringing";
43    private SettingObserver mSettingObserver;
44
45    public VibrateWhenRingPreferenceController(Context context) {
46        super(context);
47    }
48
49    @Override
50    public void displayPreference(PreferenceScreen screen) {
51        super.displayPreference(screen);
52        Preference preference = screen.findPreference(KEY_VIBRATE_WHEN_RINGING);
53        if (preference != null) {
54            mSettingObserver = new SettingObserver(preference);
55            preference.setPersistent(false);
56        }
57    }
58
59    @Override
60    public void onResume() {
61        if (mSettingObserver != null) {
62            mSettingObserver.register(true /* register */);
63        }
64    }
65
66    @Override
67    public void onPause() {
68        if (mSettingObserver != null) {
69            mSettingObserver.register(false /* register */);
70        }
71    }
72
73    @Override
74    public boolean handlePreferenceTreeClick(Preference preference) {
75        return false;
76    }
77
78    @Override
79    public String getPreferenceKey() {
80        return KEY_VIBRATE_WHEN_RINGING;
81    }
82
83    @Override
84    public boolean isAvailable() {
85        return Utils.isVoiceCapable(mContext);
86    }
87
88    @Override
89    public void updateState(Preference preference) {
90        ((TwoStatePreference) preference).setChecked(
91            Settings.System.getInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING, 0) != 0);
92    }
93
94    @Override
95    public boolean onPreferenceChange(Preference preference, Object newValue) {
96        final boolean val = (Boolean) newValue;
97        return Settings.System.putInt(mContext.getContentResolver(),
98            VIBRATE_WHEN_RINGING, val ? 1 : 0);
99    }
100
101    private final class SettingObserver extends ContentObserver {
102
103        private final Uri VIBRATE_WHEN_RINGING_URI =
104            Settings.System.getUriFor(VIBRATE_WHEN_RINGING);
105
106        private final Preference mPreference;
107
108        public SettingObserver(Preference preference) {
109            super(new Handler());
110            mPreference = preference;
111        }
112
113        public void register(boolean register) {
114            final ContentResolver cr = mContext.getContentResolver();
115            if (register) {
116                cr.registerContentObserver(VIBRATE_WHEN_RINGING_URI, false, this);
117            } else {
118                cr.unregisterContentObserver(this);
119            }
120        }
121
122        @Override
123        public void onChange(boolean selfChange, Uri uri) {
124            super.onChange(selfChange, uri);
125            if (VIBRATE_WHEN_RINGING_URI.equals(uri)) {
126                updateState(mPreference);
127            }
128        }
129    }
130
131}
132