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 String account;
 34		public String contact;
 35		public String text;
 36	}
 37
 38	private Share share;
 39
 40	private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
 41	private ListView mListView;
 42	private List<Conversation> mConversations = new ArrayList<>();
 43
 44	private UiCallback<Message> attachFileCallback = new UiCallback<Message>() {
 45
 46		@Override
 47		public void userInputRequried(PendingIntent pi, Message object) {
 48			// TODO Auto-generated method stub
 49
 50		}
 51
 52		@Override
 53		public void success(Message message) {
 54			xmppConnectionService.sendMessage(message);
 55		}
 56
 57		@Override
 58		public void error(int errorCode, Message object) {
 59			// TODO Auto-generated method stub
 60
 61		}
 62	};
 63
 64	protected void onActivityResult(int requestCode, int resultCode,
 65			final Intent data) {
 66		super.onActivityResult(requestCode, resultCode, data);
 67		if (requestCode == REQUEST_START_NEW_CONVERSATION
 68				&& resultCode == RESULT_OK) {
 69			share.contact = data.getStringExtra("contact");
 70			share.account = data.getStringExtra("account");
 71			Log.d(Config.LOGTAG, "contact: " + share.contact + " account:"
 72					+ share.account);
 73		}
 74		if (xmppConnectionServiceBound && share != null
 75				&& share.contact != null && share.account != null) {
 76			share();
 77		}
 78	}
 79
 80	@Override
 81	protected void onCreate(Bundle savedInstanceState) {
 82		super.onCreate(savedInstanceState);
 83
 84		if (getActionBar() != null) {
 85			getActionBar().setDisplayHomeAsUpEnabled(false);
 86			getActionBar().setHomeButtonEnabled(false);
 87		}
 88
 89		setContentView(R.layout.share_with);
 90		setTitle(getString(R.string.title_activity_sharewith));
 91
 92		mListView = (ListView) findViewById(R.id.choose_conversation_list);
 93		ConversationAdapter mAdapter = new ConversationAdapter(this,
 94				this.mConversations);
 95		mListView.setAdapter(mAdapter);
 96		mListView.setOnItemClickListener(new OnItemClickListener() {
 97
 98			@Override
 99			public void onItemClick(AdapterView<?> arg0, View arg1,
100					int position, long arg3) {
101				Conversation conversation = mConversations.get(position);
102				if (conversation.getMode() == Conversation.MODE_SINGLE
103						|| share.uri == null) {
104					share(mConversations.get(position));
105				}
106			}
107		});
108
109		this.share = new Share();
110	}
111
112	@Override
113	public boolean onCreateOptionsMenu(Menu menu) {
114		getMenuInflater().inflate(R.menu.share_with, menu);
115		return true;
116	}
117
118	@Override
119	public boolean onOptionsItemSelected(MenuItem item) {
120		switch (item.getItemId()) {
121		case R.id.action_add:
122			Intent intent = new Intent(getApplicationContext(),
123					ChooseContactActivity.class);
124			startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
125			return true;
126		}
127		return super.onOptionsItemSelected(item);
128	}
129
130	@Override
131	public void onStart() {
132		if (getIntent().getType() != null
133				&& !getIntent().getType().startsWith("text/")) {
134			this.share.uri = (Uri) getIntent().getParcelableExtra(
135					Intent.EXTRA_STREAM);
136		} else {
137			this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
138		}
139		if (xmppConnectionServiceBound) {
140			xmppConnectionService.populateWithOrderedConversations(
141					mConversations, this.share.uri == null);
142		}
143		super.onStart();
144	}
145
146	@Override
147	void onBackendConnected() {
148		if (xmppConnectionServiceBound && share != null
149				&& share.contact != null && share.account != null) {
150			share();
151			return;
152		}
153		xmppConnectionService.populateWithOrderedConversations(mConversations,
154				this.share != null && this.share.uri == null);
155	}
156
157	private void share() {
158        Account account;
159        try {
160            account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
161        } catch (final InvalidJidException e) {
162            account = null;
163        }
164        if (account == null) {
165			return;
166		}
167        final Conversation conversation;
168        try {
169            conversation = xmppConnectionService
170                    .findOrCreateConversation(account, Jid.fromString(share.contact), false);
171        } catch (final InvalidJidException e) {
172            return;
173        }
174        share(conversation);
175	}
176
177	private void share(final Conversation conversation) {
178		if (share.uri != null) {
179			selectPresence(conversation, new OnPresenceSelected() {
180				@Override
181				public void onPresenceSelected() {
182					final String type = URLConnection.guessContentTypeFromName(share.uri.getPath());
183					if (type != null && type.startsWith("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}