Select add/edit contact

pull/177/head
M66B 6 years ago
parent 00cf7504a6
commit 206c84bfb0

@ -1356,7 +1356,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchContact.setVisibility(show_addresses && (hasFrom || hasTo) ? View.VISIBLE : View.GONE); ibSearchContact.setVisibility(show_addresses && (hasFrom || hasTo) ? View.VISIBLE : View.GONE);
ibNotifyContact.setVisibility(show_addresses && hasChannel && hasFrom ? View.VISIBLE : View.GONE); ibNotifyContact.setVisibility(show_addresses && hasChannel && hasFrom ? View.VISIBLE : View.GONE);
ibPinContact.setVisibility(show_addresses && pin && hasFrom ? View.VISIBLE : View.GONE); ibPinContact.setVisibility(show_addresses && pin && hasFrom ? View.VISIBLE : View.GONE);
ibAddContact.setVisibility(show_addresses && hasFrom ? View.VISIBLE : View.GONE); ibAddContact.setVisibility(show_addresses && contacts && hasFrom ? View.VISIBLE : View.GONE);
tvSubmitterTitle.setVisibility(show_addresses && !TextUtils.isEmpty(submitter) ? View.VISIBLE : View.GONE); tvSubmitterTitle.setVisibility(show_addresses && !TextUtils.isEmpty(submitter) ? View.VISIBLE : View.GONE);
tvSubmitter.setVisibility(show_addresses && !TextUtils.isEmpty(submitter) ? View.VISIBLE : View.GONE); tvSubmitter.setVisibility(show_addresses && !TextUtils.isEmpty(submitter) ? View.VISIBLE : View.GONE);
@ -3041,17 +3041,14 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
String email = ia.getAddress(); String email = ia.getAddress();
Uri lookupUri = null; Uri lookupUri = null;
if (contacts) {
String like = "%" + (name == null ? email : name) + "%";
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID, ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY ContactsContract.Contacts.LOOKUP_KEY
}, },
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?" + ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
" OR " + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", new String[]{email}, null)) {
new String[]{email, like}, null)) {
if (cursor != null && cursor.moveToNext()) { if (cursor != null && cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID); int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
@ -3062,20 +3059,73 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); lookupUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
} }
} }
if (lookupUri == null) {
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, ibAddContact);
popupMenu.getMenu().add(Menu.NONE, R.string.title_insert_contact, 1, R.string.title_insert_contact);
popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_contact, 2, R.string.title_edit_contact);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.string.title_insert_contact:
onInsertContact(name, email);
return true;
case R.string.title_edit_contact:
onPickContact(name, email);
return true;
default:
return false;
} }
}
});
popupMenu.show();
} else
onEditContact(name, email, lookupUri);
}
private void onPickContact(String name, String email) {
Intent pick = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
if (pick.resolveActivity(context.getPackageManager()) == null)
Snackbar.make(view, R.string.title_no_contacts, Snackbar.LENGTH_LONG).show();
else {
properties.setValue("name", name);
properties.setValue("email", email);
parentFragment.startActivityForResult(
Helper.getChooser(context, pick), FragmentMessages.REQUEST_PICK_CONTACT);
}
}
private void onInsertContact(String name, String email) {
// https://developer.android.com/training/contacts-provider/modify-data
Intent insert = new Intent();
insert.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
if (!TextUtils.isEmpty(name))
insert.putExtra(ContactsContract.Intents.Insert.NAME, name);
insert.setAction(Intent.ACTION_INSERT);
insert.setType(ContactsContract.Contacts.CONTENT_TYPE);
PackageManager pm = context.getPackageManager();
if (insert.resolveActivity(pm) == null)
Snackbar.make(parentFragment.getView(),
R.string.title_no_contacts, Snackbar.LENGTH_LONG).show();
else
context.startActivity(insert);
}
private void onEditContact(String name, String email, Uri lookupUri) {
// https://developer.android.com/training/contacts-provider/modify-data // https://developer.android.com/training/contacts-provider/modify-data
Intent edit = new Intent(); Intent edit = new Intent();
edit.putExtra(ContactsContract.Intents.Insert.EMAIL, email); edit.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
if (!TextUtils.isEmpty(name)) if (!TextUtils.isEmpty(name))
edit.putExtra(ContactsContract.Intents.Insert.NAME, name); edit.putExtra(ContactsContract.Intents.Insert.NAME, name);
if (lookupUri == null) {
edit.setAction(Intent.ACTION_INSERT);
edit.setType(ContactsContract.Contacts.CONTENT_TYPE);
} else {
edit.setAction(Intent.ACTION_EDIT); edit.setAction(Intent.ACTION_EDIT);
edit.setDataAndTypeAndNormalize(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE); edit.setDataAndTypeAndNormalize(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
}
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
if (edit.resolveActivity(pm) == null) if (edit.resolveActivity(pm) == null)
@ -5080,6 +5130,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
interface IProperties { interface IProperties {
void setValue(String key, String value);
void setValue(String name, long id, boolean enabled); void setValue(String name, long id, boolean enabled);
boolean getValue(String name, long id); boolean getValue(String name, long id);

@ -24,6 +24,7 @@ import android.app.Dialog;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -52,6 +53,7 @@ import android.os.Parcelable;
import android.print.PrintAttributes; import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter; import android.print.PrintDocumentAdapter;
import android.print.PrintManager; import android.print.PrintManager;
import android.provider.ContactsContract;
import android.security.KeyChain; import android.security.KeyChain;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
@ -285,6 +287,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private Long closeId = null; private Long closeId = null;
private int autoCloseCount = 0; private int autoCloseCount = 0;
private boolean autoExpanded = true; private boolean autoExpanded = true;
private Map<String, String> kv = new HashMap<>();
private Map<String, List<Long>> values = new HashMap<>(); private Map<String, List<Long>> values = new HashMap<>();
private LongSparseArray<Float> sizes = new LongSparseArray<>(); private LongSparseArray<Float> sizes = new LongSparseArray<>();
private LongSparseArray<Integer> heights = new LongSparseArray<>(); private LongSparseArray<Integer> heights = new LongSparseArray<>();
@ -317,6 +320,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private static final int REQUEST_ACCOUNT = 19; private static final int REQUEST_ACCOUNT = 19;
private static final int REQUEST_EMPTY_FOLDER = 20; private static final int REQUEST_EMPTY_FOLDER = 20;
private static final int REQUEST_BOUNDARY_RETRY = 21; private static final int REQUEST_BOUNDARY_RETRY = 21;
static final int REQUEST_PICK_CONTACT = 22;
static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW"; static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW";
static final String ACTION_DECRYPT = BuildConfig.APPLICATION_ID + ".DECRYPT"; static final String ACTION_DECRYPT = BuildConfig.APPLICATION_ID + ".DECRYPT";
@ -1388,6 +1392,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
} }
private AdapterMessage.IProperties iProperties = new AdapterMessage.IProperties() { private AdapterMessage.IProperties iProperties = new AdapterMessage.IProperties() {
@Override
public void setValue(String key, String value) {
kv.put(key, value);
}
@Override @Override
public void setValue(String name, long id, boolean enabled) { public void setValue(String name, long id, boolean enabled) {
if (!values.containsKey(name)) if (!values.containsKey(name))
@ -4867,6 +4876,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (resultCode == RESULT_OK) if (resultCode == RESULT_OK)
onBoundaryRetry(); onBoundaryRetry();
break; break;
case REQUEST_PICK_CONTACT:
if (resultCode == RESULT_OK && data != null)
onPickContact(data.getData());
break;
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
@ -6365,6 +6378,23 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
model.retry(viewType); model.retry(viewType);
} }
private void onPickContact(Uri contactUri) {
String name = kv.get("name");
String email = kv.get("email");
// This requires contacts permission
ContentResolver resolver = getContext().getContentResolver();
Uri lookupUri = ContactsContract.Contacts.getLookupUri(resolver, contactUri);
Intent edit = new Intent();
edit.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
if (!TextUtils.isEmpty(name))
edit.putExtra(ContactsContract.Intents.Insert.NAME, name);
edit.setAction(Intent.ACTION_EDIT);
edit.setDataAndTypeAndNormalize(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
startActivity(edit);
}
static void search( static void search(
final Context context, final LifecycleOwner owner, final FragmentManager manager, final Context context, final LifecycleOwner owner, final FragmentManager manager,
long account, long folder, boolean server, String query) { long account, long folder, boolean server, String query) {

@ -620,6 +620,8 @@
<string name="title_create_channel">Create notification channel</string> <string name="title_create_channel">Create notification channel</string>
<string name="title_edit_channel">Edit notification channel</string> <string name="title_edit_channel">Edit notification channel</string>
<string name="title_delete_channel">Delete notification channel</string> <string name="title_delete_channel">Delete notification channel</string>
<string name="title_insert_contact">Add contact</string>
<string name="title_edit_contact">Edit contact</string>
<string name="title_create_sub_folder">Create sub folder</string> <string name="title_create_sub_folder">Create sub folder</string>
<string name="title_empty_trash_ask">Delete all local trashed messages permanently?</string> <string name="title_empty_trash_ask">Delete all local trashed messages permanently?</string>

Loading…
Cancel
Save