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