Cleaning up gtask preferences

pull/253/head
Alex Baker 10 years ago
parent 5fa931e0db
commit 52b4219419

@ -345,6 +345,10 @@
android:theme="@android:style/Theme"
android:screenOrientation="portrait"/>
<activity
android:name=".activities.ClearGtaskDataActivity"
android:theme="@style/ReminderDialog" />
<!-- repeats -->
<receiver android:name="com.todoroo.astrid.repeats.RepeatTaskCompleteListener">

@ -7,7 +7,6 @@ package com.todoroo.astrid.gtasks;
import com.todoroo.andlib.utility.DateUtilities;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
@ -38,10 +37,6 @@ public class GtasksPreferenceService {
return IDENTIFIER;
}
public int getSyncIntervalKey() {
return R.string.gtasks_GPr_interval_key;
}
public String getDefaultList() {
return preferences.getStringValue(PREF_DEFAULT_LIST);
}

@ -5,20 +5,15 @@
*/
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 com.todoroo.astrid.utility.TodorooPreferenceActivity;
@ -26,31 +21,16 @@ import com.todoroo.astrid.utility.TodorooPreferenceActivity;
import org.tasks.R;
import org.tasks.scheduling.BackgroundScheduler;
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.
*
* @author Tim Su <tim@todoroo.com>
*
*/
public class GtasksPreferences extends TodorooPreferenceActivity {
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject GtasksSyncV2Provider gtasksSyncV2Provider;
@Inject BackgroundScheduler backgroundScheduler;
@Override
public int getPreferenceResource() {
return R.xml.preferences_gtasks;
}
private void startSync() {
if (!gtasksPreferenceService.isLoggedIn()) {
startLogin();
@ -82,10 +62,6 @@ public class GtasksPreferences extends TodorooPreferenceActivity {
startActivityForResult(intent, REQUEST_LOGIN);
}
public void logOut() {
gtasksSyncV2Provider.signOut();
}
@Override
protected void onPause() {
super.onPause();
@ -103,6 +79,9 @@ public class GtasksPreferences extends TodorooPreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_gtasks);
getListView().setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
@ -112,7 +91,7 @@ public class GtasksPreferences extends TodorooPreferenceActivity {
@Override
public void onChildViewAdded(View parent, View child) {
View view = findViewById(org.tasks.R.id.status);
View view = findViewById(R.id.status);
if(view != null) {
view.setBackgroundColor(statusColor);
}
@ -122,176 +101,77 @@ public class GtasksPreferences extends TodorooPreferenceActivity {
@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.R.array.sync_SPr_interval_values),
value);
if (index <= 0) {
preference.setSummary(org.tasks.R.string.sync_SPr_interval_desc_disabled);
} else {
preference.setSummary(r.getString(
org.tasks.R.string.sync_SPr_interval_desc,
r.getStringArray(org.tasks.R.array.sync_SPr_interval_entries)[index]));
}
if (getString(R.string.gtasks_GPr_interval_key).equals(preference.getKey())) {
updateSyncInterval(preference, value);
} else if (getString(R.string.sync_SPr_status_key).equals(preference.getKey())) {
updateStatus(preference);
}
}
// status
else if (r.getString(org.tasks.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.R.string.sync_status_loggedout);
statusColor = Color.rgb(19, 132, 165);
}
// sync is occurring
else if(gtasksPreferenceService.isOngoing()) {
status = r.getString(org.tasks.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.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.R.string.sync_status_errors, dateString);
statusColor = Color.rgb(100, 100, 0);
}
}
else if(gtasksPreferenceService.getLastSyncDate() > 0) {
status = r.getString(org.tasks.R.string.sync_status_success,
DateUtilities.getDateStringWithTime(GtasksPreferences.this,
newDate(gtasksPreferenceService.getLastSyncDate())));
statusColor = Color.rgb(0, 100, 0);
} else {
status = r.getString(org.tasks.R.string.sync_status_never);
statusColor = Color.rgb(0, 0, 100);
}
preference.setTitle(org.tasks.R.string.sync_SPr_sync);
preference.setSummary(r.getString(org.tasks.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.R.id.status);
if(view != null) {
view.setBackgroundColor(statusColor);
}
private void updateSyncInterval(Preference preference, Object value) {
int index = AndroidUtilities.indexOf(getResources().getStringArray(R.array.sync_SPr_interval_values), value);
if (index == -1) {
index = 0;
}
else if (r.getString(org.tasks.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.R.string.sync_SPr_last_error);
preference.setSummary(org.tasks.R.string.sync_SPr_last_error_subtitle);
preference.setSummary(getResources().getStringArray(R.array.sync_SPr_interval_entries)[index]);
}
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference pref) {
// Show last error
new AlertDialog.Builder(GtasksPreferences.this)
.setTitle(org.tasks.R.string.sync_SPr_last_error)
.setMessage(lastErrorDisplay)
.setPositiveButton(org.tasks.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.R.string.sync_SPr_send_report)));
}
})
.setNegativeButton(org.tasks.R.string.DLG_close, null)
.create().show();
return true;
}
});
private void updateStatus(Preference preference) {
boolean loggedIn = gtasksPreferenceService.isLoggedIn();
String status;
//String subtitle = ""; //$NON-NLS-1$
} else {
PreferenceCategory statusCategory = (PreferenceCategory) findPreference(r.getString(org.tasks.R.string.sync_SPr_group_status));
statusCategory.removePreference(findPreference(r.getString(org.tasks.R.string.sync_SPr_key_last_error)));
}
// ! logged in - display message, click -> sync
if(!loggedIn) {
status = getString(R.string.sync_status_loggedout);
statusColor = Color.rgb(19, 132, 165);
}
// log out button
else if (r.getString(org.tasks.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.R.string.sync_forget_confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
logOut();
finish();
}
}, null
);
return true;
}
});
if(!loggedIn) {
PreferenceCategory category = (PreferenceCategory) findPreference(r.getString(org.tasks.R.string.sync_SPr_key_options));
category.removePreference(preference);
// sync is occurring
else if(gtasksPreferenceService.isOngoing()) {
status = getString(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 = getString(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 = getString(R.string.sync_status_errors, dateString);
statusColor = Color.rgb(100, 100, 0);
}
}
}
/**
* 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<String, Integer> exceptionsToDisplayMessages;
private static HashMap<String, Integer> getExceptionMap() {
if (exceptionsToDisplayMessages == null) {
exceptionsToDisplayMessages = new HashMap<>();
exceptionsToDisplayMessages.put("java.net.ConnectionException", org.tasks.R.string.sync_error_offline);
exceptionsToDisplayMessages.put("java.net.UnknownHostException", org.tasks.R.string.sync_error_offline);
else if(gtasksPreferenceService.getLastSyncDate() > 0) {
status = getString(R.string.sync_status_success,
DateUtilities.getDateStringWithTime(GtasksPreferences.this,
newDate(gtasksPreferenceService.getLastSyncDate())));
statusColor = Color.rgb(0, 100, 0);
} else {
status = getString(R.string.sync_status_never);
statusColor = Color.rgb(0, 0, 100);
}
return exceptionsToDisplayMessages;
}
preference.setTitle(R.string.sync_SPr_sync);
preference.setSummary(getString(R.string.sync_SPr_status_subtitle, status));
private static String adjustErrorForDisplay(Resources r, String lastError, String service) {
Set<String> exceptions = getExceptionMap().keySet();
Integer resource = null;
for (String key : exceptions) {
if (lastError.contains(key)) {
resource = getExceptionMap().get(key);
break;
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference p) {
startSync();
return true;
}
});
View view = findViewById(R.id.status);
if(view != null) {
view.setBackgroundColor(statusColor);
}
if (resource == null) {
return lastError;
}
return r.getString(resource, service);
}
}

@ -12,7 +12,6 @@ package com.todoroo.astrid.utility;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
@ -33,10 +32,6 @@ import javax.inject.Inject;
*/
abstract public class TodorooPreferenceActivity extends InjectingPreferenceActivity {
// --- abstract methods
public abstract int getPreferenceResource();
/**
* Update preferences for the given preference
* @param value setting. may be null.
@ -47,13 +42,6 @@ abstract public class TodorooPreferenceActivity extends InjectingPreferenceActiv
@Inject Preferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(getPreferenceResource());
}
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return preferences.getPrefs();

@ -0,0 +1,37 @@
package org.tasks.activities;
import android.content.DialogInterface;
import android.os.Bundle;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
import org.tasks.R;
import org.tasks.injection.InjectingActivity;
import javax.inject.Inject;
public class ClearGtaskDataActivity extends InjectingActivity {
@Inject GtasksSyncV2Provider gtasksSyncV2Provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DialogUtilities.okCancelDialog(ClearGtaskDataActivity.this,
getString(R.string.sync_forget_confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
gtasksSyncV2Provider.signOut();
finish();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}
);
}
}

@ -25,6 +25,7 @@ import com.todoroo.astrid.tags.RenameTagActivity;
import com.todoroo.astrid.widget.WidgetConfigActivity;
import org.tasks.activities.ClearAllDataActivity;
import org.tasks.activities.ClearGtaskDataActivity;
import org.tasks.activities.DeleteAllCalendarEventsActivity;
import org.tasks.activities.DeleteCompletedActivity;
import org.tasks.activities.DeleteCompletedEventsActivity;
@ -72,7 +73,8 @@ import dagger.Provides;
PurgeDeletedActivity.class,
DeleteCompletedActivity.class,
DeleteCompletedEventsActivity.class,
DeleteAllCalendarEventsActivity.class
DeleteAllCalendarEventsActivity.class,
ClearGtaskDataActivity.class
})
public class ActivityModule {

@ -203,13 +203,7 @@
<!-- Preference Key (do not translate) -->
<string name="sync_SPr_status_key">sync_status</string>
<!-- Preference Key (do not translate) -->
<string name="sync_SPr_forget_key">sync_forget</string>
<!-- Preference Key (do not translate) -->
<string name="sync_SPr_key_options">sync_options</string>
<!-- Preference Key (do not translate) -->
<string name="sync_SPr_key_last_error">sync_last_error</string>
<!-- =========================================================== GTASKS == -->
<string name="gtasks_GPr_interval_key">gtasks_sync_freq</string>

@ -44,9 +44,6 @@
<!-- information dialog title -->
<string name="DLG_information_title">Information</string>
<!-- general dialog close-->
<string name="DLG_close">Close</string>
<!-- error dialog (%s => error message)-->
<string name="DLG_error">Oops, looks like an error occurred! Here\'s what happened:\n\n%s</string>
@ -55,9 +52,6 @@
<!-- ================================================== SyncPreferences == -->
<!-- Status Group Label -->
<string name="sync_SPr_group_status">Status</string>
<!-- Sync status subtitle, %s-> status message -->
<string name="sync_SPr_status_subtitle">Status: %s</string>
<!-- Sync Status: log in -->
@ -73,27 +67,12 @@
<!-- Sync Status: never sync'd -->
<string name="sync_status_never">Never synchronized!</string>
<!-- Options Group Label -->
<string name="sync_SPr_group_options">Options</string>
<!-- Preference: Synchronization Interval Title -->
<string name="sync_SPr_interval_title">Background sync</string>
<!-- Preference: Synchronization Interval Description (when disabled) -->
<string name="sync_SPr_interval_desc_disabled">Background synchronization is disabled</string>
<!-- Preference: Synchronization Interval Description (%s => setting) -->
<string name="sync_SPr_interval_desc">Currently set to: %s</string>
<!-- Synchronize Now Button -->
<string name="sync_SPr_sync">Synchronize now</string>
<!-- Sync: Last error -->
<string name="sync_SPr_last_error">Status report</string>
<!-- Sync: last error click for more -->
<string name="sync_SPr_last_error_subtitle">Click to send a report to the Tasks team</string>
<!-- Sync: send error report button -->
<string name="sync_SPr_send_report">Send report</string>
<!-- Sync: Clear Data Title -->
<string name="sync_SPr_forget">Log out</string>
<!-- Sync: Clear Data Description -->
@ -102,9 +81,6 @@
<!-- confirmation dialog for sync log out -->
<string name="sync_forget_confirm">Log out/clear synchronization data?</string>
<!-- Sync error: network connectivity problems. %s-> name of sync service-->
<string name="sync_error_offline">There was a problem connecting to the network
during the last sync with %s. Please try again later.</string>
<string name="widget_hide_due_date">Hide due dates</string>
<string name="widget_hide_checkboxes">Hide checkboxes</string>
<string name="widget_hide_header">Hide header</string>

@ -1,44 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
** Copyright (c) 2012 Todoroo Inc
**
** See the file "LICENSE" for the full license governing this code.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="@string/sync_SPr_group_status"
android:title="@string/sync_SPr_group_status">
<com.todoroo.astrid.ui.MultilinePreference
android:layout="@layout/status_preference"
android:key="@string/sync_SPr_status_key"
android:textSize="24sp"
android:gravity="center"/>
<com.todoroo.astrid.ui.MultilinePreference
android:key="@string/sync_SPr_key_last_error"
android:textSize="24sp"
android:gravity="center"/>
</PreferenceCategory>
<PreferenceCategory
android:key="@string/sync_SPr_key_options"
android:title="@string/sync_SPr_group_options">
<com.todoroo.astrid.ui.MultilineListPreference
android:key="@string/gtasks_GPr_interval_key"
android:entries="@array/sync_SPr_interval_entries"
android:entryValues="@array/sync_SPr_interval_values"
android:title="@string/sync_SPr_interval_title" />
<com.todoroo.astrid.ui.MultilinePreference
android:key="@string/sync_SPr_forget_key"
android:title="@string/sync_SPr_forget"
android:summary="@string/sync_SPr_forget_description" />
</PreferenceCategory>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/gtasks_GPr_header">
<com.todoroo.astrid.ui.MultilinePreference
android:gravity="center"
android:key="@string/sync_SPr_status_key"
android:layout="@layout/status_preference"
android:textSize="24sp" />
<com.todoroo.astrid.ui.MultilineListPreference
android:entries="@array/sync_SPr_interval_entries"
android:entryValues="@array/sync_SPr_interval_values"
android:key="@string/gtasks_GPr_interval_key"
android:title="@string/sync_SPr_interval_title" />
<com.todoroo.astrid.ui.MultilinePreference
android:summary="@string/sync_SPr_forget_description"
android:title="@string/sync_SPr_forget">
<intent
android:targetClass="org.tasks.activities.ClearGtaskDataActivity"
android:targetPackage="org.tasks" />
</com.todoroo.astrid.ui.MultilinePreference>
</PreferenceScreen>

Loading…
Cancel
Save