From 7ab8d3a8118255059fd22dd8b49b63fa51590238 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 19 Jul 2014 01:22:48 -0500 Subject: [PATCH] Remove SyncProviderUtilities --- .../gtasks/GtasksPreferenceService.java | 85 +++++++++++++- .../astrid/gtasks/GtasksPreferences.java | 3 +- .../astrid/helper/SyncActionHelper.java | 6 +- .../astrid/sync/SyncProviderPreferences.java | 3 +- .../astrid/sync/SyncProviderUtilities.java | 109 ------------------ .../todoroo/astrid/sync/SyncV2Provider.java | 4 +- ...erminateProgressBarSyncResultCallback.java | 6 +- .../tasks/sync/RecordSyncStatusCallback.java | 16 +-- 8 files changed, 100 insertions(+), 132 deletions(-) delete mode 100644 astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderUtilities.java diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferenceService.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferenceService.java index b630315d5..171176371 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferenceService.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferenceService.java @@ -5,7 +5,7 @@ */ package com.todoroo.astrid.gtasks; -import com.todoroo.astrid.sync.SyncProviderUtilities; +import com.todoroo.andlib.utility.DateUtilities; import org.tasks.R; import org.tasks.preferences.Preferences; @@ -20,7 +20,9 @@ import javax.inject.Singleton; * */ @Singleton -public class GtasksPreferenceService extends SyncProviderUtilities { +public class GtasksPreferenceService { + + private final Preferences preferences; public static final String IDENTIFIER = "gtasks"; //$NON-NLS-1$ @@ -29,15 +31,13 @@ public class GtasksPreferenceService extends SyncProviderUtilities { @Inject public GtasksPreferenceService(Preferences preferences) { - super(preferences); + this.preferences = preferences; } - @Override public String getIdentifier() { return IDENTIFIER; } - @Override public int getSyncIntervalKey() { return R.string.gtasks_GPr_interval_key; } @@ -57,4 +57,79 @@ public class GtasksPreferenceService extends SyncProviderUtilities { public void setUserName(String userName) { preferences.setString(PREF_USER_NAME, userName); } + + protected static final String PREF_TOKEN = "_token"; //$NON-NLS-1$ + + protected static final String PREF_LAST_SYNC = "_last_sync"; //$NON-NLS-1$ + + protected static final String PREF_LAST_ATTEMPTED_SYNC = "_last_attempted"; //$NON-NLS-1$ + + protected static final String PREF_LAST_ERROR = "_last_err"; //$NON-NLS-1$ + + protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$ + + /** + * @return true if we have a token for this user, false otherwise + */ + public boolean isLoggedIn() { + return preferences.getStringValue(getIdentifier() + PREF_TOKEN) != null; + } + + /** authentication token, or null if doesn't exist */ + public String getToken() { + return preferences.getStringValue(getIdentifier() + PREF_TOKEN); + } + + /** Sets the authentication token. Set to null to clear. */ + public void setToken(String setting) { + preferences.setString(getIdentifier() + PREF_TOKEN, setting); + } + + /** @return Last Successful Sync Date, or 0 */ + public long getLastSyncDate() { + return preferences.getLong(getIdentifier() + PREF_LAST_SYNC, 0); + } + + /** @return Last Attempted Sync Date, or 0 if it was successful */ + public long getLastAttemptedSyncDate() { + return preferences.getLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0); + } + + /** @return Last Error, or null if no last error */ + public String getLastError() { + return preferences.getStringValue(getIdentifier() + PREF_LAST_ERROR); + } + + /** @return Last Error, or null if no last error */ + public boolean isOngoing() { + return preferences.getBoolean(getIdentifier() + PREF_ONGOING, false); + } + + /** Deletes Last Successful Sync Date */ + public void clearLastSyncDate() { + preferences.clear(getIdentifier() + PREF_LAST_SYNC); + } + + /** Set Last Error */ + public void setLastError(String error) { + preferences.setString(getIdentifier() + PREF_LAST_ERROR, error); + } + + /** Set Ongoing */ + public void stopOngoing() { + preferences.setBoolean(getIdentifier() + PREF_ONGOING, false); + } + + /** Set Last Successful Sync Date */ + public void recordSuccessfulSync() { + preferences.setLong(getIdentifier() + PREF_LAST_SYNC, DateUtilities.now() + 1000); + preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0); + } + + /** Set Last Attempted Sync Date */ + public void recordSyncStart() { + preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, DateUtilities.now()); + preferences.clear(getIdentifier() + PREF_LAST_ERROR); + preferences.setBoolean(getIdentifier() + PREF_ONGOING, true); + } } 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 a3531a43a..b168ba206 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java @@ -9,7 +9,6 @@ import android.content.Intent; import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity; import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider; -import com.todoroo.astrid.sync.SyncProviderUtilities; import org.tasks.R; import org.tasks.injection.InjectingSyncProviderPreferences; @@ -71,7 +70,7 @@ public class GtasksPreferences extends InjectingSyncProviderPreferences { } @Override - public SyncProviderUtilities getUtilities() { + public GtasksPreferenceService getUtilities() { return gtasksPreferenceService; } diff --git a/astrid/src/main/java/com/todoroo/astrid/helper/SyncActionHelper.java b/astrid/src/main/java/com/todoroo/astrid/helper/SyncActionHelper.java index 466af40d5..45f2a1c34 100644 --- a/astrid/src/main/java/com/todoroo/astrid/helper/SyncActionHelper.java +++ b/astrid/src/main/java/com/todoroo/astrid/helper/SyncActionHelper.java @@ -24,8 +24,8 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.SyncAction; +import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.service.SyncV2Service; -import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.astrid.sync.SyncResultCallback; import com.todoroo.astrid.sync.SyncV2Provider; @@ -69,12 +69,12 @@ public class SyncActionHelper { // --- boilerplate - public SyncActionHelper(SyncProviderUtilities syncProviderUtilities, SyncV2Service syncService, final FragmentActivity activity, Preferences preferences, Fragment fragment) { + public SyncActionHelper(GtasksPreferenceService gtasksPreferenceService, SyncV2Service syncService, final FragmentActivity activity, Preferences preferences, Fragment fragment) { this.syncService = syncService; this.activity = activity; this.preferences = preferences; this.fragment = fragment; - syncResultCallback = new IndeterminateProgressBarSyncResultCallback(syncProviderUtilities, activity, new Runnable() { + syncResultCallback = new IndeterminateProgressBarSyncResultCallback(gtasksPreferenceService, activity, new Runnable() { @Override public void run() { activity.sendBroadcast( diff --git a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java b/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java index 360f5a6cc..d3463c380 100644 --- a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java +++ b/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderPreferences.java @@ -21,6 +21,7 @@ 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; @@ -62,7 +63,7 @@ abstract public class SyncProviderPreferences extends TodorooPreferenceActivity /** * @return get preference utilities */ - abstract public SyncProviderUtilities getUtilities(); + abstract public GtasksPreferenceService getUtilities(); protected static final int REQUEST_LOGIN = 0; diff --git a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderUtilities.java b/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderUtilities.java deleted file mode 100644 index cf2cdfbf1..000000000 --- a/astrid/src/main/java/com/todoroo/astrid/sync/SyncProviderUtilities.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ -package com.todoroo.astrid.sync; - -import com.todoroo.andlib.utility.DateUtilities; - -import org.tasks.preferences.Preferences; - -/** - * Sync Provider Utility class for accessing preferences - */ -abstract public class SyncProviderUtilities { - - protected final Preferences preferences; - - public SyncProviderUtilities(Preferences preferences) { - this.preferences = preferences; - } - - /** - * @return your plugin identifier - */ - abstract public String getIdentifier(); - - /** - * @return key for sync interval - */ - abstract public int getSyncIntervalKey(); - - // --- implementation - - protected static final String PREF_TOKEN = "_token"; //$NON-NLS-1$ - - protected static final String PREF_LAST_SYNC = "_last_sync"; //$NON-NLS-1$ - - protected static final String PREF_LAST_ATTEMPTED_SYNC = "_last_attempted"; //$NON-NLS-1$ - - protected static final String PREF_LAST_ERROR = "_last_err"; //$NON-NLS-1$ - - protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$ - - /** - * @return true if we have a token for this user, false otherwise - */ - public boolean isLoggedIn() { - return preferences.getStringValue(getIdentifier() + PREF_TOKEN) != null; - } - - /** authentication token, or null if doesn't exist */ - public String getToken() { - return preferences.getStringValue(getIdentifier() + PREF_TOKEN); - } - - /** Sets the authentication token. Set to null to clear. */ - public void setToken(String setting) { - preferences.setString(getIdentifier() + PREF_TOKEN, setting); - } - - /** @return Last Successful Sync Date, or 0 */ - public long getLastSyncDate() { - return preferences.getLong(getIdentifier() + PREF_LAST_SYNC, 0); - } - - /** @return Last Attempted Sync Date, or 0 if it was successful */ - public long getLastAttemptedSyncDate() { - return preferences.getLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0); - } - - /** @return Last Error, or null if no last error */ - public String getLastError() { - return preferences.getStringValue(getIdentifier() + PREF_LAST_ERROR); - } - - /** @return Last Error, or null if no last error */ - public boolean isOngoing() { - return preferences.getBoolean(getIdentifier() + PREF_ONGOING, false); - } - - /** Deletes Last Successful Sync Date */ - public void clearLastSyncDate() { - preferences.clear(getIdentifier() + PREF_LAST_SYNC); - } - - /** Set Last Error */ - public void setLastError(String error) { - preferences.setString(getIdentifier() + PREF_LAST_ERROR, error); - } - - /** Set Ongoing */ - public void stopOngoing() { - preferences.setBoolean(getIdentifier() + PREF_ONGOING, false); - } - - /** Set Last Successful Sync Date */ - public void recordSuccessfulSync() { - preferences.setLong(getIdentifier() + PREF_LAST_SYNC, DateUtilities.now() + 1000); - preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0); - } - - /** Set Last Attempted Sync Date */ - public void recordSyncStart() { - preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, DateUtilities.now()); - preferences.clear(getIdentifier() + PREF_LAST_ERROR); - preferences.setBoolean(getIdentifier() + PREF_ONGOING, true); - } -} diff --git a/astrid/src/main/java/com/todoroo/astrid/sync/SyncV2Provider.java b/astrid/src/main/java/com/todoroo/astrid/sync/SyncV2Provider.java index 1c81f477b..f27ca3a66 100644 --- a/astrid/src/main/java/com/todoroo/astrid/sync/SyncV2Provider.java +++ b/astrid/src/main/java/com/todoroo/astrid/sync/SyncV2Provider.java @@ -5,6 +5,8 @@ */ package com.todoroo.astrid.sync; +import com.todoroo.astrid.gtasks.GtasksPreferenceService; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,7 +50,7 @@ abstract public class SyncV2Provider { /** * @return sync utility instance */ - abstract protected SyncProviderUtilities getUtilities(); + abstract protected GtasksPreferenceService getUtilities(); protected void finishSync(SyncResultCallback callback) { getUtilities().recordSuccessfulSync(); diff --git a/astrid/src/main/java/org/tasks/sync/IndeterminateProgressBarSyncResultCallback.java b/astrid/src/main/java/org/tasks/sync/IndeterminateProgressBarSyncResultCallback.java index b6212e8fd..4fee946cf 100644 --- a/astrid/src/main/java/org/tasks/sync/IndeterminateProgressBarSyncResultCallback.java +++ b/astrid/src/main/java/org/tasks/sync/IndeterminateProgressBarSyncResultCallback.java @@ -2,7 +2,7 @@ package org.tasks.sync; import android.support.v4.app.FragmentActivity; -import com.todoroo.astrid.sync.SyncProviderUtilities; +import com.todoroo.astrid.gtasks.GtasksPreferenceService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,8 +14,8 @@ public class IndeterminateProgressBarSyncResultCallback extends RecordSyncStatus private final FragmentActivity activity; private Runnable onFinished; - public IndeterminateProgressBarSyncResultCallback(SyncProviderUtilities syncProviderUtilities, FragmentActivity activity, Runnable onFinished) { - super(syncProviderUtilities); + public IndeterminateProgressBarSyncResultCallback(GtasksPreferenceService gtasksPreferenceService, FragmentActivity activity, Runnable onFinished) { + super(gtasksPreferenceService); this.activity = activity; this.onFinished = onFinished; diff --git a/astrid/src/main/java/org/tasks/sync/RecordSyncStatusCallback.java b/astrid/src/main/java/org/tasks/sync/RecordSyncStatusCallback.java index deeffc937..0acaa1dd6 100644 --- a/astrid/src/main/java/org/tasks/sync/RecordSyncStatusCallback.java +++ b/astrid/src/main/java/org/tasks/sync/RecordSyncStatusCallback.java @@ -1,32 +1,32 @@ package org.tasks.sync; -import com.todoroo.astrid.sync.SyncProviderUtilities; +import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.sync.SyncResultCallback; import org.tasks.Broadcaster; public class RecordSyncStatusCallback implements SyncResultCallback { - private SyncProviderUtilities syncProviderUtilities; + private GtasksPreferenceService gtasksPreferenceService; private Broadcaster broadcaster; - public RecordSyncStatusCallback(SyncProviderUtilities syncProviderUtilities) { - this(syncProviderUtilities, null); + public RecordSyncStatusCallback(GtasksPreferenceService gtasksPreferenceService) { + this(gtasksPreferenceService, null); } - public RecordSyncStatusCallback(SyncProviderUtilities syncProviderUtilities, Broadcaster broadcaster) { - this.syncProviderUtilities = syncProviderUtilities; + public RecordSyncStatusCallback(GtasksPreferenceService gtasksPreferenceService, Broadcaster broadcaster) { + this.gtasksPreferenceService = gtasksPreferenceService; this.broadcaster = broadcaster; } @Override public void started() { - syncProviderUtilities.recordSyncStart(); + gtasksPreferenceService.recordSyncStart(); } @Override public void finished() { - syncProviderUtilities.stopOngoing(); + gtasksPreferenceService.stopOngoing(); if (broadcaster != null) { broadcaster.eventRefresh(); }