1package eu.siacs.conversations.ui;
2
3import java.io.FileNotFoundException;
4import java.lang.ref.WeakReference;
5import java.util.concurrent.RejectedExecutionException;
6
7import eu.siacs.conversations.Config;
8import eu.siacs.conversations.R;
9import eu.siacs.conversations.entities.Account;
10import eu.siacs.conversations.entities.Contact;
11import eu.siacs.conversations.entities.Conversation;
12import eu.siacs.conversations.entities.Message;
13import eu.siacs.conversations.entities.Presences;
14import eu.siacs.conversations.services.XmppConnectionService;
15import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
16import eu.siacs.conversations.utils.ExceptionHelper;
17import android.app.Activity;
18import android.app.AlertDialog;
19import android.app.PendingIntent;
20import android.app.AlertDialog.Builder;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
25import android.content.IntentSender.SendIntentException;
26import android.content.res.Resources;
27import android.content.Intent;
28import android.content.ServiceConnection;
29import android.graphics.Bitmap;
30import android.graphics.drawable.BitmapDrawable;
31import android.graphics.drawable.Drawable;
32import android.net.Uri;
33import android.os.AsyncTask;
34import android.os.Bundle;
35import android.os.IBinder;
36import android.text.InputType;
37import android.util.DisplayMetrics;
38import android.util.Log;
39import android.view.MenuItem;
40import android.view.View;
41import android.view.inputmethod.InputMethodManager;
42import android.widget.EditText;
43import android.widget.ImageView;
44
45public abstract class XmppActivity extends Activity {
46
47 protected static final int REQUEST_ANNOUNCE_PGP = 0x0101;
48 protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102;
49
50 public XmppConnectionService xmppConnectionService;
51 public boolean xmppConnectionServiceBound = false;
52 protected boolean handledViewIntent = false;
53
54 protected int mPrimaryTextColor;
55 protected int mSecondaryTextColor;
56 protected int mWarningTextColor;
57 protected int mPrimaryColor;
58
59 private DisplayMetrics metrics;
60
61 protected interface OnValueEdited {
62 public void onValueEdited(String value);
63 }
64
65 public interface OnPresenceSelected {
66 public void onPresenceSelected();
67 }
68
69 protected ServiceConnection mConnection = new ServiceConnection() {
70
71 @Override
72 public void onServiceConnected(ComponentName className, IBinder service) {
73 XmppConnectionBinder binder = (XmppConnectionBinder) service;
74 xmppConnectionService = binder.getService();
75 xmppConnectionServiceBound = true;
76 onBackendConnected();
77 }
78
79 @Override
80 public void onServiceDisconnected(ComponentName arg0) {
81 xmppConnectionServiceBound = false;
82 }
83 };
84
85 @Override
86 protected void onStart() {
87 super.onStart();
88 if (!xmppConnectionServiceBound) {
89 connectToBackend();
90 }
91 }
92
93 public void connectToBackend() {
94 Intent intent = new Intent(this, XmppConnectionService.class);
95 intent.setAction("ui");
96 startService(intent);
97 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
98 }
99
100 @Override
101 protected void onStop() {
102 super.onStop();
103 if (xmppConnectionServiceBound) {
104 unbindService(mConnection);
105 xmppConnectionServiceBound = false;
106 }
107 }
108
109 protected void hideKeyboard() {
110 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
111
112 View focus = getCurrentFocus();
113
114 if (focus != null) {
115
116 inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
117 InputMethodManager.HIDE_NOT_ALWAYS);
118 }
119 }
120
121 public boolean hasPgp() {
122 return xmppConnectionService.getPgpEngine() != null;
123 }
124
125 public void showInstallPgpDialog() {
126 Builder builder = new AlertDialog.Builder(this);
127 builder.setTitle(getString(R.string.openkeychain_required));
128 builder.setIconAttribute(android.R.attr.alertDialogIcon);
129 builder.setMessage(getText(R.string.openkeychain_required_long));
130 builder.setNegativeButton(getString(R.string.cancel), null);
131 builder.setNeutralButton(getString(R.string.restart),
132 new OnClickListener() {
133
134 @Override
135 public void onClick(DialogInterface dialog, int which) {
136 if (xmppConnectionServiceBound) {
137 unbindService(mConnection);
138 xmppConnectionServiceBound = false;
139 }
140 stopService(new Intent(XmppActivity.this,
141 XmppConnectionService.class));
142 finish();
143 }
144 });
145 builder.setPositiveButton(getString(R.string.install),
146 new OnClickListener() {
147
148 @Override
149 public void onClick(DialogInterface dialog, int which) {
150 Uri uri = Uri
151 .parse("market://details?id=org.sufficientlysecure.keychain");
152 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
153 startActivity(intent);
154 finish();
155 }
156 });
157 builder.create().show();
158 }
159
160 abstract void onBackendConnected();
161
162 public boolean onOptionsItemSelected(MenuItem item) {
163 switch (item.getItemId()) {
164 case R.id.action_settings:
165 startActivity(new Intent(this, SettingsActivity.class));
166 break;
167 case R.id.action_accounts:
168 startActivity(new Intent(this, ManageAccountActivity.class));
169 break;
170 case android.R.id.home:
171 finish();
172 break;
173 }
174 return super.onOptionsItemSelected(item);
175 }
176
177 @Override
178 protected void onCreate(Bundle savedInstanceState) {
179 super.onCreate(savedInstanceState);
180 metrics = getResources().getDisplayMetrics();
181 ExceptionHelper.init(getApplicationContext());
182 mPrimaryTextColor = getResources().getColor(R.color.primarytext);
183 mSecondaryTextColor = getResources().getColor(R.color.secondarytext);
184 mWarningTextColor = getResources().getColor(R.color.warningtext);
185 mPrimaryColor = getResources().getColor(R.color.primary);
186 }
187
188 public void switchToConversation(Conversation conversation) {
189 switchToConversation(conversation, null, false);
190 }
191
192 public void switchToConversation(Conversation conversation, String text,
193 boolean newTask) {
194 Intent viewConversationIntent = new Intent(this,
195 ConversationActivity.class);
196 viewConversationIntent.setAction(Intent.ACTION_VIEW);
197 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
198 conversation.getUuid());
199 if (text != null) {
200 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
201 }
202 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
203 if (newTask) {
204 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
205 | Intent.FLAG_ACTIVITY_NEW_TASK
206 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
207 } else {
208 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
209 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
210 }
211 startActivity(viewConversationIntent);
212 }
213
214 public void switchToContactDetails(Contact contact) {
215 Intent intent = new Intent(this, ContactDetailsActivity.class);
216 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
217 intent.putExtra("account", contact.getAccount().getJid());
218 intent.putExtra("contact", contact.getJid());
219 startActivity(intent);
220 }
221
222 protected void inviteToConversation(Conversation conversation) {
223 Intent intent = new Intent(getApplicationContext(),
224 ChooseContactActivity.class);
225 intent.putExtra("conversation", conversation.getUuid());
226 startActivityForResult(intent, REQUEST_INVITE_TO_CONVERSATION);
227 }
228
229 protected void announcePgp(Account account, final Conversation conversation) {
230 xmppConnectionService.getPgpEngine().generateSignature(account,
231 "online", new UiCallback<Account>() {
232
233 @Override
234 public void userInputRequried(PendingIntent pi,
235 Account account) {
236 try {
237 startIntentSenderForResult(pi.getIntentSender(),
238 REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
239 } catch (SendIntentException e) {
240 }
241 }
242
243 @Override
244 public void success(Account account) {
245 xmppConnectionService.databaseBackend
246 .updateAccount(account);
247 xmppConnectionService.sendPresencePacket(account,
248 xmppConnectionService.getPresenceGenerator()
249 .sendPresence(account));
250 if (conversation != null) {
251 conversation
252 .setNextEncryption(Message.ENCRYPTION_PGP);
253 }
254 }
255
256 @Override
257 public void error(int error, Account account) {
258 displayErrorDialog(error);
259 }
260 });
261 }
262
263 protected void displayErrorDialog(final int errorCode) {
264 runOnUiThread(new Runnable() {
265
266 @Override
267 public void run() {
268 AlertDialog.Builder builder = new AlertDialog.Builder(
269 XmppActivity.this);
270 builder.setIconAttribute(android.R.attr.alertDialogIcon);
271 builder.setTitle(getString(R.string.error));
272 builder.setMessage(errorCode);
273 builder.setNeutralButton(R.string.accept, null);
274 builder.create().show();
275 }
276 });
277
278 }
279
280 protected void showAddToRosterDialog(final Conversation conversation) {
281 String jid = conversation.getContactJid();
282 AlertDialog.Builder builder = new AlertDialog.Builder(this);
283 builder.setTitle(jid);
284 builder.setMessage(getString(R.string.not_in_roster));
285 builder.setNegativeButton(getString(R.string.cancel), null);
286 builder.setPositiveButton(getString(R.string.add_contact),
287 new DialogInterface.OnClickListener() {
288
289 @Override
290 public void onClick(DialogInterface dialog, int which) {
291 String jid = conversation.getContactJid();
292 Account account = conversation.getAccount();
293 Contact contact = account.getRoster().getContact(jid);
294 xmppConnectionService.createContact(contact);
295 switchToContactDetails(contact);
296 }
297 });
298 builder.create().show();
299 }
300
301 private void showAskForPresenceDialog(final Contact contact) {
302 AlertDialog.Builder builder = new AlertDialog.Builder(this);
303 builder.setTitle(contact.getJid());
304 builder.setMessage(R.string.request_presence_updates);
305 builder.setNegativeButton(R.string.cancel, null);
306 builder.setPositiveButton(R.string.request_now,
307 new DialogInterface.OnClickListener() {
308
309 @Override
310 public void onClick(DialogInterface dialog, int which) {
311 if (xmppConnectionServiceBound) {
312 xmppConnectionService.sendPresencePacket(contact.getAccount(),
313 xmppConnectionService.getPresenceGenerator()
314 .requestPresenceUpdatesFrom(contact));
315 }
316 }
317 });
318 builder.create().show();
319 }
320
321 private void warnMutalPresenceSubscription(final Conversation conversation,final OnPresenceSelected listener) {
322 AlertDialog.Builder builder = new AlertDialog.Builder(this);
323 builder.setTitle(conversation.getContact().getJid());
324 builder.setMessage(R.string.without_mutual_presence_updates);
325 builder.setNegativeButton(R.string.cancel, null);
326 builder.setPositiveButton(R.string.ignore, new OnClickListener() {
327
328 @Override
329 public void onClick(DialogInterface dialog, int which) {
330 conversation.setNextPresence(null);
331 if (listener!=null) {
332 listener.onPresenceSelected();
333 }
334 }
335 });
336 builder.create().show();
337 }
338
339 protected void quickEdit(String previousValue, OnValueEdited callback) {
340 quickEdit(previousValue, callback, false);
341 }
342
343 protected void quickPasswordEdit(String previousValue,
344 OnValueEdited callback) {
345 quickEdit(previousValue, callback, true);
346 }
347
348 private void quickEdit(final String previousValue,
349 final OnValueEdited callback, boolean password) {
350 AlertDialog.Builder builder = new AlertDialog.Builder(this);
351 View view = (View) getLayoutInflater()
352 .inflate(R.layout.quickedit, null);
353 final EditText editor = (EditText) view.findViewById(R.id.editor);
354 OnClickListener mClickListener = new OnClickListener() {
355
356 @Override
357 public void onClick(DialogInterface dialog, int which) {
358 String value = editor.getText().toString();
359 if (!previousValue.equals(value) && value.trim().length() > 0) {
360 callback.onValueEdited(value);
361 }
362 }
363 };
364 if (password) {
365 editor.setInputType(InputType.TYPE_CLASS_TEXT
366 | InputType.TYPE_TEXT_VARIATION_PASSWORD);
367 editor.setHint(R.string.password);
368 builder.setPositiveButton(R.string.accept, mClickListener);
369 } else {
370 builder.setPositiveButton(R.string.edit, mClickListener);
371 }
372 editor.requestFocus();
373 editor.setText(previousValue);
374 builder.setView(view);
375 builder.setNegativeButton(R.string.cancel, null);
376 builder.create().show();
377 }
378
379 public void selectPresence(final Conversation conversation,
380 final OnPresenceSelected listener) {
381 Contact contact = conversation.getContact();
382 if (!contact.showInRoster()) {
383 showAddToRosterDialog(conversation);
384 } else {
385 Presences presences = contact.getPresences();
386 if (presences.size() == 0) {
387 if (!contact.getOption(Contact.Options.TO)
388 && !contact.getOption(Contact.Options.ASKING)
389 && contact.getAccount().getStatus() == Account.STATUS_ONLINE) {
390 showAskForPresenceDialog(contact);
391 } else if (!contact.getOption(Contact.Options.TO) || !contact.getOption(Contact.Options.FROM)) {
392 warnMutalPresenceSubscription(conversation,listener);
393 } else {
394 conversation.setNextPresence(null);
395 listener.onPresenceSelected();
396 }
397 } else if (presences.size() == 1) {
398 String presence = (String) presences.asStringArray()[0];
399 conversation.setNextPresence(presence);
400 listener.onPresenceSelected();
401 } else {
402 final StringBuilder presence = new StringBuilder();
403 AlertDialog.Builder builder = new AlertDialog.Builder(this);
404 builder.setTitle(getString(R.string.choose_presence));
405 final String[] presencesArray = presences.asStringArray();
406 int preselectedPresence = 0;
407 for (int i = 0; i < presencesArray.length; ++i) {
408 if (presencesArray[i].equals(contact.lastseen.presence)) {
409 preselectedPresence = i;
410 break;
411 }
412 }
413 presence.append(presencesArray[preselectedPresence]);
414 builder.setSingleChoiceItems(presencesArray,
415 preselectedPresence,
416 new DialogInterface.OnClickListener() {
417
418 @Override
419 public void onClick(DialogInterface dialog,
420 int which) {
421 presence.delete(0, presence.length());
422 presence.append(presencesArray[which]);
423 }
424 });
425 builder.setNegativeButton(R.string.cancel, null);
426 builder.setPositiveButton(R.string.ok, new OnClickListener() {
427
428 @Override
429 public void onClick(DialogInterface dialog, int which) {
430 conversation.setNextPresence(presence.toString());
431 listener.onPresenceSelected();
432 }
433 });
434 builder.create().show();
435 }
436 }
437 }
438
439 protected void onActivityResult(int requestCode, int resultCode,
440 final Intent data) {
441 super.onActivityResult(requestCode, resultCode, data);
442 if (requestCode == REQUEST_INVITE_TO_CONVERSATION
443 && resultCode == RESULT_OK) {
444 String contactJid = data.getStringExtra("contact");
445 String conversationUuid = data.getStringExtra("conversation");
446 Conversation conversation = xmppConnectionService
447 .findConversationByUuid(conversationUuid);
448 if (conversation.getMode() == Conversation.MODE_MULTI) {
449 xmppConnectionService.invite(conversation, contactJid);
450 }
451 Log.d(Config.LOGTAG, "inviting " + contactJid + " to "
452 + conversation.getName());
453 }
454 }
455
456 public int getSecondaryTextColor() {
457 return this.mSecondaryTextColor;
458 }
459
460 public int getPrimaryTextColor() {
461 return this.mPrimaryTextColor;
462 }
463
464 public int getWarningTextColor() {
465 return this.mWarningTextColor;
466 }
467
468 public int getPrimaryColor() {
469 return this.mPrimaryColor;
470 }
471
472 class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
473 private final WeakReference<ImageView> imageViewReference;
474 private Message message = null;
475
476 public BitmapWorkerTask(ImageView imageView) {
477 imageViewReference = new WeakReference<ImageView>(imageView);
478 }
479
480 @Override
481 protected Bitmap doInBackground(Message... params) {
482 message = params[0];
483 try {
484 return xmppConnectionService.getFileBackend().getThumbnail(
485 message, (int) (metrics.density * 288), false);
486 } catch (FileNotFoundException e) {
487 return null;
488 }
489 }
490
491 @Override
492 protected void onPostExecute(Bitmap bitmap) {
493 if (imageViewReference != null && bitmap != null) {
494 final ImageView imageView = imageViewReference.get();
495 if (imageView != null) {
496 imageView.setImageBitmap(bitmap);
497 imageView.setBackgroundColor(0x00000000);
498 }
499 }
500 }
501 }
502
503 public void loadBitmap(Message message, ImageView imageView) {
504 Bitmap bm;
505 try {
506 bm = xmppConnectionService.getFileBackend().getThumbnail(message,
507 (int) (metrics.density * 288), true);
508 } catch (FileNotFoundException e) {
509 bm = null;
510 }
511 if (bm != null) {
512 imageView.setImageBitmap(bm);
513 imageView.setBackgroundColor(0x00000000);
514 } else {
515 if (cancelPotentialWork(message, imageView)) {
516 imageView.setBackgroundColor(0xff333333);
517 final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
518 final AsyncDrawable asyncDrawable = new AsyncDrawable(
519 getResources(), null, task);
520 imageView.setImageDrawable(asyncDrawable);
521 try {
522 task.execute(message);
523 } catch (RejectedExecutionException e) {
524 return;
525 }
526 }
527 }
528 }
529
530 public static boolean cancelPotentialWork(Message message,
531 ImageView imageView) {
532 final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
533
534 if (bitmapWorkerTask != null) {
535 final Message oldMessage = bitmapWorkerTask.message;
536 if (oldMessage == null || message != oldMessage) {
537 bitmapWorkerTask.cancel(true);
538 } else {
539 return false;
540 }
541 }
542 return true;
543 }
544
545 private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
546 if (imageView != null) {
547 final Drawable drawable = imageView.getDrawable();
548 if (drawable instanceof AsyncDrawable) {
549 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
550 return asyncDrawable.getBitmapWorkerTask();
551 }
552 }
553 return null;
554 }
555
556 static class AsyncDrawable extends BitmapDrawable {
557 private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
558
559 public AsyncDrawable(Resources res, Bitmap bitmap,
560 BitmapWorkerTask bitmapWorkerTask) {
561 super(res, bitmap);
562 bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(
563 bitmapWorkerTask);
564 }
565
566 public BitmapWorkerTask getBitmapWorkerTask() {
567 return bitmapWorkerTaskReference.get();
568 }
569 }
570}