Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.15 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2013 Slimroms 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.nexus;
  18.  
  19. import android.app.AlertDialog;
  20. import android.app.Dialog;
  21. import android.app.DialogFragment;
  22. import android.content.DialogInterface;
  23. import android.content.DialogInterface.OnCancelListener;
  24. import android.content.pm.PackageManager;
  25. import android.content.res.Resources;
  26. import android.os.Bundle;
  27. import android.preference.EditTextPreference;
  28. import android.preference.ListPreference;
  29. import android.preference.Preference;
  30. import android.preference.Preference.OnPreferenceChangeListener;
  31. import android.preference.PreferenceFragment;
  32. import android.preference.PreferenceScreen;
  33. import android.preference.SwitchPreference;
  34. import android.provider.Settings;
  35. import android.provider.Settings.SettingNotFoundException;
  36. import android.text.format.DateFormat;
  37. import android.util.Log;
  38. import android.view.Menu;
  39. import android.view.MenuInflater;
  40. import android.view.MenuItem;
  41. import android.widget.EditText;
  42.  
  43. import com.android.settings.R;
  44. import com.android.settings.SettingsPreferenceFragment;
  45. import com.android.settings.Utils;
  46.  
  47. import net.margaritov.preference.colorpicker.ColorPickerPreference;
  48.  
  49. import java.util.Date;
  50. import java.util.Locale;
  51.  
  52. public class StatusBarClockStyle extends SettingsPreferenceFragment
  53. implements OnPreferenceChangeListener {
  54.  
  55. private static final String TAG = "StatusBarClockStyle";
  56.  
  57. private static final String PREF_ENABLE = "clock_style";
  58. private static final String PREF_AM_PM_STYLE = "status_bar_am_pm";
  59. private static final String PREF_COLOR_PICKER = "clock_color";
  60. private static final String PREF_CLOCK_DATE_DISPLAY = "clock_date_display";
  61. private static final String PREF_CLOCK_DATE_STYLE = "clock_date_style";
  62. private static final String PREF_CLOCK_DATE_FORMAT = "clock_date_format";
  63. private static final String STATUS_BAR_CLOCK = "status_bar_show_clock";
  64.  
  65. public static final int CLOCK_DATE_STYLE_LOWERCASE = 1;
  66. public static final int CLOCK_DATE_STYLE_UPPERCASE = 2;
  67. private static final int CUSTOM_CLOCK_DATE_FORMAT_INDEX = 18;
  68.  
  69. private static final int MENU_RESET = Menu.FIRST;
  70.  
  71. private static final int DLG_RESET = 0;
  72.  
  73. private ListPreference mClockStyle;
  74. private ListPreference mClockAmPmStyle;
  75. private ColorPickerPreference mColorPicker;
  76. private ListPreference mClockDateDisplay;
  77. private ListPreference mClockDateStyle;
  78. private ListPreference mClockDateFormat;
  79. private SwitchPreference mStatusBarClock;
  80.  
  81. private boolean mCheckPreferences;
  82.  
  83. @Override
  84. public void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. createCustomView();
  87. }
  88.  
  89. private PreferenceScreen createCustomView() {
  90. mCheckPreferences = false;
  91. PreferenceScreen prefSet = getPreferenceScreen();
  92. if (prefSet != null) {
  93. prefSet.removeAll();
  94. }
  95.  
  96. addPreferencesFromResource(R.xml.status_bar_clock_style);
  97. prefSet = getPreferenceScreen();
  98.  
  99. PackageManager pm = getPackageManager();
  100. Resources systemUiResources;
  101. try {
  102. systemUiResources = pm.getResourcesForApplication("com.android.systemui");
  103. } catch (Exception e) {
  104. Log.e(TAG, "can't access systemui resources",e);
  105. return null;
  106. }
  107.  
  108. mClockStyle = (ListPreference) findPreference(PREF_ENABLE);
  109. mClockStyle.setOnPreferenceChangeListener(this);
  110. mClockStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
  111. .getContentResolver(), Settings.System.STATUSBAR_CLOCK_STYLE,
  112. 0)));
  113. mClockStyle.setSummary(mClockStyle.getEntry());
  114.  
  115. mClockAmPmStyle = (ListPreference) prefSet.findPreference(PREF_AM_PM_STYLE);
  116. mClockAmPmStyle.setOnPreferenceChangeListener(this);
  117. mClockAmPmStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
  118. .getContentResolver(), Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE,
  119. 0)));
  120. boolean is24hour = DateFormat.is24HourFormat(getActivity());
  121. if (is24hour) {
  122. mClockAmPmStyle.setSummary(R.string.status_bar_am_pm_info);
  123. } else {
  124. mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntry());
  125. }
  126. mClockAmPmStyle.setEnabled(!is24hour);
  127.  
  128. mColorPicker = (ColorPickerPreference) findPreference(PREF_COLOR_PICKER);
  129. mColorPicker.setOnPreferenceChangeListener(this);
  130. mColorPicker.setSummary(mColorPicker.getSummaryText() + ColorPickerPreference.convertToARGB(Settings.System.getInt(getActivity().getContentResolver(),
  131. Settings.System.STATUSBAR_CLOCK_COLOR, mColorPicker.getPrefDefault())));
  132. mColorPicker.setNewPreviewColor(Settings.System.getInt(getActivity().getContentResolver(),
  133. Settings.System.STATUSBAR_CLOCK_COLOR, mColorPicker.getPrefDefault()));
  134.  
  135. mClockDateDisplay = (ListPreference) findPreference(PREF_CLOCK_DATE_DISPLAY);
  136. mClockDateDisplay.setOnPreferenceChangeListener(this);
  137. mClockDateDisplay.setValue(Integer.toString(Settings.System.getInt(getActivity()
  138. .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY,
  139. 0)));
  140. mClockDateDisplay.setSummary(mClockDateDisplay.getEntry());
  141.  
  142. mClockDateStyle = (ListPreference) findPreference(PREF_CLOCK_DATE_STYLE);
  143. mClockDateStyle.setOnPreferenceChangeListener(this);
  144. mClockDateStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
  145. .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE,
  146. 0)));
  147. mClockDateStyle.setSummary(mClockDateStyle.getEntry());
  148.  
  149. mClockDateFormat = (ListPreference) findPreference(PREF_CLOCK_DATE_FORMAT);
  150. mClockDateFormat.setOnPreferenceChangeListener(this);
  151. if (mClockDateFormat.getValue() == null) {
  152. mClockDateFormat.setValue("EEE");
  153. }
  154.  
  155. parseClockDateFormats();
  156.  
  157. mStatusBarClock = (SwitchPreference) prefSet.findPreference(STATUS_BAR_CLOCK);
  158. mStatusBarClock.setChecked((Settings.System.getInt(
  159. getActivity().getApplicationContext().getContentResolver(),
  160. Settings.System.STATUS_BAR_CLOCK, 1) == 1));
  161. mColorPicker.setPreviewDim(mStatusBarClock.isChecked());
  162. mStatusBarClock.setOnPreferenceChangeListener(this);
  163.  
  164. boolean mClockDateToggle = Settings.System.getInt(getActivity().getContentResolver(),
  165. Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, 0) != 0;
  166. if (!mClockDateToggle) {
  167. mClockDateStyle.setEnabled(false);
  168. mClockDateFormat.setEnabled(false);
  169. }
  170.  
  171. setHasOptionsMenu(true);
  172. mCheckPreferences = true;
  173. return prefSet;
  174. }
  175.  
  176. public boolean onPreferenceChange(Preference preference, Object newValue) {
  177. if (!mCheckPreferences) {
  178. return false;
  179. }
  180. AlertDialog dialog;
  181.  
  182. if (preference == mClockAmPmStyle) {
  183. int val = Integer.parseInt((String) newValue);
  184. int index = mClockAmPmStyle.findIndexOfValue((String) newValue);
  185. Settings.System.putInt(getActivity().getContentResolver(),
  186. Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE, val);
  187. mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntries()[index]);
  188. return true;
  189. } else if (preference == mClockStyle) {
  190. int val = Integer.parseInt((String) newValue);
  191. int index = mClockStyle.findIndexOfValue((String) newValue);
  192. Settings.System.putInt(getActivity().getContentResolver(),
  193. Settings.System.STATUSBAR_CLOCK_STYLE, val);
  194. mClockStyle.setSummary(mClockStyle.getEntries()[index]);
  195. return true;
  196. } else if (preference == mColorPicker) {
  197. Settings.System.putInt(getActivity().getContentResolver(),
  198. Settings.System.STATUSBAR_CLOCK_COLOR, (Integer) newValue);
  199. preference.setSummary(((ColorPickerPreference) preference).getSummaryText() + ColorPickerPreference.convertToARGB((Integer) newValue));
  200. return true;
  201. } else if (preference == mClockDateDisplay) {
  202. int val = Integer.parseInt((String) newValue);
  203. int index = mClockDateDisplay.findIndexOfValue((String) newValue);
  204. Settings.System.putInt(getActivity().getContentResolver(),
  205. Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, val);
  206. mClockDateDisplay.setSummary(mClockDateDisplay.getEntries()[index]);
  207. if (val == 0) {
  208. mClockDateStyle.setEnabled(false);
  209. mClockDateFormat.setEnabled(false);
  210. } else {
  211. mClockDateStyle.setEnabled(true);
  212. mClockDateFormat.setEnabled(true);
  213. }
  214. return true;
  215. } else if (preference == mClockDateStyle) {
  216. int val = Integer.parseInt((String) newValue);
  217. int index = mClockDateStyle.findIndexOfValue((String) newValue);
  218. Settings.System.putInt(getActivity().getContentResolver(),
  219. Settings.System.STATUSBAR_CLOCK_DATE_STYLE, val);
  220. mClockDateStyle.setSummary(mClockDateStyle.getEntries()[index]);
  221. parseClockDateFormats();
  222. return true;
  223. } else if (preference == mStatusBarClock) {
  224. Settings.System.putInt(getActivity().getApplicationContext().getContentResolver(),
  225. Settings.System.STATUS_BAR_CLOCK,
  226. (Boolean) newValue ? 1 : 0);
  227. mColorPicker.setPreviewDim((Boolean) newValue);
  228. return true;
  229. } else if (preference == mClockDateFormat) {
  230. int index = mClockDateFormat.findIndexOfValue((String) newValue);
  231.  
  232. if (index == CUSTOM_CLOCK_DATE_FORMAT_INDEX) {
  233. AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
  234. alert.setTitle(R.string.clock_date_string_edittext_title);
  235. alert.setMessage(R.string.clock_date_string_edittext_summary);
  236.  
  237. final EditText input = new EditText(getActivity());
  238. String oldText = Settings.System.getString(
  239. getActivity().getContentResolver(),
  240. Settings.System.STATUSBAR_CLOCK_DATE_FORMAT);
  241. if (oldText != null) {
  242. input.setText(oldText);
  243. }
  244. alert.setView(input);
  245.  
  246. alert.setPositiveButton(R.string.menu_save, new DialogInterface.OnClickListener() {
  247. public void onClick(DialogInterface dialogInterface, int whichButton) {
  248. String value = input.getText().toString();
  249. if (value.equals("")) {
  250. return;
  251. }
  252. Settings.System.putString(getActivity().getContentResolver(),
  253. Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, value);
  254.  
  255. return;
  256. }
  257. });
  258.  
  259. alert.setNegativeButton(R.string.menu_cancel,
  260. new DialogInterface.OnClickListener() {
  261. public void onClick(DialogInterface dialogInterface, int which) {
  262. return;
  263. }
  264. });
  265. dialog = alert.create();
  266. dialog.show();
  267. } else {
  268. if ((String) newValue != null) {
  269. Settings.System.putString(getActivity().getContentResolver(),
  270. Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, (String) newValue);
  271. }
  272. }
  273. return true;
  274. }
  275. return false;
  276. }
  277.  
  278. @Override
  279. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  280. menu.add(0, MENU_RESET, 0, R.string.reset)
  281. .setIcon(R.drawable.ic_settings_reset)
  282. .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  283. }
  284.  
  285. @Override
  286. public boolean onOptionsItemSelected(MenuItem item) {
  287. switch (item.getItemId()) {
  288. case MENU_RESET:
  289. showDialogInner(DLG_RESET);
  290. return true;
  291. default:
  292. return super.onContextItemSelected(item);
  293. }
  294. }
  295.  
  296. private void parseClockDateFormats() {
  297. // Parse and repopulate mClockDateFormats's entries based on current date.
  298. String[] dateEntries = getResources().getStringArray(R.array.clock_date_format_entries_values);
  299. CharSequence parsedDateEntries[];
  300. parsedDateEntries = new String[dateEntries.length];
  301. Date now = new Date();
  302.  
  303. int lastEntry = dateEntries.length - 1;
  304. int dateFormat = Settings.System.getInt(getActivity()
  305. .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE, 0);
  306. for (int i = 0; i < dateEntries.length; i++) {
  307. if (i == lastEntry) {
  308. parsedDateEntries[i] = dateEntries[i];
  309. } else {
  310. String newDate;
  311. CharSequence dateString = DateFormat.format(dateEntries[i], now);
  312. if (dateFormat == CLOCK_DATE_STYLE_LOWERCASE) {
  313. newDate = dateString.toString().toLowerCase();
  314. } else if (dateFormat == CLOCK_DATE_STYLE_UPPERCASE) {
  315. newDate = dateString.toString().toUpperCase();
  316. } else {
  317. newDate = dateString.toString();
  318. }
  319.  
  320. parsedDateEntries[i] = newDate;
  321. }
  322. }
  323. mClockDateFormat.setEntries(parsedDateEntries);
  324. }
  325.  
  326. private void showDialogInner(int id) {
  327. DialogFragment newFragment = MyAlertDialogFragment.newInstance(id);
  328. newFragment.setTargetFragment(this, 0);
  329. newFragment.show(getFragmentManager(), "dialog " + id);
  330. }
  331.  
  332. public static class MyAlertDialogFragment extends DialogFragment {
  333.  
  334. public static MyAlertDialogFragment newInstance(int id) {
  335. MyAlertDialogFragment frag = new MyAlertDialogFragment();
  336. Bundle args = new Bundle();
  337. args.putInt("id", id);
  338. frag.setArguments(args);
  339. return frag;
  340. }
  341.  
  342. StatusBarClockStyle getOwner() {
  343. return (StatusBarClockStyle) getTargetFragment();
  344. }
  345.  
  346. @Override
  347. public Dialog onCreateDialog(Bundle savedInstanceState) {
  348. int id = getArguments().getInt("id");
  349. switch (id) {
  350. case DLG_RESET:
  351. return new AlertDialog.Builder(getActivity())
  352. .setTitle(R.string.reset)
  353. .setMessage(R.string.status_bar_clock_style_reset_message)
  354. .setNegativeButton(R.string.cancel, null)
  355. .setPositiveButton(R.string.dlg_ok,
  356. new DialogInterface.OnClickListener() {
  357. public void onClick(DialogInterface dialog, int which) {
  358. Settings.System.putInt(getActivity().getContentResolver(),
  359. Settings.System.STATUSBAR_CLOCK_COLOR, -2);
  360. getOwner().createCustomView();
  361. }
  362. })
  363. .create();
  364. }
  365. throw new IllegalArgumentException("unknown id " + id);
  366. }
  367.  
  368. @Override
  369. public void onCancel(DialogInterface dialog) {
  370.  
  371. }
  372. }
  373.  
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement