Merge "Prepopulate the users autofill text."
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 60a9336..30b1d6f 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -41,6 +41,8 @@
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
+ <uses-permission android:name="android.permission.READ_PROFILE" />
+ <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
diff --git a/src/com/android/browser/AutofillHandler.java b/src/com/android/browser/AutofillHandler.java
index c4b14d7..dc23d00 100644
--- a/src/com/android/browser/AutofillHandler.java
+++ b/src/com/android/browser/AutofillHandler.java
@@ -21,9 +21,11 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
+import android.net.Uri;
import android.os.AsyncTask;
import android.os.Message;
import android.preference.PreferenceManager;
+import android.provider.ContactsContract;
import android.webkit.WebSettings.AutoFillProfile;
import java.util.concurrent.CountDownLatch;
@@ -114,8 +116,58 @@
c.close();
autoFillDb.close();
+ if (mAutoFillProfile == null) {
+ // We did not load a profile from disk. Try to populate one with the user's
+ // "me" contact.
+ final Uri profileUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
+ ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
+
+ String name = getContactField(profileUri,
+ ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
+ ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
+ // Only attempt to read other data and set a profile if we could successfully
+ // get a name.
+ if (name != null) {
+ String email = getContactField(profileUri,
+ ContactsContract.CommonDataKinds.Email.ADDRESS,
+ ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
+ String phone = getContactField(profileUri,
+ ContactsContract.CommonDataKinds.Phone.NUMBER,
+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
+ String company = getContactField(profileUri,
+ ContactsContract.CommonDataKinds.Organization.COMPANY,
+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
+
+ // Can't easily get structured postal address information (even using
+ // CommonDataKinds.StructuredPostal) so omit prepopulating that for now.
+ // When querying structured postal data, it often all comes back as a string
+ // inside the "street" field.
+
+ mAutoFillProfile = new AutoFillProfile(
+ 1, name, email, company, null, null, null, null,
+ null, null, phone);
+ }
+ }
+
mLoaded.countDown();
}
+
+ private String getContactField(Uri uri, String field, String itemType) {
+ String result = null;
+
+ Cursor c = mContext.getContentResolver().query(uri, new String[] { field },
+ ContactsContract.Data.MIMETYPE + "=?", new String[] { itemType }, null);
+
+ try {
+ // Just use the first returned value if we get more than one.
+ if (c.moveToFirst()) {
+ result = c.getString(0);
+ }
+ } finally {
+ c.close();
+ }
+ return result;
+ }
}
public void setAutoFillProfile(AutoFillProfile profile, Message msg) {