Use Android integrated Google login

This commit is contained in:
Felix Kloft 2013-05-15 11:24:27 +02:00
parent 88fdb63f06
commit 10d6c0360d
5 changed files with 177 additions and 2 deletions

View File

@ -8,9 +8,11 @@
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"

View File

@ -56,4 +56,5 @@
<string name="menu_faction">Faction</string>
<string name="menu_info">Info</string>
<string name="menu_debug">Debug</string>
<string name="choose_account_to_login">Choose account to login</string>
</resources>

View File

@ -0,0 +1,141 @@
package com.cradle.iitc_mobile;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class DeviceAccountLogin implements AccountManagerCallback<Bundle> {
private class AccountAdapter extends BaseAdapter {
@Override
public int getCount() {
return mAccounts.length;
}
@Override
public Account getItem(int position) {
return mAccounts[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = mActivity.getLayoutInflater();
View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setText(mAccounts[position].name);
return tv;
}
}
private Account mAccount;
private AccountAdapter mAccountAdapter;
private AccountManager mAccountManager;
private Account[] mAccounts;
private IITC_Mobile mActivity;
private String mAuthToken;
private WebView mWebView;
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which >= 0 && which < mAccounts.length) {
mAccount = mAccounts[which];
startAuthentication();
}
dialog.cancel();
}
};
public DeviceAccountLogin(IITC_Mobile activity, WebView webView, WebViewClient webViewClient) {
mActivity = activity;
mWebView = webView;
mAccountManager = AccountManager.get(activity);
mAccountAdapter = new AccountAdapter();
}
private void displayAccountList() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder
.setTitle(R.string.choose_account_to_login)
.setSingleChoiceItems(mAccountAdapter, 0, onClickListener)
.setNegativeButton(android.R.string.cancel, onClickListener);
AlertDialog dialog = builder.create();
dialog.show();
}
private void onLoginFailed() {
Toast.makeText(mActivity, "Login failed.", Toast.LENGTH_SHORT).show();
// TODO l10n
}
private void startAuthentication() {
mAccountManager.getAuthToken(mAccount, mAuthToken, null, mActivity, this, null);
}
public void onActivityResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK)
startAuthentication();
else
onLoginFailed();
}
@Override
public void run(AccountManagerFuture<Bundle> value) {
try {
Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT);
if (launch != null) {
mActivity.startLoginActivity(launch);
return;
}
String result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN);
if (result != null) {
mWebView.loadUrl(result);
} else {
onLoginFailed();
}
} catch (Exception e) {
onLoginFailed();
}
}
public void startLogin(String realm, String accountName, String args) {
mAccounts = mAccountManager.getAccountsByType(realm);
mAccountAdapter.notifyDataSetChanged();
mAuthToken = "weblogin:" + args;
if (mAccounts.length == 0) {
return;
}
for (Account account : mAccounts) {
if (account.name.equals(accountName)) {
mAccountManager.getAuthToken(account, mAuthToken, null, mActivity, this, null);
return;
}
}
displayAccountList();
}
}

View File

@ -24,10 +24,13 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Toast;
public class IITC_Mobile extends Activity {
private static final int REQUEST_LOGIN = 1;
private IITC_WebView iitc_view;
private boolean back_button_pressed = false;
private OnSharedPreferenceChangeListener listener;
@ -38,6 +41,7 @@ public class IITC_Mobile extends Activity {
private boolean fullscreen_mode = false;
private boolean fullscreen_actionbar = false;
private ActionBar actionBar;
private DeviceAccountLogin mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -68,7 +72,7 @@ public class IITC_Mobile extends Activity {
user_loc = sharedPreferences.getBoolean("pref_user_loc",
false);
if (key.equals("pref_fullscreen_actionbar")) {
fullscreen_actionbar =sharedPreferences.getBoolean("pref_fullscreen_actionbar",
fullscreen_actionbar = sharedPreferences.getBoolean("pref_fullscreen_actionbar",
false);
if (fullscreen_mode)
IITC_Mobile.this.getActionBar().hide();
@ -360,4 +364,26 @@ public class IITC_Mobile extends Activity {
public IITC_WebView getWebView() {
return this.iitc_view;
}
public void startLoginActivity(Intent launch) {
startActivityForResult(launch, REQUEST_LOGIN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_LOGIN :
mLogin.onActivityResult(resultCode, data);
break;
default :
super.onActivityResult(requestCode, resultCode, data);
}
}
public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view,
String realm, String account, String args) {
mLogin = new DeviceAccountLogin(this, view, client);
mLogin.startLogin(realm, account, args);
}
}

View File

@ -133,6 +133,11 @@ public class IITC_WebViewClient extends WebViewClient {
handler.proceed();
};
@Override
public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
((IITC_Mobile) context).onReceivedLoginRequest(this, view, realm, account, args);
}
// plugins should be loaded after the main script is injected
@Override
public void onPageFinished(WebView view, String url) {