2 * Copyright (C) 2015 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.cyanogenmod;
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 import android.widget.Switch;
25 import com.android.settings.widget.SwitchBar;
27 import cyanogenmod.providers.CMSettings;
29 public class CMBaseSystemSettingSwitchBar implements SwitchBar.OnSwitchChangeListener {
30 private Context mContext;
31 private SwitchBar mSwitchBar;
32 private SettingsObserver mSettingsObserver;
33 private boolean mListeningToOnSwitchChange = false;
35 private boolean mStateMachineEvent;
37 private final String mSettingKey;
38 private final int mDefaultState;
40 private final SwitchBarChangeCallback mCallback;
41 public interface SwitchBarChangeCallback {
42 public void onEnablerChanged(boolean isEnabled);
45 public CMBaseSystemSettingSwitchBar(Context context, SwitchBar switchBar, String key,
46 boolean defaultState, SwitchBarChangeCallback callback) {
48 mSwitchBar = switchBar;
50 mDefaultState = defaultState ? 1 : 0;
52 mSettingsObserver = new SettingsObserver(new Handler());
56 public void setupSwitchBar() {
58 if (!mListeningToOnSwitchChange) {
59 mSwitchBar.addOnSwitchChangeListener(this);
60 mListeningToOnSwitchChange = true;
65 public void teardownSwitchBar() {
66 if (mListeningToOnSwitchChange) {
67 mSwitchBar.removeOnSwitchChangeListener(this);
68 mListeningToOnSwitchChange = false;
73 public void resume(Context context) {
75 if (!mListeningToOnSwitchChange) {
76 mSwitchBar.addOnSwitchChangeListener(this);
77 mSettingsObserver.observe();
79 mListeningToOnSwitchChange = true;
84 if (mListeningToOnSwitchChange) {
85 mSwitchBar.removeOnSwitchChangeListener(this);
86 mSettingsObserver.unobserve();
88 mListeningToOnSwitchChange = false;
92 private void setSwitchBarChecked(boolean checked) {
93 mStateMachineEvent = true;
94 mSwitchBar.setChecked(checked);
95 mStateMachineEvent = false;
96 if (mCallback != null) {
97 mCallback.onEnablerChanged(checked);
101 private void setSwitchState() {
102 boolean enabled = CMSettings.System.getInt(mContext.getContentResolver(),
103 mSettingKey, mDefaultState) == 1;
104 mStateMachineEvent = true;
105 setSwitchBarChecked(enabled);
106 mStateMachineEvent = false;
110 public void onSwitchChanged(Switch switchView, boolean isChecked) {
111 //Do nothing if called as a result of a state machine event
112 if (mStateMachineEvent) {
116 // Handle a switch change
117 CMSettings.System.putInt(mContext.getContentResolver(),
118 mSettingKey, isChecked ? 1 : 0);
120 if (mCallback != null) {
121 mCallback.onEnablerChanged(isChecked);
125 class SettingsObserver extends ContentObserver {
126 SettingsObserver(Handler handler) {
131 ContentResolver resolver = mContext.getContentResolver();
132 resolver.registerContentObserver(CMSettings.System.getUriFor(
133 mSettingKey), false, this);
138 ContentResolver resolver = mContext.getContentResolver();
139 resolver.unregisterContentObserver(this);
143 public void onChange(boolean selfChange) {
148 public void onChange(boolean selfChange, Uri uri) {
152 public void update() {