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.Iterator;
 19import java.util.List;
 20import java.util.concurrent.atomic.AtomicInteger;
 21
 22import eu.siacs.conversations.Config;
 23import eu.siacs.conversations.R;
 24import eu.siacs.conversations.entities.Account;
 25import eu.siacs.conversations.entities.Conversation;
 26import eu.siacs.conversations.entities.Message;
 27import eu.siacs.conversations.services.XmppConnectionService;
 28import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 29import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 30import eu.siacs.conversations.xmpp.jid.Jid;
 31
 32public class ShareWithActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {
 33
 34	@Override
 35	public void onConversationUpdate() {
 36		refreshUi();
 37	}
 38
 39	private class Share {
 40		public List<Uri> uris = new ArrayList<>();
 41		public boolean image;
 42		public String account;
 43		public String contact;
 44		public String text;
 45		public String uuid;
 46		public boolean multiple = false;
 47	}
 48
 49	private Share share;
 50
 51	private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
 52	private ListView mListView;
 53	private ConversationAdapter mAdapter;
 54	private List<Conversation> mConversations = new ArrayList<>();
 55	private Toast mToast;
 56	private AtomicInteger attachmentCounter = new AtomicInteger(0);
 57
 58	private UiCallback<Message> attachFileCallback = new UiCallback<Message>() {
 59
 60		@Override
 61		public void userInputRequried(PendingIntent pi, Message object) {
 62			// TODO Auto-generated method stub
 63
 64		}
 65
 66		@Override
 67		public void success(final Message message) {
 68			xmppConnectionService.sendMessage(message);
 69			runOnUiThread(new Runnable() {
 70				@Override
 71				public void run() {
 72					if (attachmentCounter.decrementAndGet() <=0 ) {
 73						int resId;
 74						if (share.image && share.multiple) {
 75							resId = R.string.shared_images_with_x;
 76						} else if (share.image) {
 77							resId = R.string.shared_image_with_x;
 78						} else {
 79							resId = R.string.shared_file_with_x;
 80						}
 81						replaceToast(getString(resId, message.getConversation().getName()));
 82						if (share.uuid != null) {
 83							finish();
 84						} else {
 85							switchToConversation(message.getConversation());
 86						}
 87					}
 88				}
 89			});
 90		}
 91
 92		@Override
 93		public void error(final int errorCode, Message object) {
 94			runOnUiThread(new Runnable() {
 95				@Override
 96				public void run() {
 97					replaceToast(getString(errorCode));
 98					if (attachmentCounter.decrementAndGet() <=0 ) {
 99						finish();
100					}
101				}
102			});
103		}
104	};
105
106	protected void hideToast() {
107		if (mToast != null) {
108			mToast.cancel();
109		}
110	}
111
112	protected void replaceToast(String msg) {
113		hideToast();
114		mToast = Toast.makeText(this, msg ,Toast.LENGTH_LONG);
115		mToast.show();
116	}
117
118	protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
119		super.onActivityResult(requestCode, resultCode, data);
120		if (requestCode == REQUEST_START_NEW_CONVERSATION
121				&& resultCode == RESULT_OK) {
122			share.contact = data.getStringExtra("contact");
123			share.account = data.getStringExtra(EXTRA_ACCOUNT);
124		}
125		if (xmppConnectionServiceBound
126				&& share != null
127				&& share.contact != null
128				&& share.account != null) {
129			share();
130		}
131	}
132
133	@Override
134	protected void onCreate(Bundle savedInstanceState) {
135		super.onCreate(savedInstanceState);
136
137		if (getActionBar() != null) {
138			getActionBar().setDisplayHomeAsUpEnabled(false);
139			getActionBar().setHomeButtonEnabled(false);
140		}
141
142		setContentView(R.layout.share_with);
143		setTitle(getString(R.string.title_activity_sharewith));
144
145		mListView = (ListView) findViewById(R.id.choose_conversation_list);
146		mAdapter = new ConversationAdapter(this, this.mConversations);
147		mListView.setAdapter(mAdapter);
148		mListView.setOnItemClickListener(new OnItemClickListener() {
149
150			@Override
151			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
152				share(mConversations.get(position));
153			}
154		});
155
156		this.share = new Share();
157	}
158
159	@Override
160	public boolean onCreateOptionsMenu(Menu menu) {
161		getMenuInflater().inflate(R.menu.share_with, menu);
162		return true;
163	}
164
165	@Override
166	public boolean onOptionsItemSelected(final MenuItem item) {
167		switch (item.getItemId()) {
168			case R.id.action_add:
169				final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
170				startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
171				return true;
172		}
173		return super.onOptionsItemSelected(item);
174	}
175
176	@Override
177	public void onStart() {
178		super.onStart();
179		Intent intent = getIntent();
180		if (intent == null) {
181			return;
182		}
183		final String type = intent.getType();
184		final String action = intent.getAction();
185		Log.d(Config.LOGTAG, "action: "+action+ ", type:"+type);
186		share.uuid = intent.getStringExtra("uuid");
187		if (Intent.ACTION_SEND.equals(action)) {
188			final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
189			final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
190			if (type != null && uri != null && text == null) {
191				this.share.uris.clear();
192				this.share.uris.add(uri);
193				this.share.image = type.startsWith("image/") || isImage(uri);
194			} else {
195				this.share.text = text;
196			}
197		} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
198			this.share.image = type != null && type.startsWith("image/");
199			if (!this.share.image) {
200				return;
201			}
202			this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
203		}
204		if (xmppConnectionServiceBound) {
205			if (share.uuid != null) {
206				share();
207			} else {
208				xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
209			}
210		}
211
212	}
213
214	protected boolean isImage(Uri uri) {
215		try {
216			String guess = URLConnection.guessContentTypeFromName(uri.toString());
217			return (guess != null && guess.startsWith("image/"));
218		} catch (final StringIndexOutOfBoundsException ignored) {
219			return false;
220		}
221	}
222
223	@Override
224	void onBackendConnected() {
225		if (xmppConnectionServiceBound && share != null
226				&& ((share.contact != null && share.account != null) || share.uuid != null)) {
227			share();
228			return;
229		}
230		refreshUiReal();
231	}
232
233	private void share() {
234		final Conversation conversation;
235		if (share.uuid != null) {
236			conversation = xmppConnectionService.findConversationByUuid(share.uuid);
237			if (conversation == null) {
238				return;
239			}
240		}else{
241			Account account;
242			try {
243				account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
244			} catch (final InvalidJidException e) {
245				account = null;
246			}
247			if (account == null) {
248				return;
249			}
250
251			try {
252				conversation = xmppConnectionService
253						.findOrCreateConversation(account, Jid.fromString(share.contact), false);
254			} catch (final InvalidJidException e) {
255				return;
256			}
257		}
258		share(conversation);
259	}
260
261	private void share(final Conversation conversation) {
262		mListView.setEnabled(false);
263		if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) {
264			if (share.uuid == null) {
265				showInstallPgpDialog();
266			} else {
267				Toast.makeText(this,R.string.openkeychain_not_installed,Toast.LENGTH_SHORT).show();
268				finish();
269			}
270			return;
271		}
272		if (share.uris.size() != 0) {
273			OnPresenceSelected callback = new OnPresenceSelected() {
274				@Override
275				public void onPresenceSelected() {
276					attachmentCounter.set(share.uris.size());
277					if (share.image) {
278						share.multiple = share.uris.size() > 1;
279						replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image));
280						for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
281							ShareWithActivity.this.xmppConnectionService
282									.attachImageToConversation(conversation, i.next(),
283											attachFileCallback);
284						}
285					} else {
286						replaceToast(getString(R.string.preparing_file));
287						ShareWithActivity.this.xmppConnectionService
288								.attachFileToConversation(conversation, share.uris.get(0),
289										attachFileCallback);
290					}
291				}
292			};
293			if (conversation.getAccount().httpUploadAvailable()) {
294				callback.onPresenceSelected();
295			} else {
296				selectPresence(conversation, callback);
297			}
298		} else {
299			switchToConversation(conversation, this.share.text, true);
300		}
301
302	}
303
304	public void refreshUiReal() {
305		xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0);
306		mAdapter.notifyDataSetChanged();
307	}
308
309	@Override
310	public void onBackPressed() {
311		if (attachmentCounter.get() >= 1) {
312			replaceToast(getString(R.string.sharing_files_please_wait));
313		} else {
314			super.onBackPressed();
315		}
316	}
317}