Convert google tasks status bar to checkbox

pull/253/head
Alex Baker 10 years ago
parent 7c4bc038ff
commit a19efd96a7

@ -47,7 +47,7 @@ public class EditPreferences extends InjectingPreferenceActivity {
addPreferencesFromResource(R.xml.preferences_appearance);
screen.addPreference(getPreference(ReminderPreferences.class, R.string.notifications));
screen.addPreference(getPreference(DefaultsPreferences.class, R.string.task_defaults));
screen.addPreference(getPreference(GtasksPreferences.class, R.string.gtasks_GPr_header));
screen.addPreference(getPreference(GtasksPreferences.class, R.string.synchronization));
addPreferencesFromResource(R.xml.preferences_backup);
screen.addPreference(getPreference(OldTaskPreferences.class, R.string.EPr_manage_header));
screen.addPreference(getPreference(MiscellaneousPreferences.class, R.string.miscellaneous));

@ -6,158 +6,80 @@
package com.todoroo.astrid.gtasks;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.view.View;
import android.view.ViewGroup;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity;
import com.todoroo.astrid.utility.TodorooPreferenceActivity;
import org.tasks.R;
import org.tasks.activities.ClearGtaskDataActivity;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.scheduling.BackgroundScheduler;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class GtasksPreferences extends TodorooPreferenceActivity {
public class GtasksPreferences extends InjectingPreferenceActivity {
private static final int REQUEST_LOGIN = 0;
private static final int REQUEST_LOGOUT = 1;
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject BackgroundScheduler backgroundScheduler;
private void startSync() {
if (!gtasksPreferenceService.isLoggedIn()) {
startLogin();
} else {
syncOrImport();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LOGIN && resultCode == RESULT_OK) {
syncOrImport();
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void syncOrImport() {
setResultForSynchronize();
}
private void setResultForSynchronize() {
setResult(RESULT_CODE_SYNCHRONIZE);
finish();
}
private void startLogin() {
Intent intent = new Intent(this, GtasksLoginActivity.class);
startActivityForResult(intent, REQUEST_LOGIN);
}
@Override
protected void onPause() {
super.onPause();
backgroundScheduler.scheduleGtaskSync();
}
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);
addPreferencesFromResource(R.xml.preferences_gtasks);
getListView().setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
Preference gtaskPreference = findPreference(getString(R.string.sync_gtasks));
((CheckBoxPreference) gtaskPreference).setChecked(gtasksPreferenceService.isLoggedIn());
gtaskPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public void onChildViewRemoved(View parent, View child) {
//
}
@Override
public void onChildViewAdded(View parent, View child) {
View view = findViewById(R.id.status);
if(view != null) {
view.setBackgroundColor(statusColor);
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((boolean) newValue && !gtasksPreferenceService.isLoggedIn()) {
startActivityForResult(new Intent(GtasksPreferences.this, GtasksLoginActivity.class), REQUEST_LOGIN);
} else {
gtasksPreferenceService.stopOngoing();
gtasksPreferenceService.setToken(null);
}
return true;
}
});
}
@Override
public void updatePreferences(Preference preference, Object value) {
if (getString(R.string.sync_SPr_status_key).equals(preference.getKey())) {
updateStatus(preference);
}
}
private void updateStatus(Preference preference) {
boolean loggedIn = gtasksPreferenceService.isLoggedIn();
String status;
//String subtitle = ""; //$NON-NLS-1$
// ! logged in - display message, click -> sync
if(!loggedIn) {
status = getString(R.string.sync_status_loggedout);
statusColor = Color.rgb(19, 132, 165);
}
// 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);
}
}
else if(gtasksPreferenceService.getLastSyncDate() > 0) {
status = getString(R.string.sync_status_success,
if (gtasksPreferenceService.getLastSyncDate() > 0) {
gtaskPreference.setSummary(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);
newDate(gtasksPreferenceService.getLastSyncDate()))));
}
preference.setTitle(R.string.sync_SPr_sync);
preference.setSummary(getString(R.string.sync_SPr_status_subtitle, status));
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
findPreference(getString(R.string.sync_SPr_forget_key)).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference p) {
startSync();
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(new Intent(GtasksPreferences.this, ClearGtaskDataActivity.class), REQUEST_LOGOUT);
return true;
}
});
}
View view = findViewById(R.id.status);
if(view != null) {
view.setBackgroundColor(statusColor);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LOGIN) {
((CheckBoxPreference) findPreference(getString(R.string.sync_gtasks))).setChecked(resultCode == RESULT_OK);
} else if(requestCode == REQUEST_LOGOUT) {
if (resultCode == RESULT_OK) {
finish();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onPause() {
super.onPause();
backgroundScheduler.scheduleGtaskSync();
}
}

@ -21,6 +21,7 @@ import android.widget.TextView;
import android.widget.Toast;
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
import com.google.common.base.Predicate;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
@ -36,6 +37,9 @@ import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import static com.google.common.collect.Iterables.tryFind;
import static com.google.common.collect.Lists.newArrayList;
/**
* This activity allows users to sign in or log in to Google Tasks
* through the Android account manager
@ -59,6 +63,23 @@ public class GtasksLoginActivity extends InjectingListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountManager = new GoogleAccountManager(this);
Account[] accounts = accountManager.getAccounts();
final String existingUsername = gtasksPreferenceService.getUserName();
if (existingUsername != null) {
Account account = tryFind(newArrayList(accounts), new Predicate<Account>() {
@Override
public boolean apply(Account account) {
return existingUsername.equals(account.name);
}
}).orNull();
if (account != null) {
getAuthToken(account);
return;
}
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gtasks_login_activity);
TextView header = new TextView(this);
@ -67,8 +88,6 @@ public class GtasksLoginActivity extends InjectingListActivity {
header.setPadding(10, 0, 10, 50);
getListView().addHeaderView(header);
accountManager = new GoogleAccountManager(this);
Account[] accounts = accountManager.getAccounts();
ArrayList<String> accountNames = new ArrayList<>();
for (Account a : accounts) {
accountNames.add(a.name);
@ -90,14 +109,17 @@ public class GtasksLoginActivity extends InjectingListActivity {
super.onListItemClick(l, v, position, id);
int offsetPosition = position - 1; // Subtract 1 because apparently android counts the header view as part of the adapter.
if (offsetPosition >= 0 && offsetPosition < nameArray.length) {
final ProgressDialog pd = DialogUtilities.progressDialog(this, this.getString(R.string.gtasks_GLA_authenticating));
pd.show();
final Account a = accountManager.getAccountByName(nameArray[offsetPosition]);
accountName = a.name;
getAuthToken(a, pd);
getAuthToken(accountManager.getAccountByName(nameArray[offsetPosition]));
}
}
private void getAuthToken(Account account) {
final ProgressDialog pd = DialogUtilities.progressDialog(this, this.getString(R.string.gtasks_GLA_authenticating));
pd.show();
accountName = account.name;
getAuthToken(account, pd);
}
private void getAuthToken(Account a, final ProgressDialog pd) {
AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {
@Override

@ -1,86 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.utility;
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceGroup;
import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
/**
* Displays a preference screen for users to edit their preferences. Override
* updatePreferences to update the summary with preference values.
*
* @author Tim Su <tim@todoroo.com>
*
*/
abstract public class TodorooPreferenceActivity extends InjectingPreferenceActivity {
/**
* Update preferences for the given preference
* @param value setting. may be null.
*/
public abstract void updatePreferences(Preference preference, Object value);
// --- implementation
@Inject Preferences preferences;
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return preferences.getPrefs();
}
private void initializePreference(Preference preference) {
if(preference instanceof PreferenceGroup) {
PreferenceGroup group = (PreferenceGroup)preference;
for(int i = 0; i < group.getPreferenceCount(); i++) {
initializePreference(group.getPreference(i));
}
updatePreferences(group, null);
} else {
Object value = null;
if(preference instanceof ListPreference) {
value = ((ListPreference) preference).getValue();
} else if(preference instanceof CheckBoxPreference) {
value = ((CheckBoxPreference) preference).isChecked();
}
updatePreferences(preference, value);
if (preference.getOnPreferenceChangeListener() == null) {
preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference myPreference, Object newValue) {
updatePreferences(myPreference, newValue);
return true;
}
});
}
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
initializePreference(getPreferenceScreen());
}
}
}

