| 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..a19ee1149c3a266ff9b6368d61b65d931961bff1
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/PersonalAutofillPopulator.java
|
| @@ -0,0 +1,294 @@
|
| +// 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;
|
| +
|
| +import org.chromium.base.CalledByNative;
|
| +
|
| +import java.util.ArrayList;
|
| +
|
| +/**
|
| + * Loads user profile information stored under the "Me" contact.
|
| + * Requires permissions: READ_CONTACTS and READ_PROFILE.
|
| + */
|
| +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 {
|
| + private 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 {
|
| + private 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 {
|
| + private static final int STREET = 0;
|
| + private static final int POBOX = 1;
|
| + private static final int NEIGHBORHOOD = 2;
|
| + private static final int CITY = 3;
|
| + private static final int REGION = 4;
|
| + private static final int POSTALCODE = 5;
|
| + private 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,
|
| + };
|
| + }
|
| +
|
| + @Override
|
| + public String mimeType() {
|
| + return ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
|
| + }
|
| + }
|
| +
|
| + private static class NameProfileQuery extends ProfileQuery {
|
| + private static final int GIVEN_NAME = 0;
|
| + private static final int MIDDLE_NAME = 1;
|
| + private static final int FAMILY_NAME = 2;
|
| + private static final int SUFFIX = 3;
|
| +
|
| + @Override
|
| + 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
|
| + };
|
| + }
|
| +
|
| + @Override
|
| + 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.
|
| + * @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() {
|
| + 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;
|
| + }
|
| +}
|
|
|