diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-06-28 00:05:12 +0200 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-06-28 00:05:12 +0200 |
commit | 3f3e1cdb05ee0e5ab9a5a4aed8ac3ef4ef581f24 (patch) | |
tree | a3905f4cecd1692eb6eb11893c99b75b3a9aa531 /OpenKeychain/src/main | |
parent | 1d5606a1a31d49248d90d0fd6c9734b27cbb1a08 (diff) | |
download | open-keychain-3f3e1cdb05ee0e5ab9a5a4aed8ac3ef4ef581f24.tar.gz open-keychain-3f3e1cdb05ee0e5ab9a5a4aed8ac3ef4ef581f24.tar.bz2 open-keychain-3f3e1cdb05ee0e5ab9a5a4aed8ac3ef4ef581f24.zip |
Magic to find possible mails and names of device owner
Diffstat (limited to 'OpenKeychain/src/main')
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 @@ <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> + <uses-permission android:name="android.permission.READ_PROFILE" /> <!-- android:allowBackup="false": Don't allow backup over adb backup or other apps! --> <application diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java index d8a7e8427..e639824ec 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/ContactHelper.java @@ -22,8 +22,10 @@ import android.accounts.AccountManager; import android.content.*; import android.database.Cursor; import android.net.Uri; +import android.os.Build; import android.provider.ContactsContract; import android.util.Patterns; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.KeyRing; @@ -58,7 +60,27 @@ public class ContactHelper { ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?"; public static final String ID_SELECTION = ContactsContract.RawContacts._ID + "=?"; - public static List<String> getMailAccounts(Context context) { + public static List<String> getPossibleUserEmails(Context context) { + Set<String> accountMails = getAccountEmails(context); + accountMails.addAll(getMainProfileContactEmails(context)); + // now return the Set (without duplicates) as a List + return new ArrayList<String>(accountMails); + } + + public static List<String> getPossibleUserNames(Context context) { + Set<String> accountMails = getAccountEmails(context); + Set<String> names = getContactNamesFromEmails(context, accountMails); + names.addAll(getMainProfileContactName(context)); + return new ArrayList<String>(names); + } + + /** + * Get emails from AccountManager + * + * @param context + * @return + */ + private static Set<String> getAccountEmails(Context context) { final Account[] accounts = AccountManager.get(context).getAccounts(); final Set<String> emailSet = new HashSet<String>(); for (Account account : accounts) { @@ -66,7 +88,118 @@ public class ContactHelper { emailSet.add(account.name); } } - return new ArrayList<String>(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<String> getContactNamesFromEmails(Context context, Set<String> emails) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + Set<String> names = new HashSet<String>(); + 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<String> currNames = new HashSet<String>(); + 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<String>(); + } + } + + /** + * Retrieves the emails of the primary profile contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param context + * @return + */ + private static Set<String> 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<String> emails = new HashSet<String>(); + while (profileCursor.moveToNext()) { + String email = profileCursor.getString(0); + if (email != null) { + emails.add(email); + } + } + profileCursor.close(); + return emails; + } else { + return new HashSet<String>(); + } + } + + /** + * Retrieves the name of the primary profile contact + * http://developer.android.com/reference/android/provider/ContactsContract.Profile.html + * + * @param context + * @return + */ + private static List<String> 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<String> names = new HashSet<String>(); + // should only contain one entry! + while (profileCursor.moveToNext()) { + String name = profileCursor.getString(0); + if (name != null) { + names.add(name); + } + } + profileCursor.close(); + return new ArrayList<String>(names); + } else { + return new ArrayList<String>(); + } } public static List<String> 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<String> (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<String> + (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<String> (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" /> - <EditText + <AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" |