Contacts a forum is already shared with should be selected. Dev task #79

This commit is contained in:
akwizgran
2014-03-17 14:49:11 +00:00
parent 8c18773141
commit abfff10f6b
4 changed files with 106 additions and 95 deletions

View File

@@ -1,7 +1,9 @@
package org.briarproject.android.contact; package org.briarproject.android.contact;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.briarproject.R; import org.briarproject.R;
@@ -17,27 +19,35 @@ import android.support.v4.app.DialogFragment;
public class SelectContactsDialog extends DialogFragment public class SelectContactsDialog extends DialogFragment
implements DialogInterface.OnMultiChoiceClickListener { implements DialogInterface.OnMultiChoiceClickListener {
private final Set<ContactId> selected = new HashSet<ContactId>();
private Listener listener = null; private Listener listener = null;
private Contact[] contacts = null; private List<Contact> contacts = null;
private Set<ContactId> selected = null;
public void setListener(Listener listener) { public void setListener(Listener listener) {
this.listener = listener; this.listener = listener;
} }
public void setContacts(Collection<Contact> contacts) { public void setContacts(Collection<Contact> contacts) {
this.contacts = contacts.toArray(new Contact[contacts.size()]); this.contacts = new ArrayList<Contact>(contacts);
}
public void setSelected(Collection<ContactId> selected) {
this.selected = new HashSet<ContactId>(selected);
} }
@Override @Override
public Dialog onCreateDialog(Bundle state) { public Dialog onCreateDialog(Bundle state) {
if(listener == null || contacts == null) return null; if(listener == null || contacts == null) return null;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String[] names = new String[contacts.length]; int size = contacts.size();
for(int i = 0; i < contacts.length; i++) String[] names = new String[size];
names[i] = contacts[i].getAuthor().getName(); boolean[] checked = new boolean[size];
builder.setMultiChoiceItems(names, new boolean[contacts.length], this); for(int i = 0; i < size; i++) {
Contact c = contacts.get(i);
names[i] = c.getAuthor().getName();
checked[i] = selected.contains(c.getId());
}
builder.setMultiChoiceItems(names, checked, this);
builder.setPositiveButton(R.string.done_button, builder.setPositiveButton(R.string.done_button,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
@@ -54,8 +64,8 @@ implements DialogInterface.OnMultiChoiceClickListener {
} }
public void onClick(DialogInterface dialog, int which, boolean isChecked) { public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked) selected.add(contacts[which].getId()); if(isChecked) selected.add(contacts.get(which).getId());
else selected.remove(contacts[which].getId()); else selected.remove(contacts.get(which).getId());
} }
public interface Listener { public interface Listener {

View File

@@ -47,7 +47,6 @@ SelectContactsDialog.Listener {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(ConfigureGroupActivity.class.getName()); Logger.getLogger(ConfigureGroupActivity.class.getName());
private boolean subscribed = false;
private CheckBox subscribeCheckBox = null; private CheckBox subscribeCheckBox = null;
private RadioGroup radioGroup = null; private RadioGroup radioGroup = null;
private RadioButton visibleToAll = null, visibleToSome = null; private RadioButton visibleToAll = null, visibleToSome = null;
@@ -58,8 +57,11 @@ SelectContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
private volatile GroupId groupId = null;
private volatile Group group = null; private volatile Group group = null;
private volatile Collection<ContactId> selected = Collections.emptyList(); private volatile boolean subscribed = false;
private volatile Collection<Contact> contacts = null;
private volatile Collection<ContactId> selected = null;
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
@@ -68,13 +70,13 @@ SelectContactsDialog.Listener {
Intent i = getIntent(); Intent i = getIntent();
byte[] b = i.getByteArrayExtra("briar.GROUP_ID"); byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
if(b == null) throw new IllegalStateException(); if(b == null) throw new IllegalStateException();
GroupId id = new GroupId(b); groupId = new GroupId(b);
String name = i.getStringExtra("briar.GROUP_NAME"); String name = i.getStringExtra("briar.GROUP_NAME");
if(name == null) throw new IllegalStateException(); if(name == null) throw new IllegalStateException();
setTitle(name); setTitle(name);
b = i.getByteArrayExtra("briar.GROUP_SALT"); b = i.getByteArrayExtra("briar.GROUP_SALT");
if(b == null) throw new IllegalStateException(); if(b == null) throw new IllegalStateException();
group = new Group(id, name, b); group = new Group(groupId, name, b);
subscribed = i.getBooleanExtra("briar.SUBSCRIBED", false); subscribed = i.getBooleanExtra("briar.SUBSCRIBED", false);
boolean all = i.getBooleanExtra("briar.VISIBLE_TO_ALL", false); boolean all = i.getBooleanExtra("briar.VISIBLE_TO_ALL", false);
@@ -145,18 +147,16 @@ SelectContactsDialog.Listener {
visibleToAll.setEnabled(subscribe); visibleToAll.setEnabled(subscribe);
visibleToSome.setEnabled(subscribe); visibleToSome.setEnabled(subscribe);
} else if(view == visibleToSome) { } else if(view == visibleToSome) {
loadContacts(); if(contacts == null) loadContacts();
else displayContacts();
} else if(view == doneButton) { } else if(view == doneButton) {
boolean subscribe = subscribeCheckBox.isChecked(); boolean subscribe = subscribeCheckBox.isChecked();
boolean all = visibleToAll.isChecked(); boolean all = visibleToAll.isChecked();
Collection<ContactId> visible =
Collections.unmodifiableCollection(selected);
// Replace the button with a progress bar // Replace the button with a progress bar
doneButton.setVisibility(GONE); doneButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
// Update the blog in a background thread // Update the blog in a background thread
if(subscribe || subscribed) if(subscribe || subscribed) updateGroup(subscribe, all);
updateGroup(subscribe, subscribed, all, visible);
} }
} }
@@ -165,11 +165,12 @@ SelectContactsDialog.Listener {
public void run() { public void run() {
try { try {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts(); contacts = db.getContacts();
selected = db.getVisibility(groupId);
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms"); LOG.info("Load took " + duration + " ms");
displayContacts(contacts); displayContacts();
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
@@ -178,7 +179,7 @@ SelectContactsDialog.Listener {
}); });
} }
private void displayContacts(final Collection<Contact> contacts) { private void displayContacts() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
FragmentManager fm = getSupportFragmentManager(); FragmentManager fm = getSupportFragmentManager();
@@ -186,24 +187,23 @@ SelectContactsDialog.Listener {
noContactsDialog.show(fm, "NoContactsDialog"); noContactsDialog.show(fm, "NoContactsDialog");
} else { } else {
selectContactsDialog.setContacts(contacts); selectContactsDialog.setContacts(contacts);
selectContactsDialog.setSelected(selected);
selectContactsDialog.show(fm, "SelectContactsDialog"); selectContactsDialog.show(fm, "SelectContactsDialog");
} }
} }
}); });
} }
private void updateGroup(final boolean subscribe, private void updateGroup(final boolean subscribe, final boolean all) {
final boolean wasSubscribed, final boolean all,
final Collection<ContactId> visible) {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if(subscribe) { if(subscribe) {
if(!wasSubscribed) db.addGroup(group); if(!subscribed) db.addGroup(group);
db.setVisibleToAll(group.getId(), all); db.setVisibleToAll(groupId, all);
if(!all) db.setVisibility(group.getId(), visible); if(!all) db.setVisibility(groupId, selected);
} else if(wasSubscribed) { } else if(subscribed) {
db.removeGroup(group); db.removeGroup(group);
} }
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
@@ -225,7 +225,7 @@ SelectContactsDialog.Listener {
public void contactCreationCancelled() {} public void contactCreationCancelled() {}
public void contactsSelected(Collection<ContactId> selected) { public void contactsSelected(Collection<ContactId> selected) {
this.selected = selected; this.selected = Collections.unmodifiableCollection(selected);
} }
public void contactSelectionCancelled() {} public void contactSelectionCancelled() {}

View File

@@ -70,7 +70,8 @@ SelectContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile GroupFactory groupFactory; @Inject private volatile GroupFactory groupFactory;
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
private volatile Collection<ContactId> selected = Collections.emptyList(); private volatile Collection<Contact> contacts = null;
private volatile Collection<ContactId> selected = Collections.emptySet();
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
@@ -144,37 +145,78 @@ SelectContactsDialog.Listener {
} }
private void enableOrDisableCreateButton() { private void enableOrDisableCreateButton() {
if(nameEntry == null || radioGroup == null || createButton == null) if(createButton == null) return; // Activity not created yet
return; // Activity not created yet
boolean nameNotEmpty = nameEntry.getText().length() > 0; boolean nameNotEmpty = nameEntry.getText().length() > 0;
boolean visibilitySelected = radioGroup.getCheckedRadioButtonId() != -1; boolean visibilitySelected = radioGroup.getCheckedRadioButtonId() != -1;
createButton.setEnabled(nameNotEmpty && visibilitySelected); createButton.setEnabled(nameNotEmpty && visibilitySelected);
} }
// FIXME: What is this for?
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) { public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
validateName(); validateName();
return true; return true;
} }
private boolean validateName() {
if(nameEntry.getText().length() == 0) return false;
byte[] b = StringUtils.toUtf8(nameEntry.getText().toString());
if(b.length > MAX_GROUP_NAME_LENGTH) return false;
// Hide the soft keyboard
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
return true;
}
public void onClick(View view) { public void onClick(View view) {
if(view == visibleToAll) { if(view == visibleToAll) {
enableOrDisableCreateButton(); enableOrDisableCreateButton();
} else if(view == visibleToSome) { } else if(view == visibleToSome) {
loadContacts(); if(contacts == null) loadContacts();
else displayContacts();
} else if(view == createButton) { } else if(view == createButton) {
if(!validateName()) return; if(!validateName()) return; // FIXME: Show feedback
createButton.setVisibility(GONE); createButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
String name = nameEntry.getText().toString(); String name = nameEntry.getText().toString();
boolean all = visibleToAll.isChecked(); boolean all = visibleToAll.isChecked();
Collection<ContactId> visible = storeGroup(name, all);
Collections.unmodifiableCollection(selected);
storeGroup(name, all, visible);
} }
} }
private void storeGroup(final String name, final boolean all, private void loadContacts() {
final Collection<ContactId> visible) { runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayContacts();
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displayContacts() {
runOnUiThread(new Runnable() {
public void run() {
FragmentManager fm = getSupportFragmentManager();
if(contacts.isEmpty()) {
noContactsDialog.show(fm, "NoContactsDialog");
} else {
selectContactsDialog.setContacts(contacts);
selectContactsDialog.setSelected(selected);
selectContactsDialog.show(fm, "SelectContactsDialog");
}
}
});
}
private void storeGroup(final String name, final boolean all) {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
@@ -182,7 +224,7 @@ SelectContactsDialog.Listener {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
db.addGroup(g); db.addGroup(g);
if(all) db.setVisibleToAll(g.getId(), true); if(all) db.setVisibleToAll(g.getId(), true);
else db.setVisibility(g.getId(), visible); else db.setVisibility(g.getId(), selected);
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Storing group took " + duration + " ms"); LOG.info("Storing group took " + duration + " ms");
@@ -211,48 +253,6 @@ SelectContactsDialog.Listener {
}); });
} }
private void loadContacts() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayContacts(contacts);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displayContacts(final Collection<Contact> contacts) {
runOnUiThread(new Runnable() {
public void run() {
FragmentManager fm = getSupportFragmentManager();
if(contacts.isEmpty()) {
noContactsDialog.show(fm, "NoContactsDialog");
} else {
selectContactsDialog.setContacts(contacts);
selectContactsDialog.show(fm, "SelectContactsDialog");
}
}
});
}
private boolean validateName() {
if(nameEntry.getText().length() == 0) return false;
byte[] b = StringUtils.toUtf8(nameEntry.getText().toString());
if(b.length > MAX_GROUP_NAME_LENGTH) return false;
// Hide the soft keyboard
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
return true;
}
public void contactCreationSelected() { public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class)); startActivity(new Intent(this, AddContactActivity.class));
} }
@@ -262,7 +262,7 @@ SelectContactsDialog.Listener {
} }
public void contactsSelected(Collection<ContactId> selected) { public void contactsSelected(Collection<ContactId> selected) {
this.selected = selected; this.selected = Collections.unmodifiableCollection(selected);
enableOrDisableCreateButton(); enableOrDisableCreateButton();
} }

