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