mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-02-07 10:30:14 -05:00
Magic to find possible mails and names of device owner
This commit is contained in:
parent
1d5606a1a3
commit
3f3e1cdb05
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user