Check for drive permission in preferences

Update error handling for failed logins
pull/820/head
Alex Baker 6 years ago
parent 5899037e0b
commit 0bc53fcce4

@ -9,7 +9,6 @@ package com.todoroo.astrid.gtasks.auth;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.todoroo.andlib.utility.DialogUtilities;
import io.reactivex.disposables.CompositeDisposable;
import javax.inject.Inject;
@ -31,6 +30,7 @@ import org.tasks.play.AuthResultHandler;
public class GtasksLoginActivity extends InjectingAppCompatActivity {
private static final int RC_CHOOSE_ACCOUNT = 10988;
public static final String EXTRA_ERROR = "extra_error";
@Inject DialogBuilder dialogBuilder;
@Inject GoogleAccountManager googleAccountManager;
@Inject GoogleTaskListDao googleTaskListDao;
@ -85,17 +85,15 @@ public class GtasksLoginActivity extends InjectingAppCompatActivity {
googleTaskListDao.update(account);
}
setResult(RESULT_OK);
finish();
DialogUtilities.dismissDialog(GtasksLoginActivity.this, pd);
finish();
}
@Override
public void authenticationFailed(final String message) {
runOnUiThread(
() ->
Toast.makeText(GtasksLoginActivity.this, message, Toast.LENGTH_LONG)
.show());
setResult(RESULT_CANCELED, new Intent().putExtra(EXTRA_ERROR, message));
DialogUtilities.dismissDialog(GtasksLoginActivity.this, pd);
finish();
}
}));
}

@ -9,7 +9,6 @@ package org.tasks.drive;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.todoroo.andlib.utility.DialogUtilities;
import io.reactivex.disposables.CompositeDisposable;
import javax.inject.Inject;
@ -30,6 +29,7 @@ import org.tasks.preferences.Preferences;
public class DriveLoginActivity extends InjectingAppCompatActivity {
private static final int RC_CHOOSE_ACCOUNT = 10988;
public static final String EXTRA_ERROR = "extra_error";
@Inject DialogBuilder dialogBuilder;
@Inject GoogleAccountManager googleAccountManager;
@Inject Preferences preferences;
@ -76,17 +76,15 @@ public class DriveLoginActivity extends InjectingAppCompatActivity {
public void authenticationSuccessful(String accountName) {
preferences.setString(R.string.p_google_drive_backup_account, accountName);
setResult(RESULT_OK);
finish();
DialogUtilities.dismissDialog(DriveLoginActivity.this, pd);
finish();
}
@Override
public void authenticationFailed(final String message) {
runOnUiThread(
() ->
Toast.makeText(DriveLoginActivity.this, message, Toast.LENGTH_LONG)
.show());
setResult(RESULT_CANCELED, new Intent().putExtra(EXTRA_ERROR, message));
DialogUtilities.dismissDialog(DriveLoginActivity.this, pd);
finish();
}
}));
}

