Created
November 11, 2011 11:22
-
-
Save raimohanska/1357781 to your computer and use it in GitHub Desktop.
Android Google Authentication integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.accounts.Account; | |
import android.accounts.AccountManager; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
class Authenticator { | |
public final static int REQUEST_AUTHENTICATE = 1235; | |
public final static int DIALOG_ACCOUNTS = 235664; | |
private final static String ACCOUNT_TYPE = "com.google"; | |
private final static String AUTH_TOKEN_TYPE = "mail"; | |
public static interface TokenHandler { | |
void onSuccessfullAuthentication(String token); | |
} | |
private final Activity activity; | |
private final Authenticator.TokenHandler handler; | |
public Authenticator(Activity activity, Authenticator.TokenHandler handler) { | |
this.activity = activity; | |
this.handler = handler; | |
} | |
public void authenticate() { | |
String accountName = new ApplicationStorage(activity).getUsername(); | |
Account[] accounts = getAccounts(); | |
if (accountName != null) { | |
for (int i = 0; i < accounts.length; i++) { | |
Account account = accounts[i]; | |
if (accountName.equals(account.name)) { | |
continueWithAccount(account); | |
return; | |
} | |
} | |
} | |
if (accounts.length == 0) { | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
final Bundle bundle = getAccountManager().addAccount(ACCOUNT_TYPE, AUTH_TOKEN_TYPE, null, null, activity, null, null) | |
.getResult(); | |
activity.runOnUiThread(new Runnable() { | |
public void run() { | |
String accountName = (String) bundle.get(AccountManager.KEY_ACCOUNT_NAME); | |
System.out.println("Created account : + accountName"); | |
authenticate(); | |
} | |
}); | |
} catch (Exception e) { | |
handleException(e); | |
} | |
}; | |
}.start(); | |
} else if (accounts.length == 1) { | |
continueWithAccount(accounts[0]); | |
} else { | |
activity.showDialog(DIALOG_ACCOUNTS); | |
} | |
} | |
private Account[] getAccounts() { | |
return getAccountManager().getAccountsByType(ACCOUNT_TYPE); | |
} | |
private AccountManager getAccountManager() { | |
return AccountManager.get(activity); | |
} | |
private void continueWithAccount(final Account account) { | |
new ApplicationStorage(activity).setUsername(account.name); | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
final Bundle bundle = getAccountManager().getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null).getResult(); | |
activity.runOnUiThread(new Runnable() { | |
public void run() { | |
try { | |
if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) { | |
handler.onSuccessfullAuthentication(bundle.getString(AccountManager.KEY_AUTHTOKEN)); | |
} else if (bundle.containsKey(AccountManager.KEY_INTENT)) { | |
Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
activity.startActivityForResult(intent, REQUEST_AUTHENTICATE); | |
} | |
} catch (Exception e) { | |
handleException(e); | |
} | |
} | |
}); | |
} catch (Exception e) { | |
handleException(e); | |
} | |
} | |
}.start(); | |
} | |
private void handleException(Exception e) { | |
Log.v("Karma collector", "Authentication failure", e); | |
throw new RuntimeException(e); | |
} | |
public Dialog createDialog(int id) { | |
switch (id) { | |
case Authenticator.DIALOG_ACCOUNTS: | |
AlertDialog.Builder builder = new AlertDialog.Builder(activity); | |
builder.setTitle("Select a Google account"); | |
final Account[] accounts = getAccounts(); | |
String[] names = new String[accounts.length]; | |
for (int i = 0; i < accounts.length; i++) { | |
names[i] = accounts[i].name; | |
} | |
builder.setItems(names, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
continueWithAccount(accounts[which]); | |
} | |
}); | |
return builder.create(); | |
} | |
return null; | |
} | |
public boolean onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_AUTHENTICATE) { | |
if (resultCode == Activity.RESULT_OK) { | |
authenticate(); | |
} else { | |
activity.showDialog(Authenticator.DIALOG_ACCOUNTS); | |
} | |
return true; | |
} | |
return false; | |
} | |
} | |
class ApplicationStorage { | |
private final static String PREF = "lollers"; | |
private final Context context; | |
private String username; | |
public ApplicationStorage(Context context) { | |
this.context = context; | |
} | |
public String getUsername() { | |
if (Strings.isNullOrEmpty(username)) { | |
username = loadUsernameFromStorage(); | |
} | |
return username; | |
} | |
public void setUsername(String name) { | |
this.username = name; | |
SharedPreferences settings = context.getSharedPreferences(PREF, 0); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putString("accountName", name); | |
editor.commit(); | |
} | |
private String loadUsernameFromStorage() { | |
SharedPreferences settings = context.getSharedPreferences(PREF, 0); | |
return settings.getString("accountName", null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In addition, you need some hooks in your Activity: