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