diff options
author | apiccion@chromium.org <apiccion@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-27 07:05:00 +0000 |
---|---|---|
committer | apiccion@chromium.org <apiccion@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-27 07:05:00 +0000 |
commit | 1c2f1007002184c0ea9668352d4ac4748dde4586 (patch) | |
tree | e0d585298c6103b6f0ee875483cdd789fd8d207e /components/autofill/browser/android/java/src/org/chromium | |
parent | 8a5b386945d0ed8a702c3b882e23224dca919ebe (diff) | |
download | chromium_src-1c2f1007002184c0ea9668352d4ac4748dde4586.zip chromium_src-1c2f1007002184c0ea9668352d4ac4748dde4586.tar.gz chromium_src-1c2f1007002184c0ea9668352d4ac4748dde4586.tar.bz2 |
Added personal_data_manager android implementation for auto-populating auto-fill on android builds
Enabled auxiliary autofill profile for Android builds. Fixed unit tests for android version for autofill. Disabled autofill address population until we can parse the info from the contacts.
Added permissions READ_CONTACTS and READ_PROFILE to testshell. These permissions are required to read the user's contact information using the ContactsContract and ContactsContract.Profile API.
TBR=thakis@chromium.org,joi@chromium.org
BUG=173268
Review URL: https://chromiumcodereview.appspot.com/12282004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@190842 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/autofill/browser/android/java/src/org/chromium')
-rw-r--r-- | components/autofill/browser/android/java/src/org/chromium/components/browser/autofill/PersonalAutofillPopulator.java | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/components/autofill/browser/android/java/src/org/chromium/components/browser/autofill/PersonalAutofillPopulator.java b/components/autofill/browser/android/java/src/org/chromium/components/browser/autofill/PersonalAutofillPopulator.java new file mode 100644 index 0000000..b99d6dd --- /dev/null +++ b/components/autofill/browser/android/java/src/org/chromium/components/browser/autofill/PersonalAutofillPopulator.java @@ -0,0 +1,317 @@ +// Copyright 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.components.browser.autofill; + +import android.app.Activity; +import android.content.ContentProviderOperation; +import android.content.ContentResolver; +import android.content.Context; +import android.content.OperationApplicationException; +import android.content.pm.PackageManager; +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; + private boolean mHasPermissions; + + /** + * Constructor + * @param context a valid android context reference + */ + PersonalAutofillPopulator(Context context) { + mHasPermissions = hasPermissions(context); + if (mHasPermissions) { + ContentResolver contentResolver = context.getContentResolver(); + populateName(contentResolver); + populateEmail(contentResolver); + populateAddress(contentResolver); + populatePhone(contentResolver); + } + } + + // Check if the user has granted permissions. + private boolean hasPermissions(Context context) { + String [] permissions = { + "android.permission.READ_CONTACTS", + "android.permission.READ_PROFILE" + }; + for (String permission : permissions) { + int res = context.checkCallingOrSelfPermission(permission); + if (res != PackageManager.PERMISSION_GRANTED) return false; + } + return true; + } + + // 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 + private String getFirstName() { + return mGivenName; + } + + @CalledByNative + private String getLastName() { + return mFamilyName; + } + + @CalledByNative + private String getMiddleName() { + return mMiddleName; + } + + @CalledByNative + private String getSuffix() { + return mSuffix; + } + + @CalledByNative + private String[] getEmailAddresses() { + return mEmailAddresses; + } + + @CalledByNative + private String getStreet() { + return mStreet; + } + + @CalledByNative + private String getPobox() { + return mPobox; + } + + @CalledByNative + private String getNeighborhood() { + return mNeighborhood; + } + + @CalledByNative + private String getCity() { + return mCity; + } + + @CalledByNative + private String getRegion() { + return mRegion; + } + + @CalledByNative + private String getPostalCode() { + return mPostalCode; + } + + @CalledByNative + private String getCountry() { + return mCountry; + } + + @CalledByNative + private String[] getPhoneNumbers() { + return mPhoneNumbers; + } + + @CalledByNative + private boolean getHasPermissions() { + return mHasPermissions; + } +} |