Select add/edit contact

This commit is contained in:
M66B 2020-03-24 20:55:59 +01:00
parent 00cf7504a6
commit 206c84bfb0
3 changed files with 110 additions and 26 deletions

View File

@ -1356,7 +1356,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchContact.setVisibility(show_addresses && (hasFrom || hasTo) ? View.VISIBLE : View.GONE);
ibNotifyContact.setVisibility(show_addresses && hasChannel && 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);
tvSubmitter.setVisibility(show_addresses && !TextUtils.isEmpty(submitter) ? View.VISIBLE : View.GONE);
@ -3041,41 +3041,91 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
String email = ia.getAddress();
Uri lookupUri = null;
if (contacts) {
String like = "%" + (name == null ? email : name) + "%";
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY
},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?" +
" OR " + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[]{email, like}, null)) {
if (cursor != null && cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
ContactsContract.Contacts.LOOKUP_KEY
},
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
new String[]{email}, null)) {
if (cursor != null && cursor.moveToNext()) {
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
long contactId = cursor.getLong(colContactId);
String lookupKey = cursor.getString(colLookupKey);
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
Intent edit = new Intent();
edit.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
if (!TextUtils.isEmpty(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.setDataAndTypeAndNormalize(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
}
edit.setAction(Intent.ACTION_EDIT);
edit.setDataAndTypeAndNormalize(lookupUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
PackageManager pm = context.getPackageManager();
if (edit.resolveActivity(pm) == null)
@ -5080,6 +5130,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
interface IProperties {
void setValue(String key, String value);
void setValue(String name, long id, boolean enabled);
boolean getValue(String name, long id);

View File

@ -24,6 +24,7 @@ import android.app.Dialog;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -52,6 +53,7 @@ import android.os.Parcelable;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.provider.ContactsContract;
import android.security.KeyChain;
import android.text.SpannableString;
import android.text.TextUtils;
@ -285,6 +287,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private Long closeId = null;
private int autoCloseCount = 0;
private boolean autoExpanded = true;
private Map<String, String> kv = new HashMap<>();
private Map<String, List<Long>> values = new HashMap<>();
private LongSparseArray<Float> sizes = 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_EMPTY_FOLDER = 20;
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_DECRYPT = BuildConfig.APPLICATION_ID + ".DECRYPT";
@ -1388,6 +1392,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
}
private AdapterMessage.IProperties iProperties = new AdapterMessage.IProperties() {
@Override
public void setValue(String key, String value) {
kv.put(key, value);
}
@Override
public void setValue(String name, long id, boolean enabled) {
if (!values.containsKey(name))
@ -4867,6 +4876,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (resultCode == RESULT_OK)
onBoundaryRetry();
break;
case REQUEST_PICK_CONTACT:
if (resultCode == RESULT_OK && data != null)
onPickContact(data.getData());
break;
}
} catch (Throwable ex) {
Log.e(ex);
@ -6365,6 +6378,23 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
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(
final Context context, final LifecycleOwner owner, final FragmentManager manager,
long account, long folder, boolean server, String query) {

View File

@ -620,6 +620,8 @@
<string name="title_create_channel">Create notification channel</string>
<string name="title_edit_channel">Edit 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_empty_trash_ask">Delete all local trashed messages permanently?</string>