From 3f3e1cdb05ee0e5ab9a5a4aed8ac3ef4ef581f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 28 Jun 2014 00:05:12 +0200 Subject: Magic to find possible mails and names of device owner --- OpenKeychain/src/main/AndroidManifest.xml | 1 + .../keychain/helper/ContactHelper.java | 137 ++++++++++++++++++++- .../keychain/ui/WizardActivity.java | 10 +- .../keychain/ui/widget/UserIdEditor.java | 2 +- .../main/res/layout/wizard_create_key_fragment.xml | 2 +- 5 files changed, 147 insertions(+), 5 deletions(-) diff --git a/OpenKeychain/src/main/AndroidManifest.xml b/OpenKeychain/src/main/AndroidManifest.xml index d5f031ecc..3ce200008 100644 --- a/OpenKeychain/src/main/AndroidManifest.xml +++ b/OpenKeychain/src/main/AndroidManifest.xml @@ -59,6 +59,7 @@ + getMailAccounts(Context context) { + public static List getPossibleUserEmails(Context context) { + Set accountMails = getAccountEmails(context); + accountMails.addAll(getMainProfileContactEmails(context)); + // now return the Set (without duplicates) as a List + return new ArrayList(accountMails); + } + + public static List getPossibleUserNames(Context context) { + Set accountMails = getAccountEmails(context); + Set names = getContactNamesFromEmails(context, accountMails); + names.addAll(getMainProfileContactName(context)); + return new ArrayList(names); + } + + /** + * Get emails from AccountManager + * + * @param context + * @return + */ + private static Set getAccountEmails(Context context) { final Account[] accounts = AccountManager.get(context).getAccounts(); final Set emailSet = new HashSet(); for (Account account : accounts) { @@ -66,7 +88,118 @@ public class ContactHelper { emailSet.add(account.name); } } - return new ArrayList(emailSet); + return emailSet; + } + + /** + * Search for contact names based on a list of emails (to find out the names of the + * device owner based on the email addresses from AccountsManager) + * + * @param context + * @param emails + * @return + */ + private static Set getContactNamesFromEmails(Context context, Set emails) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + Set names = new HashSet(); + for (String email : emails) { + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query( + ContactsContract.CommonDataKinds.Email.CONTENT_URI, + new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS, + ContactsContract.Contacts.DISPLAY_NAME}, + ContactsContract.CommonDataKinds.Email.ADDRESS + "=?", + new String[]{email}, null + ); + if (profileCursor == null) return null; + + Set currNames = new HashSet(); + while (profileCursor.moveToNext()) { + String name = profileCursor.getString(1); + Log.d(Constants.TAG, "name" + name); + if (name != null) { + currNames.add(name); + } + } + profileCursor.close(); + names.addAll(currNames); + } + return names; + } else { + return new HashSet(); + } + } + + /** + * Retrieves the emails of the primary profile contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param context + * @return + */ + private static Set getMainProfileContactEmails(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query( + Uri.withAppendedPath( + ContactsContract.Profile.CONTENT_URI, + ContactsContract.Contacts.Data.CONTENT_DIRECTORY), + new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS, + ContactsContract.CommonDataKinds.Email.IS_PRIMARY}, + + // Selects only email addresses + ContactsContract.Contacts.Data.MIMETYPE + "=?", + new String[]{ + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, + }, + // Show primary rows first. Note that there won't be a primary email address if the + // user hasn't specified one. + ContactsContract.Contacts.Data.IS_PRIMARY + " DESC" + ); + if (profileCursor == null) return null; + + Set emails = new HashSet(); + while (profileCursor.moveToNext()) { + String email = profileCursor.getString(0); + if (email != null) { + emails.add(email); + } + } + profileCursor.close(); + return emails; + } else { + return new HashSet(); + } + } + + /** + * Retrieves the name of the primary profile contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param context + * @return + */ + private static List getMainProfileContactName(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + ContentResolver resolver = context.getContentResolver(); + Cursor profileCursor = resolver.query(ContactsContract.Profile.CONTENT_URI, + new String[]{ContactsContract.Profile.DISPLAY_NAME}, + null, null, null); + if (profileCursor == null) return null; + + Set names = new HashSet(); + // should only contain one entry! + while (profileCursor.moveToNext()) { + String name = profileCursor.getString(0); + if (name != null) { + names.add(name); + } + } + profileCursor.close(); + return new ArrayList(names); + } else { + return new ArrayList(); + } } public static List getContactMails(Context context) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java index 3a528cb18..20b47ed01 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java @@ -143,7 +143,7 @@ public class WizardActivity extends ActionBarActivity { emailView.setAdapter( new ArrayAdapter (getActivity(), android.R.layout.simple_dropdown_item_1line, - ContactHelper.getMailAccounts(getActivity()) + ContactHelper.getPossibleUserEmails(getActivity()) ) ); emailView.addTextChangedListener(new TextWatcher() { @@ -173,6 +173,14 @@ public class WizardActivity extends ActionBarActivity { } } }); + final AutoCompleteTextView nameView = (AutoCompleteTextView) view.findViewById(R.id.name); + nameView.setThreshold(1); // Start working from first character + nameView.setAdapter( + new ArrayAdapter + (getActivity(), android.R.layout.simple_dropdown_item_1line, + ContactHelper.getPossibleUserNames(getActivity()) + ) + ); return view; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/UserIdEditor.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/UserIdEditor.java index 9781dd213..04d4ca5b6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/UserIdEditor.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/UserIdEditor.java @@ -119,7 +119,7 @@ public class UserIdEditor extends LinearLayout implements Editor, OnClickListene mEmail.setAdapter( new ArrayAdapter (this.getContext(), android.R.layout.simple_dropdown_item_1line, - ContactHelper.getMailAccounts(getContext()) + ContactHelper.getPossibleUserEmails(getContext()) )); mEmail.addTextChangedListener(new TextWatcher(){ @Override diff --git a/OpenKeychain/src/main/res/layout/wizard_create_key_fragment.xml b/OpenKeychain/src/main/res/layout/wizard_create_key_fragment.xml index 213346617..258ea7223 100644 --- a/OpenKeychain/src/main/res/layout/wizard_create_key_fragment.xml +++ b/OpenKeychain/src/main/res/layout/wizard_create_key_fragment.xml @@ -11,7 +11,7 @@ android:text="Enter Full Name, Email and Passphrase!" android:textAppearance="?android:attr/textAppearanceMedium" /> -