Create a reusable ContactSelectorActivity

This commit is contained in:
Torsten Grote
2016-10-13 13:02:53 -03:00
parent feed2581c9
commit d5f6e71cba
3 changed files with 103 additions and 66 deletions

View File

@@ -12,7 +12,6 @@ import org.briarproject.R;
import org.briarproject.android.fragment.BaseFragment; import org.briarproject.android.fragment.BaseFragment;
import org.briarproject.android.view.LargeTextInputView; import org.briarproject.android.view.LargeTextInputView;
import org.briarproject.android.view.TextInputView.TextInputListener; import org.briarproject.android.view.TextInputView.TextInputListener;
import org.briarproject.util.StringUtils;
import static org.briarproject.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH; import static org.briarproject.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH;
import static org.briarproject.util.StringUtils.truncateUtf8; import static org.briarproject.util.StringUtils.truncateUtf8;
@@ -78,7 +77,10 @@ abstract class BaseMessageFragment extends BaseFragment
message.hideSoftKeyboard(); message.hideSoftKeyboard();
msg = truncateUtf8(msg, MAX_INVITATION_MESSAGE_LENGTH); msg = truncateUtf8(msg, MAX_INVITATION_MESSAGE_LENGTH);
listener.onButtonClick(msg); if(!listener.onButtonClick(msg)) {
message.setSendButtonEnabled(true);
message.showSoftKeyboard();
}
} }
public interface MessageFragmentListener { public interface MessageFragmentListener {
@@ -87,7 +89,8 @@ abstract class BaseMessageFragment extends BaseFragment
void setTitle(@StringRes int titleRes); void setTitle(@StringRes int titleRes);
void onButtonClick(String message); /** Returns true when the button click has been consumed. */
boolean onButtonClick(String message);
} }

View File

@@ -0,0 +1,92 @@
package org.briarproject.android.sharing;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.UiThread;
import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.fragment.BaseFragment.BaseFragmentListener;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.sync.GroupId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public abstract class ContactSelectorActivity extends BriarActivity implements
BaseFragmentListener, ContactSelectorListener {
final static String CONTACTS = "contacts";
protected volatile GroupId groupId;
protected volatile Collection<ContactId> contacts;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_container);
if (bundle != null) {
ArrayList<Integer> intContacts =
bundle.getIntegerArrayList(CONTACTS);
if (intContacts != null) {
contacts = getContactsFromIntegers(intContacts);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (contacts != null) {
outState.putIntegerArrayList(CONTACTS,
getContactsFromIds(contacts));
}
}
@CallSuper
@UiThread
@Override
public void contactsSelected(GroupId groupId,
Collection<ContactId> contacts) {
this.groupId = groupId;
this.contacts = contacts;
}
/**
* This must only be called from a DbThread
*/
public abstract boolean isDisabled(GroupId groupId, Contact c)
throws DbException;
static ArrayList<Integer> getContactsFromIds(
Collection<ContactId> contacts) {
// transform ContactIds to Integers so they can be added to a bundle
ArrayList<Integer> intContacts = new ArrayList<>(contacts.size());
for (ContactId contactId : contacts) {
intContacts.add(contactId.getInt());
}
return intContacts;
}
static Collection<ContactId> getContactsFromIntegers(
ArrayList<Integer> intContacts) {
// turn contact integers from a bundle back to ContactIds
List<ContactId> contacts = new ArrayList<>(intContacts.size());
for (Integer c : intContacts) {
contacts.add(new ContactId(c));
}
return contacts;
}
@Override
public void onFragmentCreated(String tag) {
}
}

View File

@@ -7,38 +7,27 @@ import android.support.annotation.UiThread;
import android.widget.Toast; import android.widget.Toast;
import org.briarproject.R; import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.fragment.BaseFragment.BaseFragmentListener;
import org.briarproject.android.sharing.BaseMessageFragment.MessageFragmentListener; import org.briarproject.android.sharing.BaseMessageFragment.MessageFragmentListener;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import static android.widget.Toast.LENGTH_SHORT; import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
public abstract class ShareActivity extends BriarActivity implements public abstract class ShareActivity extends ContactSelectorActivity implements
BaseFragmentListener, ContactSelectorListener, MessageFragmentListener { MessageFragmentListener {
private final static Logger LOG = private final static Logger LOG =
Logger.getLogger(ShareActivity.class.getName()); Logger.getLogger(ShareActivity.class.getName());
final static String CONTACTS = "contacts";
private volatile GroupId groupId;
private volatile Collection<ContactId> contacts;
@Override @Override
public void onCreate(Bundle bundle) { public void onCreate(Bundle bundle) {
super.onCreate(bundle); super.onCreate(bundle);
setContentView(R.layout.activity_fragment_container);
Intent i = getIntent(); Intent i = getIntent();
byte[] b = i.getByteArrayExtra(GROUP_ID); byte[] b = i.getByteArrayExtra(GROUP_ID);
if (b == null) throw new IllegalStateException("No GroupId"); if (b == null) throw new IllegalStateException("No GroupId");
@@ -50,21 +39,6 @@ public abstract class ShareActivity extends BriarActivity implements
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, contactSelectorFragment) .add(R.id.fragmentContainer, contactSelectorFragment)
.commit(); .commit();
} else {
ArrayList<Integer> intContacts =
bundle.getIntegerArrayList(CONTACTS);
if (intContacts != null) {
contacts = getContactsFromIntegers(intContacts);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (contacts != null) {
outState.putIntegerArrayList(CONTACTS,
getContactsFromIds(contacts));
} }
} }
@@ -72,11 +46,9 @@ public abstract class ShareActivity extends BriarActivity implements
@Override @Override
public void contactsSelected(GroupId groupId, public void contactsSelected(GroupId groupId,
Collection<ContactId> contacts) { Collection<ContactId> contacts) {
this.groupId = groupId; super.contactsSelected(groupId, contacts);
this.contacts = contacts;
BaseMessageFragment messageFragment = getMessageFragment(); BaseMessageFragment messageFragment = getMessageFragment();
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, .setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out, android.R.anim.fade_out,
@@ -90,38 +62,13 @@ public abstract class ShareActivity extends BriarActivity implements
abstract BaseMessageFragment getMessageFragment(); abstract BaseMessageFragment getMessageFragment();
/**
* This must only be called from a DbThread
*/
public abstract boolean isDisabled(GroupId groupId, Contact c)
throws DbException;
static ArrayList<Integer> getContactsFromIds(
Collection<ContactId> contacts) {
// transform ContactIds to Integers so they can be added to a bundle
ArrayList<Integer> intContacts = new ArrayList<>(contacts.size());
for (ContactId contactId : contacts) {
intContacts.add(contactId.getInt());
}
return intContacts;
}
static Collection<ContactId> getContactsFromIntegers(
ArrayList<Integer> intContacts) {
// turn contact integers from a bundle back to ContactIds
List<ContactId> contacts = new ArrayList<>(intContacts.size());
for (Integer c : intContacts) {
contacts.add(new ContactId(c));
}
return contacts;
}
@UiThread @UiThread
@Override @Override
public void onButtonClick(String message) { public boolean onButtonClick(String message) {
share(message); share(message);
setResult(RESULT_OK); setResult(RESULT_OK);
supportFinishAfterTransition(); supportFinishAfterTransition();
return true;
} }
private void share(final String msg) { private void share(final String msg) {
@@ -160,9 +107,4 @@ public abstract class ShareActivity extends BriarActivity implements
protected abstract @StringRes int getSharingError(); protected abstract @StringRes int getSharingError();
@Override
public void onFragmentCreated(String tag) {
}
} }