ShareWithActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.PendingIntent;
  4import android.content.Intent;
  5import android.net.Uri;
  6import android.os.Bundle;
  7import android.util.Log;
  8import android.view.Menu;
  9import android.view.MenuItem;
 10import android.view.View;
 11import android.widget.AdapterView;
 12import android.widget.AdapterView.OnItemClickListener;
 13import android.widget.ListView;
 14import android.widget.Toast;
 15
 16import java.net.URLConnection;
 17import java.util.ArrayList;
 18import java.util.List;
 19
 20import eu.siacs.conversations.Config;
 21import eu.siacs.conversations.R;
 22import eu.siacs.conversations.entities.Account;
 23import eu.siacs.conversations.entities.Conversation;
 24import eu.siacs.conversations.entities.Message;
 25import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 26import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 27import eu.siacs.conversations.xmpp.jid.Jid;
 28
 29public class ShareWithActivity extends XmppActivity {
 30
 31	private class Share {
 32		public Uri uri;
 33		public boolean image;
 34		public String account;
 35		public String contact;
 36		public String text;
 37	}
 38
 39	private Share share;
 40
 41	private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
 42	private ListView mListView;
 43	private List<Conversation> mConversations = new ArrayList<>();
 44
 45	private UiCallback<Message> attachFileCallback = new UiCallback<Message>() {
 46
 47		@Override
 48		public void userInputRequried(PendingIntent pi, Message object) {
 49			// TODO Auto-generated method stub
 50
 51		}
 52
 53		@Override
 54		public void success(Message message) {
 55			xmppConnectionService.sendMessage(message);
 56		}
 57
 58		@Override
 59		public void error(int errorCode, Message object) {
 60			// TODO Auto-generated method stub
 61
 62		}
 63	};
 64
 65	protected void onActivityResult(int requestCode, int resultCode,
 66			final Intent data) {
 67		super.onActivityResult(requestCode, resultCode, data);
 68		if (requestCode == REQUEST_START_NEW_CONVERSATION
 69				&& resultCode == RESULT_OK) {
 70			share.contact = data.getStringExtra("contact");
 71			share.account = data.getStringExtra("account");
 72			Log.d(Config.LOGTAG, "contact: " + share.contact + " account:"
 73					+ share.account);
 74		}
 75		if (xmppConnectionServiceBound && share != null
 76				&& share.contact != null && share.account != null) {
 77			share();
 78		}
 79	}
 80
 81	@Override
 82	protected void onCreate(Bundle savedInstanceState) {
 83		super.onCreate(savedInstanceState);
 84
 85		if (getActionBar() != null) {
 86			getActionBar().setDisplayHomeAsUpEnabled(false);
 87			getActionBar().setHomeButtonEnabled(false);
 88		}
 89
 90		setContentView(R.layout.share_with);
 91		setTitle(getString(R.string.title_activity_sharewith));
 92
 93		mListView = (ListView) findViewById(R.id.choose_conversation_list);
 94		ConversationAdapter mAdapter = new ConversationAdapter(this,
 95				this.mConversations);
 96		mListView.setAdapter(mAdapter);
 97		mListView.setOnItemClickListener(new OnItemClickListener() {
 98
 99			@Override
100			public void onItemClick(AdapterView<?> arg0, View arg1,
101					int position, long arg3) {
102				Conversation conversation = mConversations.get(position);
103				if (conversation.getMode() == Conversation.MODE_SINGLE
104						|| share.uri == null) {
105					share(mConversations.get(position));
106				}
107			}
108		});
109
110		this.share = new Share();
111	}
112
113	@Override
114	public boolean onCreateOptionsMenu(Menu menu) {
115		getMenuInflater().inflate(R.menu.share_with, menu);
116		return true;
117	}
118
119	@Override
120	public boolean onOptionsItemSelected(final MenuItem item) {
121		switch (item.getItemId()) {
122		case R.id.action_add:
123			final Intent intent = new Intent(getApplicationContext(),
124					ChooseContactActivity.class);
125			startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
126			return true;
127		}
128		return super.onOptionsItemSelected(item);
129	}
130
131	@Override
132	public void onStart() {
133		final String type = getIntent().getType();
134		if (type != null && !type.startsWith("text/")) {
135			this.share.uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
136			this.share.image = type.startsWith("image/") || URLConnection.guessContentTypeFromName(share.uri.getPath()).startsWith("image/");
137		} else {
138			this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
139		}
140		if (xmppConnectionServiceBound) {
141			xmppConnectionService.populateWithOrderedConversations(
142					mConversations, this.share.uri == null);
143		}
144		super.onStart();
145	}
146
147	@Override
148	void onBackendConnected() {
149		if (xmppConnectionServiceBound && share != null
150				&& share.contact != null && share.account != null) {
151			share();
152			return;
153		}
154		xmppConnectionService.populateWithOrderedConversations(mConversations,
155				this.share != null && this.share.uri == null);
156	}
157
158	private void share() {
159        Account account;
160        try {
161            account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
162        } catch (final InvalidJidException e) {
163            account = null;
164        }
165        if (account == null) {
166			return;
167		}
168        final Conversation conversation;
169        try {
170            conversation = xmppConnectionService
171                    .findOrCreateConversation(account, Jid.fromString(share.contact), false);
172        } catch (final InvalidJidException e) {
173            return;
174        }
175        share(conversation);
176	}
177
178	private void share(final Conversation conversation) {
179		if (share.uri != null) {
180			selectPresence(conversation, new OnPresenceSelected() {
181				@Override
182				public void onPresenceSelected() {
183					if (share.image) {
184						Toast.makeText(getApplicationContext(),
185								getText(R.string.preparing_image),
186								Toast.LENGTH_LONG).show();
187						ShareWithActivity.this.xmppConnectionService
188							.attachImageToConversation(conversation, share.uri,
189									attachFileCallback);
190					} else {
191						Toast.makeText(getApplicationContext(),
192								getText(R.string.preparing_file),
193								Toast.LENGTH_LONG).show();
194						ShareWithActivity.this.xmppConnectionService
195							.attachFileToConversation(conversation, share.uri,
196									attachFileCallback);
197					}
198					switchToConversation(conversation, null, true);
199					finish();
200				}
201			});
202
203		} else {
204			switchToConversation(conversation, this.share.text, true);
205			finish();
206		}
207
208	}
209
210}