Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(656)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java

Issue 12282004: Added personal_data_manager android implementation for auto-populating auto-fill on android builds … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Android Autofill Populator Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java
new file mode 100644
index 0000000000000000000000000000000000000000..0e4d17e786686b0fbb95df817036ee41b382dad2
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java
@@ -0,0 +1,287 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Populates data fields from Android contacts profile API (i.e. "me" contact).
+
+package org.chromium.chrome.browser.autofill;
+
+import android.app.Activity;
+import android.content.ContentProviderOperation;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.OperationApplicationException;
+import android.database.Cursor;
+import android.database.DatabaseUtils;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.RemoteException;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.Profile;
+import android.provider.ContactsContract;
+import android.util.Log;
+import android.view.View.OnClickListener;
+import android.view.View;
+import android.widget.Button;
+import android.widget.Toast;
aurimas (slooooooooow) 2013/02/27 01:56:00 Need a space after this line. See http://source.a
apiccion 2013/02/28 01:31:04 Done.
+import java.util.ArrayList;
aurimas (slooooooooow) 2013/02/27 01:56:00 java.* import should go after org.chromium and nee
apiccion 2013/02/28 01:31:04 Done.
+import org.chromium.base.CalledByNative;
+
+/**
+ * Loads user profile information stored under the "Me" contact.
+ */
+public class PersonalAutofillPopulator {
+ /**
+ * SQL query definitions for obtaining specific profile information.
+ */
+ private abstract static class ProfileQuery {
+ Uri profileDataUri = Uri.withAppendedPath(
+ ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY
+ );
+ public abstract String[] projection();
+ public abstract String mimeType();
+ }
+
+ private static class EmailProfileQuery extends ProfileQuery {
+ public static final int EMAIL_ADDRESS = 0;
+
+ @Override
+ public String[] projection() {
+ return new String[] {
+ ContactsContract.CommonDataKinds.Email.ADDRESS,
+ };
+ }
+
+ @Override
+ public String mimeType() {
+ return ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
+ }
+ }
+
+ private static class PhoneProfileQuery extends ProfileQuery {
+ public static final int NUMBER = 0;
+
+ @Override
+ public String[] projection() {
+ return new String[] {
+ ContactsContract.CommonDataKinds.Phone.NUMBER,
+ };
+ }
+
+ @Override
+ public String mimeType() {
+ return ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
+ }
+ }
+
+ private static class AddressProfileQuery extends ProfileQuery {
+ public static final int STREET = 0;
+ public static final int POBOX = 1;
+ public static final int NEIGHBORHOOD = 2;
+ public static final int CITY = 3;
+ public static final int REGION = 4;
+ public static final int POSTALCODE = 5;
+ public static final int COUNTRY = 6;
+
+ @Override
+ public String[] projection() {
+ return new String[] {
+ ContactsContract.CommonDataKinds.StructuredPostal.STREET,
+ ContactsContract.CommonDataKinds.StructuredPostal.POBOX,
+ ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD,
+ ContactsContract.CommonDataKinds.StructuredPostal.CITY,
+ ContactsContract.CommonDataKinds.StructuredPostal.REGION,
+ ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
+ ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
+ };
+ }
+
+ public String mimeType() {
+ return ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
+ }
+ }
+
+ private static class NameProfileQuery extends ProfileQuery {
+ public static final int GIVEN_NAME = 0;
aurimas (slooooooooow) 2013/02/27 01:56:00 Does this need to be public? Is it used outside of
apiccion 2013/02/28 01:31:04 Done.
+ public static final int MIDDLE_NAME = 1;
+ public static final int FAMILY_NAME = 2;
+ public static final int SUFFIX = 3;
+
+ public String[] projection() {
+ return new String[] {
+ ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
+ ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
+ ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
+ ContactsContract.CommonDataKinds.StructuredName.SUFFIX
+ };
+ }
+
+ public String mimeType() {
+ return ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE;
+ }
+ }
+
+ /**
+ * Takes a query object, transforms into actual query and returns cursor.
+ * Primary contact values will be first.
+ */
+ private Cursor cursorFromProfileQuery(ProfileQuery query, ContentResolver contentResolver) {
+ String sortDescriptor = ContactsContract.Contacts.Data.IS_PRIMARY + " DESC";
+ return contentResolver.query(
+ query.profileDataUri,
+ query.projection(),
+ ContactsContract.Contacts.Data.MIMETYPE + " = ?",
+ new String[]{query.mimeType()},
+ sortDescriptor
+ );
+ }
+ // Extracted data variables.
+ private String[] mEmailAddresses;
+ private String mGivenName;
+ private String mMiddleName;
+ private String mFamilyName;
+ private String mSuffix;
+ private String mPobox;
+ private String mStreet;
+ private String mNeighborhood;
+ private String mCity;
+ private String mRegion;
+ private String mCountry;
+ private String mPostalCode;
+ private String[] mPhoneNumbers;
+
+ /**
+ * Constructor
+ * @param context a valid android context reference
+ */
+ PersonalAutofillPopulator(Context context) {
+ ContentResolver contentResolver = context.getContentResolver();
+ populateName(contentResolver);
+ populateEmail(contentResolver);
+ populateAddress(contentResolver);
+ populatePhone(contentResolver);
+ }
+
+ // Populating data fields.
+ private void populateName(ContentResolver contentResolver) {
+ NameProfileQuery nameProfileQuery = new NameProfileQuery();
+ Cursor nameCursor = cursorFromProfileQuery(nameProfileQuery, contentResolver);
+ if (nameCursor.moveToNext()) {
+ mGivenName = nameCursor.getString(nameProfileQuery.GIVEN_NAME);
+ mMiddleName = nameCursor.getString(nameProfileQuery.MIDDLE_NAME);
+ mFamilyName = nameCursor.getString(nameProfileQuery.FAMILY_NAME);
+ mSuffix = nameCursor.getString(nameProfileQuery.SUFFIX);
+ }
+ nameCursor.close();
+ }
+
+ private void populateEmail(ContentResolver contentResolver) {
+ EmailProfileQuery emailProfileQuery = new EmailProfileQuery();
+ Cursor emailCursor = cursorFromProfileQuery(emailProfileQuery, contentResolver);
+ mEmailAddresses = new String[emailCursor.getCount()];
+ for (int i = 0; emailCursor.moveToNext(); i++) {
+ mEmailAddresses[i] = emailCursor.getString(emailProfileQuery.EMAIL_ADDRESS);
+ }
+ emailCursor.close();
+ }
+
+ private void populateAddress(ContentResolver contentResolver) {
+ AddressProfileQuery addressProfileQuery = new AddressProfileQuery();
+ Cursor addressCursor = cursorFromProfileQuery(addressProfileQuery, contentResolver);
+ if(addressCursor.moveToNext()) {
+ mPobox = addressCursor.getString(addressProfileQuery.POBOX);
+ mStreet = addressCursor.getString(addressProfileQuery.STREET);
+ mNeighborhood = addressCursor.getString(addressProfileQuery.NEIGHBORHOOD);
+ mCity = addressCursor.getString(addressProfileQuery.CITY);
+ mRegion = addressCursor.getString(addressProfileQuery.REGION);
+ mPostalCode = addressCursor.getString(addressProfileQuery.POSTALCODE);
+ mCountry = addressCursor.getString(addressProfileQuery.COUNTRY);
+ }
+ addressCursor.close();
+ }
+
+ private void populatePhone(ContentResolver contentResolver) {
+ PhoneProfileQuery phoneProfileQuery = new PhoneProfileQuery();
+ Cursor phoneCursor = cursorFromProfileQuery(phoneProfileQuery, contentResolver);
+ mPhoneNumbers = new String[phoneCursor.getCount()];
+ for (int i = 0; phoneCursor.moveToNext(); i++) {
+ mPhoneNumbers[i] = phoneCursor.getString(phoneProfileQuery.NUMBER);
+ }
+ phoneCursor.close();
+ }
+
+ /**
+ * Static factory method for instance creation
aurimas (slooooooooow) 2013/02/27 01:56:00 It is generally nice to have periods at the end of
+ * @param context valid Android context
+ * @return PersonalAutofillPopulator new instance of PersonalAutofillPopulator
+ */
+ @CalledByNative
+ static PersonalAutofillPopulator create(Context context) {
+ return new PersonalAutofillPopulator(context);
+ }
+
+ @CalledByNative
+ public String getFirstName() {
aurimas (slooooooooow) 2013/02/27 01:56:00 Since all of these are public, you should add java
apiccion 2013/02/28 01:31:04 http://www.corp.google.com/eng/doc/javadocguide.ht
+ return mGivenName;
+ }
+
+ @CalledByNative
+ public String getLastName() {
+ return mFamilyName;
+ }
+
+ @CalledByNative
+ public String getMiddleName() {
+ return mMiddleName;
+ }
+
+ @CalledByNative
+ public String getSuffix() {
+ return mSuffix;
+ }
+
+ @CalledByNative
+ public String[] getEmailAddresses() {
+ return mEmailAddresses;
+ }
+
+ @CalledByNative
+ public String getStreet() {
+ return mStreet;
+ }
+
+ @CalledByNative
+ public String getPobox() {
+ return mPobox;
+ }
+
+ @CalledByNative
+ public String getNeighborhood() {
+ return mNeighborhood;
+ }
+
+ @CalledByNative
+ public String getCity() {
+ return mCity;
+ }
+
+ @CalledByNative
+ public String getRegion() {
+ return mRegion;
+ }
+
+ @CalledByNative
+ public String getPostalCode() {
+ return mPostalCode;
+ }
+
+ @CalledByNative
+ public String getCountry() {
+ return mCountry;
+ }
+
+ @CalledByNative
+ public String[] getPhoneNumbers() {
+ return mPhoneNumbers;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698