From a4d05bf056c439bcb0d0b0ae3afded0cc50372bf Mon Sep 17 00:00:00 2001 From: JJamet Date: Wed, 26 Jul 2017 22:03:01 +0200 Subject: [PATCH] Add color calendar listener --- RememBirthday-UI/build.gradle | 2 + .../account/BackgroundStatusHandler.java | 2 +- .../activity/SettingsFragment.java | 33 +++++++- .../service/CalendarSyncAdapterService.java | 75 ++++++++++--------- .../service/MainIntentService.java | 21 +++--- .../src/main/res/values-fr/strings.xml | 3 + .../src/main/res/values/strings.xml | 2 + .../src/main/res/xml/preferences.xml | 11 ++- 8 files changed, 101 insertions(+), 48 deletions(-) diff --git a/RememBirthday-UI/build.gradle b/RememBirthday-UI/build.gradle index cbcb273..5f12346 100644 --- a/RememBirthday-UI/build.gradle +++ b/RememBirthday-UI/build.gradle @@ -71,4 +71,6 @@ dependencies { compile ('com.github.ozodrukh:CircularReveal:2.0.1@aar') { transitive = true; } + // Color preference + compile 'com.github.Kunzisoft:AndroidClearChroma:1.8' } diff --git a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/BackgroundStatusHandler.java b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/BackgroundStatusHandler.java index 71fa4f3..4251dfa 100644 --- a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/BackgroundStatusHandler.java +++ b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/BackgroundStatusHandler.java @@ -70,6 +70,6 @@ public void handleMessage(Message msg) { } public interface StatusChangeListener { - public void onStatusChange(boolean progress); + void onStatusChange(boolean progress); } } diff --git a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/activity/SettingsFragment.java b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/activity/SettingsFragment.java index 8229289..8b344ec 100644 --- a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/activity/SettingsFragment.java +++ b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/activity/SettingsFragment.java @@ -2,9 +2,11 @@ import android.annotation.SuppressLint; import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; +import android.os.Messenger; import android.support.v4.app.DialogFragment; import android.support.v7.preference.EditTextPreference; import android.support.v7.preference.Preference; @@ -13,13 +15,16 @@ import android.util.Log; import android.widget.Toast; +import com.kunzisoft.androidclearchroma.ChromaPreferenceFragmentCompat; import com.kunzisoft.remembirthday.BuildConfig; import com.kunzisoft.remembirthday.R; import com.kunzisoft.remembirthday.account.AccountResolver; +import com.kunzisoft.remembirthday.account.BackgroundStatusHandler; import com.kunzisoft.remembirthday.account.CalendarAccount; import com.kunzisoft.remembirthday.preference.PreferencesManager; import com.kunzisoft.remembirthday.preference.TimePreference; import com.kunzisoft.remembirthday.preference.TimePreferenceDialogFragmentCompat; +import com.kunzisoft.remembirthday.service.MainIntentService; import com.kunzisoft.remembirthday.utility.IntentCall; import java.util.Date; @@ -31,7 +36,7 @@ * WARNING : Use compatibility library known for display bugs * @see StackOverflow Question */ -public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { +public class SettingsFragment extends ChromaPreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG_FRAGMENT_DIALOG = "com.kunzisoft.remembirthday.TAG_FRAGMENT_DIALOG"; @@ -144,7 +149,7 @@ public void onPause() { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) { // Verify values of notifications - if(key.equals(getString(R.string.pref_notifications_days_key))) { + if (key.equals(getString(R.string.pref_notifications_days_key))) { // Only for 99 days maximum before the event Pattern p = Pattern.compile(PreferencesManager.PATTERN_REMINDER_PREF); Matcher m = p.matcher(notificationsDaysEditTextPreference.getText()); @@ -168,9 +173,31 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String } // Update list after change sort or order - if(key.equals(getString(R.string.pref_contacts_sort_list_key)) + if (key.equals(getString(R.string.pref_contacts_sort_list_key)) || key.equals(getString(R.string.pref_contacts_order_list_key))) { getActivity().setResult(Activity.RESULT_OK); } + + // set new color + if (key.equals(getString(R.string.pref_calendar_color_key))) { + startServiceAction(MainIntentService.ACTION_CHANGE_COLOR); + } + } + + /** + * Start service with action, while executing, show progress + */ + public void startServiceAction(String action) { + // Send all information needed to service to do in other thread + Intent intent = new Intent(getContext(), MainIntentService.class); + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(new BackgroundStatusHandler(null)); + intent.putExtra(MainIntentService.EXTRA_MESSENGER, messenger); + + intent.setAction(action); + + // start service with intent + getContext().startService(intent); } } diff --git a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/CalendarSyncAdapterService.java b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/CalendarSyncAdapterService.java index 92fc49d..42640e4 100644 --- a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/CalendarSyncAdapterService.java +++ b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/CalendarSyncAdapterService.java @@ -48,33 +48,6 @@ public CalendarSyncAdapterService() { super(); } - private class CalendarSyncAdapter extends AbstractThreadedSyncAdapter { - - CalendarSyncAdapter() { - super(CalendarSyncAdapterService.this, true); - } - - @Override - public void onPerformSync(Account account, Bundle extras, String authority, - ContentProviderClient provider, SyncResult syncResult) { - try { - CalendarSyncAdapterService.performSync(CalendarSyncAdapterService.this, account, extras, authority, - provider, syncResult); - } catch (OperationCanceledException e) { - Log.e(getClass().getSimpleName(), "OperationCanceledException", e); - } - } - - @Override - public void onSecurityException(Account account, Bundle extras, String authority, SyncResult syncResult) { - super.onSecurityException(account, extras, authority, syncResult); - - // contact or calendar permission has been revoked -> simply remove account - AccountResolver accountResolver = CalendarAccount.getAccount(CalendarSyncAdapterService.this, null); - accountResolver.removeAccount(); - } - } - @Override public IBinder onBind(Intent intent) { return new CalendarSyncAdapter().getSyncAdapterBinder(); @@ -93,6 +66,7 @@ public static Uri getBirthdayAdapterUri(Context context, Uri uri) { /** * Updates calendar color */ + @SuppressWarnings("deprecation") public static void updateCalendarColor(Context context) { int color = PreferencesManager.getCustomCalendarColor(context); ContentResolver contentResolver = context.getContentResolver(); @@ -105,15 +79,21 @@ public static void updateCalendarColor(Context context) { ContentProviderClient client = contentResolver .acquireContentProviderClient(CalendarContract.AUTHORITY); + if(client != null) { + ContentValues values = new ContentValues(); + values.put(CalendarContract.Calendars.CALENDAR_COLOR, color); + try { + client.update(uri, values, null, null); + } catch (RemoteException e) { + Log.e(TAG, "Error while updating calendar color!", e); + } - ContentValues values = new ContentValues(); - values.put(CalendarContract.Calendars.CALENDAR_COLOR, color); - try { - client.update(uri, values, null, null); - } catch (RemoteException e) { - Log.e(TAG, "Error while updating calendar color!", e); + if (android.os.Build.VERSION.SDK_INT < 24) { + client.release(); + } else { + client.close(); + } } - client.release(); } /** @@ -668,4 +648,31 @@ public static void performSync(Context context) { } } } + + private class CalendarSyncAdapter extends AbstractThreadedSyncAdapter { + + CalendarSyncAdapter() { + super(CalendarSyncAdapterService.this, true); + } + + @Override + public void onPerformSync(Account account, Bundle extras, String authority, + ContentProviderClient provider, SyncResult syncResult) { + try { + CalendarSyncAdapterService.performSync(CalendarSyncAdapterService.this, account, extras, authority, + provider, syncResult); + } catch (OperationCanceledException e) { + Log.e(getClass().getSimpleName(), "OperationCanceledException", e); + } + } + + @Override + public void onSecurityException(Account account, Bundle extras, String authority, SyncResult syncResult) { + super.onSecurityException(account, extras, authority, syncResult); + + // contact or calendar permission has been revoked -> simply remove account + AccountResolver accountResolver = CalendarAccount.getAccount(CalendarSyncAdapterService.this, null); + accountResolver.removeAccount(); + } + } } diff --git a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/MainIntentService.java b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/MainIntentService.java index abb9935..5ebb3d5 100644 --- a/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/MainIntentService.java +++ b/RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/service/MainIntentService.java @@ -23,7 +23,7 @@ public class MainIntentService extends IntentService { public static final String ACTION_MANUAL_COMPLETE_SYNC = "MANUAL_SYNC"; public static final String ACTION_CHANGE_COLOR = "CHANGE_COLOR"; - Messenger mMessenger; + private Messenger mMessenger; public MainIntentService() { super("BirthdayAdapterMainIntentService"); @@ -56,14 +56,17 @@ protected void onHandleIntent(Intent intent) { setProgressCircleWithHandler(true); // execute action - if (ACTION_CHANGE_COLOR.equals(action)) { - // update calendar color if enabled - if (CalendarAccount.isAccountActivated(this)) { - CalendarSyncAdapterService.updateCalendarColor(this); - } - } else if (ACTION_MANUAL_COMPLETE_SYNC.equals(action)) { - // perform blocking sync - CalendarSyncAdapterService.performSync(this); + switch (action) { + case ACTION_CHANGE_COLOR: + // update calendar color if enabled + if (CalendarAccount.isAccountActivated(this)) { + CalendarSyncAdapterService.updateCalendarColor(this); + } + break; + case ACTION_MANUAL_COMPLETE_SYNC: + // perform blocking sync + CalendarSyncAdapterService.performSync(this); + break; } setProgressCircleWithHandler(false); diff --git a/RememBirthday-UI/src/main/res/values-fr/strings.xml b/RememBirthday-UI/src/main/res/values-fr/strings.xml index d094202..58ab80b 100644 --- a/RememBirthday-UI/src/main/res/values-fr/strings.xml +++ b/RememBirthday-UI/src/main/res/values-fr/strings.xml @@ -114,6 +114,9 @@ Calendrier customisé Créer un calendrier customisé pour la gestion des notifications + Couleur du calendrier + Définit la couleur du calendrier avec [color] + Jours par défaut Ex : "0#2#7" Notification de démon visible pour le jour, 2 et 7 jours avant l\'événement. diff --git a/RememBirthday-UI/src/main/res/values/strings.xml b/RememBirthday-UI/src/main/res/values/strings.xml index c398006..65d47f5 100644 --- a/RememBirthday-UI/src/main/res/values/strings.xml +++ b/RememBirthday-UI/src/main/res/values/strings.xml @@ -123,6 +123,8 @@ create_calendar_key true + Calendar color + Define the calendar color with [color] calendar_color_key Default days diff --git a/RememBirthday-UI/src/main/res/xml/preferences.xml b/RememBirthday-UI/src/main/res/xml/preferences.xml index a2e10ad..fff61b0 100644 --- a/RememBirthday-UI/src/main/res/xml/preferences.xml +++ b/RememBirthday-UI/src/main/res/xml/preferences.xml @@ -1,5 +1,6 @@ - + @@ -31,6 +32,14 @@ android:key="@string/pref_create_calendar_key" android:summary="@string/pref_create_calendar_summary" android:defaultValue="@string/pref_notifications_service_default" /> +