From c8c170ed7bf9713c11969db4273780ae9541dd36 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 5 Apr 2012 18:49:47 -0700 Subject: [PATCH] First draft of contact picking --- .../astrid/actfm/EditPeopleControlSet.java | 42 +++++++++++++++++-- astrid/res/values/strings-core.xml | 2 + .../astrid/activity/TaskEditFragment.java | 7 ++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java index 8db6b7c99..272f5f08b 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/EditPeopleControlSet.java @@ -15,7 +15,10 @@ import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.database.Cursor; import android.graphics.Color; +import android.net.Uri; +import android.provider.ContactsContract; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.DisplayMetrics; @@ -45,6 +48,7 @@ import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.actfm.sync.ActFmSyncService; +import com.todoroo.astrid.activity.TaskEditFragment; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; @@ -112,6 +116,8 @@ public class EditPeopleControlSet extends PopupControlSet { private boolean loadedUI = false; + private boolean dontClearAssignedCustom = false; + private final List listeners = new LinkedList(); public interface AssignedChangedListener { @@ -175,7 +181,9 @@ public class EditPeopleControlSet extends PopupControlSet { @Override public void readFromTask(Task sourceTask) { setTask(sourceTask); - assignedCustom.setText(""); //$NON-NLS-1$ + if (!dontClearAssignedCustom) + assignedCustom.setText(""); //$NON-NLS-1$ + dontClearAssignedCustom = false; setUpData(task, null); } @@ -531,6 +539,14 @@ public class EditPeopleControlSet extends PopupControlSet { sharedWithDialog.show(); } }); + + assignedCustom.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); + fragment.startActivityForResult(intent, TaskEditFragment.REQUEST_CODE_CONTACT); + } + }); } // --- events @@ -811,9 +827,29 @@ public class EditPeopleControlSet extends PopupControlSet { // clear user values & reset them to trigger a save task.clearValue(Task.USER_ID); task.clearValue(Task.USER); - } - else if (requestCode == loginRequestCode) + } else if (requestCode == loginRequestCode) { makePrivateTask(); + } else if (requestCode == TaskEditFragment.REQUEST_CODE_CONTACT && resultCode == Activity.RESULT_OK) { + Uri contactData = data.getData(); + String contactId = contactData.getLastPathSegment(); + String[] args = { contactId }; + String[] projection = { ContactsContract.CommonDataKinds.Email.DATA }; + String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"; //$NON-NLS-1$ + Cursor c = activity.managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, selection, args, null); + if (c.getCount() > 0) { + c.moveToFirst(); + int emailIndex = c.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); + String email = c.getString(emailIndex); + if (!TextUtils.isEmpty(email)) { + assignedCustom.setText(email); + dontClearAssignedCustom = true; + } else { + DialogUtilities.okDialog(activity, activity.getString(R.string.TEA_contact_error), null); + } + } else { + DialogUtilities.okDialog(activity, activity.getString(R.string.TEA_contact_error), null); + } + } } @Override diff --git a/astrid/res/values/strings-core.xml b/astrid/res/values/strings-core.xml index 15b4074b9..4142dc5a3 100644 --- a/astrid/res/values/strings-core.xml +++ b/astrid/res/values/strings-core.xml @@ -450,6 +450,8 @@ I can do more when connected to the Internet. Please check your connection. + Sorry! We couldn\'t find an email address for the selected contact. + diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index f213c8d15..49b9de922 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -149,6 +149,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { public static final int REQUEST_LOG_IN = 0; private static final int REQUEST_VOICE_RECOG = 10; + public static final int REQUEST_CODE_CONTACT = 20; // --- menu codes @@ -1058,11 +1059,9 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { public void onActivityResult(int requestCode, int resultCode, Intent data) { if (taskRabbitControl != null && taskRabbitControl.activityResult(requestCode, resultCode, data)) { return; - } - else if (editNotes != null && editNotes.activityResult(requestCode, resultCode, data)) { + } else if (editNotes != null && editNotes.activityResult(requestCode, resultCode, data)) { return; - } - else if (requestCode == REQUEST_VOICE_RECOG + } else if (requestCode == REQUEST_VOICE_RECOG && resultCode == Activity.RESULT_OK) { // handle the result of voice recognition, put it into the // appropiate textfield