aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanesh M <daneshm90@gmail.com>2016-01-12 13:21:19 -0800
committerGerrit Code Review <gerrit@cyanogenmod.org>2016-01-15 15:32:34 -0800
commitff1cf255fd66ef2e2e3b73c11cce47a7258d1195 (patch)
tree4ae608b311ac5f6d3fef7852a0fd7ff59cbf45cd
parenta461a5c365b16bdeb5a6a60e4931754b038d1404 (diff)
downloadandroid_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
-rw-r--r--Android.mk5
-rw-r--r--AndroidManifest.xml2
-rw-r--r--res/xml/preferences_weather.xml1
-rw-r--r--src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java70
-rw-r--r--src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java56
-rw-r--r--src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java7
6 files changed, 128 insertions, 13 deletions
diff --git a/Android.mk b/Android.mk
index 6827a41..db27b6f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -19,7 +19,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under, src)
-LOCAL_SDK_VERSION := 21
+LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := LockClock
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true
@@ -32,6 +32,7 @@ LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dir))
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_AAPT_FLAGS += --extra-packages com.google.android.gms
-LOCAL_STATIC_JAVA_LIBRARIES := play
+LOCAL_STATIC_JAVA_LIBRARIES := play \
+ org.apache.http.legacy
include $(BUILD_PACKAGE)
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e2c979a..c15f9de 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -19,7 +19,7 @@
android:versionCode="21"
android:versionName="3.4.1" >
- <uses-sdk android:minSdkVersion="21" />
+ <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="23" />
<permission android:name="com.cyanogenmod.lockclock.permission.READ_WEATHER" />
diff --git a/res/xml/preferences_weather.xml b/res/xml/preferences_weather.xml
index b017948..db832a6 100644
--- a/res/xml/preferences_weather.xml
+++ b/res/xml/preferences_weather.xml
@@ -29,6 +29,7 @@
<ListPreference
android:key="weather_source"
android:defaultValue="yahoo"
+ android:dependency="show_weather"
android:summary="%s"
android:entries="@array/weather_source_entries"
android:entryValues="@array/weather_source_values"
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);
}