OSDN Git Service

Merge tag 'android-6.0.1_r74' into HEAD
[android-x86/packages-apps-Settings.git] / src / com / android / settings / cyanogenmod / PowerMenuActions.java
1 /*
2  * Copyright (C) 2014-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.Context;
20 import android.content.Intent;
21 import android.content.pm.UserInfo;
22 import android.content.res.Resources;
23 import android.os.Bundle;
24 import android.os.SystemProperties;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.preference.CheckBoxPreference;
28 import android.preference.Preference;
29 import android.preference.PreferenceScreen;
30 import android.preference.ListPreference;
31 import android.preference.Preference.OnPreferenceChangeListener;
32 import android.provider.Settings;
33
34 import com.android.internal.logging.MetricsLogger;
35 import com.android.settings.R;
36 import com.android.settings.SettingsPreferenceFragment;
37 import com.android.internal.util.cm.PowerMenuConstants;
38
39 import cyanogenmod.providers.CMSettings;
40
41 import org.cyanogenmod.internal.logging.CMMetricsLogger;
42
43 import static com.android.internal.util.cm.PowerMenuConstants.*;
44
45 import java.util.Arrays;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 public class PowerMenuActions extends SettingsPreferenceFragment {
50     final static String TAG = "PowerMenuActions";
51
52     private CheckBoxPreference mRebootPref;
53     private CheckBoxPreference mScreenshotPref;
54     private CheckBoxPreference mAirplanePref;
55     private CheckBoxPreference mUsersPref;
56     private CheckBoxPreference mSettingsPref;
57     private CheckBoxPreference mLockdownPref;
58     private CheckBoxPreference mBugReportPref;
59     private CheckBoxPreference mSilentPref;
60     private CheckBoxPreference mVoiceAssistPref;
61     private CheckBoxPreference mAssistPref;
62
63     Context mContext;
64     private ArrayList<String> mLocalUserConfig = new ArrayList<String>();
65     private String[] mAvailableActions;
66     private String[] mAllActions;
67
68     @Override
69     public void onCreate(Bundle savedInstanceState) {
70         super.onCreate(savedInstanceState);
71
72         addPreferencesFromResource(R.xml.power_menu_settings);
73         mContext = getActivity().getApplicationContext();
74
75         mAvailableActions = getActivity().getResources().getStringArray(
76                 R.array.power_menu_actions_array);
77         mAllActions = PowerMenuConstants.getAllActions();
78
79         for (String action : mAllActions) {
80         // Remove preferences not present in the overlay
81             if (!isActionAllowed(action)) {
82                 getPreferenceScreen().removePreference(findPreference(action));
83                 continue;
84             }
85
86             if (action.equals(GLOBAL_ACTION_KEY_REBOOT)) {
87                 mRebootPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_REBOOT);
88             } else if (action.equals(GLOBAL_ACTION_KEY_SCREENSHOT)) {
89                 mScreenshotPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_SCREENSHOT);
90             } else if (action.equals(GLOBAL_ACTION_KEY_AIRPLANE)) {
91                 mAirplanePref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_AIRPLANE);
92             } else if (action.equals(GLOBAL_ACTION_KEY_USERS)) {
93                 mUsersPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_USERS);
94             } else if (action.equals(GLOBAL_ACTION_KEY_SETTINGS)) {
95                 mSettingsPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_SETTINGS);
96             } else if (action.equals(GLOBAL_ACTION_KEY_LOCKDOWN)) {
97                 mLockdownPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_LOCKDOWN);
98             } else if (action.equals(GLOBAL_ACTION_KEY_BUGREPORT)) {
99                 mBugReportPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_BUGREPORT);
100             } else if (action.equals(GLOBAL_ACTION_KEY_SILENT)) {
101                 mSilentPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_SILENT);
102             } else if (action.equals(GLOBAL_ACTION_KEY_VOICEASSIST)) {
103                 mSilentPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_VOICEASSIST);
104             } else if (action.equals(GLOBAL_ACTION_KEY_ASSIST)) {
105                 mSilentPref = (CheckBoxPreference) findPreference(GLOBAL_ACTION_KEY_ASSIST);
106             }
107         }
108
109         getUserConfig();
110     }
111
112     @Override
113     protected int getMetricsCategory() {
114         return CMMetricsLogger.POWER_MENU_ACTIONS;
115     }
116
117     @Override
118     public void onStart() {
119         super.onStart();
120
121         if (mRebootPref != null) {
122             mRebootPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_REBOOT));
123         }
124
125         if (mScreenshotPref != null) {
126             mScreenshotPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SCREENSHOT));
127         }
128
129         if (mAirplanePref != null) {
130             mAirplanePref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_AIRPLANE));
131         }
132
133         if (mUsersPref != null) {
134             if (!UserHandle.MU_ENABLED || !UserManager.supportsMultipleUsers()) {
135                 getPreferenceScreen().removePreference(findPreference(GLOBAL_ACTION_KEY_USERS));
136                 mUsersPref = null;
137             } else {
138                 List<UserInfo> users = ((UserManager) mContext.getSystemService(
139                         Context.USER_SERVICE)).getUsers();
140                 boolean enabled = (users.size() > 1);
141                 mUsersPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_USERS) && enabled);
142                 mUsersPref.setEnabled(enabled);
143             }
144         }
145
146         if (mSettingsPref != null) {
147             mSettingsPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SETTINGS));
148         }
149
150         if (mLockdownPref != null) {
151             mLockdownPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_LOCKDOWN));
152         }
153
154         if (mBugReportPref != null) {
155             mBugReportPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_BUGREPORT));
156         }
157
158         if (mSilentPref != null) {
159             mSilentPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SILENT));
160         }
161
162         if (mVoiceAssistPref != null) {
163             mVoiceAssistPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_VOICEASSIST));
164         }
165
166         if (mAssistPref != null) {
167             mAssistPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_ASSIST));
168         }
169
170         updatePreferences();
171     }
172
173     @Override
174     public void onResume() {
175         super.onResume();
176         updatePreferences();
177     }
178
179     @Override
180     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
181         boolean value;
182
183         if (preference == mRebootPref) {
184             value = mRebootPref.isChecked();
185             updateUserConfig(value, GLOBAL_ACTION_KEY_REBOOT);
186
187         } else if (preference == mScreenshotPref) {
188             value = mScreenshotPref.isChecked();
189             updateUserConfig(value, GLOBAL_ACTION_KEY_SCREENSHOT);
190
191         } else if (preference == mAirplanePref) {
192             value = mAirplanePref.isChecked();
193             updateUserConfig(value, GLOBAL_ACTION_KEY_AIRPLANE);
194
195         } else if (preference == mUsersPref) {
196             value = mUsersPref.isChecked();
197             updateUserConfig(value, GLOBAL_ACTION_KEY_USERS);
198
199         } else if (preference == mSettingsPref) {
200             value = mSettingsPref.isChecked();
201             updateUserConfig(value, GLOBAL_ACTION_KEY_SETTINGS);
202
203         } else if (preference == mLockdownPref) {
204             value = mLockdownPref.isChecked();
205             updateUserConfig(value, GLOBAL_ACTION_KEY_LOCKDOWN);
206
207         } else if (preference == mBugReportPref) {
208             value = mBugReportPref.isChecked();
209             updateUserConfig(value, GLOBAL_ACTION_KEY_BUGREPORT);
210
211         } else if (preference == mSilentPref) {
212             value = mSilentPref.isChecked();
213             updateUserConfig(value, GLOBAL_ACTION_KEY_SILENT);
214
215         } else if (preference == mVoiceAssistPref) {
216             value = mVoiceAssistPref.isChecked();
217             updateUserConfig(value, GLOBAL_ACTION_KEY_VOICEASSIST);
218
219         } else if (preference == mAssistPref) {
220             value = mAssistPref.isChecked();
221             updateUserConfig(value, GLOBAL_ACTION_KEY_ASSIST);
222
223         } else {
224             return super.onPreferenceTreeClick(preferenceScreen, preference);
225         }
226         return true;
227     }
228
229     private boolean settingsArrayContains(String preference) {
230         return mLocalUserConfig.contains(preference);
231     }
232
233     private boolean isActionAllowed(String action) {
234         if (Arrays.asList(mAvailableActions).contains(action)) {
235             return true;
236         }
237         return false;
238     }
239
240     private void updateUserConfig(boolean enabled, String action) {
241         if (enabled) {
242             if (!settingsArrayContains(action)) {
243                 mLocalUserConfig.add(action);
244             }
245         } else {
246             if (settingsArrayContains(action)) {
247                 mLocalUserConfig.remove(action);
248             }
249         }
250         saveUserConfig();
251     }
252
253     private void updatePreferences() {
254         boolean bugreport = Settings.Secure.getInt(getContentResolver(),
255                 Settings.Secure.BUGREPORT_IN_POWER_MENU, 0) != 0;
256
257         if (mBugReportPref != null) {
258             mBugReportPref.setEnabled(bugreport);
259             if (bugreport) {
260                 mBugReportPref.setSummary(null);
261             } else {
262                 mBugReportPref.setSummary(R.string.power_menu_bug_report_disabled);
263             }
264         }
265     }
266
267     private void getUserConfig() {
268         mLocalUserConfig.clear();
269         String[] defaultActions;
270         String savedActions = CMSettings.Secure.getStringForUser(mContext.getContentResolver(),
271                 CMSettings.Secure.POWER_MENU_ACTIONS, UserHandle.USER_CURRENT);
272
273         if (savedActions == null) {
274             defaultActions = mContext.getResources().getStringArray(
275                     com.android.internal.R.array.config_globalActionsList);
276             for (String action : defaultActions) {
277                 mLocalUserConfig.add(action);
278             }
279         } else {
280             for (String action : savedActions.split("\\|")) {
281                 mLocalUserConfig.add(action);
282             }
283         }
284     }
285
286     private void saveUserConfig() {
287         StringBuilder s = new StringBuilder();
288
289         // TODO: Use DragSortListView
290         ArrayList<String> setactions = new ArrayList<String>();
291         for (String action : mAllActions) {
292             if (settingsArrayContains(action) && isActionAllowed(action)) {
293                 setactions.add(action);
294             } else {
295                 continue;
296             }
297         }
298
299         for (int i = 0; i < setactions.size(); i++) {
300             s.append(setactions.get(i).toString());
301             if (i != setactions.size() - 1) {
302                 s.append("|");
303             }
304         }
305
306         CMSettings.Secure.putStringForUser(getContentResolver(),
307                 CMSettings.Secure.POWER_MENU_ACTIONS, s.toString(), UserHandle.USER_CURRENT);
308         updatePowerMenuDialog();
309     }
310
311     private void updatePowerMenuDialog() {
312         Intent u = new Intent();
313         u.setAction(Intent.UPDATE_POWER_MENU);
314         mContext.sendBroadcastAsUser(u, UserHandle.ALL);
315     }
316 }