1package eu.siacs.conversations.ui;
2
3import android.annotation.SuppressLint;
4import android.annotation.TargetApi;
5import android.app.Activity;
6import android.app.AlertDialog;
7import android.app.AlertDialog.Builder;
8import android.app.PendingIntent;
9import android.content.ClipData;
10import android.content.ClipboardManager;
11import android.content.ComponentName;
12import android.content.Context;
13import android.content.DialogInterface;
14import android.content.DialogInterface.OnClickListener;
15import android.content.Intent;
16import android.content.IntentSender.SendIntentException;
17import android.content.ServiceConnection;
18import android.content.SharedPreferences;
19import android.content.pm.PackageManager;
20import android.content.pm.ResolveInfo;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.Color;
24import android.graphics.Point;
25import android.graphics.drawable.BitmapDrawable;
26import android.graphics.drawable.Drawable;
27import android.net.Uri;
28import android.nfc.NdefMessage;
29import android.nfc.NdefRecord;
30import android.nfc.NfcAdapter;
31import android.nfc.NfcEvent;
32import android.os.AsyncTask;
33import android.os.Build;
34import android.os.Bundle;
35import android.os.IBinder;
36import android.preference.PreferenceManager;
37import android.text.InputType;
38import android.util.DisplayMetrics;
39import android.util.Log;
40import android.view.MenuItem;
41import android.view.View;
42import android.view.inputmethod.InputMethodManager;
43import android.widget.EditText;
44import android.widget.ImageView;
45import android.widget.Toast;
46
47import com.google.zxing.BarcodeFormat;
48import com.google.zxing.EncodeHintType;
49import com.google.zxing.WriterException;
50import com.google.zxing.common.BitMatrix;
51import com.google.zxing.qrcode.QRCodeWriter;
52import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
53
54import net.java.otr4j.session.SessionID;
55
56import java.io.FileNotFoundException;
57import java.lang.ref.WeakReference;
58import java.util.ArrayList;
59import java.util.Hashtable;
60import java.util.List;
61import java.util.concurrent.RejectedExecutionException;
62
63import eu.siacs.conversations.Config;
64import eu.siacs.conversations.R;
65import eu.siacs.conversations.entities.Account;
66import eu.siacs.conversations.entities.Contact;
67import eu.siacs.conversations.entities.Conversation;
68import eu.siacs.conversations.entities.Message;
69import eu.siacs.conversations.entities.Presences;
70import eu.siacs.conversations.services.AvatarService;
71import eu.siacs.conversations.services.XmppConnectionService;
72import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
73import eu.siacs.conversations.utils.ExceptionHelper;
74import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
75import eu.siacs.conversations.xmpp.jid.InvalidJidException;
76import eu.siacs.conversations.xmpp.jid.Jid;
77
78public abstract class XmppActivity extends Activity {
79
80 protected static final int REQUEST_ANNOUNCE_PGP = 0x0101;
81 protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102;
82
83 public XmppConnectionService xmppConnectionService;
84 public boolean xmppConnectionServiceBound = false;
85 protected boolean registeredListeners = false;
86
87 protected int mPrimaryTextColor;
88 protected int mSecondaryTextColor;
89 protected int mSecondaryBackgroundColor;
90 protected int mColorRed;
91 protected int mColorOrange;
92 protected int mColorGreen;
93 protected int mPrimaryColor;
94
95 protected boolean mUseSubject = true;
96
97 private DisplayMetrics metrics;
98 protected int mTheme;
99 protected boolean mUsingEnterKey = false;
100
101 protected interface OnValueEdited {
102 public void onValueEdited(String value);
103 }
104
105 public interface OnPresenceSelected {
106 public void onPresenceSelected();
107 }
108
109 protected ServiceConnection mConnection = new ServiceConnection() {
110
111 @Override
112 public void onServiceConnected(ComponentName className, IBinder service) {
113 XmppConnectionBinder binder = (XmppConnectionBinder) service;
114 xmppConnectionService = binder.getService();
115 xmppConnectionServiceBound = true;
116 if (!registeredListeners && shouldRegisterListeners()) {
117 registerListeners();
118 registeredListeners = true;
119 }
120 onBackendConnected();
121 }
122
123 @Override
124 public void onServiceDisconnected(ComponentName arg0) {
125 xmppConnectionServiceBound = false;
126 }
127 };
128
129 @Override
130 protected void onStart() {
131 super.onStart();
132 if (!xmppConnectionServiceBound) {
133 connectToBackend();
134 } else {
135 if (!registeredListeners) {
136 this.registerListeners();
137 this.registeredListeners = true;
138 }
139 this.onBackendConnected();
140 }
141 }
142
143 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
144 protected boolean shouldRegisterListeners() {
145 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
146 return !isDestroyed() && !isFinishing();
147 } else {
148 return !isFinishing();
149 }
150 }
151
152 public void connectToBackend() {
153 Intent intent = new Intent(this, XmppConnectionService.class);
154 intent.setAction("ui");
155 startService(intent);
156 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
157 }
158
159 @Override
160 protected void onStop() {
161 super.onStop();
162 if (xmppConnectionServiceBound) {
163 if (registeredListeners) {
164 this.unregisterListeners();
165 this.registeredListeners = false;
166 }
167 unbindService(mConnection);
168 xmppConnectionServiceBound = false;
169 }
170 }
171
172 protected void hideKeyboard() {
173 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
174
175 View focus = getCurrentFocus();
176
177 if (focus != null) {
178
179 inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
180 InputMethodManager.HIDE_NOT_ALWAYS);
181 }
182 }
183
184 public boolean hasPgp() {
185 return xmppConnectionService.getPgpEngine() != null;
186 }
187
188 public void showInstallPgpDialog() {
189 Builder builder = new AlertDialog.Builder(this);
190 builder.setTitle(getString(R.string.openkeychain_required));
191 builder.setIconAttribute(android.R.attr.alertDialogIcon);
192 builder.setMessage(getText(R.string.openkeychain_required_long));
193 builder.setNegativeButton(getString(R.string.cancel), null);
194 builder.setNeutralButton(getString(R.string.restart),
195 new OnClickListener() {
196
197 @Override
198 public void onClick(DialogInterface dialog, int which) {
199 if (xmppConnectionServiceBound) {
200 unbindService(mConnection);
201 xmppConnectionServiceBound = false;
202 }
203 stopService(new Intent(XmppActivity.this,
204 XmppConnectionService.class));
205 finish();
206 }
207 });
208 builder.setPositiveButton(getString(R.string.install),
209 new OnClickListener() {
210
211 @Override
212 public void onClick(DialogInterface dialog, int which) {
213 Uri uri = Uri
214 .parse("market://details?id=org.sufficientlysecure.keychain");
215 Intent marketIntent = new Intent(Intent.ACTION_VIEW,
216 uri);
217 PackageManager manager = getApplicationContext()
218 .getPackageManager();
219 List<ResolveInfo> infos = manager
220 .queryIntentActivities(marketIntent, 0);
221 if (infos.size() > 0) {
222 startActivity(marketIntent);
223 } else {
224 uri = Uri.parse("http://www.openkeychain.org/");
225 Intent browserIntent = new Intent(
226 Intent.ACTION_VIEW, uri);
227 startActivity(browserIntent);
228 }
229 finish();
230 }
231 });
232 builder.create().show();
233 }
234
235 abstract void onBackendConnected();
236
237 protected void registerListeners() {
238 if (this instanceof XmppConnectionService.OnConversationUpdate) {
239 this.xmppConnectionService.setOnConversationListChangedListener((XmppConnectionService.OnConversationUpdate) this);
240 }
241 if (this instanceof XmppConnectionService.OnAccountUpdate) {
242 this.xmppConnectionService.setOnAccountListChangedListener((XmppConnectionService.OnAccountUpdate) this);
243 }
244 if (this instanceof XmppConnectionService.OnRosterUpdate) {
245 this.xmppConnectionService.setOnRosterUpdateListener((XmppConnectionService.OnRosterUpdate) this);
246 }
247 if (this instanceof XmppConnectionService.OnMucRosterUpdate) {
248 this.xmppConnectionService.setOnMucRosterUpdateListener((XmppConnectionService.OnMucRosterUpdate) this);
249 }
250 if (this instanceof OnUpdateBlocklist) {
251 this.xmppConnectionService.setOnUpdateBlocklistListener((OnUpdateBlocklist) this);
252 }
253 }
254
255 protected void unregisterListeners() {
256 if (this instanceof XmppConnectionService.OnConversationUpdate) {
257 this.xmppConnectionService.removeOnConversationListChangedListener();
258 }
259 if (this instanceof XmppConnectionService.OnAccountUpdate) {
260 this.xmppConnectionService.removeOnAccountListChangedListener();
261 }
262 if (this instanceof XmppConnectionService.OnRosterUpdate) {
263 this.xmppConnectionService.removeOnRosterUpdateListener();
264 }
265 if (this instanceof XmppConnectionService.OnMucRosterUpdate) {
266 this.xmppConnectionService.removeOnMucRosterUpdateListener();
267 }
268 if (this instanceof OnUpdateBlocklist) {
269 this.xmppConnectionService.removeOnUpdateBlocklistListener();
270 }
271 }
272
273 @Override
274 public boolean onOptionsItemSelected(final MenuItem item) {
275 switch (item.getItemId()) {
276 case R.id.action_settings:
277 startActivity(new Intent(this, SettingsActivity.class));
278 break;
279 case R.id.action_accounts:
280 startActivity(new Intent(this, ManageAccountActivity.class));
281 break;
282 case android.R.id.home:
283 finish();
284 break;
285 case R.id.action_show_qr_code:
286 showQrCode();
287 break;
288 }
289 return super.onOptionsItemSelected(item);
290 }
291
292 @Override
293 protected void onCreate(Bundle savedInstanceState) {
294 super.onCreate(savedInstanceState);
295 metrics = getResources().getDisplayMetrics();
296 ExceptionHelper.init(getApplicationContext());
297 mPrimaryTextColor = getResources().getColor(R.color.primarytext);
298 mSecondaryTextColor = getResources().getColor(R.color.secondarytext);
299 mColorRed = getResources().getColor(R.color.red);
300 mColorOrange = getResources().getColor(R.color.orange);
301 mColorGreen = getResources().getColor(R.color.green);
302 mPrimaryColor = getResources().getColor(R.color.primary);
303 mSecondaryBackgroundColor = getResources().getColor(
304 R.color.secondarybackground);
305 this.mTheme = findTheme();
306 setTheme(this.mTheme);
307 this.mUsingEnterKey = usingEnterKey();
308 mUseSubject = getPreferences().getBoolean("use_subject", true);
309 }
310
311 protected boolean usingEnterKey() {
312 return getPreferences().getBoolean("display_enter_key", false);
313 }
314
315 protected SharedPreferences getPreferences() {
316 return PreferenceManager
317 .getDefaultSharedPreferences(getApplicationContext());
318 }
319
320 public boolean useSubjectToIdentifyConference() {
321 return mUseSubject;
322 }
323
324 public void switchToConversation(Conversation conversation) {
325 switchToConversation(conversation, null, false);
326 }
327
328 public void switchToConversation(Conversation conversation, String text,
329 boolean newTask) {
330 switchToConversation(conversation,text,null,newTask);
331 }
332
333 public void highlightInMuc(Conversation conversation, String nick) {
334 switchToConversation(conversation,null,nick,false);
335 }
336
337 private void switchToConversation(Conversation conversation, String text, String nick, boolean newTask) {
338 Intent viewConversationIntent = new Intent(this,
339 ConversationActivity.class);
340 viewConversationIntent.setAction(Intent.ACTION_VIEW);
341 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
342 conversation.getUuid());
343 if (text != null) {
344 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
345 }
346 if (nick != null) {
347 viewConversationIntent.putExtra(ConversationActivity.NICK, nick);
348 }
349 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
350 if (newTask) {
351 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
352 | Intent.FLAG_ACTIVITY_NEW_TASK
353 | Intent.FLAG_ACTIVITY_SINGLE_TOP);
354 } else {
355 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
356 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
357 }
358 startActivity(viewConversationIntent);
359 finish();
360 }
361
362 public void switchToContactDetails(Contact contact) {
363 Intent intent = new Intent(this, ContactDetailsActivity.class);
364 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
365 intent.putExtra("account", contact.getAccount().getJid().toBareJid().toString());
366 intent.putExtra("contact", contact.getJid().toString());
367 startActivity(intent);
368 }
369
370 public void switchToAccount(Account account) {
371 Intent intent = new Intent(this, EditAccountActivity.class);
372 intent.putExtra("jid", account.getJid().toBareJid().toString());
373 startActivity(intent);
374 }
375
376 protected void inviteToConversation(Conversation conversation) {
377 Intent intent = new Intent(getApplicationContext(),
378 ChooseContactActivity.class);
379 intent.putExtra("conversation", conversation.getUuid());
380 startActivityForResult(intent, REQUEST_INVITE_TO_CONVERSATION);
381 }
382
383 protected void announcePgp(Account account, final Conversation conversation) {
384 xmppConnectionService.getPgpEngine().generateSignature(account,
385 "online", new UiCallback<Account>() {
386
387 @Override
388 public void userInputRequried(PendingIntent pi,
389 Account account) {
390 try {
391 startIntentSenderForResult(pi.getIntentSender(),
392 REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
393 } catch (final SendIntentException ignored) {
394 }
395 }
396
397 @Override
398 public void success(Account account) {
399 xmppConnectionService.databaseBackend
400 .updateAccount(account);
401 xmppConnectionService.sendPresencePacket(account,
402 xmppConnectionService.getPresenceGenerator()
403 .sendPresence(account));
404 if (conversation != null) {
405 conversation
406 .setNextEncryption(Message.ENCRYPTION_PGP);
407 xmppConnectionService.databaseBackend
408 .updateConversation(conversation);
409 }
410 }
411
412 @Override
413 public void error(int error, Account account) {
414 displayErrorDialog(error);
415 }
416 });
417 }
418
419 protected void displayErrorDialog(final int errorCode) {
420 runOnUiThread(new Runnable() {
421
422 @Override
423 public void run() {
424 AlertDialog.Builder builder = new AlertDialog.Builder(
425 XmppActivity.this);
426 builder.setIconAttribute(android.R.attr.alertDialogIcon);
427 builder.setTitle(getString(R.string.error));
428 builder.setMessage(errorCode);
429 builder.setNeutralButton(R.string.accept, null);
430 builder.create().show();
431 }
432 });
433
434 }
435
436 protected void showAddToRosterDialog(final Conversation conversation) {
437 final Jid jid = conversation.getJid();
438 AlertDialog.Builder builder = new AlertDialog.Builder(this);
439 builder.setTitle(jid.toString());
440 builder.setMessage(getString(R.string.not_in_roster));
441 builder.setNegativeButton(getString(R.string.cancel), null);
442 builder.setPositiveButton(getString(R.string.add_contact),
443 new DialogInterface.OnClickListener() {
444
445 @Override
446 public void onClick(DialogInterface dialog, int which) {
447 final Jid jid = conversation.getJid();
448 Account account = conversation.getAccount();
449 Contact contact = account.getRoster().getContact(jid);
450 xmppConnectionService.createContact(contact);
451 switchToContactDetails(contact);
452 }
453 });
454 builder.create().show();
455 }
456
457 private void showAskForPresenceDialog(final Contact contact) {
458 AlertDialog.Builder builder = new AlertDialog.Builder(this);
459 builder.setTitle(contact.getJid().toString());
460 builder.setMessage(R.string.request_presence_updates);
461 builder.setNegativeButton(R.string.cancel, null);
462 builder.setPositiveButton(R.string.request_now,
463 new DialogInterface.OnClickListener() {
464
465 @Override
466 public void onClick(DialogInterface dialog, int which) {
467 if (xmppConnectionServiceBound) {
468 xmppConnectionService.sendPresencePacket(contact
469 .getAccount(), xmppConnectionService
470 .getPresenceGenerator()
471 .requestPresenceUpdatesFrom(contact));
472 }
473 }
474 });
475 builder.create().show();
476 }
477
478 private void warnMutalPresenceSubscription(final Conversation conversation,
479 final OnPresenceSelected listener) {
480 AlertDialog.Builder builder = new AlertDialog.Builder(this);
481 builder.setTitle(conversation.getContact().getJid().toString());
482 builder.setMessage(R.string.without_mutual_presence_updates);
483 builder.setNegativeButton(R.string.cancel, null);
484 builder.setPositiveButton(R.string.ignore, new OnClickListener() {
485
486 @Override
487 public void onClick(DialogInterface dialog, int which) {
488 conversation.setNextCounterpart(null);
489 if (listener != null) {
490 listener.onPresenceSelected();
491 }
492 }
493 });
494 builder.create().show();
495 }
496
497 protected void quickEdit(String previousValue, OnValueEdited callback) {
498 quickEdit(previousValue, callback, false);
499 }
500
501 protected void quickPasswordEdit(String previousValue,
502 OnValueEdited callback) {
503 quickEdit(previousValue, callback, true);
504 }
505
506 @SuppressLint("InflateParams")
507 private void quickEdit(final String previousValue,
508 final OnValueEdited callback, boolean password) {
509 AlertDialog.Builder builder = new AlertDialog.Builder(this);
510 View view = getLayoutInflater().inflate(R.layout.quickedit, null);
511 final EditText editor = (EditText) view.findViewById(R.id.editor);
512 OnClickListener mClickListener = new OnClickListener() {
513
514 @Override
515 public void onClick(DialogInterface dialog, int which) {
516 String value = editor.getText().toString();
517 if (!previousValue.equals(value) && value.trim().length() > 0) {
518 callback.onValueEdited(value);
519 }
520 }
521 };
522 if (password) {
523 editor.setInputType(InputType.TYPE_CLASS_TEXT
524 | InputType.TYPE_TEXT_VARIATION_PASSWORD);
525 editor.setHint(R.string.password);
526 builder.setPositiveButton(R.string.accept, mClickListener);
527 } else {
528 builder.setPositiveButton(R.string.edit, mClickListener);
529 }
530 editor.requestFocus();
531 editor.setText(previousValue);
532 builder.setView(view);
533 builder.setNegativeButton(R.string.cancel, null);
534 builder.create().show();
535 }
536
537 public void selectPresence(final Conversation conversation,
538 final OnPresenceSelected listener) {
539 final Contact contact = conversation.getContact();
540 if (conversation.hasValidOtrSession()) {
541 SessionID id = conversation.getOtrSession().getSessionID();
542 Jid jid;
543 try {
544 jid = Jid.fromString(id.getAccountID() + "/" + id.getUserID());
545 } catch (InvalidJidException e) {
546 jid = null;
547 }
548 conversation.setNextCounterpart(jid);
549 listener.onPresenceSelected();
550 } else if (!contact.showInRoster()) {
551 showAddToRosterDialog(conversation);
552 } else {
553 Presences presences = contact.getPresences();
554 if (presences.size() == 0) {
555 if (!contact.getOption(Contact.Options.TO)
556 && !contact.getOption(Contact.Options.ASKING)
557 && contact.getAccount().getStatus() == Account.State.ONLINE) {
558 showAskForPresenceDialog(contact);
559 } else if (!contact.getOption(Contact.Options.TO)
560 || !contact.getOption(Contact.Options.FROM)) {
561 warnMutalPresenceSubscription(conversation, listener);
562 } else {
563 conversation.setNextCounterpart(null);
564 listener.onPresenceSelected();
565 }
566 } else if (presences.size() == 1) {
567 String presence = presences.asStringArray()[0];
568 try {
569 conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence));
570 } catch (InvalidJidException e) {
571 conversation.setNextCounterpart(null);
572 }
573 listener.onPresenceSelected();
574 } else {
575 final StringBuilder presence = new StringBuilder();
576 AlertDialog.Builder builder = new AlertDialog.Builder(this);
577 builder.setTitle(getString(R.string.choose_presence));
578 final String[] presencesArray = presences.asStringArray();
579 int preselectedPresence = 0;
580 for (int i = 0; i < presencesArray.length; ++i) {
581 if (presencesArray[i].equals(contact.lastseen.presence)) {
582 preselectedPresence = i;
583 break;
584 }
585 }
586 presence.append(presencesArray[preselectedPresence]);
587 builder.setSingleChoiceItems(presencesArray,
588 preselectedPresence,
589 new DialogInterface.OnClickListener() {
590
591 @Override
592 public void onClick(DialogInterface dialog,
593 int which) {
594 presence.delete(0, presence.length());
595 presence.append(presencesArray[which]);
596 }
597 });
598 builder.setNegativeButton(R.string.cancel, null);
599 builder.setPositiveButton(R.string.ok, new OnClickListener() {
600
601 @Override
602 public void onClick(DialogInterface dialog, int which) {
603 try {
604 conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence.toString()));
605 } catch (InvalidJidException e) {
606 conversation.setNextCounterpart(null);
607 }
608 listener.onPresenceSelected();
609 }
610 });
611 builder.create().show();
612 }
613 }
614 }
615
616 protected void onActivityResult(int requestCode, int resultCode,
617 final Intent data) {
618 super.onActivityResult(requestCode, resultCode, data);
619 if (requestCode == REQUEST_INVITE_TO_CONVERSATION
620 && resultCode == RESULT_OK) {
621 try {
622 Jid jid = Jid.fromString(data.getStringExtra("contact"));
623 String conversationUuid = data.getStringExtra("conversation");
624 Conversation conversation = xmppConnectionService
625 .findConversationByUuid(conversationUuid);
626 if (conversation.getMode() == Conversation.MODE_MULTI) {
627 xmppConnectionService.invite(conversation, jid);
628 } else {
629 List<Jid> jids = new ArrayList<Jid>();
630 jids.add(conversation.getJid().toBareJid());
631 jids.add(jid);
632 xmppConnectionService.createAdhocConference(conversation.getAccount(), jids, adhocCallback);
633 }
634 } catch (final InvalidJidException ignored) {
635
636 }
637 }
638 }
639
640 private UiCallback<Conversation> adhocCallback = new UiCallback<Conversation>() {
641 @Override
642 public void success(final Conversation conversation) {
643 switchToConversation(conversation);
644 runOnUiThread(new Runnable() {
645 @Override
646 public void run() {
647 Toast.makeText(XmppActivity.this,R.string.conference_created,Toast.LENGTH_LONG).show();
648 }
649 });
650 }
651
652 @Override
653 public void error(final int errorCode, Conversation object) {
654 runOnUiThread(new Runnable() {
655 @Override
656 public void run() {
657 Toast.makeText(XmppActivity.this,errorCode,Toast.LENGTH_LONG).show();
658 }
659 });
660 }
661
662 @Override
663 public void userInputRequried(PendingIntent pi, Conversation object) {
664
665 }
666 };
667
668 public int getSecondaryTextColor() {
669 return this.mSecondaryTextColor;
670 }
671
672 public int getPrimaryTextColor() {
673 return this.mPrimaryTextColor;
674 }
675
676 public int getWarningTextColor() {
677 return this.mColorRed;
678 }
679
680 public int getPrimaryColor() {
681 return this.mPrimaryColor;
682 }
683
684 public int getSecondaryBackgroundColor() {
685 return this.mSecondaryBackgroundColor;
686 }
687
688 public int getPixel(int dp) {
689 DisplayMetrics metrics = getResources().getDisplayMetrics();
690 return ((int) (dp * metrics.density));
691 }
692
693 public boolean copyTextToClipboard(String text, int labelResId) {
694 ClipboardManager mClipBoardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
695 String label = getResources().getString(labelResId);
696 if (mClipBoardManager != null) {
697 ClipData mClipData = ClipData.newPlainText(label, text);
698 mClipBoardManager.setPrimaryClip(mClipData);
699 return true;
700 }
701 return false;
702 }
703
704 protected void registerNdefPushMessageCallback() {
705 NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
706 if (nfcAdapter != null && nfcAdapter.isEnabled()) {
707 nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
708 @Override
709 public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
710 return new NdefMessage(new NdefRecord[]{
711 NdefRecord.createUri(getShareableUri()),
712 NdefRecord.createApplicationRecord("eu.siacs.conversations")
713 });
714 }
715 }, this);
716 }
717 }
718
719 protected void unregisterNdefPushMessageCallback() {
720 NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
721 if (nfcAdapter != null && nfcAdapter.isEnabled()) {
722 nfcAdapter.setNdefPushMessageCallback(null,this);
723 }
724 }
725
726 protected String getShareableUri() {
727 return null;
728 }
729
730 @Override
731 public void onResume() {
732 super.onResume();
733 if (this.getShareableUri()!=null) {
734 this.registerNdefPushMessageCallback();
735 }
736 }
737
738 protected int findTheme() {
739 if (getPreferences().getBoolean("use_larger_font", false)) {
740 return R.style.ConversationsTheme_LargerText;
741 } else {
742 return R.style.ConversationsTheme;
743 }
744 }
745
746 @Override
747 public void onPause() {
748 super.onPause();
749 this.unregisterNdefPushMessageCallback();
750 }
751
752 protected void showQrCode() {
753 String uri = getShareableUri();
754 if (uri!=null) {
755 Point size = new Point();
756 getWindowManager().getDefaultDisplay().getSize(size);
757 final int width = (size.x < size.y ? size.x : size.y);
758 Bitmap bitmap = createQrCodeBitmap(uri, width);
759 ImageView view = new ImageView(this);
760 view.setImageBitmap(bitmap);
761 AlertDialog.Builder builder = new AlertDialog.Builder(this);
762 builder.setView(view);
763 builder.create().show();
764 }
765 }
766
767 protected Bitmap createQrCodeBitmap(String input, int size) {
768 Log.d(Config.LOGTAG,"qr code requested size: "+size);
769 try {
770 final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
771 final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
772 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
773 final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
774 final int width = result.getWidth();
775 final int height = result.getHeight();
776 final int[] pixels = new int[width * height];
777 for (int y = 0; y < height; y++) {
778 final int offset = y * width;
779 for (int x = 0; x < width; x++) {
780 pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
781 }
782 }
783 final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
784 Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
785 bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
786 return bitmap;
787 } catch (final WriterException e) {
788 return null;
789 }
790 }
791
792 public AvatarService avatarService() {
793 return xmppConnectionService.getAvatarService();
794 }
795
796 class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
797 private final WeakReference<ImageView> imageViewReference;
798 private Message message = null;
799
800 public BitmapWorkerTask(ImageView imageView) {
801 imageViewReference = new WeakReference<>(imageView);
802 }
803
804 @Override
805 protected Bitmap doInBackground(Message... params) {
806 message = params[0];
807 try {
808 return xmppConnectionService.getFileBackend().getThumbnail(
809 message, (int) (metrics.density * 288), false);
810 } catch (FileNotFoundException e) {
811 return null;
812 }
813 }
814
815 @Override
816 protected void onPostExecute(Bitmap bitmap) {
817 if (bitmap != null) {
818 final ImageView imageView = imageViewReference.get();
819 if (imageView != null) {
820 imageView.setImageBitmap(bitmap);
821 imageView.setBackgroundColor(0x00000000);
822 }
823 }
824 }
825 }
826
827 public void loadBitmap(Message message, ImageView imageView) {
828 Bitmap bm;
829 try {
830 bm = xmppConnectionService.getFileBackend().getThumbnail(message,
831 (int) (metrics.density * 288), true);
832 } catch (FileNotFoundException e) {
833 bm = null;
834 }
835 if (bm != null) {
836 imageView.setImageBitmap(bm);
837 imageView.setBackgroundColor(0x00000000);
838 } else {
839 if (cancelPotentialWork(message, imageView)) {
840 imageView.setBackgroundColor(0xff333333);
841 final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
842 final AsyncDrawable asyncDrawable = new AsyncDrawable(
843 getResources(), null, task);
844 imageView.setImageDrawable(asyncDrawable);
845 try {
846 task.execute(message);
847 } catch (final RejectedExecutionException ignored) {
848 }
849 }
850 }
851 }
852
853 public static boolean cancelPotentialWork(Message message,
854 ImageView imageView) {
855 final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
856
857 if (bitmapWorkerTask != null) {
858 final Message oldMessage = bitmapWorkerTask.message;
859 if (oldMessage == null || message != oldMessage) {
860 bitmapWorkerTask.cancel(true);
861 } else {
862 return false;
863 }
864 }
865 return true;
866 }
867
868 private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
869 if (imageView != null) {
870 final Drawable drawable = imageView.getDrawable();
871 if (drawable instanceof AsyncDrawable) {
872 final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
873 return asyncDrawable.getBitmapWorkerTask();
874 }
875 }
876 return null;
877 }
878
879 static class AsyncDrawable extends BitmapDrawable {
880 private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
881
882 public AsyncDrawable(Resources res, Bitmap bitmap,
883 BitmapWorkerTask bitmapWorkerTask) {
884 super(res, bitmap);
885 bitmapWorkerTaskReference = new WeakReference<>(
886 bitmapWorkerTask);
887 }
888
889 public BitmapWorkerTask getBitmapWorkerTask() {
890 return bitmapWorkerTaskReference.get();
891 }
892 }
893}