FileDocCategorySizeDatePackage
DateTimeSettings.javaAPI DocAndroid 1.5 API12758Wed May 06 22:42:48 BST 2009com.android.settings

DateTimeSettings

public class DateTimeSettings extends android.preference.PreferenceActivity implements android.content.SharedPreferences.OnSharedPreferenceChangeListener, TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener

Fields Summary
private static final String
HOURS_12
private static final String
HOURS_24
private Calendar
mDummyDate
private static final String
KEY_DATE_FORMAT
private static final String
KEY_AUTO_TIME
private static final int
DIALOG_DATEPICKER
private static final int
DIALOG_TIMEPICKER
private android.preference.CheckBoxPreference
mAutoPref
private android.preference.Preference
mTimePref
private android.preference.Preference
mTime24Pref
private android.preference.Preference
mTimeZone
private android.preference.Preference
mDatePref
private android.preference.ListPreference
mDateFormat
private android.content.BroadcastReceiver
mIntentReceiver
Constructors Summary
Methods Summary
private char[]formatOffset(int off)

        off = off / 1000 / 60;

        char[] buf = new char[9];
        buf[0] = 'G";
        buf[1] = 'M";
        buf[2] = 'T";

        if (off < 0) {
            buf[3] = '-";
            off = -off;
        } else {
            buf[3] = '+";
        }

        int hours = off / 60; 
        int minutes = off % 60;

        buf[4] = (char) ('0" + hours / 10);
        buf[5] = (char) ('0" + hours % 10);

        buf[6] = ':";

        buf[7] = (char) ('0" + minutes / 10);
        buf[8] = (char) ('0" + minutes % 10);

        return buf;
    
private booleangetAutoState()

        try {
            return Settings.System.getInt(getContentResolver(), 
                Settings.System.AUTO_TIME) > 0;            
        } catch (SettingNotFoundException snfe) {
            return true;
        }
    
private java.lang.StringgetDateFormat()

        return Settings.System.getString(getContentResolver(), 
                Settings.System.DATE_FORMAT);
    
private java.lang.StringgetTimeZoneText()

        TimeZone    tz = java.util.Calendar.getInstance().getTimeZone();
        boolean daylight = tz.inDaylightTime(new Date());
        StringBuilder sb = new StringBuilder();

        sb.append(formatOffset(tz.getRawOffset() +
                               (daylight ? tz.getDSTSavings() : 0))).
            append(", ").
            append(tz.getDisplayName(daylight, TimeZone.LONG));

        return sb.toString();        
    
private voidinitUI()

        boolean autoEnabled = getAutoState();

        mDummyDate = Calendar.getInstance();
        mDummyDate.set(mDummyDate.get(Calendar.YEAR), 11, 31, 13, 0, 0);
        
        mAutoPref = (CheckBoxPreference) findPreference(KEY_AUTO_TIME);
        mAutoPref.setChecked(autoEnabled);
        mTimePref = findPreference("time");
        mTime24Pref = findPreference("24 hour");
        mTimeZone = findPreference("timezone");
        mDatePref = findPreference("date");
        mDateFormat = (ListPreference) findPreference(KEY_DATE_FORMAT);
        
        int currentFormatIndex = -1;
        String [] dateFormats = getResources().getStringArray(R.array.date_format_values);
        String [] formattedDates = new String[dateFormats.length];
        String currentFormat = getDateFormat();
        // Initialize if DATE_FORMAT is not set in the system settings
        // This can happen after a factory reset (or data wipe)
        if (currentFormat == null) {
            currentFormat = getResources().getString(R.string.default_date_format);
            setDateFormat(currentFormat);
        }
        for (int i = 0; i < formattedDates.length; i++) {
            formattedDates[i] = DateFormat.format(dateFormats[i], mDummyDate).toString();
            if (currentFormat.equals(dateFormats[i])) currentFormatIndex = i;
        }
        
        mDateFormat.setEntries(formattedDates);
        mDateFormat.setEntryValues(R.array.date_format_values);
        mDateFormat.setValue(currentFormat);
        
        mTimePref.setEnabled(!autoEnabled);
        mDatePref.setEnabled(!autoEnabled);
        mTimeZone.setEnabled(!autoEnabled);

        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);        
    
private booleanis24Hour()

        return DateFormat.is24HourFormat(this);
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        updateTimeAndDateDisplay();
    
protected voidonCreate(android.os.Bundle icicle)

    
    
        
        super.onCreate(icicle);
        
        addPreferencesFromResource(R.xml.date_time_prefs);
        
        initUI();        
    
public android.app.DialogonCreateDialog(int id)

        Dialog d;

        switch (id) {
        case DIALOG_DATEPICKER: {
            final Calendar calendar = Calendar.getInstance();
            d = new DatePickerDialog(
                this,
                this,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
            d.setTitle(getResources().getString(R.string.date_time_changeDate_text));
            break;
        }
        case DIALOG_TIMEPICKER: {
            final Calendar calendar = Calendar.getInstance();
            d = new TimePickerDialog(
                    this,
                    this,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    DateFormat.is24HourFormat(this));
            d.setTitle(getResources().getString(R.string.date_time_changeTime_text));
            break;
        }
        default:
            d = null;
            break;
        }

        return d;
    
public voidonDateSet(android.widget.DatePicker view, int year, int month, int day)

        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            SystemClock.setCurrentTimeMillis(when);
        }
        updateTimeAndDateDisplay();
    
protected voidonPause()

        super.onPause();
        unregisterReceiver(mIntentReceiver);
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)

        if (preference == mDatePref) {
            showDialog(DIALOG_DATEPICKER);
        } else if (preference == mTimePref) {
            // The 24-hour mode may have changed, so recreate the dialog
            removeDialog(DIALOG_TIMEPICKER);
            showDialog(DIALOG_TIMEPICKER);
        } else if (preference == mTime24Pref) {
            set24Hour(((CheckBoxPreference)mTime24Pref).isChecked());
            updateTimeAndDateDisplay();
            timeUpdated();
        } else if (preference == mTimeZone) {
            Intent intent = new Intent();
            intent.setClass(this, ZoneList.class);
            startActivityForResult(intent, 0);
        }
        return false;
    
public voidonPrepareDialog(int id, android.app.Dialog d)

        switch (id) {
        case DIALOG_DATEPICKER: {
            DatePickerDialog datePicker = (DatePickerDialog)d;
            final Calendar calendar = Calendar.getInstance();
            datePicker.updateDate(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH));
            break;
        }
        case DIALOG_TIMEPICKER: {
            TimePickerDialog timePicker = (TimePickerDialog)d;
            final Calendar calendar = Calendar.getInstance();
            timePicker.updateTime(
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE));
            break;
        }
        default:
            break;
        }
    
