mirror of https://github.com/tasks/tasks
Fixed a bunch of producteev issues as we move forward
parent
5c0d050069
commit
aa6f01e730
@ -0,0 +1,137 @@
|
||||
package com.todoroo.astrid.common;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.todoroo.andlib.service.ContextManager;
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
|
||||
/**
|
||||
* Sync Provider Utility class for accessing preferences
|
||||
*/
|
||||
abstract public class SyncProviderUtilities {
|
||||
|
||||
/**
|
||||
* @return your plugin identifier
|
||||
*/
|
||||
abstract public String getIdentifier();
|
||||
|
||||
/**
|
||||
* @return key for sync interval
|
||||
*/
|
||||
abstract public int getSyncIntervalKey();
|
||||
|
||||
// --- implementation
|
||||
|
||||
private static final String PREF_TOKEN = "_token"; //$NON-NLS-1$
|
||||
|
||||
private static final String PREF_LAST_SYNC = "_last_sync"; //$NON-NLS-1$
|
||||
|
||||
private static final String PREF_LAST_ATTEMPTED_SYNC = "_last_attempted"; //$NON-NLS-1$
|
||||
|
||||
private static final String PREF_LAST_ERROR = "_last_error"; //$NON-NLS-1$
|
||||
|
||||
private static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$
|
||||
|
||||
/** Get preferences object from the context */
|
||||
protected static SharedPreferences getPrefs() {
|
||||
return PreferenceManager.getDefaultSharedPreferences(ContextManager.getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if we have a token for this user, false otherwise
|
||||
*/
|
||||
public boolean isLoggedIn() {
|
||||
return getPrefs().getString(getIdentifier() + PREF_TOKEN, null) != null;
|
||||
}
|
||||
|
||||
/** authentication token, or null if doesn't exist */
|
||||
public String getToken() {
|
||||
return getPrefs().getString(getIdentifier() + PREF_TOKEN, null);
|
||||
}
|
||||
|
||||
/** Sets the authentication token. Set to null to clear. */
|
||||
public void setToken(String setting) {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putString(getIdentifier() + PREF_TOKEN, setting);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** @return Last Successful Sync Date, or 0 */
|
||||
public long getLastSyncDate() {
|
||||
return getPrefs().getLong(getIdentifier() + PREF_LAST_SYNC, 0);
|
||||
}
|
||||
|
||||
/** @return Last Attempted Sync Date, or 0 if it was successful */
|
||||
public long getLastAttemptedSyncDate() {
|
||||
return getPrefs().getLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
|
||||
}
|
||||
|
||||
/** @return Last Error, or null if no last error */
|
||||
public String getLastError() {
|
||||
return getPrefs().getString(PREF_LAST_ERROR, null);
|
||||
}
|
||||
|
||||
/** @return Last Error, or null if no last error */
|
||||
public boolean isOngoing() {
|
||||
return getPrefs().getBoolean(getIdentifier() + PREF_ONGOING, false);
|
||||
}
|
||||
|
||||
/** Deletes Last Successful Sync Date */
|
||||
public void clearLastSyncDate() {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.remove(getIdentifier() + PREF_LAST_SYNC);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** Set Last Successful Sync Date */
|
||||
public void setLastError(String error) {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putString(getIdentifier() + PREF_LAST_ERROR, error);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** Set Ongoing */
|
||||
public void stopOngoing() {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putBoolean(getIdentifier() + PREF_ONGOING, false);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** Set Last Successful Sync Date */
|
||||
public void recordSuccessfulSync() {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putLong(getIdentifier() + PREF_LAST_SYNC, DateUtilities.now());
|
||||
editor.putLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** Set Last Attempted Sync Date */
|
||||
public void recordSyncStart() {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC,
|
||||
DateUtilities.now());
|
||||
editor.putString(getIdentifier() + PREF_LAST_ERROR, null);
|
||||
editor.putBoolean(getIdentifier() + PREF_ONGOING, true);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the frequency, in seconds, auto-sync should occur.
|
||||
*
|
||||
* @return seconds duration, or 0 if not desired
|
||||
*/
|
||||
public int getSyncAutoSyncFrequency() {
|
||||
String value = getPrefs().getString(
|
||||
ContextManager.getContext().getString(
|
||||
getSyncIntervalKey()), null);
|
||||
if (value == null)
|
||||
return 0;
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.todoroo.astrid.producteev;
|
||||
|
||||
import android.content.SharedPreferences.Editor;
|
||||
|
||||
import com.timsu.astrid.R;
|
||||
import com.todoroo.astrid.common.SyncProviderUtilities;
|
||||
|
||||
/**
|
||||
* Displays synchronization preferences and an action panel so users can
|
||||
* initiate actions from the menu.
|
||||
*
|
||||
* @author timsu
|
||||
*
|
||||
*/
|
||||
public class ProducteevUtilities extends SyncProviderUtilities {
|
||||
|
||||
public static final ProducteevUtilities INSTANCE = new ProducteevUtilities();
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "pdv"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSyncIntervalKey() {
|
||||
return R.string.producteev_PPr_interval_key;
|
||||
}
|
||||
|
||||
// --- producteev-specific preferences
|
||||
|
||||
private static final String PREF_SERVER_LAST_SYNC = "_last_server"; //$NON-NLS-1$
|
||||
|
||||
/** @return last sync date, or null if no last */
|
||||
public String getLastServerSync() {
|
||||
return getPrefs().getString(getIdentifier() + PREF_SERVER_LAST_SYNC, null);
|
||||
}
|
||||
|
||||
/** Deletes Last Successful Sync Date */
|
||||
public void setLastServerSync(String value) {
|
||||
Editor editor = getPrefs().edit();
|
||||
editor.putString(getIdentifier() + PREF_SERVER_LAST_SYNC, value);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
Loading…
Reference in New Issue