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		Log.d(Config.LOGTAG, "action: "+intent.getAction()+ ", type:"+type);
185		share.uuid = intent.getStringExtra("uuid");
186		if (Intent.ACTION_SEND.equals(intent.getAction())) {
187			final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
188			if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) {
189				this.share.uris.clear();
190				this.share.uris.add(uri);
191				this.share.image = type.startsWith("image/") || isImage(uri);
192			} else {
193				this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
194			}
195		} else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
196			this.share.image = type != null && type.startsWith("image/");
197			if (!this.share.image) {
198				return;
199			}
200
201			this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
202		}
203		if (xmppConnectionServiceBound) {
204			if (share.uuid != null) {
205				share();
206			} else {
207				xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
208			}
209		}
210
211	}
212
213	protected boolean isImage(Uri uri) {
214		try {
215			String guess = URLConnection.guessContentTypeFromName(uri.toString());
216			return (guess != null && guess.startsWith("image/"));
217		} catch (final StringIndexOutOfBoundsException ignored) {
218			return false;
219		}
220	}
221
222	@Override
223	void onBackendConnected() {
224		if (xmppConnectionServiceBound && share != null
225				&& ((share.contact != null && share.account != null) || share.uuid != null)) {
226			share();
227			return;
228		}
229		refreshUiReal();
230	}
231
232	private void share() {
233		final Conversation conversation;
234		if (share.uuid != null) {
235			conversation = xmppConnectionService.findConversationByUuid(share.uuid);
236			if (conversation == null) {
237				return;
238			}
239		}else{
240			Account account;
241			try {
242				account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
243			} catch (final InvalidJidException e) {
244				account = null;
245			}
246			if (account == null) {
247				return;
248			}
249
250			try {
251				conversation = xmppConnectionService
252						.findOrCreateConversation(account, Jid.fromString(share.contact), false);
253			} catch (final InvalidJidException e) {
254				return;
255			}
256		}
257		share(conversation);
258	}
259
260	private void share(final Conversation conversation) {
261		mListView.setEnabled(false);
262		if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) {
263			if (share.uuid == null) {
264				showInstallPgpDialog();
265			} else {
266				Toast.makeText(this,R.string.openkeychain_not_installed,Toast.LENGTH_SHORT).show();
267				finish();
268			}
269			return;
270		}
271		if (share.uris.size() != 0) {
272			OnPresenceSelected callback = new OnPresenceSelected() {
273				@Override
274				public void onPresenceSelected() {
275					attachmentCounter.set(share.uris.size());
276					if (share.image) {
277						share.multiple = share.uris.size() > 1;
278						replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image));
279						for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
280							ShareWithActivity.this.xmppConnectionService
281									.attachImageToConversation(conversation, i.next(),
282											attachFileCallback);
283						}
284					} else {
285						replaceToast(getString(R.string.preparing_file));
286						ShareWithActivity.this.xmppConnectionService
287								.attachFileToConversation(conversation, share.uris.get(0),
288										attachFileCallback);
289					}
290				}
291			};
292			if (conversation.getAccount().httpUploadAvailable()) {
293				callback.onPresenceSelected();
294			} else {
295				selectPresence(conversation, callback);
296			}
297		} else {
298			switchToConversation(conversation, this.share.text, true);
299		}
300
301	}
302
303	public void refreshUiReal() {
304		xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0);
305		mAdapter.notifyDataSetChanged();
306	}
307
308	@Override
309	public void onBackPressed() {
310		if (attachmentCounter.get() >= 1) {
311			replaceToast(getString(R.string.sharing_files_please_wait));
312		} else {
313			super.onBackPressed();
314		}
315	}
316}