2 * Copyright (C) 2014-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.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;
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;
39 import cyanogenmod.providers.CMSettings;
41 import org.cyanogenmod.internal.logging.CMMetricsLogger;
43 import static com.android.internal.util.cm.PowerMenuConstants.*;
45 import java.util.Arrays;
46 import java.util.ArrayList;
47 import java.util.List;
49 public class PowerMenuActions extends SettingsPreferenceFragment {
50 final static String TAG = "PowerMenuActions";
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;
64 private ArrayList<String> mLocalUserConfig = new ArrayList<String>();
65 private String[] mAvailableActions;
66 private String[] mAllActions;
69 public void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
72 addPreferencesFromResource(R.xml.power_menu_settings);
73 mContext = getActivity().getApplicationContext();
75 mAvailableActions = getActivity().getResources().getStringArray(
76 R.array.power_menu_actions_array);
77 mAllActions = PowerMenuConstants.getAllActions();
79 for (String action : mAllActions) {
80 // Remove preferences not present in the overlay
81 if (!isActionAllowed(action)) {
82 getPreferenceScreen().removePreference(findPreference(action));
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);
113 protected int getMetricsCategory() {
114 return CMMetricsLogger.POWER_MENU_ACTIONS;
118 public void onStart() {
121 if (mRebootPref != null) {
122 mRebootPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_REBOOT));
125 if (mScreenshotPref != null) {
126 mScreenshotPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SCREENSHOT));
129 if (mAirplanePref != null) {
130 mAirplanePref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_AIRPLANE));
133 if (mUsersPref != null) {
134 if (!UserHandle.MU_ENABLED || !UserManager.supportsMultipleUsers()) {
135 getPreferenceScreen().removePreference(findPreference(GLOBAL_ACTION_KEY_USERS));
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);
146 if (mSettingsPref != null) {
147 mSettingsPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SETTINGS));
150 if (mLockdownPref != null) {
151 mLockdownPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_LOCKDOWN));
154 if (mBugReportPref != null) {
155 mBugReportPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_BUGREPORT));
158 if (mSilentPref != null) {
159 mSilentPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SILENT));
162 if (mVoiceAssistPref != null) {
163 mVoiceAssistPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_VOICEASSIST));
166 if (mAssistPref != null) {
167 mAssistPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_ASSIST));
174 public void onResume() {
180 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
183 if (preference == mRebootPref) {
184 value = mRebootPref.isChecked();
185 updateUserConfig(value, GLOBAL_ACTION_KEY_REBOOT);
187 } else if (preference == mScreenshotPref) {
188 value = mScreenshotPref.isChecked();
189 updateUserConfig(value, GLOBAL_ACTION_KEY_SCREENSHOT);
191 } else if (preference == mAirplanePref) {
192 value = mAirplanePref.isChecked();
193 updateUserConfig(value, GLOBAL_ACTION_KEY_AIRPLANE);
195 } else if (preference == mUsersPref) {
196 value = mUsersPref.isChecked();
197 updateUserConfig(value, GLOBAL_ACTION_KEY_USERS);
199 } else if (preference == mSettingsPref) {
200 value = mSettingsPref.isChecked();
201 updateUserConfig(value, GLOBAL_ACTION_KEY_SETTINGS);
203 } else if (preference == mLockdownPref) {
204 value = mLockdownPref.isChecked();
205 updateUserConfig(value, GLOBAL_ACTION_KEY_LOCKDOWN);
207 } else if (preference == mBugReportPref) {
208 value = mBugReportPref.isChecked();
209 updateUserConfig(value, GLOBAL_ACTION_KEY_BUGREPORT);
211 } else if (preference == mSilentPref) {
212 value = mSilentPref.isChecked();
213 updateUserConfig(value, GLOBAL_ACTION_KEY_SILENT);
215 } else if (preference == mVoiceAssistPref) {
216 value = mVoiceAssistPref.isChecked();
217 updateUserConfig(value, GLOBAL_ACTION_KEY_VOICEASSIST);
219 } else if (preference == mAssistPref) {
220 value = mAssistPref.isChecked();
221 updateUserConfig(value, GLOBAL_ACTION_KEY_ASSIST);
224 return super.onPreferenceTreeClick(preferenceScreen, preference);
229 private boolean settingsArrayContains(String preference) {
230 return mLocalUserConfig.contains(preference);
233 private boolean isActionAllowed(String action) {
234 if (Arrays.asList(mAvailableActions).contains(action)) {
240 private void updateUserConfig(boolean enabled, String action) {
242 if (!settingsArrayContains(action)) {
243 mLocalUserConfig.add(action);
246 if (settingsArrayContains(action)) {
247 mLocalUserConfig.remove(action);
253 private void updatePreferences() {
254 boolean bugreport = Settings.Secure.getInt(getContentResolver(),
255 Settings.Secure.BUGREPORT_IN_POWER_MENU, 0) != 0;
257 if (mBugReportPref != null) {
258 mBugReportPref.setEnabled(bugreport);
260 mBugReportPref.setSummary(null);
262 mBugReportPref.setSummary(R.string.power_menu_bug_report_disabled);
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);
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);
280 for (String action : savedActions.split("\\|")) {
281 mLocalUserConfig.add(action);
286 private void saveUserConfig() {
287 StringBuilder s = new StringBuilder();
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);
299 for (int i = 0; i < setactions.size(); i++) {
300 s.append(setactions.get(i).toString());
301 if (i != setactions.size() - 1) {
306 CMSettings.Secure.putStringForUser(getContentResolver(),
307 CMSettings.Secure.POWER_MENU_ACTIONS, s.toString(), UserHandle.USER_CURRENT);
308 updatePowerMenuDialog();
311 private void updatePowerMenuDialog() {
312 Intent u = new Intent();
313 u.setAction(Intent.UPDATE_POWER_MENU);
314 mContext.sendBroadcastAsUser(u, UserHandle.ALL);