From 584fdefe4708bfc06d8f4a8b3f8803d4c21f1b34 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 19 Jul 2014 01:31:15 -0500 Subject: [PATCH] Remove SyncProviderPreferences --- .../astrid/activity/EditPreferences.java | 11 +- .../astrid/activity/TaskListFragment.java | 4 +- .../astrid/gtasks/GtasksPreferences.java | 228 +++++++++++++- .../astrid/sync/SyncProviderPreferences.java | 279 ------------------ .../InjectingSyncProviderPreferences.java | 4 +- 5 files changed, 230 insertions(+), 296 deletions(-) delete mode 100644 astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/EditPreferences.java b/astrid/src/main/java/com/todoroo/astrid/activity/EditPreferences.java index d573d0b30..45118bad9 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/EditPreferences.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/EditPreferences.java @@ -34,7 +34,6 @@ import com.todoroo.astrid.helper.MetadataHelper; import com.todoroo.astrid.service.MarketStrategy.AmazonMarketStrategy; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.TaskService; -import com.todoroo.astrid.sync.SyncProviderPreferences; import com.todoroo.astrid.utility.Constants; import com.todoroo.astrid.utility.TodorooPreferenceActivity; import com.todoroo.astrid.voice.VoiceInputAssistant; @@ -180,7 +179,7 @@ public class EditPreferences extends TodorooPreferenceActivity { private static final HashMap, Integer> PREFERENCE_REQUEST_CODES = new HashMap<>(); static { - PREFERENCE_REQUEST_CODES.put(SyncProviderPreferences.class, REQUEST_CODE_SYNC); + PREFERENCE_REQUEST_CODES.put(GtasksPreferences.class, REQUEST_CODE_SYNC); } private void addPluginPreferences(PreferenceScreen screen) { @@ -214,8 +213,8 @@ public class EditPreferences extends TodorooPreferenceActivity { } try { Class intentComponent = Class.forName(intent.getComponent().getClassName()); - if (intentComponent.getSuperclass().equals(SyncProviderPreferences.class)) { - intentComponent = SyncProviderPreferences.class; + if (intentComponent.getSuperclass().equals(GtasksPreferences.class)) { + intentComponent = GtasksPreferences.class; } if (PREFERENCE_REQUEST_CODES.containsKey(intentComponent)) { final int code = PREFERENCE_REQUEST_CODES.get(intentComponent); @@ -332,8 +331,8 @@ public class EditPreferences extends TodorooPreferenceActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_CODE_SYNC && resultCode == SyncProviderPreferences.RESULT_CODE_SYNCHRONIZE) { - setResult(SyncProviderPreferences.RESULT_CODE_SYNCHRONIZE); + if (requestCode == REQUEST_CODE_SYNC && resultCode == GtasksPreferences.RESULT_CODE_SYNCHRONIZE) { + setResult(GtasksPreferences.RESULT_CODE_SYNCHRONIZE); finish(); return; } else if (requestCode == REQUEST_CODE_FILES_DIR && resultCode == RESULT_OK) { diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java index 5200cc545..3f6f3c8aa 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListFragment.java @@ -59,6 +59,7 @@ import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.TaskAttachment; import com.todoroo.astrid.data.TaskListMetadata; import com.todoroo.astrid.gtasks.GtasksPreferenceService; +import com.todoroo.astrid.gtasks.GtasksPreferences; import com.todoroo.astrid.helper.SyncActionHelper; import com.todoroo.astrid.service.SyncV2Service; import com.todoroo.astrid.service.TaskDeleter; @@ -67,7 +68,6 @@ import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.subtasks.SubtasksHelper; import com.todoroo.astrid.subtasks.SubtasksListFragment; import com.todoroo.astrid.subtasks.SubtasksUpdater; -import com.todoroo.astrid.sync.SyncProviderPreferences; import com.todoroo.astrid.tags.TaskToTagMetadata; import com.todoroo.astrid.timers.TimerPlugin; import com.todoroo.astrid.ui.QuickAddBar; @@ -607,7 +607,7 @@ public class TaskListFragment extends InjectingListFragment implements OnSortSel getActivity().startActivity(getActivity().getIntent()); TasksWidget.updateWidgets(getActivity()); return; - } else if (resultCode == SyncProviderPreferences.RESULT_CODE_SYNCHRONIZE) { + } else if (resultCode == GtasksPreferences.RESULT_CODE_SYNCHRONIZE) { preferences.setLong(SyncActionHelper.PREF_LAST_AUTO_SYNC, 0); // Forces autosync to occur after login } } diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java index b168ba206..097c86981 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java @@ -5,16 +5,33 @@ */ package com.todoroo.astrid.gtasks; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Color; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceCategory; +import android.view.View; +import android.view.ViewGroup; +import com.todoroo.andlib.utility.AndroidUtilities; +import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity; import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider; import org.tasks.R; import org.tasks.injection.InjectingSyncProviderPreferences; +import java.util.HashMap; +import java.util.Set; + import javax.inject.Inject; +import static org.tasks.date.DateTimeUtils.newDate; + /** * Displays synchronization preferences and an action panel so users can * initiate actions from the menu. @@ -33,8 +50,7 @@ public class GtasksPreferences extends InjectingSyncProviderPreferences { return R.xml.preferences_gtasks; } - @Override - public void startSync() { + private void startSync() { if (!gtasksPreferenceService.isLoggedIn()) { startLogin(); } else { @@ -69,14 +85,212 @@ public class GtasksPreferences extends InjectingSyncProviderPreferences { gtasksSyncV2Provider.signOut(); } - @Override - public GtasksPreferenceService getUtilities() { - return gtasksPreferenceService; - } - @Override protected void onPause() { super.onPause(); gtasksScheduler.scheduleService(); } + + public static final int RESULT_CODE_SYNCHRONIZE = 2; + + protected static final int REQUEST_LOGIN = 0; + + // --- implementation + + private int statusColor = Color.BLACK; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getListView().setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { + + @Override + public void onChildViewRemoved(View parent, View child) { + // + } + + @Override + public void onChildViewAdded(View parent, View child) { + View view = findViewById(org.tasks.api.R.id.status); + if(view != null) { + view.setBackgroundColor(statusColor); + } + } + }); + } + + @Override + public void updatePreferences(Preference preference, Object value) { + final Resources r = getResources(); + + // interval + if (r.getString(gtasksPreferenceService.getSyncIntervalKey()).equals( + preference.getKey())) { + int index = AndroidUtilities.indexOf( + r.getStringArray(org.tasks.api.R.array.sync_SPr_interval_values), + value); + if (index <= 0) { + preference.setSummary(org.tasks.api.R.string.sync_SPr_interval_desc_disabled); + } else { + preference.setSummary(r.getString( + org.tasks.api.R.string.sync_SPr_interval_desc, + r.getStringArray(org.tasks.api.R.array.sync_SPr_interval_entries)[index])); + } + } + + // status + else if (r.getString(org.tasks.api.R.string.sync_SPr_status_key).equals(preference.getKey())) { + boolean loggedIn = gtasksPreferenceService.isLoggedIn(); + String status; + //String subtitle = ""; //$NON-NLS-1$ + + // ! logged in - display message, click -> sync + if(!loggedIn) { + status = r.getString(org.tasks.api.R.string.sync_status_loggedout); + statusColor = Color.rgb(19, 132, 165); + } + // sync is occurring + else if(gtasksPreferenceService.isOngoing()) { + status = r.getString(org.tasks.api.R.string.sync_status_ongoing); + statusColor = Color.rgb(0, 0, 100); + } + // last sync had errors + else if(gtasksPreferenceService.getLastError() != null || gtasksPreferenceService.getLastAttemptedSyncDate() != 0) { + // last sync was failure + if(gtasksPreferenceService.getLastAttemptedSyncDate() != 0) { + status = r.getString(org.tasks.api.R.string.sync_status_failed, + DateUtilities.getDateStringWithTime(GtasksPreferences.this, + newDate(gtasksPreferenceService.getLastAttemptedSyncDate()))); + statusColor = Color.rgb(100, 0, 0); + } else { + long lastSyncDate = gtasksPreferenceService.getLastSyncDate(); + String dateString = lastSyncDate > 0 ? + DateUtilities.getDateStringWithTime(GtasksPreferences.this, + newDate(lastSyncDate)) : ""; //$NON-NLS-1$ + status = r.getString(org.tasks.api.R.string.sync_status_errors, dateString); + statusColor = Color.rgb(100, 100, 0); + } + } + else if(gtasksPreferenceService.getLastSyncDate() > 0) { + status = r.getString(org.tasks.api.R.string.sync_status_success, + DateUtilities.getDateStringWithTime(GtasksPreferences.this, + newDate(gtasksPreferenceService.getLastSyncDate()))); + statusColor = Color.rgb(0, 100, 0); + } else { + status = r.getString(org.tasks.api.R.string.sync_status_never); + statusColor = Color.rgb(0, 0, 100); + } + preference.setTitle(org.tasks.api.R.string.sync_SPr_sync); + preference.setSummary(r.getString(org.tasks.api.R.string.sync_SPr_status_subtitle, status)); + + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference p) { + startSync(); + return true; + } + }); + + View view = findViewById(org.tasks.api.R.id.status); + if(view != null) { + view.setBackgroundColor(statusColor); + } + } + else if (r.getString(org.tasks.api.R.string.sync_SPr_key_last_error).equals(preference.getKey())) { + if (gtasksPreferenceService.getLastError() != null) { + // Display error + final String service = getTitle().toString(); + final String lastErrorFull = gtasksPreferenceService.getLastError(); + final String lastErrorDisplay = adjustErrorForDisplay(r, lastErrorFull, service); + preference.setTitle(org.tasks.api.R.string.sync_SPr_last_error); + preference.setSummary(org.tasks.api.R.string.sync_SPr_last_error_subtitle); + + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference pref) { + // Show last error + new AlertDialog.Builder(GtasksPreferences.this) + .setTitle(org.tasks.api.R.string.sync_SPr_last_error) + .setMessage(lastErrorDisplay) + .setPositiveButton(org.tasks.api.R.string.sync_SPr_send_report, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent emailIntent = new Intent(Intent.ACTION_SEND); + emailIntent.setType("plain/text") + .putExtra(Intent.EXTRA_EMAIL, new String[] { "baker.alex@gmail.com"} ) + .putExtra(Intent.EXTRA_SUBJECT, service + " Sync Error") + .putExtra(Intent.EXTRA_TEXT, lastErrorFull); + startActivity(Intent.createChooser(emailIntent, r.getString(org.tasks.api.R.string.sync_SPr_send_report))); + } + }) + .setNegativeButton(org.tasks.api.R.string.DLG_close, null) + .create().show(); + return true; + } + }); + + } else { + PreferenceCategory statusCategory = (PreferenceCategory) findPreference(r.getString(org.tasks.api.R.string.sync_SPr_group_status)); + statusCategory.removePreference(findPreference(r.getString(org.tasks.api.R.string.sync_SPr_key_last_error))); + } + } + // log out button + else if (r.getString(org.tasks.api.R.string.sync_SPr_forget_key).equals(preference.getKey())) { + boolean loggedIn = gtasksPreferenceService.isLoggedIn(); + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference p) { + DialogUtilities.okCancelDialog(GtasksPreferences.this, + r.getString(org.tasks.api.R.string.sync_forget_confirm), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, + int which) { + logOut(); + initializePreference(getPreferenceScreen()); + } + }, null + ); + return true; + } + }); + if(!loggedIn) { + PreferenceCategory category = (PreferenceCategory) findPreference(r.getString(org.tasks.api.R.string.sync_SPr_key_options)); + category.removePreference(preference); + } + + } + } + + /** + * We can define exception strings in this map that we want to replace with more user-friendly + * messages. As we discover new exception types, we can expand the map. + * + * NOTE: All resources are currently required to have a single string format argument + * for inserting the service name into the error message + */ + private static HashMap exceptionsToDisplayMessages; + + private static HashMap getExceptionMap() { + if (exceptionsToDisplayMessages == null) { + exceptionsToDisplayMessages = new HashMap<>(); + exceptionsToDisplayMessages.put("java.net.ConnectionException", org.tasks.api.R.string.sync_error_offline); + exceptionsToDisplayMessages.put("java.net.UnknownHostException", org.tasks.api.R.string.sync_error_offline); + } + return exceptionsToDisplayMessages; + } + + private static String adjustErrorForDisplay(Resources r, String lastError, String service) { + Set exceptions = getExceptionMap().keySet(); + Integer resource = null; + for (String key : exceptions) { + if (lastError.contains(key)) { + resource = getExceptionMap().get(key); + break; + } + } + if (resource == null) { + return lastError; + } + return r.getString(resource, service); + } } diff --git a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java b/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java deleted file mode 100644 index d3463c380..000000000 --- a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java +++ /dev/null @@ -1,279 +0,0 @@ -/** - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ -package com.todoroo.astrid.sync; - -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.res.Resources; -import android.graphics.Color; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceClickListener; -import android.preference.PreferenceCategory; -import android.view.View; -import android.view.ViewGroup.OnHierarchyChangeListener; - -import com.todoroo.andlib.utility.AndroidUtilities; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.andlib.utility.DialogUtilities; -import com.todoroo.astrid.gtasks.GtasksPreferenceService; -import com.todoroo.astrid.utility.TodorooPreferenceActivity; - -import org.tasks.api.R; - -import java.util.HashMap; -import java.util.Set; - -import static org.tasks.date.DateTimeUtils.newDate; - -/** - * Utility class for common synchronization action: displaying synchronization - * preferences and an action panel so users can initiate actions from the menu. - * - * @author Tim Su sync - if(!loggedIn) { - status = r.getString(R.string.sync_status_loggedout); - statusColor = Color.rgb(19, 132, 165); - } - // sync is occurring - else if(getUtilities().isOngoing()) { - status = r.getString(R.string.sync_status_ongoing); - statusColor = Color.rgb(0, 0, 100); - } - // last sync had errors - else if(getUtilities().getLastError() != null || getUtilities().getLastAttemptedSyncDate() != 0) { - // last sync was failure - if(getUtilities().getLastAttemptedSyncDate() != 0) { - status = r.getString(R.string.sync_status_failed, - DateUtilities.getDateStringWithTime(SyncProviderPreferences.this, - newDate(getUtilities().getLastAttemptedSyncDate()))); - statusColor = Color.rgb(100, 0, 0); - } else { - long lastSyncDate = getUtilities().getLastSyncDate(); - String dateString = lastSyncDate > 0 ? - DateUtilities.getDateStringWithTime(SyncProviderPreferences.this, - newDate(lastSyncDate)) : ""; //$NON-NLS-1$ - status = r.getString(R.string.sync_status_errors, dateString); - statusColor = Color.rgb(100, 100, 0); - } - } - else if(getUtilities().getLastSyncDate() > 0) { - status = r.getString(R.string.sync_status_success, - DateUtilities.getDateStringWithTime(SyncProviderPreferences.this, - newDate(getUtilities().getLastSyncDate()))); - statusColor = Color.rgb(0, 100, 0); - } else { - status = r.getString(R.string.sync_status_never); - statusColor = Color.rgb(0, 0, 100); - } - preference.setTitle(R.string.sync_SPr_sync); - preference.setSummary(r.getString(R.string.sync_SPr_status_subtitle, status)); - - preference.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference p) { - startSync(); - return true; - } - }); - - View view = findViewById(R.id.status); - if(view != null) { - view.setBackgroundColor(statusColor); - } - } - else if (r.getString(R.string.sync_SPr_key_last_error).equals(preference.getKey())) { - if (getUtilities().getLastError() != null) { - // Display error - final String service = getTitle().toString(); - final String lastErrorFull = getUtilities().getLastError(); - final String lastErrorDisplay = adjustErrorForDisplay(r, lastErrorFull, service); - preference.setTitle(R.string.sync_SPr_last_error); - preference.setSummary(R.string.sync_SPr_last_error_subtitle); - - preference.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference pref) { - // Show last error - new AlertDialog.Builder(SyncProviderPreferences.this) - .setTitle(R.string.sync_SPr_last_error) - .setMessage(lastErrorDisplay) - .setPositiveButton(R.string.sync_SPr_send_report, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Intent emailIntent = new Intent(Intent.ACTION_SEND); - emailIntent.setType("plain/text") - .putExtra(Intent.EXTRA_EMAIL, new String[] { "baker.alex@gmail.com"} ) - .putExtra(Intent.EXTRA_SUBJECT, service + " Sync Error") - .putExtra(Intent.EXTRA_TEXT, lastErrorFull); - startActivity(Intent.createChooser(emailIntent, r.getString(R.string.sync_SPr_send_report))); - } - }) - .setNegativeButton(R.string.DLG_close, null) - .create().show(); - return true; - } - }); - - } else { - PreferenceCategory statusCategory = (PreferenceCategory) findPreference(r.getString(R.string.sync_SPr_group_status)); - statusCategory.removePreference(findPreference(r.getString(R.string.sync_SPr_key_last_error))); - } - } - // log out button - else if (r.getString(R.string.sync_SPr_forget_key).equals(preference.getKey())) { - boolean loggedIn = getUtilities().isLoggedIn(); - preference.setOnPreferenceClickListener(new OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference p) { - DialogUtilities.okCancelDialog(SyncProviderPreferences.this, - r.getString(R.string.sync_forget_confirm), new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, - int which) { - logOut(); - initializePreference(getPreferenceScreen()); - } - }, null); - return true; - } - }); - if(!loggedIn) { - PreferenceCategory category = (PreferenceCategory) findPreference(r.getString(R.string.sync_SPr_key_options)); - category.removePreference(preference); - } - - } - } - - /** - * We can define exception strings in this map that we want to replace with more user-friendly - * messages. As we discover new exception types, we can expand the map. - * - * NOTE: All resources are currently required to have a single string format argument - * for inserting the service name into the error message - */ - private static HashMap exceptionsToDisplayMessages; - - private static HashMap getExceptionMap() { - if (exceptionsToDisplayMessages == null) { - exceptionsToDisplayMessages = new HashMap<>(); - exceptionsToDisplayMessages.put("java.net.ConnectionException", R.string.sync_error_offline); - exceptionsToDisplayMessages.put("java.net.UnknownHostException", R.string.sync_error_offline); - } - return exceptionsToDisplayMessages; - } - - private static String adjustErrorForDisplay(Resources r, String lastError, String service) { - Set exceptions = getExceptionMap().keySet(); - Integer resource = null; - for (String key : exceptions) { - if (lastError.contains(key)) { - resource = getExceptionMap().get(key); - break; - } - } - if (resource == null) { - return lastError; - } - return r.getString(resource, service); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_LOGIN && resultCode == RESULT_OK) { - setResult(RESULT_CODE_SYNCHRONIZE); - finish(); - } else { - super.onActivityResult(requestCode, resultCode, data); - } - } - -} diff --git a/astrid/src/main/java/org/tasks/injection/InjectingSyncProviderPreferences.java b/astrid/src/main/java/org/tasks/injection/InjectingSyncProviderPreferences.java index 20ad111e4..752c8059c 100644 --- a/astrid/src/main/java/org/tasks/injection/InjectingSyncProviderPreferences.java +++ b/astrid/src/main/java/org/tasks/injection/InjectingSyncProviderPreferences.java @@ -2,11 +2,11 @@ package org.tasks.injection; import android.os.Bundle; -import com.todoroo.astrid.sync.SyncProviderPreferences; +import com.todoroo.astrid.utility.TodorooPreferenceActivity; import dagger.ObjectGraph; -public abstract class InjectingSyncProviderPreferences extends SyncProviderPreferences implements Injector { +public abstract class InjectingSyncProviderPreferences extends TodorooPreferenceActivity implements Injector { private ObjectGraph objectGraph; @Override