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