From 10d6c0360d7d167a95b200992dca9661325fb7b2 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 11:24:27 +0200 Subject: [PATCH 1/9] Use Android integrated Google login --- mobile/AndroidManifest.xml | 4 +- mobile/res/values/strings.xml | 1 + .../iitc_mobile/DeviceAccountLogin.java | 141 ++++++++++++++++++ .../com/cradle/iitc_mobile/IITC_Mobile.java | 28 +++- .../iitc_mobile/IITC_WebViewClient.java | 5 + 5 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java diff --git a/mobile/AndroidManifest.xml b/mobile/AndroidManifest.xml index 25606ea8..5fd825d1 100644 --- a/mobile/AndroidManifest.xml +++ b/mobile/AndroidManifest.xml @@ -8,9 +8,11 @@ android:minSdkVersion="14" android:targetSdkVersion="17" /> - + + + Faction Info Debug + Choose account to login \ No newline at end of file diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java new file mode 100644 index 00000000..9d008028 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java @@ -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 { + 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 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(); + } +} diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index a30c8fd9..dff1098f 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -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); + } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index f606d5ac..647e8189 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -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) { From 680626b220edc15181bd3b4e6c19880daf2b79c4 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 11:45:11 +0200 Subject: [PATCH 2/9] Add forgotten string --- mobile/res/values/strings.xml | 1 + mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mobile/res/values/strings.xml b/mobile/res/values/strings.xml index fc6f996a..c98a730d 100644 --- a/mobile/res/values/strings.xml +++ b/mobile/res/values/strings.xml @@ -57,4 +57,5 @@ Info Debug Choose account to login + Login failed. \ No newline at end of file diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java index 9d008028..e5dd31ff 100644 --- a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java @@ -85,8 +85,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { } private void onLoginFailed() { - Toast.makeText(mActivity, "Login failed.", Toast.LENGTH_SHORT).show(); - // TODO l10n + Toast.makeText(mActivity, R.string.login_failed, Toast.LENGTH_SHORT).show(); } private void startAuthentication() { From 69ee110182a3739803116feccfe2956aa3152227 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 12:29:30 +0200 Subject: [PATCH 3/9] Add progress bar --- .../com/cradle/iitc_mobile/DeviceAccountLogin.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java index e5dd31ff..b000ee3d 100644 --- a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java @@ -53,6 +53,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { private Account[] mAccounts; private IITC_Mobile mActivity; private String mAuthToken; + private AlertDialog mProgressbar; private WebView mWebView; private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { @@ -74,8 +75,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { } private void displayAccountList() { - AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); - builder + AlertDialog.Builder builder = new AlertDialog.Builder(mActivity) .setTitle(R.string.choose_account_to_login) .setSingleChoiceItems(mAccountAdapter, 0, onClickListener) .setNegativeButton(android.R.string.cancel, onClickListener); @@ -89,6 +89,12 @@ public class DeviceAccountLogin implements AccountManagerCallback { } private void startAuthentication() { + mProgressbar = new AlertDialog.Builder(mActivity) + .setCancelable(false) + .setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null)) + .create(); + mProgressbar.show(); + mAccountManager.getAuthToken(mAccount, mAuthToken, null, mActivity, this, null); } @@ -101,6 +107,9 @@ public class DeviceAccountLogin implements AccountManagerCallback { @Override public void run(AccountManagerFuture value) { + if (mProgressbar != null) + mProgressbar.hide(); + try { Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT); if (launch != null) { From 8cf0f3fff00bd1659ab240020706bfee3d6c338c Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 13:13:42 +0200 Subject: [PATCH 4/9] Add progress bars - indeterminate progress bar for authentication stuff - determinate progress bar for page loading (The second one was added so you can see that there is something happening after selecting your account) --- mobile/res/layout/dialog_progressbar.xml | 6 ++++ .../iitc_mobile/DeviceAccountLogin.java | 12 ++++---- .../com/cradle/iitc_mobile/IITC_Mobile.java | 30 ++++++++++++++----- 3 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 mobile/res/layout/dialog_progressbar.xml diff --git a/mobile/res/layout/dialog_progressbar.xml b/mobile/res/layout/dialog_progressbar.xml new file mode 100644 index 00000000..88191be7 --- /dev/null +++ b/mobile/res/layout/dialog_progressbar.xml @@ -0,0 +1,6 @@ + + diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java index b000ee3d..9b2a8d03 100644 --- a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java @@ -72,6 +72,11 @@ public class DeviceAccountLogin implements AccountManagerCallback { mWebView = webView; mAccountManager = AccountManager.get(activity); mAccountAdapter = new AccountAdapter(); + + mProgressbar = new AlertDialog.Builder(mActivity) + .setCancelable(false) + .setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null)) + .create(); } private void displayAccountList() { @@ -89,10 +94,6 @@ public class DeviceAccountLogin implements AccountManagerCallback { } private void startAuthentication() { - mProgressbar = new AlertDialog.Builder(mActivity) - .setCancelable(false) - .setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null)) - .create(); mProgressbar.show(); mAccountManager.getAuthToken(mAccount, mAuthToken, null, mActivity, this, null); @@ -107,8 +108,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { @Override public void run(AccountManagerFuture value) { - if (mProgressbar != null) - mProgressbar.hide(); + mProgressbar.hide(); try { Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 09c079e7..da5c3711 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -2,7 +2,13 @@ package com.cradle.iitc_mobile; import java.io.IOException; -import com.cradle.iitc_mobile.R; +import android.app.ActionBar; +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.content.res.Configuration; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; @@ -13,17 +19,12 @@ import android.os.Bundle; import android.os.Handler; import android.os.StrictMode; import android.preference.PreferenceManager; -import android.app.ActionBar; -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.content.res.Configuration; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.view.Window; import android.view.WindowManager; +import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.Toast; @@ -47,12 +48,25 @@ public class IITC_Mobile extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_PROGRESS); + // TODO build an async task for url.openStream() in IITC_WebViewClient StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); setContentView(R.layout.activity_main); iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview); + iitc_view.setWebChromeClient(new WebChromeClient() + { + @Override + public void onProgressChanged(WebView view, int newProgress) { + super.onProgressChanged(view, newProgress); + + // maximum for newProgress is 100 + // maximum for setProgress is 10,000 + setProgress(newProgress * 100); + } + }); // fetch actionbar, set display flags, title and enable home button actionBar = this.getActionBar(); From 8f9ee4382db0bf490bbca9c2b874aec8ff525eb6 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 13:38:17 +0200 Subject: [PATCH 5/9] Remove instance after successful login --- mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java | 1 + mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java index 9b2a8d03..a94aa2f7 100644 --- a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java @@ -120,6 +120,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { String result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN); if (result != null) { mWebView.loadUrl(result); + mActivity.loginSucceded(); } else { onLoginFailed(); } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index da5c3711..f2ac715b 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -394,4 +394,8 @@ public class IITC_Mobile extends Activity { mLogin = new DeviceAccountLogin(this, view, client); mLogin.startLogin(realm, account, args); } + + public void loginSucceded() { + mLogin = null; + } } From 15ad360f0bd00b49907554d5648efff14d3e3097 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 23:06:26 +0200 Subject: [PATCH 6/9] Restored geolocation feature --- mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java | 11 ----------- .../src/com/cradle/iitc_mobile/IITC_WebView.java | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index f2ac715b..a7187e71 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -56,17 +56,6 @@ public class IITC_Mobile extends Activity { StrictMode.setThreadPolicy(policy); setContentView(R.layout.activity_main); iitc_view = (IITC_WebView) findViewById(R.id.iitc_webview); - iitc_view.setWebChromeClient(new WebChromeClient() - { - @Override - public void onProgressChanged(WebView view, int newProgress) { - super.onProgressChanged(view, newProgress); - - // maximum for newProgress is 100 - // maximum for setProgress is 10,000 - setProgress(newProgress * 100); - } - }); // fetch actionbar, set display flags, title and enable home button actionBar = this.getActionBar(); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java index 866d1f38..e11e1d09 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java @@ -2,6 +2,7 @@ package com.cradle.iitc_mobile; import android.annotation.SuppressLint; import android.annotation.TargetApi; +import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.net.ConnectivityManager; @@ -39,14 +40,23 @@ public class IITC_WebView extends WebView { this.js_interface = new IITC_JSInterface(c); this.addJavascriptInterface(js_interface, "android"); - // our webchromeclient should share geolocation with the iitc script - // allow access by default this.setWebChromeClient(new WebChromeClient() { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { + // our webchromeclient should share geolocation with the iitc script + // allow access by default callback.invoke(origin, true, false); } + + @Override + public void onProgressChanged(WebView view, int newProgress) { + super.onProgressChanged(view, newProgress); + + // maximum for newProgress is 100 + // maximum for setProgress is 10,000 + ((Activity) getContext()).setProgress(newProgress * 100); + } }); webclient = new IITC_WebViewClient(c); From 42b03d2daa27825fd8d6096c94ba55c38333ddf4 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Wed, 15 May 2013 23:54:42 +0200 Subject: [PATCH 7/9] Remove obsolete import --- mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index a7187e71..ca927fcb 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -24,7 +24,6 @@ import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; -import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.Toast; From 2194a0c74fb04663ecdbb7c3cc888aa60b859dfc Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Fri, 17 May 2013 08:56:03 +0200 Subject: [PATCH 8/9] Rename class --- .../{DeviceAccountLogin.java => IITC_DeviceAccountLogin.java} | 4 ++-- mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename mobile/src/com/cradle/iitc_mobile/{DeviceAccountLogin.java => IITC_DeviceAccountLogin.java} (96%) diff --git a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java similarity index 96% rename from mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java rename to mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java index a94aa2f7..601425c4 100644 --- a/mobile/src/com/cradle/iitc_mobile/DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java @@ -18,7 +18,7 @@ import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; -public class DeviceAccountLogin implements AccountManagerCallback { +public class IITC_DeviceAccountLogin implements AccountManagerCallback { private class AccountAdapter extends BaseAdapter { @Override public int getCount() { @@ -67,7 +67,7 @@ public class DeviceAccountLogin implements AccountManagerCallback { } }; - public DeviceAccountLogin(IITC_Mobile activity, WebView webView, WebViewClient webViewClient) { + public IITC_DeviceAccountLogin(IITC_Mobile activity, WebView webView, WebViewClient webViewClient) { mActivity = activity; mWebView = webView; mAccountManager = AccountManager.get(activity); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index ca927fcb..3d11abcc 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -41,7 +41,7 @@ public class IITC_Mobile extends Activity { private boolean fullscreen_mode = false; private boolean fullscreen_actionbar = false; private ActionBar actionBar; - private DeviceAccountLogin mLogin; + private IITC_DeviceAccountLogin mLogin; @Override protected void onCreate(Bundle savedInstanceState) { @@ -379,7 +379,7 @@ public class IITC_Mobile extends Activity { public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view, String realm, String account, String args) { - mLogin = new DeviceAccountLogin(this, view, client); + mLogin = new IITC_DeviceAccountLogin(this, view, client); mLogin.startLogin(realm, account, args); } From 114103f08c79da1d7a7d7744892a2674a3f62929 Mon Sep 17 00:00:00 2001 From: Felix Kloft Date: Fri, 17 May 2013 09:36:10 +0200 Subject: [PATCH 9/9] added comments --- .../iitc_mobile/IITC_DeviceAccountLogin.java | 45 +++++++++++++++++-- .../com/cradle/iitc_mobile/IITC_Mobile.java | 15 ++++++- .../com/cradle/iitc_mobile/IITC_WebView.java | 10 ++++- .../iitc_mobile/IITC_WebViewClient.java | 3 ++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java b/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java index 601425c4..ba0ce3d1 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_DeviceAccountLogin.java @@ -18,7 +18,13 @@ import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; +/** + * this class manages automatic login using the Google account stored on the device + */ public class IITC_DeviceAccountLogin implements AccountManagerCallback { + /** + * Adapter to show available accounts in a ListView. Accounts are read from mAccounts + */ private class AccountAdapter extends BaseAdapter { @Override public int getCount() { @@ -56,11 +62,15 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback { private AlertDialog mProgressbar; 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() { @Override - public void onClick(DialogInterface dialog, int which) { - if (which >= 0 && which < mAccounts.length) { - mAccount = mAccounts[which]; + public void onClick(DialogInterface dialog, int index) { + if (index >= 0 && index < mAccounts.length) { + mAccount = mAccounts[index]; startAuthentication(); } dialog.cancel(); @@ -73,12 +83,16 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback { mAccountManager = AccountManager.get(activity); mAccountAdapter = new AccountAdapter(); + // dialog that serves as a progress bar overlay mProgressbar = new AlertDialog.Builder(mActivity) .setCancelable(false) .setView(mActivity.getLayoutInflater().inflate(R.layout.dialog_progressbar, null)) .create(); } + /** + * display all available accounts to the user + */ private void displayAccountList() { AlertDialog.Builder builder = new AlertDialog.Builder(mActivity) .setTitle(R.string.choose_account_to_login) @@ -89,23 +103,38 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback { dialog.show(); } + /** + * called when something failed. Shows a toast message. Classic login is still available + */ private void onLoginFailed() { 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() { mProgressbar.show(); 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) { if (resultCode == Activity.RESULT_OK) + // authentication activity succeeded, request token again startAuthentication(); else onLoginFailed(); } + /** + * called by AccountManager + */ @Override public void run(AccountManagerFuture value) { mProgressbar.hide(); @@ -113,12 +142,16 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback { try { Intent launch = (Intent) value.getResult().get(AccountManager.KEY_INTENT); 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); return; } String result = value.getResult().getString(AccountManager.KEY_AUTHTOKEN); if (result != null) { + // authentication succeded, we can load the given url, which will redirect back to the intel map mWebView.loadUrl(result); mActivity.loginSucceded(); } else { @@ -129,6 +162,12 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback { } } + /** + * 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) { mAccounts = mAccountManager.getAccountsByType(realm); mAccountAdapter.notifyDataSetChanged(); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 3d11abcc..e937c686 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -47,6 +47,7 @@ public class IITC_Mobile extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // enable progress bar above action bar requestWindowFeature(Window.FEATURE_PROGRESS); // 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; } + /** + * 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) { - startActivityForResult(launch, REQUEST_LOGIN); + startActivityForResult(launch, REQUEST_LOGIN); // REQUEST_LOGIN is to recognize the result } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_LOGIN : + // authentication activity has returned. mLogin will continue authentication mLogin.onActivityResult(resultCode, data); 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, String realm, String account, String args) { mLogin = new IITC_DeviceAccountLogin(this, view, client); mLogin.startLogin(realm, account, args); } + /** + * called after successful login + */ public void loginSucceded() { + // garbage collection mLogin = null; } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java index e11e1d09..b3a752a3 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebView.java @@ -41,14 +41,20 @@ public class IITC_WebView extends WebView { this.addJavascriptInterface(js_interface, "android"); this.setWebChromeClient(new WebChromeClient() { + /** + * our webchromeclient should share geolocation with the iitc script + * + * allow access by default + */ @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { - // our webchromeclient should share geolocation with the iitc script - // allow access by default callback.invoke(origin, true, false); } + /** + * display progress bar in activity + */ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java index 647e8189..6f75ab6f 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_WebViewClient.java @@ -133,6 +133,9 @@ public class IITC_WebViewClient extends WebViewClient { handler.proceed(); }; + /** + * this method is called automatically when the Google login form is opened. + */ @Override public void onReceivedLoginRequest(WebView view, String realm, String account, String args) { ((IITC_Mobile) context).onReceivedLoginRequest(this, view, realm, account, args);