Commit 3a6c0112 authored by Finnur Thorarinsson's avatar Finnur Thorarinsson Committed by Commit Bot

Contacts Picker: Convert selection to JSON.

Bug: 860467
Change-Id: Ia844491c6c1245493e5d1b3521ee53151ae68316
Reviewed-on: https://chromium-review.googlesource.com/1196887
Commit-Queue: Finnur Thorarinsson <finnur@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588410}
parent 6b41453d
......@@ -5,7 +5,9 @@
package org.chromium.chrome.browser.contacts_picker;
import android.support.annotation.Nullable;
import android.util.JsonWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
......@@ -78,6 +80,23 @@ public class ContactDetails implements Comparable<ContactDetails> {
return builder.toString();
}
/**
* Appends to a string |builder| this contact (in json form).
* @param writer The JsonWriter object to add the data to.
*/
public void appendJson(JsonWriter writer) throws IOException {
writer.beginObject();
writer.name("name");
writer.value(getDisplayName());
writer.name("emails");
writer.beginArray();
for (String email : mEmails) {
writer.value(email);
}
writer.endArray();
writer.endObject();
}
/**
* A comparison function (results in a full name ascending sorting).
* @param other The other ContactDetails object to compare it with.
......
......@@ -9,6 +9,7 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.JsonWriter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
......@@ -24,6 +25,8 @@ import org.chromium.chrome.browser.widget.selection.SelectionDelegate;
import org.chromium.ui.ContactsPickerListener;
import org.chromium.ui.UiUtils;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
......@@ -275,15 +278,24 @@ public class PickerCategoryView extends RelativeLayout
* Notifies any listeners that one or more contacts have been selected.
*/
private void notifyContactsSelected() {
List<ContactDetails> selectedFiles = mSelectionDelegate.getSelectedItemsAsList();
Collections.sort(selectedFiles);
String[] contacts = new String[selectedFiles.size()];
int i = 0;
for (ContactDetails contactDetails : selectedFiles) {
contacts[i++] = contactDetails.getDisplayName();
}
List<ContactDetails> selectedContacts = mSelectionDelegate.getSelectedItemsAsList();
Collections.sort(selectedContacts);
StringWriter out = new StringWriter();
final JsonWriter writer = new JsonWriter(out);
executeAction(ContactsPickerListener.ContactsPickerAction.CONTACTS_SELECTED, contacts);
try {
writer.beginArray();
for (ContactDetails contactDetails : selectedContacts) {
contactDetails.appendJson(writer);
}
writer.endArray();
executeAction(
ContactsPickerListener.ContactsPickerAction.CONTACTS_SELECTED, out.toString());
} catch (IOException e) {
assert false;
executeAction(ContactsPickerListener.ContactsPickerAction.CANCEL, null);
}
}
/**
......@@ -292,7 +304,7 @@ public class PickerCategoryView extends RelativeLayout
* @param contacts The contacts that were selected (if any).
*/
private void executeAction(
ContactsPickerListener.ContactsPickerAction action, String[] contacts) {
ContactsPickerListener.ContactsPickerAction action, String contacts) {
mListener.onContactsPickerUserAction(action, contacts);
mDialog.dismiss();
UiUtils.onContactsPickerDismissed();
......
......@@ -19,7 +19,7 @@ public interface ContactsPickerListener {
/**
* Called when the user has selected an action. For possible actions see above.
*
* @param contacts The contacts that were selected.
* @param contacts The contacts that were selected (string contains json format).
*/
void onContactsPickerUserAction(ContactsPickerAction action, String[] contacts);
void onContactsPickerUserAction(ContactsPickerAction action, String contacts);
}
......@@ -431,7 +431,7 @@ public class SelectFileDialog
}
@Override
public void onContactsPickerUserAction(ContactsPickerAction action, String[] contacts) {
public void onContactsPickerUserAction(ContactsPickerAction action, String contacts) {
switch (action) {
case CANCEL:
onFileNotSelected();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment