XmppActivity.java

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