@ -24,11 +24,13 @@ public class ClearGtaskDataActivity extends InjectingActivity {
@Override
public void onClick(DialogInterface dialog, int which) {
gtasksSyncV2Provider.signOut();
setResult(RESULT_OK);
finish();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
}

@ -1,61 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Layout for a visually child-like Preference in a PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="none"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2"
android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>

@ -200,9 +200,8 @@
<item>259200</item>
<item>604800</item>
</string-array>
<!-- Preference Key (do not translate) -->
<string name="sync_SPr_status_key">sync_status</string>
<string name="sync_SPr_forget_key">sync_forget</string>
<!-- =========================================================== GTASKS == -->
@ -245,6 +244,7 @@
<string name="TEA_ctrl_timer_pref">TEA_ctrl_timer_pref</string>
<string name="TEA_ctrl_share_pref">TEA_ctrl_share_pref</string>
<string name="TEA_ctrl_gcal">TEA_ctrl_gcal</string>
<string name="sync_gtasks">sync_gtasks</string>
<string-array name="TEA_control_sets_prefs">
<item>@string/TEA_ctrl_when_pref</item>

@ -52,27 +52,12 @@
<!-- ================================================== SyncPreferences == -->
<!-- Sync status subtitle, %s-> status message -->
<string name="sync_SPr_status_subtitle">Status: %s</string>
<!-- Sync Status: log in -->
<string name="sync_status_loggedout">Not logged in</string>
<!-- Status: ongoing -->
<string name="sync_status_ongoing">Sync ongoing...</string>
<!-- Sync Status: success status (%s -> last sync date). Keep it short!-->
<string name="sync_status_success">Last sync:\n%s</string>
<!-- Sync Status: failure status (%s -> last attempted sync date) -->
<string name="sync_status_failed">Failed on: %s</string>
<!-- Sync Status: error status (%s -> last sync date) -->
<string name="sync_status_errors">Sync w/ errors: %s</string>
<!-- Sync Status: never sync'd -->
<string name="sync_status_never">Never synchronized!</string>
<!-- Preference: Synchronization Interval Title -->
<string name="sync_SPr_interval_title">Background sync</string>
<!-- Synchronize Now Button -->
<string name="sync_SPr_sync">Synchronize now</string>
<!-- Sync: Clear Data Title -->
<string name="sync_SPr_forget">Log out</string>
<!-- Sync: Clear Data Description -->
@ -93,6 +78,8 @@
<string name="attachment_directory">Attachment directory</string>
<string name="debug_logging">Debug logging</string>
<string name="miscellaneous">Miscellaneous</string>
<string name="synchronization">Synchronization</string>
<string name="enabled">Enabled</string>
<string-array name="sync_SPr_interval_entries">
<!-- sync_SPr_interval_entries: Synchronization Intervals -->

@ -4,27 +4,29 @@
** See the file "LICENSE" for the full license governing this code.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/gtasks_GPr_header">
android:title="@string/synchronization">
<com.todoroo.astrid.ui.MultilinePreference
android:gravity="center"
android:key="@string/sync_SPr_status_key"
android:layout="@layout/status_preference"
android:textSize="24sp" />
<PreferenceCategory android:title="@string/gtasks_GPr_header">
<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"
android:defaultValue="0"/>
<com.todoroo.astrid.ui.MultilineCheckboxPreference
android:defaultValue="false"
android:key="@string/sync_gtasks"
android:title="@string/enabled" />
<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>
<com.todoroo.astrid.ui.MultilineListPreference
android:defaultValue="0"
android:dependency="@string/sync_gtasks"
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:dependency="@string/sync_gtasks"
android:key="@string/sync_SPr_forget_key"
android:summary="@string/sync_SPr_forget_description"
android:title="@string/sync_SPr_forget">
</com.todoroo.astrid.ui.MultilinePreference>
</PreferenceCategory>
</PreferenceScreen>

Loading…
Cancel
Save