added comments

This commit is contained in:
Felix Kloft 2013-05-17 09:36:10 +02:00
parent 2194a0c74f
commit 114103f08c
4 changed files with 67 additions and 6 deletions

View File

@ -18,7 +18,13 @@ import android.widget.BaseAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
/**
* this class manages automatic login using the Google account stored on the device
*/
public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> { public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
/**
* Adapter to show available accounts in a ListView. Accounts are read from mAccounts
*/
private class AccountAdapter extends BaseAdapter { private class AccountAdapter extends BaseAdapter {
@Override @Override
public int getCount() { public int getCount() {
@ -56,11 +62,15 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
private AlertDialog mProgressbar; private AlertDialog mProgressbar;
private WebView mWebView; private WebView mWebView;
/**
* This listener is invoked when an item in the account list is selected. (It is also used when the 'cancel' button
* is clicked, (in which case `index` is <0)
*/
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int index) {
if (which >= 0 && which < mAccounts.length) { if (index >= 0 && index < mAccounts.length) {
mAccount = mAccounts[which]; mAccount = mAccounts[index];
startAuthentication(); startAuthentication();
} }
dialog.cancel(); dialog.cancel();
@ -73,12 +83,16 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
mAccountManager = AccountManager.get(activity); mAccountManager = AccountManager.get(activity);
mAccountAdapter = new AccountAdapter(); mAccountAdapter = new AccountAdapter();
// dialog that serves as a progress bar overlay
mProgressbar = new AlertDialog.Builder(mActivity) mProgressbar = new AlertDialog.Builder(mActivity)
.setCancelable(false) .setCancelable(false)
.setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null)) .setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null))
.create(); .create();
} }
/**
* display all available accounts to the user
*/
private void displayAccountList() { private void displayAccountList() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity) AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
.setTitle(R.string.choose_account_to_login) .setTitle(R.string.choose_account_to_login)
@ -89,23 +103,38 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
dialog.show(); dialog.show();
} }
/**
* called when something failed. Shows a toast message. Classic login is still available
*/
private void onLoginFailed() { private void onLoginFailed() {
Toast.makeText(mActivity, R.string.login_failed, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.login_failed, Toast.LENGTH_SHORT).show();
} }
/**
* called to start authenticating using AccountManager.
*
* After a token is created, AccountManager will call the run() method.
*/
private void startAuthentication() { private void startAuthentication() {
mProgressbar.show(); mProgressbar.show();
mAccountManager.getAuthToken(mAccount, mAuthToken, null, mActivity, this, null); mAccountManager.getAuthToken(mAccount, mAuthToken, null, mActivity, this, null);
} }
/**
* called by IITC_Mobile when the authentication activity has finished.
*/
public void onActivityResult(int resultCode, Intent data) { public void onActivityResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) if (resultCode == Activity.RESULT_OK)
// authentication activity succeeded, request token again
startAuthentication(); startAuthentication();
else else
onLoginFailed(); onLoginFailed();
} }
/**
* called by AccountManager
*/
@Override @Override
public void run(AccountManagerFuture<Bundle> value) { public void run(AccountManagerFuture<Bundle> value) {
mProgressbar.hide(); mProgressbar.hide();
@ -113,12 +142,16 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
try { try {
Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT); Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT);
if (launch != null) { if (launch != null) {
// There is a reason we need to start the given activity if we want an authentication token.
// (Could be user confirmation or something else. Whatever, we have to start it)
// IITC_Mobile will call it using startActivityForResult
mActivity.startLoginActivity(launch); mActivity.startLoginActivity(launch);
return; return;
} }
String result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN); String result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN);
if (result != null) { if (result != null) {
// authentication succeded, we can load the given url, which will redirect back to the intel map
mWebView.loadUrl(result); mWebView.loadUrl(result);
mActivity.loginSucceded(); mActivity.loginSucceded();
} else { } else {
@ -129,6 +162,12 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
} }
} }
/**
* start authentication
*
* if we already have a username (e.g. because the existing login has timed out), we can directly start
* authentication if an account with that username is found.
*/
public void startLogin(String realm, String accountName, String args) { public void startLogin(String realm, String accountName, String args) {
mAccounts = mAccountManager.getAccountsByType(realm); mAccounts = mAccountManager.getAccountsByType(realm);
mAccountAdapter.notifyDataSetChanged(); mAccountAdapter.notifyDataSetChanged();

View File

@ -47,6 +47,7 @@ public class IITC_Mobile extends Activity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// enable progress bar above action bar
requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_PROGRESS);
// TODO build an async task for url.openStream() in IITC_WebViewClient // TODO build an async task for url.openStream() in IITC_WebViewClient
@ -361,14 +362,19 @@ public class IITC_Mobile extends Activity {
return this.iitc_view; return this.iitc_view;
} }
/**
* It can occur that in order to authenticate, an external activity has to be launched. (This could for example be a
* confirmation dialog.)
*/
public void startLoginActivity(Intent launch) { public void startLoginActivity(Intent launch) {
startActivityForResult(launch, REQUEST_LOGIN); startActivityForResult(launch, REQUEST_LOGIN); // REQUEST_LOGIN is to recognize the result
} }
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { switch (requestCode) {
case REQUEST_LOGIN : case REQUEST_LOGIN :
// authentication activity has returned. mLogin will continue authentication
mLogin.onActivityResult(resultCode, data); mLogin.onActivityResult(resultCode, data);
break; break;
@ -377,13 +383,20 @@ public class IITC_Mobile extends Activity {
} }
} }
/**
* called by IITC_WebViewClient when the Google login form is opened.
*/
public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view, public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view,
String realm, String account, String args) { String realm, String account, String args) {
mLogin = new IITC_DeviceAccountLogin(this, view, client); mLogin = new IITC_DeviceAccountLogin(this, view, client);
mLogin.startLogin(realm, account, args); mLogin.startLogin(realm, account, args);
} }
/**
* called after successful login
*/
public void loginSucceded() { public void loginSucceded() {
// garbage collection
mLogin = null; mLogin = null;
} }
} }

View File

@ -41,14 +41,20 @@ public class IITC_WebView extends WebView {
this.addJavascriptInterface(js_interface, "android"); this.addJavascriptInterface(js_interface, "android");
this.setWebChromeClient(new WebChromeClient() { this.setWebChromeClient(new WebChromeClient() {
/**
* our webchromeclient should share geolocation with the iitc script
*
* allow access by default
*/
@Override @Override
public void onGeolocationPermissionsShowPrompt(String origin, public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) { GeolocationPermissions.Callback callback) {
// our webchromeclient should share geolocation with the iitc script
// allow access by default
callback.invoke(origin, true, false); callback.invoke(origin, true, false);
} }
/**
* display progress bar in activity
*/
@Override @Override
public void onProgressChanged(WebView view, int newProgress) { public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress); super.onProgressChanged(view, newProgress);

View File

@ -133,6 +133,9 @@ public class IITC_WebViewClient extends WebViewClient {
handler.proceed(); handler.proceed();
}; };
/**
* this method is called automatically when the Google login form is opened.
*/
@Override @Override
public void onReceivedLoginRequest(WebView view, String realm, String account, String args) { public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
((IITC_Mobile) context).onReceivedLoginRequest(this, view, realm, account, args); ((IITC_Mobile) context).onReceivedLoginRequest(this, view, realm, account, args);