diff options
author | Danesh M <daneshm90@gmail.com> | 2016-01-12 13:21:19 -0800 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2016-01-15 15:32:34 -0800 |
commit | ff1cf255fd66ef2e2e3b73c11cce47a7258d1195 (patch) | |
tree | 4ae608b311ac5f6d3fef7852a0fd7ff59cbf45cd /src/com/cyanogenmod | |
parent | a461a5c365b16bdeb5a6a60e4931754b038d1404 (diff) | |
download | android_packages_apps_LockClock-ff1cf255fd66ef2e2e3b73c11cce47a7258d1195.tar.gz android_packages_apps_LockClock-ff1cf255fd66ef2e2e3b73c11cce47a7258d1195.tar.bz2 android_packages_apps_LockClock-ff1cf255fd66ef2e2e3b73c11cce47a7258d1195.zip |
LockClock : Update to v23 + runtime permissions
CYNGNOS-1559
Change-Id: Iaefe9de0a77251b3a2d0f772405ca1d1cdb73630
Diffstat (limited to 'src/com/cyanogenmod')
3 files changed, 123 insertions, 10 deletions
diff --git a/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java b/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java index e125929..5f0f352 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; @@ -39,13 +43,16 @@ import java.util.ArrayList; import java.util.List; 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 +61,23 @@ 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); + } } @Override @@ -92,6 +98,54 @@ 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()); + calendarList.setEntries(calEntries.getEntries()); + calendarList.setEntryValues(calEntries.getEntryValues()); + } + + @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 //=============================================================================================== diff --git a/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java b/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java index 97dcef6..1a42a20 100644 --- a/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java +++ b/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java @@ -16,6 +16,7 @@ package com.cyanogenmod.lockclock.preference; +import android.Manifest; import android.app.AlertDialog; import android.app.Dialog; import android.content.ContentResolver; @@ -23,6 +24,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.location.LocationManager; import android.os.Bundle; import android.preference.EditTextPreference; @@ -41,8 +43,9 @@ import com.cyanogenmod.lockclock.misc.Preferences; import com.cyanogenmod.lockclock.weather.WeatherUpdateService; public class WeatherPreferences extends PreferenceFragment implements - SharedPreferences.OnSharedPreferenceChangeListener { + SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener { private static final String TAG = "WeatherPreferences"; + private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; private static final String[] LOCATION_PREF_KEYS = new String[] { Constants.WEATHER_USE_CUSTOM_LOCATION, @@ -60,9 +63,10 @@ public class WeatherPreferences extends PreferenceFragment implements private SwitchPreference mUseMetric; private IconSelectionPreference mIconSet; private SwitchPreference mUseCustomlocation; - + private SwitchPreference mShowWeather; private Context mContext; private ContentResolver mResolver; + private Runnable mPostResumeRunnable; @Override public void onCreate(Bundle savedInstanceState) { @@ -81,6 +85,9 @@ public class WeatherPreferences extends PreferenceFragment implements mUseMetric = (SwitchPreference) findPreference(Constants.WEATHER_USE_METRIC); mUseCustomlocation = (SwitchPreference) findPreference(Constants.WEATHER_USE_CUSTOM_LOCATION); + mShowWeather = (SwitchPreference) findPreference(Constants.SHOW_WEATHER); + mShowWeather.setOnPreferenceChangeListener(this); + // At first placement/start default the use of Metric units based on locale // If we had a previously set value already, this will just reset the same value Boolean defValue = Preferences.useMetricUnits(mContext); @@ -99,6 +106,16 @@ public class WeatherPreferences extends PreferenceFragment implements super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + + if (!hasLocationPermission(mContext)) { + mShowWeather.setChecked(false); + } + + if (mPostResumeRunnable != null) { + mPostResumeRunnable.run(); + mPostResumeRunnable = null; + } + updateLocationSummary(); updateFontColorsSummary(); updateIconSetSummary(); @@ -171,6 +188,11 @@ public class WeatherPreferences extends PreferenceFragment implements mContext.sendBroadcast(updateIntent); } + public static boolean hasLocationPermission(Context context) { + return context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED; + } + //=============================================================================================== // Utility classes and supporting methods //=============================================================================================== @@ -222,4 +244,34 @@ public class WeatherPreferences extends PreferenceFragment implements mIconSet.setSummary(mIconSet.getEntry()); } } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, + int[] grantResults) { + if (requestCode == LOCATION_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 + mPostResumeRunnable = new Runnable() { + @Override + public void run() { + mShowWeather.setChecked(true); + } + }; + } + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (preference == mShowWeather) { + if (!hasLocationPermission(mContext)) { + String[] permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}; + requestPermissions(permissions, LOCATION_PERMISSION_REQUEST_CODE); + return false; + } + } + return true; + } } diff --git a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java index a228ec7..ea0b89c 100644 --- a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java +++ b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java @@ -39,6 +39,8 @@ import com.cyanogenmod.lockclock.ClockWidgetProvider; import com.cyanogenmod.lockclock.misc.Constants; import com.cyanogenmod.lockclock.misc.Preferences; import com.cyanogenmod.lockclock.misc.WidgetUtils; +import com.cyanogenmod.lockclock.preference.WeatherPreferences; + import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; @@ -129,6 +131,11 @@ public class WeatherUpdateService extends Service { return false; } + if (!WeatherPreferences.hasLocationPermission(this)) { + if (D) Log.v(TAG, "Application does not have the location permission"); + return false; + } + if (force) { Preferences.setCachedWeatherInfo(this, 0, null); } |