ShareWithActivity.java

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