mirror of https://github.com/tasks/tasks
Add account selection dialog
parent
2969214b37
commit
37ea42b356
@ -1,183 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.astrid.actfm;
|
|
||||||
|
|
||||||
import android.accounts.Account;
|
|
||||||
import android.accounts.AccountManager;
|
|
||||||
import android.accounts.AccountManagerCallback;
|
|
||||||
import android.accounts.AccountManagerFuture;
|
|
||||||
import android.app.ListActivity;
|
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.View.OnClickListener;
|
|
||||||
import android.view.Window;
|
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.ListView;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
|
|
||||||
import com.todoroo.andlib.utility.DialogUtilities;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.tasks.R;
|
|
||||||
import org.tasks.dialogs.DialogBuilder;
|
|
||||||
import org.tasks.injection.InjectingListActivity;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This activity allows users to sign in or log in to Google Tasks
|
|
||||||
* through the Android account manager
|
|
||||||
*
|
|
||||||
* @author Sam Bosley
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ActFmGoogleAuthActivity extends InjectingListActivity {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(ActFmGoogleAuthActivity.class);
|
|
||||||
|
|
||||||
public static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile"; //$NON-NLS-1$
|
|
||||||
|
|
||||||
public static final String RESULT_EMAIL = "email"; //$NON-NLS-1$
|
|
||||||
public static final String RESULT_TOKEN = "token"; //$NON-NLS-1$
|
|
||||||
|
|
||||||
// --- ui initialization
|
|
||||||
|
|
||||||
private GoogleAccountManager accountManager;
|
|
||||||
private String[] nameArray;
|
|
||||||
|
|
||||||
private String authToken;
|
|
||||||
private String accountName;
|
|
||||||
|
|
||||||
private boolean onSuccess = false;
|
|
||||||
private boolean dismissDialog = false;
|
|
||||||
|
|
||||||
@Inject DialogBuilder dialogBuilder;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
||||||
setContentView(R.layout.gtasks_login_activity);
|
|
||||||
TextView header = new TextView(this);
|
|
||||||
header.setText(R.string.actfm_GAA_title);
|
|
||||||
header.setTextAppearance(this, R.style.TextAppearance_Medium);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
nameArray = accountNames.toArray(new String[accountNames.size()]);
|
|
||||||
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, nameArray));
|
|
||||||
findViewById(R.id.empty_button).setOnClickListener(new OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
onAuthCancel();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
|
||||||
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 = dialogBuilder.newProgressDialog(R.string.gtasks_GLA_authenticating);
|
|
||||||
pd.show();
|
|
||||||
final Account a = accountManager.getAccountByName(nameArray[position - 1]);
|
|
||||||
accountName = a.name;
|
|
||||||
getAuthToken(a, pd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getAuthToken(final Account a, final ProgressDialog pd) {
|
|
||||||
AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {
|
|
||||||
@Override
|
|
||||||
public void run(final AccountManagerFuture<Bundle> future) {
|
|
||||||
new Thread() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
Bundle bundle = future.getResult(30, TimeUnit.SECONDS);
|
|
||||||
if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
|
||||||
authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
||||||
if (!onSuccess) {
|
|
||||||
accountManager.getAccountManager().invalidateAuthToken(AUTH_TOKEN_TYPE, authToken);
|
|
||||||
getAuthToken(a, pd);
|
|
||||||
onSuccess = true;
|
|
||||||
} else {
|
|
||||||
onAuthTokenSuccess();
|
|
||||||
dismissDialog = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
dismissDialog = true;
|
|
||||||
}
|
|
||||||
} catch (final Exception e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int error = e instanceof IOException ? R.string.gtasks_GLA_errorIOAuth :
|
|
||||||
R.string.gtasks_GLA_errorAuth;
|
|
||||||
Toast.makeText(ActFmGoogleAuthActivity.this,
|
|
||||||
error,
|
|
||||||
Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
if (dismissDialog) {
|
|
||||||
DialogUtilities.dismissDialog(ActFmGoogleAuthActivity.this, pd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
accountManager.getAccountManager().getAuthToken(a, AUTH_TOKEN_TYPE, null, this, callback, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onAuthCancel() {
|
|
||||||
setResult(RESULT_CANCELED);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onAuthTokenSuccess() {
|
|
||||||
Intent data = new Intent();
|
|
||||||
data.putExtra(RESULT_EMAIL, accountName);
|
|
||||||
data.putExtra(RESULT_TOKEN, authToken);
|
|
||||||
setResult(RESULT_OK, data);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final int REQUEST_AUTHENTICATE = 0;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
|
||||||
if(requestCode == REQUEST_AUTHENTICATE && resultCode == RESULT_OK){
|
|
||||||
final ProgressDialog pd = dialogBuilder.newProgressDialog(R.string.gtasks_GLA_authenticating);
|
|
||||||
pd.show();
|
|
||||||
final Account a = accountManager.getAccountByName(accountName);
|
|
||||||
getAuthToken(a, pd);
|
|
||||||
} else {
|
|
||||||
onAuthCancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,110 @@
|
|||||||
|
package org.tasks;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManagerCallback;
|
||||||
|
import android.accounts.AccountManagerFuture;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Predicate;
|
||||||
|
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static com.google.common.collect.Iterables.tryFind;
|
||||||
|
import static com.google.common.collect.Lists.transform;
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
|
||||||
|
public class AccountManager {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(AccountManager.class);
|
||||||
|
|
||||||
|
public interface AuthResultHandler {
|
||||||
|
void authenticationSuccessful(String accountName, String authToken);
|
||||||
|
|
||||||
|
void authenticationFailed(String message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GoogleAccountManager googleAccountManager;
|
||||||
|
private Activity activity;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public AccountManager(Activity activity) {
|
||||||
|
this.activity = activity;
|
||||||
|
|
||||||
|
googleAccountManager = new GoogleAccountManager(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getAccounts() {
|
||||||
|
return transform(getAccountList(), new Function<Account, String>() {
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String apply(Account account) {
|
||||||
|
return account.name;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAccount(final String name) {
|
||||||
|
return getAccount(name) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return getAccounts().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getAuthToken(final String accountName, final AuthResultHandler handler) {
|
||||||
|
Account account = getAccount(accountName);
|
||||||
|
if (account == null) {
|
||||||
|
handler.authenticationFailed(activity.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||||
|
} else {
|
||||||
|
googleAccountManager.getAccountManager().getAuthToken(account, GtasksInvoker.AUTH_TOKEN_TYPE, null, activity, new AccountManagerCallback<Bundle>() {
|
||||||
|
@Override
|
||||||
|
public void run(final AccountManagerFuture<Bundle> future) {
|
||||||
|
new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Bundle bundle = future.getResult(30, TimeUnit.SECONDS);
|
||||||
|
if (bundle.containsKey(android.accounts.AccountManager.KEY_AUTHTOKEN)) {
|
||||||
|
handler.authenticationSuccessful(accountName, bundle.getString(android.accounts.AccountManager.KEY_AUTHTOKEN));
|
||||||
|
} else {
|
||||||
|
log.error("No auth token found in response bundle");
|
||||||
|
handler.authenticationFailed(activity.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||||
|
}
|
||||||
|
} catch (final Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
handler.authenticationFailed(activity.getString(e instanceof IOException
|
||||||
|
? R.string.gtasks_GLA_errorIOAuth
|
||||||
|
: R.string.gtasks_GLA_errorAuth));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
}, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Account> getAccountList() {
|
||||||
|
return asList(googleAccountManager.getAccounts());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Account getAccount(final String name) {
|
||||||
|
return tryFind(getAccountList(), new Predicate<Account>() {
|
||||||
|
@Override
|
||||||
|
public boolean apply(Account account) {
|
||||||
|
return name.equalsIgnoreCase(account.name);
|
||||||
|
}
|
||||||
|
}).orNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package org.tasks.dialogs;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.tasks.AccountManager;
|
||||||
|
import org.tasks.R;
|
||||||
|
import org.tasks.injection.InjectingDialogFragment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
public class AccountSelectionDialog extends InjectingDialogFragment {
|
||||||
|
|
||||||
|
private AccountSelectionHandler handler;
|
||||||
|
|
||||||
|
public interface AccountSelectionHandler {
|
||||||
|
void accountSelected(String account);
|
||||||
|
|
||||||
|
void onCancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject DialogBuilder dialogBuilder;
|
||||||
|
@Inject AccountManager accountManager;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
final List<String> accounts = accountManager.getAccounts();
|
||||||
|
|
||||||
|
return dialogBuilder.newDialog()
|
||||||
|
.setTitle(R.string.choose_google_account)
|
||||||
|
.setItems(accounts.toArray(new String[accounts.size()]), new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
handler.accountSelected(accounts.get(which));
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
if (handler != null) {
|
||||||
|
handler.onCancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccountSelectionHandler(AccountSelectionHandler handler) {
|
||||||
|
this.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
super.onCancel(dialog);
|
||||||
|
|
||||||
|
if (handler != null) {
|
||||||
|
handler.onCancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,39 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!--
|
|
||||||
** Copyright (c) 2012 Todoroo Inc
|
|
||||||
**
|
|
||||||
** See the file "LICENSE" for the full license governing this code.
|
|
||||||
-->
|
|
||||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:background="?attr/asContentBackground">
|
|
||||||
|
|
||||||
<ListView android:id="@android:id/list"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="fill_parent"/>
|
|
||||||
|
|
||||||
<LinearLayout android:id="@android:id/empty"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:paddingLeft="20dip"
|
|
||||||
android:paddingRight="20dip"
|
|
||||||
android:gravity="center"
|
|
||||||
android:orientation="vertical">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="@string/gtasks_GLA_noaccounts"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:gravity="center" />
|
|
||||||
|
|
||||||
<Button android:id="@+id/empty_button"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="50dip"
|
|
||||||
android:text="@android:string/ok" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</FrameLayout>
|
|
||||||
Loading…
Reference in New Issue