@ -60,6 +60,10 @@ public class GoogleAccountManager {
return tryFind(getAccountList(), account -> name.equalsIgnoreCase(account.name)).orNull();
}
public boolean canAccessAccount(String name) {
return getAccount(name) != null;
}
public Bundle getAccessToken(String name, String scope) {
assertNotMainThread();
@ -115,7 +119,10 @@ public class GoogleAccountManager {
handler.authenticationSuccessful(accountName);
}
},
e -> handler.authenticationFailed(e.getMessage()));
e -> {
Timber.e(e);
handler.authenticationFailed(e.getMessage());
});
}
public void invalidateToken(String token) {

@ -23,6 +23,7 @@ import android.preference.PreferenceScreen;
import androidx.annotation.NonNull;
import com.google.common.base.Strings;
import com.todoroo.astrid.core.OldTaskPreferences;
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity;
import com.todoroo.astrid.reminders.ReminderPreferences;
import java.util.List;
import javax.inject.Inject;
@ -38,6 +39,7 @@ import org.tasks.billing.PurchaseActivity;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.drive.DriveLoginActivity;
import org.tasks.files.FileHelper;
import org.tasks.gtasks.GoogleAccountManager;
import org.tasks.gtasks.PlayServices;
import org.tasks.injection.ActivityComponent;
import org.tasks.injection.InjectingPreferenceActivity;
@ -78,6 +80,7 @@ public class BasicPreferences extends InjectingPreferenceActivity
@Inject Toaster toaster;
@Inject Device device;
@Inject ActivityPermissionRequestor permissionRequestor;
@Inject GoogleAccountManager googleAccountManager;
private Bundle result;
@ -178,7 +181,6 @@ public class BasicPreferences extends InjectingPreferenceActivity
CheckBoxPreference googleDriveBackup =
(CheckBoxPreference) findPreference(R.string.p_google_drive_backup);
googleDriveBackup.setChecked(preferences.getBoolean(R.string.p_google_drive_backup, false));
googleDriveBackup.setOnPreferenceChangeListener(
(preference, newValue) -> {
if (newValue == null) {
@ -191,11 +193,13 @@ public class BasicPreferences extends InjectingPreferenceActivity
}
return false;
} else {
preference.setSummary(null);
return true;
}
});
findPreference(R.string.third_party_licenses).setOnPreferenceClickListener(
findPreference(R.string.third_party_licenses)
.setOnPreferenceClickListener(
preference -> {
startActivity(new Intent(this, AttributionActivity.class));
return false;
@ -215,7 +219,8 @@ public class BasicPreferences extends InjectingPreferenceActivity
upgradeToPro.setOnPreferenceClickListener(
p -> {
startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.manage_subscription_url))));
new Intent(
Intent.ACTION_VIEW, Uri.parse(getString(R.string.manage_subscription_url))));
return false;
});
} else {
@ -226,7 +231,8 @@ public class BasicPreferences extends InjectingPreferenceActivity
});
}
findPreference(R.string.changelog).setSummary(getString(R.string.version_string, BuildConfig.VERSION_NAME));
findPreference(R.string.changelog)
.setSummary(getString(R.string.version_string, BuildConfig.VERSION_NAME));
requires(
R.string.settings_localization,
@ -238,11 +244,7 @@ public class BasicPreferences extends InjectingPreferenceActivity
//noinspection ConstantConditions
if (BuildConfig.FLAVOR.equals("generic")) {
requires(
R.string.about,
false,
R.string.rate_tasks,
R.string.upgrade_to_pro);
requires(R.string.about, false, R.string.rate_tasks, R.string.upgrade_to_pro);
requires(R.string.privacy, false, R.string.p_collect_statistics);
}
@ -257,6 +259,18 @@ public class BasicPreferences extends InjectingPreferenceActivity
protected void onResume() {
super.onResume();
CheckBoxPreference googleDriveBackup =
(CheckBoxPreference) findPreference(R.string.p_google_drive_backup);
String account = preferences.getStringValue(R.string.p_google_drive_backup_account);
if (preferences.getBoolean(R.string.p_google_drive_backup, false)
&& googleAccountManager.canAccessAccount(account)) {
googleDriveBackup.setChecked(true);
googleDriveBackup.setSummary(account);
} else {
googleDriveBackup.setChecked(false);
}
googleDriveBackup.setChecked(preferences.getBoolean(R.string.p_google_drive_backup, false));
//noinspection ConstantConditions
if (!BuildConfig.FLAVOR.equals("googleplay")) {
return;
@ -426,8 +440,11 @@ public class BasicPreferences extends InjectingPreferenceActivity
result.putBoolean(AppearancePreferences.EXTRA_RESTART, true);
}
} else if (requestCode == RC_DRIVE_BACKUP) {
((CheckBoxPreference) findPreference(R.string.p_google_drive_backup))
.setChecked(resultCode == RESULT_OK);
boolean success = resultCode == RESULT_OK;
((CheckBoxPreference) findPreference(R.string.p_google_drive_backup)).setChecked(success);
if (!success && data != null) {
toaster.longToast(data.getStringExtra(GtasksLoginActivity.EXTRA_ERROR));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}

@ -39,6 +39,7 @@ import org.tasks.preferences.ActivityPermissionRequestor;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.preferences.Preferences;
import org.tasks.ui.Toaster;
public class SynchronizationPreferences extends InjectingPreferenceActivity {
@ -63,6 +64,7 @@ public class SynchronizationPreferences extends InjectingPreferenceActivity {
@Inject CaldavDao caldavDao;
@Inject Inventory inventory;
@Inject TaskDeleter taskDeleter;
@Inject Toaster toaster;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -223,6 +225,8 @@ public class SynchronizationPreferences extends InjectingPreferenceActivity {
tracker.reportEvent(Tracking.Events.GTASK_ENABLED);
workManager.updateBackgroundSync();
restart();
} else if (data != null) {
toaster.longToast(data.getStringExtra(GtasksLoginActivity.EXTRA_ERROR));
}
} else if (requestCode == REQUEST_CALDAV_SETTINGS) {
if (resultCode == RESULT_OK) {

@ -5,6 +5,7 @@ import static android.widget.Toast.LENGTH_LONG;
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.StringRes;
import com.google.common.base.Strings;
import javax.inject.Inject;
import org.tasks.injection.ForActivity;
import org.tasks.locale.Locale;
@ -33,8 +34,10 @@ public class Toaster {
}
public void longToast(String text) {
if (!Strings.isNullOrEmpty(text)) {
Toast.makeText(context, text, LENGTH_LONG).show();
}
}
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated

Loading…
Cancel
Save