View File

@@ -109,13 +109,24 @@ implements OnEditorActionListener, OnClickListener {
setContentView(layout); setContentView(layout);
} }
// FIXME: What is this for?
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) { public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
validateNickname(); validateNickname();
return true; return true;
} }
private boolean validateNickname() {
if(nicknameEntry.getText().length() == 0) return false;
byte[] b = StringUtils.toUtf8(nicknameEntry.getText().toString());
if(b.length > MAX_AUTHOR_NAME_LENGTH) return false;
// Hide the soft keyboard
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
return true;
}
public void onClick(View view) { public void onClick(View view) {
if(!validateNickname()) return; if(!validateNickname()) return; // FIXME: Show feedback
final String nickname = nicknameEntry.getText().toString(); final String nickname = nicknameEntry.getText().toString();
// Replace the button with a progress bar // Replace the button with a progress bar
createButton.setVisibility(GONE); createButton.setVisibility(GONE);
@@ -133,16 +144,6 @@ implements OnEditorActionListener, OnClickListener {
}); });
} }
private boolean validateNickname() {
if(nicknameEntry.getText().length() == 0) return false;
byte[] b = StringUtils.toUtf8(nicknameEntry.getText().toString());
if(b.length > MAX_AUTHOR_NAME_LENGTH) return false;
// Hide the soft keyboard
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
return true;
}
private void storeLocalAuthor(final LocalAuthor a) { private void storeLocalAuthor(final LocalAuthor a) {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {