Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2013 Slimroms Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.android.settings.nexus;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnCancelListener;
- import android.content.pm.PackageManager;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.preference.EditTextPreference;
- import android.preference.ListPreference;
- import android.preference.Preference;
- import android.preference.Preference.OnPreferenceChangeListener;
- import android.preference.PreferenceFragment;
- import android.preference.PreferenceScreen;
- import android.preference.SwitchPreference;
- import android.provider.Settings;
- import android.provider.Settings.SettingNotFoundException;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.EditText;
- import com.android.settings.R;
- import com.android.settings.SettingsPreferenceFragment;
- import com.android.settings.Utils;
- import net.margaritov.preference.colorpicker.ColorPickerPreference;
- import java.util.Date;
- import java.util.Locale;
- public class StatusBarClockStyle extends SettingsPreferenceFragment
- implements OnPreferenceChangeListener {
- private static final String TAG = "StatusBarClockStyle";
- private static final String PREF_ENABLE = "clock_style";
- private static final String PREF_AM_PM_STYLE = "status_bar_am_pm";
- private static final String PREF_COLOR_PICKER = "clock_color";
- private static final String PREF_CLOCK_DATE_DISPLAY = "clock_date_display";
- private static final String PREF_CLOCK_DATE_STYLE = "clock_date_style";
- private static final String PREF_CLOCK_DATE_FORMAT = "clock_date_format";
- private static final String STATUS_BAR_CLOCK = "status_bar_show_clock";
- public static final int CLOCK_DATE_STYLE_LOWERCASE = 1;
- public static final int CLOCK_DATE_STYLE_UPPERCASE = 2;
- private static final int CUSTOM_CLOCK_DATE_FORMAT_INDEX = 18;
- private static final int MENU_RESET = Menu.FIRST;
- private static final int DLG_RESET = 0;
- private ListPreference mClockStyle;
- private ListPreference mClockAmPmStyle;
- private ColorPickerPreference mColorPicker;
- private ListPreference mClockDateDisplay;
- private ListPreference mClockDateStyle;
- private ListPreference mClockDateFormat;
- private SwitchPreference mStatusBarClock;
- private boolean mCheckPreferences;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- createCustomView();
- }
- private PreferenceScreen createCustomView() {
- mCheckPreferences = false;
- PreferenceScreen prefSet = getPreferenceScreen();
- if (prefSet != null) {
- prefSet.removeAll();
- }
- addPreferencesFromResource(R.xml.status_bar_clock_style);
- prefSet = getPreferenceScreen();
- PackageManager pm = getPackageManager();
- Resources systemUiResources;
- try {
- systemUiResources = pm.getResourcesForApplication("com.android.systemui");
- } catch (Exception e) {
- Log.e(TAG, "can't access systemui resources",e);
- return null;
- }
- mClockStyle = (ListPreference) findPreference(PREF_ENABLE);
- mClockStyle.setOnPreferenceChangeListener(this);
- mClockStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
- .getContentResolver(), Settings.System.STATUSBAR_CLOCK_STYLE,
- 0)));
- mClockStyle.setSummary(mClockStyle.getEntry());
- mClockAmPmStyle = (ListPreference) prefSet.findPreference(PREF_AM_PM_STYLE);
- mClockAmPmStyle.setOnPreferenceChangeListener(this);
- mClockAmPmStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
- .getContentResolver(), Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE,
- 0)));
- boolean is24hour = DateFormat.is24HourFormat(getActivity());
- if (is24hour) {
- mClockAmPmStyle.setSummary(R.string.status_bar_am_pm_info);
- } else {
- mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntry());
- }
- mClockAmPmStyle.setEnabled(!is24hour);
- mColorPicker = (ColorPickerPreference) findPreference(PREF_COLOR_PICKER);
- mColorPicker.setOnPreferenceChangeListener(this);
- mColorPicker.setSummary(mColorPicker.getSummaryText() + ColorPickerPreference.convertToARGB(Settings.System.getInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_COLOR, mColorPicker.getPrefDefault())));
- mColorPicker.setNewPreviewColor(Settings.System.getInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_COLOR, mColorPicker.getPrefDefault()));
- mClockDateDisplay = (ListPreference) findPreference(PREF_CLOCK_DATE_DISPLAY);
- mClockDateDisplay.setOnPreferenceChangeListener(this);
- mClockDateDisplay.setValue(Integer.toString(Settings.System.getInt(getActivity()
- .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY,
- 0)));
- mClockDateDisplay.setSummary(mClockDateDisplay.getEntry());
- mClockDateStyle = (ListPreference) findPreference(PREF_CLOCK_DATE_STYLE);
- mClockDateStyle.setOnPreferenceChangeListener(this);
- mClockDateStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
- .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE,
- 0)));
- mClockDateStyle.setSummary(mClockDateStyle.getEntry());
- mClockDateFormat = (ListPreference) findPreference(PREF_CLOCK_DATE_FORMAT);
- mClockDateFormat.setOnPreferenceChangeListener(this);
- if (mClockDateFormat.getValue() == null) {
- mClockDateFormat.setValue("EEE");
- }
- parseClockDateFormats();
- mStatusBarClock = (SwitchPreference) prefSet.findPreference(STATUS_BAR_CLOCK);
- mStatusBarClock.setChecked((Settings.System.getInt(
- getActivity().getApplicationContext().getContentResolver(),
- Settings.System.STATUS_BAR_CLOCK, 1) == 1));
- mColorPicker.setPreviewDim(mStatusBarClock.isChecked());
- mStatusBarClock.setOnPreferenceChangeListener(this);
- boolean mClockDateToggle = Settings.System.getInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, 0) != 0;
- if (!mClockDateToggle) {
- mClockDateStyle.setEnabled(false);
- mClockDateFormat.setEnabled(false);
- }
- setHasOptionsMenu(true);
- mCheckPreferences = true;
- return prefSet;
- }
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- if (!mCheckPreferences) {
- return false;
- }
- AlertDialog dialog;
- if (preference == mClockAmPmStyle) {
- int val = Integer.parseInt((String) newValue);
- int index = mClockAmPmStyle.findIndexOfValue((String) newValue);
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE, val);
- mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntries()[index]);
- return true;
- } else if (preference == mClockStyle) {
- int val = Integer.parseInt((String) newValue);
- int index = mClockStyle.findIndexOfValue((String) newValue);
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_STYLE, val);
- mClockStyle.setSummary(mClockStyle.getEntries()[index]);
- return true;
- } else if (preference == mColorPicker) {
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_COLOR, (Integer) newValue);
- preference.setSummary(((ColorPickerPreference) preference).getSummaryText() + ColorPickerPreference.convertToARGB((Integer) newValue));
- return true;
- } else if (preference == mClockDateDisplay) {
- int val = Integer.parseInt((String) newValue);
- int index = mClockDateDisplay.findIndexOfValue((String) newValue);
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, val);
- mClockDateDisplay.setSummary(mClockDateDisplay.getEntries()[index]);
- if (val == 0) {
- mClockDateStyle.setEnabled(false);
- mClockDateFormat.setEnabled(false);
- } else {
- mClockDateStyle.setEnabled(true);
- mClockDateFormat.setEnabled(true);
- }
- return true;
- } else if (preference == mClockDateStyle) {
- int val = Integer.parseInt((String) newValue);
- int index = mClockDateStyle.findIndexOfValue((String) newValue);
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_STYLE, val);
- mClockDateStyle.setSummary(mClockDateStyle.getEntries()[index]);
- parseClockDateFormats();
- return true;
- } else if (preference == mStatusBarClock) {
- Settings.System.putInt(getActivity().getApplicationContext().getContentResolver(),
- Settings.System.STATUS_BAR_CLOCK,
- (Boolean) newValue ? 1 : 0);
- mColorPicker.setPreviewDim((Boolean) newValue);
- return true;
- } else if (preference == mClockDateFormat) {
- int index = mClockDateFormat.findIndexOfValue((String) newValue);
- if (index == CUSTOM_CLOCK_DATE_FORMAT_INDEX) {
- AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
- alert.setTitle(R.string.clock_date_string_edittext_title);
- alert.setMessage(R.string.clock_date_string_edittext_summary);
- final EditText input = new EditText(getActivity());
- String oldText = Settings.System.getString(
- getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_FORMAT);
- if (oldText != null) {
- input.setText(oldText);
- }
- alert.setView(input);
- alert.setPositiveButton(R.string.menu_save, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialogInterface, int whichButton) {
- String value = input.getText().toString();
- if (value.equals("")) {
- return;
- }
- Settings.System.putString(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, value);
- return;
- }
- });
- alert.setNegativeButton(R.string.menu_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialogInterface, int which) {
- return;
- }
- });
- dialog = alert.create();
- dialog.show();
- } else {
- if ((String) newValue != null) {
- Settings.System.putString(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, (String) newValue);
- }
- }
- return true;
- }
- return false;
- }
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- menu.add(0, MENU_RESET, 0, R.string.reset)
- .setIcon(R.drawable.ic_settings_reset)
- .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case MENU_RESET:
- showDialogInner(DLG_RESET);
- return true;
- default:
- return super.onContextItemSelected(item);
- }
- }
- private void parseClockDateFormats() {
- // Parse and repopulate mClockDateFormats's entries based on current date.
- String[] dateEntries = getResources().getStringArray(R.array.clock_date_format_entries_values);
- CharSequence parsedDateEntries[];
- parsedDateEntries = new String[dateEntries.length];
- Date now = new Date();
- int lastEntry = dateEntries.length - 1;
- int dateFormat = Settings.System.getInt(getActivity()
- .getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE, 0);
- for (int i = 0; i < dateEntries.length; i++) {
- if (i == lastEntry) {
- parsedDateEntries[i] = dateEntries[i];
- } else {
- String newDate;
- CharSequence dateString = DateFormat.format(dateEntries[i], now);
- if (dateFormat == CLOCK_DATE_STYLE_LOWERCASE) {
- newDate = dateString.toString().toLowerCase();
- } else if (dateFormat == CLOCK_DATE_STYLE_UPPERCASE) {
- newDate = dateString.toString().toUpperCase();
- } else {
- newDate = dateString.toString();
- }
- parsedDateEntries[i] = newDate;
- }
- }
- mClockDateFormat.setEntries(parsedDateEntries);
- }
- private void showDialogInner(int id) {
- DialogFragment newFragment = MyAlertDialogFragment.newInstance(id);
- newFragment.setTargetFragment(this, 0);
- newFragment.show(getFragmentManager(), "dialog " + id);
- }
- public static class MyAlertDialogFragment extends DialogFragment {
- public static MyAlertDialogFragment newInstance(int id) {
- MyAlertDialogFragment frag = new MyAlertDialogFragment();
- Bundle args = new Bundle();
- args.putInt("id", id);
- frag.setArguments(args);
- return frag;
- }
- StatusBarClockStyle getOwner() {
- return (StatusBarClockStyle) getTargetFragment();
- }
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- int id = getArguments().getInt("id");
- switch (id) {
- case DLG_RESET:
- return new AlertDialog.Builder(getActivity())
- .setTitle(R.string.reset)
- .setMessage(R.string.status_bar_clock_style_reset_message)
- .setNegativeButton(R.string.cancel, null)
- .setPositiveButton(R.string.dlg_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Settings.System.putInt(getActivity().getContentResolver(),
- Settings.System.STATUSBAR_CLOCK_COLOR, -2);
- getOwner().createCustomView();
- }
- })
- .create();
- }
- throw new IllegalArgumentException("unknown id " + id);
- }
- @Override
- public void onCancel(DialogInterface dialog) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement