Merge "Fix crash trying to add data: URLs to icon db"
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 227a7ac..d26aae7 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -26,6 +26,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
+ <uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a18814d..e762f8d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -890,4 +890,8 @@
reached. [CHAR LIMIT=50] -->
<string name="max_tabs_warning">No more tabs available</string>
+ <!-- Title of the picker dialog to choose an account. [CHAR-LIMIT=none]-->
+ <string name="account_picker_title">Choose an account or cancel to login
+ manually</string>
+
</resources>
diff --git a/src/com/android/browser/UrlHandler.java b/src/com/android/browser/UrlHandler.java
index 37c90a6..6cdd071 100644
--- a/src/com/android/browser/UrlHandler.java
+++ b/src/com/android/browser/UrlHandler.java
@@ -16,13 +16,31 @@
package com.android.browser;
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.util.EntityUtils;
+
+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.ActivityNotFoundException;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnCancelListener;
+import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
+import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
+import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
@@ -54,8 +72,7 @@
if (view.isPrivateBrowsingEnabled()) {
// Don't allow urls to leave the browser app when in
// private browsing mode
- mController.loadUrl(view, url);
- return true;
+ return false;
}
if (url.startsWith(SCHEME_WTAI)) {
@@ -108,6 +125,14 @@
}
}
+ // Check for service login and prompt the user for an account to use.
+ if (url.startsWith("https://www.google.com/accounts/ServiceLogin?") ||
+ url.startsWith("https://www.google.com/accounts/Login?")) {
+ if (loginWithDeviceAccount(view, url)) {
+ return true;
+ }
+ }
+
Intent intent;
// perform generic parsing of the URI to turn it into an Intent.
try {
@@ -163,6 +188,162 @@
return false;
}
+ // Url for issuing the uber token.
+ private final static Uri ISSUE_AUTH_TOKEN_URL = Uri.parse(
+ "https://www.google.com/accounts/IssueAuthToken?service=gaia&Session=false");
+ // Url for signing into a particular service.
+ private final static Uri TOKEN_AUTH_URL = Uri.parse(
+ "https://www.google.com/accounts/TokenAuth");
+
+ private class GoogleServiceLogin extends Thread implements
+ AccountManagerCallback<Bundle>, OnClickListener, OnCancelListener {
+ // For choosing the account.
+ private final Account[] mAccounts;
+ private int mCurrentAccount; // initially 0 for the first account
+
+ // For loading the auth token urls or the original url on error.
+ private final WebView mWebView;
+ private final String mUrl;
+
+ // SID and LSID retrieval process.
+ private String mSid;
+ private String mLsid;
+ private int mState; // {NONE(0), SID(1), LSID(2)}
+
+ GoogleServiceLogin(Account[] accounts, WebView view, String url) {
+ mAccounts = accounts;
+ mWebView = view;
+ mUrl = url;
+ }
+
+ // Thread
+ public void run() {
+ String url = ISSUE_AUTH_TOKEN_URL.buildUpon()
+ .appendQueryParameter("SID", mSid)
+ .appendQueryParameter("LSID", mLsid)
+ .build().toString();
+ // Intentionally not using Proxy.
+ AndroidHttpClient client = AndroidHttpClient.newInstance(
+ mWebView.getSettings().getUserAgentString());
+ HttpPost request = new HttpPost(url);
+
+ String result = null;
+ try {
+ HttpResponse response = client.execute(request);
+ if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+ onCancel(null);
+ return;
+ }
+ HttpEntity entity = response.getEntity();
+ if (entity == null) {
+ onCancel(null);
+ return;
+ }
+ result = EntityUtils.toString(entity, "UTF-8");
+ } catch (Exception e) {
+ request.abort();
+ onCancel(null);
+ } finally {
+ client.close();
+ }
+ Uri parsedUri = Uri.parse(mUrl);
+ String service = parsedUri.getQueryParameter("service");
+ String redirect = parsedUri.getQueryParameter("continue");
+ final String newUrl = TOKEN_AUTH_URL.buildUpon()
+ .appendQueryParameter("service", service)
+ .appendQueryParameter("source", "android-browser")
+ .appendQueryParameter("auth", result)
+ .appendQueryParameter("continue", redirect)
+ .build().toString();
+ mActivity.runOnUiThread(new Runnable() {
+ @Override public void run() {
+ mController.loadUrl(mWebView, newUrl);
+ }
+ });
+ }
+
+ // AccountManager callbacks.
+ public void run(AccountManagerFuture<Bundle> value) {
+ try {
+ String id = value.getResult().getString(
+ AccountManager.KEY_AUTHTOKEN);
+ switch (mState) {
+ default:
+ case 0:
+ throw new IllegalStateException(
+ "Impossible to get into this state");
+ case 1:
+ mSid = id;
+ mState = 2; // LSID
+ AccountManager.get(mActivity).getAuthToken(
+ mAccounts[mCurrentAccount], "LSID", null,
+ mActivity, this, null);
+ break;
+ case 2:
+ mLsid = id;
+ this.start();
+ break;
+ }
+ } catch (Exception e) {
+ // For all exceptions load the original signin page.
+ // TODO: toast login failed?
+ onCancel(null);
+ }
+ }
+
+ // Handle picking an account and "OK."
+ public void onClick(DialogInterface unused, int which) {
+ if (which == DialogInterface.BUTTON_POSITIVE) {
+ // TODO: toast loading...?
+ Account current = mAccounts[mCurrentAccount];
+ mState = 1; // SID
+ AccountManager.get(mActivity).getAuthToken(
+ mAccounts[mCurrentAccount], "SID", null,
+ mActivity, this, null);
+ } else if (which == DialogInterface.BUTTON_NEGATIVE) {
+ onCancel(null);
+ } else {
+ mCurrentAccount = which;
+ }
+ }
+
+ // Handle "cancel."
+ public void onCancel(DialogInterface unusued) {
+ // load the original url to login manually.
+ mController.loadUrl(mWebView, mUrl);
+ }
+ }
+
+ private boolean loginWithDeviceAccount(WebView view, String url) {
+ Uri parsedUri = Uri.parse(url);
+ if ("true".equals(parsedUri.getQueryParameter("go"))) {
+ return false;
+ }
+ Account[] accounts =
+ AccountManager.get(mActivity).getAccountsByType("com.google");
+ if (accounts.length == 0) {
+ return false;
+ }
+
+ // Populate the account list.
+ CharSequence[] names = new CharSequence[accounts.length];
+ int i = 0;
+ for (Account a : accounts) {
+ names[i++] = a.name;
+ }
+
+ GoogleServiceLogin login = new GoogleServiceLogin(accounts, view, url);
+ new AlertDialog.Builder(mActivity)
+ .setTitle(R.string.account_picker_title)
+ .setSingleChoiceItems(names, 0 /* first choice */, login)
+ .setPositiveButton(R.string.ok, login)
+ .setNegativeButton(R.string.cancel, login)
+ .setCancelable(true)
+ .setOnCancelListener(login)
+ .show();
+ return true;
+ }
+
private class RLZTask extends AsyncTask<Void, Void, String> {
private Uri mSiteUri;
private WebView mWebView;
diff --git a/src/com/android/browser/preferences/PersonalPreferencesFragment.java b/src/com/android/browser/preferences/PersonalPreferencesFragment.java
index 0620df2..38ebbae 100644
--- a/src/com/android/browser/preferences/PersonalPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PersonalPreferencesFragment.java
@@ -33,6 +33,7 @@
import android.content.DialogInterface;
import android.content.OperationApplicationException;
import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
@@ -62,6 +63,7 @@
Preference mChromeSync;
boolean mEnabled;
+ SharedPreferences mSharedPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -76,11 +78,32 @@
super.onResume();
// Setup the proper state for the sync with chrome item
- Context context = getActivity();
mChromeSync = findPreference(PREF_CHROME_SYNC);
- refreshUi(context);
+ refreshUi();
+ mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
+ mSharedPrefs.registerOnSharedPreferenceChangeListener(mListener);
}
+ @Override
+ public void onPause() {
+ super.onPause();
+
+ mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mListener);
+ }
+
+ OnSharedPreferenceChangeListener mListener
+ = new OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(
+ SharedPreferences sharedPreferences, String key) {
+ if (BrowserBookmarksPage.PREF_ACCOUNT_NAME.equals(key)
+ || BrowserBookmarksPage.PREF_ACCOUNT_TYPE.equals(key)) {
+ refreshUi();
+ }
+ }
+
+ };
+
private class GetAccountsTask extends AsyncTask<Void, Void, String> {
private Context mContext;
@@ -88,6 +111,7 @@
mContext = ctx;
}
+ @Override
protected String doInBackground(Void... unused) {
AccountManager am = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
Account[] accounts = am.getAccountsByType("com.google");
@@ -120,6 +144,7 @@
return null;
}
+ @Override
protected void onPostExecute(String summary) {
if (summary != null) {
mChromeSync.setSummary(summary);
@@ -127,8 +152,8 @@
}
}
- void refreshUi(Context context) {
- new GetAccountsTask(context).execute();
+ void refreshUi() {
+ new GetAccountsTask(getActivity()).execute();
PreferenceScreen autoFillSettings =
(PreferenceScreen)findPreference(BrowserSettings.PREF_AUTOFILL_PROFILE);
@@ -150,7 +175,7 @@
return true;
}
- final class AccountChooserDialog extends DialogFragment
+ public static class AccountChooserDialog extends DialogFragment
implements DialogInterface.OnClickListener {
AlertDialog mDialog;
@@ -184,12 +209,11 @@
String accountName = mDialog.getListView().getAdapter().getItem(which).toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.edit().putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountName).apply();
- refreshUi(getActivity());
dismiss();
}
}
- final class ImportWizardDialog extends DialogFragment implements OnClickListener {
+ public static class ImportWizardDialog extends DialogFragment implements OnClickListener {
View mRemoveButton;
View mCancelButton;
String mDefaultAccount;
@@ -257,7 +281,6 @@
ContentResolver.setIsSyncable(account, BrowserContract.AUTHORITY, 1);
}
- refreshUi(getActivity());
dismiss();
}
@@ -348,7 +371,7 @@
.withSelection(Bookmarks.ACCOUNT_NAME + " IS NULL AND " +
Bookmarks._ID + "<>1", null)
.build());
-
+
try {
resolver.applyBatch(BrowserContract.AUTHORITY, ops);
} catch (RemoteException e) {