diff options
Diffstat (limited to 'src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java')
-rw-r--r-- | src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java | 92 |
1 files changed, 84 insertions, 8 deletions
diff --git a/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java b/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java index e125929..2d6b4a8 100644 --- a/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java +++ b/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java @@ -16,11 +16,13 @@ package com.cyanogenmod.lockclock.preference; +import android.Manifest; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -28,6 +30,8 @@ import android.preference.ListPreference; import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; import android.provider.CalendarContract; import com.cyanogenmod.lockclock.ClockWidgetProvider; @@ -37,15 +41,19 @@ import com.cyanogenmod.lockclock.misc.Constants; import java.util.ArrayList; import java.util.List; +import java.util.HashSet; public class CalendarPreferences extends PreferenceFragment implements - OnSharedPreferenceChangeListener { + OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener { + + private static final int CALENDAR_PERMISSION_REQUEST_CODE = 1; private Context mContext; private ListPreference mFontColor; private ListPreference mEventDetailsFontColor; private ListPreference mHighlightFontColor; private ListPreference mHighlightDetailsFontColor; + private SwitchPreference mShowCalendar; @Override public void onCreate(Bundle savedInstanceState) { @@ -54,24 +62,26 @@ public class CalendarPreferences extends PreferenceFragment implements addPreferencesFromResource(R.xml.preferences_calendar); mContext = getActivity(); - // The calendar list entries and values are determined at run time, not in XML - MultiSelectListPreference calendarList = - (MultiSelectListPreference) findPreference(Constants.CALENDAR_LIST); - CalendarEntries calEntries = CalendarEntries.findCalendars(getActivity()); - calendarList.setEntries(calEntries.getEntries()); - calendarList.setEntryValues(calEntries.getEntryValues()); - mFontColor = (ListPreference) findPreference(Constants.CALENDAR_FONT_COLOR); mEventDetailsFontColor = (ListPreference) findPreference(Constants.CALENDAR_DETAILS_FONT_COLOR); mHighlightFontColor = (ListPreference) findPreference(Constants.CALENDAR_UPCOMING_EVENTS_FONT_COLOR); mHighlightDetailsFontColor = (ListPreference) findPreference(Constants.CALENDAR_UPCOMING_EVENTS_DETAILS_FONT_COLOR); updateFontColorsSummary(); + + mShowCalendar = (SwitchPreference) findPreference(Constants.SHOW_CALENDAR); + mShowCalendar.setOnPreferenceChangeListener(this); } @Override public void onResume() { super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + if (!hasCalendarPermission()) { + mShowCalendar.setChecked(false); + } else { + mShowCalendar.setChecked(true); + updateCalendars(); + } } @Override @@ -92,6 +102,72 @@ public class CalendarPreferences extends PreferenceFragment implements mContext.sendBroadcast(updateIntent); } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, + int[] grantResults) { + if (requestCode == CALENDAR_PERMISSION_REQUEST_CODE) { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // We only get here if user tried to enable the preference, + // hence safe to turn it on after permission is granted + mShowCalendar.setChecked(true); + updateCalendars(); + } + } + } + + private boolean hasCalendarPermission() { + return mContext.checkSelfPermission(Manifest.permission.READ_CALENDAR) + == PackageManager.PERMISSION_GRANTED; + } + + private void updateCalendars() { + if (!hasCalendarPermission()) { + return; + } + // The calendar list entries and values are determined at run time, not in XML + MultiSelectListPreference calendarList = + (MultiSelectListPreference) findPreference(Constants.CALENDAR_LIST); + CalendarEntries calEntries = CalendarEntries.findCalendars(getActivity()); + + boolean firstTime = com.cyanogenmod.lockclock.misc.Preferences.calendarsToDisplay(mContext) == null; + calendarList.setEntries(calEntries.getEntries()); + calendarList.setEntryValues(calEntries.getEntryValues()); + if (firstTime) { + // by default, select all the things + HashSet defaults = new HashSet(); + for (CharSequence s : calEntries.getEntryValues()) { + defaults.add((String) s); + } + calendarList.setValues(defaults); + } + + if (calEntries.getEntryValues().length == 0) { + calendarList.setSummary(R.string.calendars_none_found_summary); + calendarList.setEnabled(false); + } else { + calendarList.setSummary(R.string.calendars_summary); + calendarList.setEnabled(true); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (preference == mShowCalendar) { + if (hasCalendarPermission()) { + updateCalendars(); + } else { + Boolean enabled = (Boolean) newValue; + if (enabled) { + String[] permissions = new String[]{Manifest.permission.READ_CALENDAR}; + requestPermissions(permissions, CALENDAR_PERMISSION_REQUEST_CODE); + return false; + } + } + } + return true; + } + //=============================================================================================== // Utility classes and supporting methods //=============================================================================================== |