OSDN Git Service

Merge tag 'android-6.0.1_r74' into HEAD
[android-x86/packages-apps-Settings.git] / src / com / android / settings / cyanogenmod / CMBaseSystemSettingSwitchBar.java
1 /*
2 * Copyright (C) 2015 The CyanogenMod 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.cyanogenmod;
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 import android.widget.Switch;
25 import com.android.settings.widget.SwitchBar;
26
27 import cyanogenmod.providers.CMSettings;
28
29 public class CMBaseSystemSettingSwitchBar implements SwitchBar.OnSwitchChangeListener  {
30     private Context mContext;
31     private SwitchBar mSwitchBar;
32     private SettingsObserver mSettingsObserver;
33     private boolean mListeningToOnSwitchChange = false;
34
35     private boolean mStateMachineEvent;
36
37     private final String mSettingKey;
38     private final int mDefaultState;
39
40     private final SwitchBarChangeCallback mCallback;
41     public interface SwitchBarChangeCallback {
42         public void onEnablerChanged(boolean isEnabled);
43     }
44
45     public CMBaseSystemSettingSwitchBar(Context context, SwitchBar switchBar, String key,
46                                       boolean defaultState, SwitchBarChangeCallback callback) {
47         mContext = context;
48         mSwitchBar = switchBar;
49         mSettingKey = key;
50         mDefaultState = defaultState ? 1 : 0;
51         mCallback = callback;
52         mSettingsObserver = new SettingsObserver(new Handler());
53         setupSwitchBar();
54     }
55
56     public void setupSwitchBar() {
57         setSwitchState();
58         if (!mListeningToOnSwitchChange) {
59             mSwitchBar.addOnSwitchChangeListener(this);
60             mListeningToOnSwitchChange = true;
61         }
62         mSwitchBar.show();
63     }
64
65     public void teardownSwitchBar() {
66         if (mListeningToOnSwitchChange) {
67             mSwitchBar.removeOnSwitchChangeListener(this);
68             mListeningToOnSwitchChange = false;
69         }
70         mSwitchBar.hide();
71     }
72
73     public void resume(Context context) {
74         mContext = context;
75         if (!mListeningToOnSwitchChange) {
76             mSwitchBar.addOnSwitchChangeListener(this);
77             mSettingsObserver.observe();
78
79             mListeningToOnSwitchChange = true;
80         }
81     }
82
83     public void pause() {
84         if (mListeningToOnSwitchChange) {
85             mSwitchBar.removeOnSwitchChangeListener(this);
86             mSettingsObserver.unobserve();
87
88             mListeningToOnSwitchChange = false;
89         }
90     }
91
92     private void setSwitchBarChecked(boolean checked) {
93         mStateMachineEvent = true;
94         mSwitchBar.setChecked(checked);
95         mStateMachineEvent = false;
96         if (mCallback != null) {
97             mCallback.onEnablerChanged(checked);
98         }
99     }
100
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;
107     }
108
109     @Override
110     public void onSwitchChanged(Switch switchView, boolean isChecked) {
111         //Do nothing if called as a result of a state machine event
112         if (mStateMachineEvent) {
113             return;
114         }
115
116         // Handle a switch change
117         CMSettings.System.putInt(mContext.getContentResolver(),
118                 mSettingKey, isChecked ? 1 : 0);
119
120         if (mCallback != null) {
121             mCallback.onEnablerChanged(isChecked);
122         }
123     }
124
125     class SettingsObserver extends ContentObserver {
126         SettingsObserver(Handler handler) {
127             super(handler);
128         }
129
130         void observe() {
131             ContentResolver resolver = mContext.getContentResolver();
132             resolver.registerContentObserver(CMSettings.System.getUriFor(
133                     mSettingKey), false, this);
134             update();
135         }
136
137         void unobserve() {
138             ContentResolver resolver = mContext.getContentResolver();
139             resolver.unregisterContentObserver(this);
140         }
141
142         @Override
143         public void onChange(boolean selfChange) {
144             update();
145         }
146
147         @Override
148         public void onChange(boolean selfChange, Uri uri) {
149             update();
150         }
151
152         public void update() {
153             setSwitchState();
154         }
155     }
156 }