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