mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Factor out a reuseable MessageFragment
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<FrameLayout
|
|
||||||
android:id="@+id/shareContainer"
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"/>
|
|
||||||
10
briar-android/res/layout/fragment_message.xml
Normal file
10
briar-android/res/layout/fragment_message.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<org.briarproject.android.view.LargeTextInputView
|
||||||
|
android:id="@+id/messageView"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
app:buttonText="@string/forum_share_button"
|
||||||
|
app:fillHeight="true"
|
||||||
|
app:hint="@string/forum_share_message"/>
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:orientation="vertical">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/introductionText"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:padding="@dimen/margin_activity_horizontal"
|
|
||||||
android:text="@string/forum_share_message"
|
|
||||||
android:textColor="@color/briar_text_primary"
|
|
||||||
android:textSize="@dimen/text_size_medium"/>
|
|
||||||
|
|
||||||
<org.briarproject.android.view.LargeTextInputView
|
|
||||||
android:id="@+id/invitationMessageView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="0dp"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:gravity="bottom"
|
|
||||||
app:buttonText="@string/forum_share_button"
|
|
||||||
app:fillHeight="true"
|
|
||||||
app:hint="@string/introduction_message_hint"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package org.briarproject.android.sharing;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.StringRes;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import org.briarproject.R;
|
||||||
|
import org.briarproject.android.fragment.BaseFragment;
|
||||||
|
import org.briarproject.android.view.LargeTextInputView;
|
||||||
|
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.util.StringUtils.truncateUtf8;
|
||||||
|
|
||||||
|
abstract class BaseMessageFragment extends BaseFragment
|
||||||
|
implements TextInputListener {
|
||||||
|
|
||||||
|
protected LargeTextInputView message;
|
||||||
|
private MessageFragmentListener listener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
listener = (MessageFragmentListener) context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
|
||||||
|
// allow for "up" button to act as back button
|
||||||
|
setHasOptionsMenu(true);
|
||||||
|
|
||||||
|
// inflate view
|
||||||
|
View v = inflater.inflate(R.layout.fragment_message, container,
|
||||||
|
false);
|
||||||
|
message = (LargeTextInputView) v.findViewById(R.id.messageView);
|
||||||
|
message.setButtonText(getString(getButtonText()));
|
||||||
|
message.setHint(getHintText());
|
||||||
|
message.setListener(this);
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setTitle(int res) {
|
||||||
|
listener.setTitle(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract @StringRes int getButtonText();
|
||||||
|
protected abstract @StringRes int getHintText();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
message.showSoftKeyboard();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case android.R.id.home:
|
||||||
|
listener.onBackPressed();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSendClick(String msg) {
|
||||||
|
// disable button to prevent accidental double actions
|
||||||
|
message.setSendButtonEnabled(false);
|
||||||
|
message.hideSoftKeyboard();
|
||||||
|
|
||||||
|
msg = truncateUtf8(msg, MAX_INVITATION_MESSAGE_LENGTH);
|
||||||
|
listener.onButtonClick(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface MessageFragmentListener {
|
||||||
|
|
||||||
|
void onBackPressed();
|
||||||
|
|
||||||
|
void setTitle(@StringRes int titleRes);
|
||||||
|
|
||||||
|
void onButtonClick(String message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,11 +2,14 @@ package org.briarproject.android.sharing;
|
|||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.support.annotation.StringRes;
|
||||||
|
import android.support.annotation.UiThread;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.BriarActivity;
|
import org.briarproject.android.BriarActivity;
|
||||||
import org.briarproject.android.fragment.BaseFragment.BaseFragmentListener;
|
import org.briarproject.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
|
import org.briarproject.android.sharing.BaseMessageFragment.MessageFragmentListener;
|
||||||
import org.briarproject.api.contact.Contact;
|
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;
|
||||||
@@ -15,34 +18,77 @@ import org.briarproject.api.sync.GroupId;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static android.widget.Toast.LENGTH_SHORT;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
public abstract class ShareActivity extends BriarActivity implements
|
public abstract class ShareActivity extends BriarActivity implements
|
||||||
BaseFragmentListener, ContactSelectorListener {
|
BaseFragmentListener, ContactSelectorListener, MessageFragmentListener {
|
||||||
|
|
||||||
|
private final static Logger LOG =
|
||||||
|
Logger.getLogger(ShareActivity.class.getName());
|
||||||
final static String CONTACTS = "contacts";
|
final static String CONTACTS = "contacts";
|
||||||
|
|
||||||
@Override
|
private volatile GroupId groupId;
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
private volatile Collection<ContactId> contacts;
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_share);
|
@Override
|
||||||
|
public void onCreate(Bundle 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");
|
||||||
GroupId groupId = new GroupId(b);
|
groupId = new GroupId(b);
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (bundle == null) {
|
||||||
ContactSelectorFragment contactSelectorFragment =
|
ContactSelectorFragment contactSelectorFragment =
|
||||||
ContactSelectorFragment.newInstance(groupId);
|
ContactSelectorFragment.newInstance(groupId);
|
||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.add(R.id.shareContainer, contactSelectorFragment)
|
.add(R.id.fragmentContainer, contactSelectorFragment)
|
||||||
.commit();
|
.commit();
|
||||||
|
} else {
|
||||||
|
ArrayList<Integer> intContacts =
|
||||||
|
bundle.getIntegerArrayList(CONTACTS);
|
||||||
|
if (intContacts != null) {
|
||||||
|
contacts = getContactsFromIntegers(intContacts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract ShareMessageFragment getMessageFragment(GroupId groupId,
|
@Override
|
||||||
Collection<ContactId> contacts);
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
if (contacts != null) {
|
||||||
|
outState.putIntegerArrayList(CONTACTS,
|
||||||
|
getContactsFromIds(contacts));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void contactsSelected(GroupId groupId,
|
||||||
|
Collection<ContactId> contacts) {
|
||||||
|
this.groupId = groupId;
|
||||||
|
this.contacts = contacts;
|
||||||
|
|
||||||
|
BaseMessageFragment messageFragment = getMessageFragment();
|
||||||
|
|
||||||
|
getSupportFragmentManager().beginTransaction()
|
||||||
|
.setCustomAnimations(android.R.anim.fade_in,
|
||||||
|
android.R.anim.fade_out,
|
||||||
|
android.R.anim.slide_in_left,
|
||||||
|
android.R.anim.slide_out_right)
|
||||||
|
.replace(R.id.fragmentContainer, messageFragment,
|
||||||
|
ContactSelectorFragment.TAG)
|
||||||
|
.addToBackStack(null)
|
||||||
|
.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract BaseMessageFragment getMessageFragment();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This must only be called from a DbThread
|
* This must only be called from a DbThread
|
||||||
@@ -50,26 +96,8 @@ public abstract class ShareActivity extends BriarActivity implements
|
|||||||
public abstract boolean isDisabled(GroupId groupId, Contact c)
|
public abstract boolean isDisabled(GroupId groupId, Contact c)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void contactsSelected(GroupId groupId,
|
|
||||||
Collection<ContactId> contacts) {
|
|
||||||
ShareMessageFragment messageFragment =
|
|
||||||
getMessageFragment(groupId, contacts);
|
|
||||||
|
|
||||||
getSupportFragmentManager().beginTransaction()
|
|
||||||
.setCustomAnimations(android.R.anim.fade_in,
|
|
||||||
android.R.anim.fade_out,
|
|
||||||
android.R.anim.slide_in_left,
|
|
||||||
android.R.anim.slide_out_right)
|
|
||||||
.replace(R.id.shareContainer, messageFragment,
|
|
||||||
ContactSelectorFragment.TAG)
|
|
||||||
.addToBackStack(null)
|
|
||||||
.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
static ArrayList<Integer> getContactsFromIds(
|
static ArrayList<Integer> getContactsFromIds(
|
||||||
Collection<ContactId> contacts) {
|
Collection<ContactId> contacts) {
|
||||||
|
|
||||||
// transform ContactIds to Integers so they can be added to a bundle
|
// transform ContactIds to Integers so they can be added to a bundle
|
||||||
ArrayList<Integer> intContacts = new ArrayList<>(contacts.size());
|
ArrayList<Integer> intContacts = new ArrayList<>(contacts.size());
|
||||||
for (ContactId contactId : contacts) {
|
for (ContactId contactId : contacts) {
|
||||||
@@ -78,15 +106,8 @@ public abstract class ShareActivity extends BriarActivity implements
|
|||||||
return intContacts;
|
return intContacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sharingSuccessful(View v) {
|
|
||||||
setResult(RESULT_OK);
|
|
||||||
hideSoftKeyboard(v);
|
|
||||||
supportFinishAfterTransition();
|
|
||||||
}
|
|
||||||
|
|
||||||
static Collection<ContactId> getContactsFromIntegers(
|
static Collection<ContactId> getContactsFromIntegers(
|
||||||
ArrayList<Integer> intContacts) {
|
ArrayList<Integer> intContacts) {
|
||||||
|
|
||||||
// turn contact integers from a bundle back to ContactIds
|
// turn contact integers from a bundle back to ContactIds
|
||||||
List<ContactId> contacts = new ArrayList<>(intContacts.size());
|
List<ContactId> contacts = new ArrayList<>(intContacts.size());
|
||||||
for (Integer c : intContacts) {
|
for (Integer c : intContacts) {
|
||||||
@@ -95,6 +116,50 @@ public abstract class ShareActivity extends BriarActivity implements
|
|||||||
return contacts;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void onButtonClick(String message) {
|
||||||
|
share(message);
|
||||||
|
setResult(RESULT_OK);
|
||||||
|
supportFinishAfterTransition();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void share(final String msg) {
|
||||||
|
runOnDbThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
for (ContactId c : contacts) {
|
||||||
|
share(groupId, c, msg);
|
||||||
|
}
|
||||||
|
} catch (DbException e) {
|
||||||
|
// TODO proper error handling
|
||||||
|
sharingError();
|
||||||
|
if (LOG.isLoggable(WARNING))
|
||||||
|
LOG.log(WARNING, e.toString(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method must be run from the DbThread.
|
||||||
|
*/
|
||||||
|
protected abstract void share(GroupId g, ContactId c, String msg)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
|
private void sharingError() {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int res = getSharingError();
|
||||||
|
Toast.makeText(ShareActivity.this, res, LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract @StringRes int getSharingError();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFragmentCreated(String tag) {
|
public void onFragmentCreated(String tag) {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.android.sharing;
|
package org.briarproject.android.sharing;
|
||||||
|
|
||||||
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.ActivityComponent;
|
import org.briarproject.android.ActivityComponent;
|
||||||
import org.briarproject.api.blogs.BlogSharingManager;
|
import org.briarproject.api.blogs.BlogSharingManager;
|
||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
@@ -7,18 +8,17 @@ 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.Collection;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
public class ShareBlogActivity extends ShareActivity {
|
public class ShareBlogActivity extends ShareActivity {
|
||||||
|
|
||||||
|
// Fields that are accessed from background threads must be volatile
|
||||||
@Inject
|
@Inject
|
||||||
volatile BlogSharingManager blogSharingManager;
|
protected volatile BlogSharingManager blogSharingManager;
|
||||||
|
|
||||||
ShareMessageFragment getMessageFragment(GroupId groupId,
|
@Override
|
||||||
Collection<ContactId> contacts) {
|
BaseMessageFragment getMessageFragment() {
|
||||||
return ShareBlogMessageFragment.newInstance(groupId, contacts);
|
return ShareBlogMessageFragment.newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -26,10 +26,20 @@ public class ShareBlogActivity extends ShareActivity {
|
|||||||
component.inject(this);
|
component.inject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* This must only be called from a DbThread
|
|
||||||
*/
|
|
||||||
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
|
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
|
||||||
return !blogSharingManager.canBeShared(groupId, c);
|
return !blogSharingManager.canBeShared(groupId, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void share(GroupId g, ContactId c, String msg)
|
||||||
|
throws DbException {
|
||||||
|
blogSharingManager.sendInvitation(g, c, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getSharingError() {
|
||||||
|
return R.string.blogs_sharing_error;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,38 +4,16 @@ import android.os.Bundle;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.ActivityComponent;
|
import org.briarproject.android.ActivityComponent;
|
||||||
import org.briarproject.api.blogs.BlogSharingManager;
|
|
||||||
import org.briarproject.api.contact.ContactId;
|
|
||||||
import org.briarproject.api.db.DbException;
|
|
||||||
import org.briarproject.api.sync.GroupId;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
public class ShareBlogMessageFragment extends BaseMessageFragment {
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
|
|
||||||
public class ShareBlogMessageFragment extends ShareMessageFragment {
|
|
||||||
|
|
||||||
public final static String TAG = ShareBlogMessageFragment.class.getName();
|
public final static String TAG = ShareBlogMessageFragment.class.getName();
|
||||||
private static final Logger LOG = Logger.getLogger(TAG);
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
public static ShareBlogMessageFragment newInstance() {
|
||||||
@Inject
|
return new ShareBlogMessageFragment();
|
||||||
protected volatile BlogSharingManager blogSharingManager;
|
|
||||||
|
|
||||||
public static ShareBlogMessageFragment newInstance(GroupId groupId,
|
|
||||||
Collection<ContactId> contacts) {
|
|
||||||
|
|
||||||
ShareBlogMessageFragment fragment = new ShareBlogMessageFragment();
|
|
||||||
fragment.setArguments(getArguments(groupId, contacts));
|
|
||||||
return fragment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -43,10 +21,17 @@ public class ShareBlogMessageFragment extends ShareMessageFragment {
|
|||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
|
|
||||||
setTitle(R.string.blogs_sharing_share);
|
setTitle(R.string.blogs_sharing_share);
|
||||||
|
return super.onCreateView(inflater, container, savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
View v = super.onCreateView(inflater, container, savedInstanceState);
|
@Override
|
||||||
ui.message.setButtonText(getString(R.string.blogs_sharing_button));
|
protected int getButtonText() {
|
||||||
return v;
|
return R.string.blogs_sharing_button;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getHintText() {
|
||||||
|
return R.string.forum_share_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -59,32 +44,4 @@ public class ShareBlogMessageFragment extends ShareMessageFragment {
|
|||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void share(final String msg) {
|
|
||||||
listener.runOnDbThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
for (ContactId c : getContacts()) {
|
|
||||||
blogSharingManager.sendInvitation(getGroupId(), c, msg);
|
|
||||||
}
|
|
||||||
} catch (DbException e) {
|
|
||||||
sharingError();
|
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void sharingError() {
|
|
||||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int res = R.string.blogs_sharing_error;
|
|
||||||
Toast.makeText(getContext(), res, LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.android.sharing;
|
package org.briarproject.android.sharing;
|
||||||
|
|
||||||
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.ActivityComponent;
|
import org.briarproject.android.ActivityComponent;
|
||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
@@ -7,17 +8,17 @@ import org.briarproject.api.db.DbException;
|
|||||||
import org.briarproject.api.forum.ForumSharingManager;
|
import org.briarproject.api.forum.ForumSharingManager;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
public class ShareForumActivity extends ShareActivity {
|
public class ShareForumActivity extends ShareActivity {
|
||||||
@Inject
|
|
||||||
volatile ForumSharingManager forumSharingManager;
|
|
||||||
|
|
||||||
ShareMessageFragment getMessageFragment(GroupId groupId,
|
// Fields that are accessed from background threads must be volatile
|
||||||
Collection<ContactId> contacts) {
|
@Inject
|
||||||
return ShareForumMessageFragment.newInstance(groupId, contacts);
|
protected volatile ForumSharingManager forumSharingManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
BaseMessageFragment getMessageFragment() {
|
||||||
|
return ShareForumMessageFragment.newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -25,10 +26,20 @@ public class ShareForumActivity extends ShareActivity {
|
|||||||
component.inject(this);
|
component.inject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* This must only be called from a DbThread
|
|
||||||
*/
|
|
||||||
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
|
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
|
||||||
return !forumSharingManager.canBeShared(groupId, c);
|
return !forumSharingManager.canBeShared(groupId, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void share(GroupId g, ContactId c, String msg)
|
||||||
|
throws DbException {
|
||||||
|
forumSharingManager.sendInvitation(g, c, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getSharingError() {
|
||||||
|
return R.string.forum_share_error;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,38 +4,16 @@ import android.os.Bundle;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.ActivityComponent;
|
import org.briarproject.android.ActivityComponent;
|
||||||
import org.briarproject.api.contact.ContactId;
|
|
||||||
import org.briarproject.api.db.DbException;
|
|
||||||
import org.briarproject.api.forum.ForumSharingManager;
|
|
||||||
import org.briarproject.api.sync.GroupId;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
public class ShareForumMessageFragment extends BaseMessageFragment {
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static android.widget.Toast.LENGTH_SHORT;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
|
|
||||||
public class ShareForumMessageFragment extends ShareMessageFragment {
|
|
||||||
|
|
||||||
public final static String TAG = ShareForumMessageFragment.class.getName();
|
public final static String TAG = ShareForumMessageFragment.class.getName();
|
||||||
private static final Logger LOG = Logger.getLogger(TAG);
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
public static ShareForumMessageFragment newInstance() {
|
||||||
@Inject
|
return new ShareForumMessageFragment();
|
||||||
protected volatile ForumSharingManager forumSharingManager;
|
|
||||||
|
|
||||||
public static ShareForumMessageFragment newInstance(GroupId groupId,
|
|
||||||
Collection<ContactId> contacts) {
|
|
||||||
|
|
||||||
ShareForumMessageFragment fragment = new ShareForumMessageFragment();
|
|
||||||
fragment.setArguments(getArguments(groupId, contacts));
|
|
||||||
return fragment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -46,6 +24,16 @@ public class ShareForumMessageFragment extends ShareMessageFragment {
|
|||||||
return super.onCreateView(inflater, container, savedInstanceState);
|
return super.onCreateView(inflater, container, savedInstanceState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getButtonText() {
|
||||||
|
return R.string.forum_share_button;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getHintText() {
|
||||||
|
return R.string.forum_share_message;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void injectFragment(ActivityComponent component) {
|
public void injectFragment(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
@@ -56,33 +44,4 @@ public class ShareForumMessageFragment extends ShareMessageFragment {
|
|||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void share(final String msg) {
|
|
||||||
listener.runOnDbThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
for (ContactId c : getContacts()) {
|
|
||||||
forumSharingManager.
|
|
||||||
sendInvitation(getGroupId(), c, msg);
|
|
||||||
}
|
|
||||||
} catch (DbException e) {
|
|
||||||
sharingError();
|
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
LOG.log(WARNING, e.toString(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void sharingError() {
|
|
||||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
int res = R.string.forum_share_error;
|
|
||||||
Toast.makeText(getContext(), res, LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
package org.briarproject.android.sharing;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import org.briarproject.R;
|
|
||||||
import org.briarproject.android.fragment.BaseFragment;
|
|
||||||
import org.briarproject.android.view.LargeTextInputView;
|
|
||||||
import org.briarproject.android.view.TextInputView.TextInputListener;
|
|
||||||
import org.briarproject.api.blogs.BlogSharingManager;
|
|
||||||
import org.briarproject.api.contact.ContactId;
|
|
||||||
import org.briarproject.api.forum.ForumSharingManager;
|
|
||||||
import org.briarproject.api.sync.GroupId;
|
|
||||||
import org.briarproject.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static org.briarproject.android.sharing.ShareActivity.CONTACTS;
|
|
||||||
import static org.briarproject.android.sharing.ShareActivity.getContactsFromIds;
|
|
||||||
import static org.briarproject.api.sharing.SharingConstants.GROUP_ID;
|
|
||||||
import static org.briarproject.api.sharing.SharingConstants.MAX_INVITATION_MESSAGE_LENGTH;
|
|
||||||
|
|
||||||
abstract class ShareMessageFragment extends BaseFragment
|
|
||||||
implements TextInputListener {
|
|
||||||
|
|
||||||
protected ViewHolder ui;
|
|
||||||
private ShareActivity shareActivity;
|
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
|
||||||
@Inject
|
|
||||||
protected volatile ForumSharingManager forumSharingManager;
|
|
||||||
@Inject
|
|
||||||
protected volatile BlogSharingManager blogSharingManager;
|
|
||||||
private volatile GroupId groupId;
|
|
||||||
private volatile Collection<ContactId> contacts;
|
|
||||||
|
|
||||||
protected static Bundle getArguments(GroupId groupId,
|
|
||||||
Collection<ContactId> contacts) {
|
|
||||||
|
|
||||||
Bundle args = new Bundle();
|
|
||||||
args.putByteArray(GROUP_ID, groupId.getBytes());
|
|
||||||
args.putIntegerArrayList(CONTACTS, getContactsFromIds(contacts));
|
|
||||||
return args;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAttach(Context context) {
|
|
||||||
super.onAttach(context);
|
|
||||||
shareActivity = (ShareActivity) context;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
||||||
Bundle savedInstanceState) {
|
|
||||||
|
|
||||||
// allow for "up" button to act as back button
|
|
||||||
setHasOptionsMenu(true);
|
|
||||||
|
|
||||||
// get groupID and contactIDs from fragment arguments
|
|
||||||
groupId = new GroupId(getArguments().getByteArray(GROUP_ID));
|
|
||||||
ArrayList<Integer> intContacts =
|
|
||||||
getArguments().getIntegerArrayList(CONTACTS);
|
|
||||||
if (intContacts == null) throw new IllegalArgumentException();
|
|
||||||
contacts = ShareActivity.getContactsFromIntegers(intContacts);
|
|
||||||
|
|
||||||
// inflate view
|
|
||||||
View v = inflater.inflate(R.layout.fragment_share_message, container,
|
|
||||||
false);
|
|
||||||
ui = new ViewHolder(v);
|
|
||||||
ui.message.setListener(this);
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
ui.message.showSoftKeyboard();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(final MenuItem item) {
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case android.R.id.home:
|
|
||||||
shareActivity.onBackPressed();
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setTitle(int res) {
|
|
||||||
shareActivity.setTitle(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSendClick(String msg) {
|
|
||||||
// disable button to prevent accidental double invitations
|
|
||||||
ui.message.setSendButtonEnabled(false);
|
|
||||||
|
|
||||||
msg = StringUtils.truncateUtf8(msg, MAX_INVITATION_MESSAGE_LENGTH);
|
|
||||||
share(msg);
|
|
||||||
|
|
||||||
// don't wait for the invitation to be made before finishing activity
|
|
||||||
shareActivity.sharingSuccessful(ui.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract void share(final String msg);
|
|
||||||
|
|
||||||
abstract void sharingError();
|
|
||||||
|
|
||||||
protected Collection<ContactId> getContacts() {
|
|
||||||
return contacts;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected GroupId getGroupId() {
|
|
||||||
return groupId;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class ViewHolder {
|
|
||||||
protected final LargeTextInputView message;
|
|
||||||
|
|
||||||
private ViewHolder(View v) {
|
|
||||||
message = (LargeTextInputView) v
|
|
||||||
.findViewById(R.id.invitationMessageView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user