protected voidonResume()

        super.onResume();

        ((CheckBoxPreference)mTime24Pref).setChecked(is24Hour());

        // Register for time ticks and other reasons for time change
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(mIntentReceiver, filter, null, null);
        
        updateTimeAndDateDisplay();
    
public voidonSharedPreferenceChanged(android.content.SharedPreferences preferences, java.lang.String key)

        if (key.equals(KEY_DATE_FORMAT)) {
            String format = preferences.getString(key, 
                    getResources().getString(R.string.default_date_format));
            Settings.System.putString(getContentResolver(), 
                    Settings.System.DATE_FORMAT, format);
            updateTimeAndDateDisplay();
        } else if (key.equals(KEY_AUTO_TIME)) {
            boolean autoEnabled = preferences.getBoolean(key, true);
            Settings.System.putInt(getContentResolver(), 
                    Settings.System.AUTO_TIME, 
                    autoEnabled ? 1 : 0);
            mTimePref.setEnabled(!autoEnabled);
            mDatePref.setEnabled(!autoEnabled);
            mTimeZone.setEnabled(!autoEnabled);
        }
    
public voidonTimeSet(android.widget.TimePicker view, int hourOfDay, int minute)

        Calendar c = Calendar.getInstance();

        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            SystemClock.setCurrentTimeMillis(when);
        }
        updateTimeAndDateDisplay();
        
        // We don't need to call timeUpdated() here because the TIME_CHANGED
        // broadcast is sent by the AlarmManager as a side effect of setting the
        // SystemClock time.
    
private voidset24Hour(boolean is24Hour)

        Settings.System.putString(getContentResolver(),
                Settings.System.TIME_12_24,
                is24Hour? HOURS_24 : HOURS_12);
    
private voidsetDateFormat(java.lang.String format)

        Settings.System.putString(getContentResolver(), Settings.System.DATE_FORMAT, format);        
    
private voidtimeUpdated()

        Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
        sendBroadcast(timeChanged);
    
private voidupdateTimeAndDateDisplay()

        java.text.DateFormat shortDateFormat = DateFormat.getDateFormat(this);
        Date now = Calendar.getInstance().getTime();
        Date dummyDate = mDummyDate.getTime();
        mTimePref.setSummary(DateFormat.getTimeFormat(this).format(now));
        mTimeZone.setSummary(getTimeZoneText());
        mDatePref.setSummary(shortDateFormat.format(now));
        mDateFormat.setSummary(shortDateFormat.format(dummyDate));