Skip to content
Snippets Groups Projects
ShareMessageFragment.java 3.71 KiB
Newer Older
Torsten Grote's avatar
Torsten Grote committed
package org.briarproject.android.sharing;
Torsten Grote's avatar
Torsten Grote committed

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;
Torsten Grote's avatar
Torsten Grote committed
import org.briarproject.api.blogs.BlogSharingManager;
Torsten Grote's avatar
Torsten Grote committed
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.forum.ForumSharingManager;
import org.briarproject.api.sync.GroupId;
import org.briarproject.util.StringUtils;
Torsten Grote's avatar
Torsten Grote committed

import java.util.ArrayList;
import java.util.Collection;

import javax.inject.Inject;

Torsten Grote's avatar
Torsten Grote committed
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;
Torsten Grote's avatar
Torsten Grote committed
	private ShareActivity shareActivity;
Torsten Grote's avatar
Torsten Grote committed

	// Fields that are accessed from background threads must be volatile
akwizgran's avatar
akwizgran committed
	@Inject
	protected volatile ForumSharingManager forumSharingManager;
Torsten Grote's avatar
Torsten Grote committed
	@Inject
	protected volatile BlogSharingManager blogSharingManager;
Torsten Grote's avatar
Torsten Grote committed
	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));
Torsten Grote's avatar
Torsten Grote committed
	}

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		shareActivity = (ShareActivity) context;
Torsten Grote's avatar
Torsten Grote committed
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// allow for "up" button to act as back button
Torsten Grote's avatar
Torsten Grote committed
		setHasOptionsMenu(true);

		// get groupID and contactIDs from fragment arguments
Torsten Grote's avatar
Torsten Grote committed
		groupId = new GroupId(getArguments().getByteArray(GROUP_ID));
		ArrayList<Integer> intContacts =
				getArguments().getIntegerArrayList(CONTACTS);
		if (intContacts == null) throw new IllegalArgumentException();
		contacts = ShareActivity.getContactsFromIntegers(intContacts);

Torsten Grote's avatar
Torsten Grote committed
		// inflate view
Torsten Grote's avatar
Torsten Grote committed
		View v = inflater.inflate(R.layout.fragment_share_message, container,
akwizgran's avatar
akwizgran committed
				false);
Torsten Grote's avatar
Torsten Grote committed
		ui = new ViewHolder(v);
		ui.message.setListener(this);
	@Override
	public void onStart() {
		super.onStart();
		ui.message.showSoftKeyboard();
	}

Torsten Grote's avatar
Torsten Grote committed
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
Torsten Grote's avatar
Torsten Grote committed
				shareActivity.onBackPressed();
Torsten Grote's avatar
Torsten Grote committed
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	protected void setTitle(int res) {
		shareActivity.setTitle(res);
	@Override
	public void onSendClick(String msg) {
Torsten Grote's avatar
Torsten Grote committed
		// disable button to prevent accidental double invitations
		ui.message.setSendButtonEnabled(false);
		msg = StringUtils.truncateUtf8(msg, MAX_INVITATION_MESSAGE_LENGTH);
		// don't wait for the invitation to be made before finishing activity
Torsten Grote's avatar
Torsten Grote committed
		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);