mirror of https://github.com/tasks/tasks
Merge upstream/master
commit
7233b14f35
@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* ASTRID: Android's Simple Task Recording Dashboard
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009 Tim Su
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*/
|
||||||
|
package com.todoroo.astrid.actfm;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
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.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
import com.todoroo.andlib.service.ContextManager;
|
||||||
|
import com.todoroo.andlib.utility.DialogUtilities;
|
||||||
|
import com.todoroo.astrid.service.StatisticsService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 ListActivity {
|
||||||
|
|
||||||
|
private 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;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
ContextManager.setContext(this);
|
||||||
|
|
||||||
|
setContentView(R.layout.gtasks_login_activity);
|
||||||
|
setTitle(R.string.actfm_GAA_title);
|
||||||
|
|
||||||
|
accountManager = new GoogleAccountManager(this);
|
||||||
|
Account[] accounts = accountManager.getAccounts();
|
||||||
|
ArrayList<String> accountNames = new ArrayList<String>();
|
||||||
|
for (Account a : accounts) {
|
||||||
|
accountNames.add(a.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
nameArray = accountNames.toArray(new String[accountNames.size()]);
|
||||||
|
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameArray));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||||
|
super.onListItemClick(l, v, position, id);
|
||||||
|
final ProgressDialog pd = DialogUtilities.progressDialog(this, this.getString(R.string.gtasks_GLA_authenticating));
|
||||||
|
pd.show();
|
||||||
|
final Account a = accountManager.getAccountByName(nameArray[position]);
|
||||||
|
accountName = a.name;
|
||||||
|
getAuthToken(a, pd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getAuthToken(Account a, final ProgressDialog pd) {
|
||||||
|
AccountManagerCallback<Bundle> callback = new AccountManagerCallback<Bundle>() {
|
||||||
|
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);
|
||||||
|
onAuthTokenSuccess();
|
||||||
|
}
|
||||||
|
} catch (final Exception e) {
|
||||||
|
Log.e("actfm-google-auth", "Login Error", e); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
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 {
|
||||||
|
DialogUtilities.dismissDialog(ActFmGoogleAuthActivity.this, pd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
accountManager.manager.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
StatisticsService.sessionStart(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
StatisticsService.sessionPause();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
StatisticsService.sessionStop(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = DialogUtilities.progressDialog(this, this.getString(R.string.gtasks_GLA_authenticating));
|
||||||
|
pd.show();
|
||||||
|
final Account a = accountManager.getAccountByName(accountName);
|
||||||
|
getAuthToken(a, pd);
|
||||||
|
} else {
|
||||||
|
onAuthCancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,14 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="fill_parent"
|
||||||
|
style="@style/Content">
|
||||||
|
|
||||||
<ListView android:id="@android:id/list"
|
<ListView android:id="@android:id/list"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"/>
|
android:layout_height="fill_parent"/>
|
||||||
|
|
||||||
<TextView android:id="@android:id/empty"
|
<TextView android:id="@android:id/empty"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="fill_parent"
|
||||||
android:text="@string/gtasks_GLA_noaccounts"/>
|
android:text="@string/gtasks_GLA_noaccounts"
|
||||||
|
android:gravity="center"
|
||||||
|
style="@style/TextAppearance.TLA_NoItems" />
|
||||||
|
|
||||||
</LinearLayout>
|
</FrameLayout>
|
||||||
Loading…
Reference in New Issue