XmppActivity.java

   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.Handler;
  37import android.os.IBinder;
  38import android.os.SystemClock;
  39import android.preference.PreferenceManager;
  40import android.text.InputType;
  41import android.util.DisplayMetrics;
  42import android.util.Log;
  43import android.view.MenuItem;
  44import android.view.View;
  45import android.view.inputmethod.InputMethodManager;
  46import android.widget.CompoundButton;
  47import android.widget.EditText;
  48import android.widget.ImageView;
  49import android.widget.LinearLayout;
  50import android.widget.TextView;
  51import android.widget.Toast;
  52
  53import com.google.zxing.BarcodeFormat;
  54import com.google.zxing.EncodeHintType;
  55import com.google.zxing.WriterException;
  56import com.google.zxing.common.BitMatrix;
  57import com.google.zxing.qrcode.QRCodeWriter;
  58import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  59
  60import net.java.otr4j.session.SessionID;
  61
  62import java.io.FileNotFoundException;
  63import java.lang.ref.WeakReference;
  64import java.util.ArrayList;
  65import java.util.Hashtable;
  66import java.util.List;
  67import java.util.concurrent.RejectedExecutionException;
  68
  69import eu.siacs.conversations.Config;
  70import eu.siacs.conversations.R;
  71import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
  72import eu.siacs.conversations.entities.Account;
  73import eu.siacs.conversations.entities.Contact;
  74import eu.siacs.conversations.entities.Conversation;
  75import eu.siacs.conversations.entities.Message;
  76import eu.siacs.conversations.entities.MucOptions;
  77import eu.siacs.conversations.entities.Presences;
  78import eu.siacs.conversations.services.AvatarService;
  79import eu.siacs.conversations.services.XmppConnectionService;
  80import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
  81import eu.siacs.conversations.ui.widget.Switch;
  82import eu.siacs.conversations.utils.CryptoHelper;
  83import eu.siacs.conversations.utils.ExceptionHelper;
  84import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
  85import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
  86import eu.siacs.conversations.xmpp.jid.InvalidJidException;
  87import eu.siacs.conversations.xmpp.jid.Jid;
  88
  89public abstract class XmppActivity extends Activity {
  90
  91	protected static final int REQUEST_ANNOUNCE_PGP = 0x0101;
  92	protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102;
  93	protected static final int REQUEST_CHOOSE_PGP_ID = 0x0103;
  94
  95	public XmppConnectionService xmppConnectionService;
  96	public boolean xmppConnectionServiceBound = false;
  97	protected boolean registeredListeners = false;
  98
  99	protected int mPrimaryTextColor;
 100	protected int mSecondaryTextColor;
 101	protected int mTertiaryTextColor;
 102	protected int mPrimaryBackgroundColor;
 103	protected int mSecondaryBackgroundColor;
 104	protected int mColorRed;
 105	protected int mColorOrange;
 106	protected int mColorGreen;
 107	protected int mPrimaryColor;
 108
 109	protected boolean mUseSubject = true;
 110
 111	private DisplayMetrics metrics;
 112	protected int mTheme;
 113	protected boolean mUsingEnterKey = false;
 114
 115	private long mLastUiRefresh = 0;
 116	private Handler mRefreshUiHandler = new Handler();
 117	private Runnable mRefreshUiRunnable = new Runnable() {
 118		@Override
 119		public void run() {
 120			mLastUiRefresh = SystemClock.elapsedRealtime();
 121			refreshUiReal();
 122		}
 123	};
 124
 125	protected ConferenceInvite mPendingConferenceInvite = null;
 126
 127
 128	protected final void refreshUi() {
 129		final long diff = SystemClock.elapsedRealtime() - mLastUiRefresh;
 130		if (diff > Config.REFRESH_UI_INTERVAL) {
 131			mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
 132			runOnUiThread(mRefreshUiRunnable);
 133		} else {
 134			final long next = Config.REFRESH_UI_INTERVAL - diff;
 135			mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
 136			mRefreshUiHandler.postDelayed(mRefreshUiRunnable,next);
 137		}
 138	}
 139
 140	abstract protected void refreshUiReal();
 141
 142	protected interface OnValueEdited {
 143		public void onValueEdited(String value);
 144	}
 145
 146	public interface OnPresenceSelected {
 147		public void onPresenceSelected();
 148	}
 149
 150	protected ServiceConnection mConnection = new ServiceConnection() {
 151
 152		@Override
 153		public void onServiceConnected(ComponentName className, IBinder service) {
 154			XmppConnectionBinder binder = (XmppConnectionBinder) service;
 155			xmppConnectionService = binder.getService();
 156			xmppConnectionServiceBound = true;
 157			if (!registeredListeners && shouldRegisterListeners()) {
 158				registerListeners();
 159				registeredListeners = true;
 160			}
 161			onBackendConnected();
 162		}
 163
 164		@Override
 165		public void onServiceDisconnected(ComponentName arg0) {
 166			xmppConnectionServiceBound = false;
 167		}
 168	};
 169
 170	@Override
 171	protected void onStart() {
 172		super.onStart();
 173		if (!xmppConnectionServiceBound) {
 174			connectToBackend();
 175		} else {
 176			if (!registeredListeners) {
 177				this.registerListeners();
 178				this.registeredListeners = true;
 179			}
 180			this.onBackendConnected();
 181		}
 182	}
 183
 184	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 185	protected boolean shouldRegisterListeners() {
 186		if  (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
 187			return !isDestroyed() && !isFinishing();
 188		} else {
 189			return !isFinishing();
 190		}
 191	}
 192
 193	public void connectToBackend() {
 194		Intent intent = new Intent(this, XmppConnectionService.class);
 195		intent.setAction("ui");
 196		startService(intent);
 197		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 198	}
 199
 200	@Override
 201	protected void onStop() {
 202		super.onStop();
 203		if (xmppConnectionServiceBound) {
 204			if (registeredListeners) {
 205				this.unregisterListeners();
 206				this.registeredListeners = false;
 207			}
 208			unbindService(mConnection);
 209			xmppConnectionServiceBound = false;
 210		}
 211	}
 212
 213	protected void hideKeyboard() {
 214		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 215
 216		View focus = getCurrentFocus();
 217
 218		if (focus != null) {
 219
 220			inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
 221					InputMethodManager.HIDE_NOT_ALWAYS);
 222		}
 223	}
 224
 225	public boolean hasPgp() {
 226		return xmppConnectionService.getPgpEngine() != null;
 227	}
 228
 229	public void showInstallPgpDialog() {
 230		Builder builder = new AlertDialog.Builder(this);
 231		builder.setTitle(getString(R.string.openkeychain_required));
 232		builder.setIconAttribute(android.R.attr.alertDialogIcon);
 233		builder.setMessage(getText(R.string.openkeychain_required_long));
 234		builder.setNegativeButton(getString(R.string.cancel), null);
 235		builder.setNeutralButton(getString(R.string.restart),
 236				new OnClickListener() {
 237
 238					@Override
 239					public void onClick(DialogInterface dialog, int which) {
 240						if (xmppConnectionServiceBound) {
 241							unbindService(mConnection);
 242							xmppConnectionServiceBound = false;
 243						}
 244						stopService(new Intent(XmppActivity.this,
 245									XmppConnectionService.class));
 246						finish();
 247					}
 248				});
 249		builder.setPositiveButton(getString(R.string.install),
 250				new OnClickListener() {
 251
 252					@Override
 253					public void onClick(DialogInterface dialog, int which) {
 254						Uri uri = Uri
 255							.parse("market://details?id=org.sufficientlysecure.keychain");
 256						Intent marketIntent = new Intent(Intent.ACTION_VIEW,
 257								uri);
 258						PackageManager manager = getApplicationContext()
 259							.getPackageManager();
 260						List<ResolveInfo> infos = manager
 261							.queryIntentActivities(marketIntent, 0);
 262						if (infos.size() > 0) {
 263							startActivity(marketIntent);
 264						} else {
 265							uri = Uri.parse("http://www.openkeychain.org/");
 266							Intent browserIntent = new Intent(
 267									Intent.ACTION_VIEW, uri);
 268							startActivity(browserIntent);
 269						}
 270						finish();
 271					}
 272				});
 273		builder.create().show();
 274	}
 275
 276	abstract void onBackendConnected();
 277
 278	protected void registerListeners() {
 279		if (this instanceof XmppConnectionService.OnConversationUpdate) {
 280			this.xmppConnectionService.setOnConversationListChangedListener((XmppConnectionService.OnConversationUpdate) this);
 281		}
 282		if (this instanceof XmppConnectionService.OnAccountUpdate) {
 283			this.xmppConnectionService.setOnAccountListChangedListener((XmppConnectionService.OnAccountUpdate) this);
 284		}
 285		if (this instanceof XmppConnectionService.OnCaptchaRequested) {
 286			this.xmppConnectionService.setOnCaptchaRequestedListener((XmppConnectionService.OnCaptchaRequested) this);
 287		}
 288		if (this instanceof XmppConnectionService.OnRosterUpdate) {
 289			this.xmppConnectionService.setOnRosterUpdateListener((XmppConnectionService.OnRosterUpdate) this);
 290		}
 291		if (this instanceof XmppConnectionService.OnMucRosterUpdate) {
 292			this.xmppConnectionService.setOnMucRosterUpdateListener((XmppConnectionService.OnMucRosterUpdate) this);
 293		}
 294		if (this instanceof OnUpdateBlocklist) {
 295			this.xmppConnectionService.setOnUpdateBlocklistListener((OnUpdateBlocklist) this);
 296		}
 297		if (this instanceof XmppConnectionService.OnShowErrorToast) {
 298			this.xmppConnectionService.setOnShowErrorToastListener((XmppConnectionService.OnShowErrorToast) this);
 299		}
 300		if (this instanceof OnKeyStatusUpdated) {
 301			this.xmppConnectionService.setOnKeyStatusUpdatedListener((OnKeyStatusUpdated) this);
 302		}
 303	}
 304
 305	protected void unregisterListeners() {
 306		if (this instanceof XmppConnectionService.OnConversationUpdate) {
 307			this.xmppConnectionService.removeOnConversationListChangedListener();
 308		}
 309		if (this instanceof XmppConnectionService.OnAccountUpdate) {
 310			this.xmppConnectionService.removeOnAccountListChangedListener();
 311		}
 312		if (this instanceof XmppConnectionService.OnCaptchaRequested) {
 313			this.xmppConnectionService.removeOnCaptchaRequestedListener();
 314		}
 315		if (this instanceof XmppConnectionService.OnRosterUpdate) {
 316			this.xmppConnectionService.removeOnRosterUpdateListener();
 317		}
 318		if (this instanceof XmppConnectionService.OnMucRosterUpdate) {
 319			this.xmppConnectionService.removeOnMucRosterUpdateListener();
 320		}
 321		if (this instanceof OnUpdateBlocklist) {
 322			this.xmppConnectionService.removeOnUpdateBlocklistListener();
 323		}
 324		if (this instanceof XmppConnectionService.OnShowErrorToast) {
 325			this.xmppConnectionService.removeOnShowErrorToastListener();
 326		}
 327		if (this instanceof OnKeyStatusUpdated) {
 328			this.xmppConnectionService.removeOnNewKeysAvailableListener();
 329		}
 330	}
 331
 332	@Override
 333	public boolean onOptionsItemSelected(final MenuItem item) {
 334		switch (item.getItemId()) {
 335			case R.id.action_settings:
 336				startActivity(new Intent(this, SettingsActivity.class));
 337				break;
 338			case R.id.action_accounts:
 339				startActivity(new Intent(this, ManageAccountActivity.class));
 340				break;
 341			case android.R.id.home:
 342				finish();
 343				break;
 344			case R.id.action_show_qr_code:
 345				showQrCode();
 346				break;
 347		}
 348		return super.onOptionsItemSelected(item);
 349	}
 350
 351	@Override
 352	protected void onCreate(Bundle savedInstanceState) {
 353		super.onCreate(savedInstanceState);
 354		metrics = getResources().getDisplayMetrics();
 355		ExceptionHelper.init(getApplicationContext());
 356		mPrimaryTextColor = getResources().getColor(R.color.black87);
 357		mSecondaryTextColor = getResources().getColor(R.color.black54);
 358		mTertiaryTextColor = getResources().getColor(R.color.black12);
 359		mColorRed = getResources().getColor(R.color.red800);
 360		mColorOrange = getResources().getColor(R.color.orange500);
 361		mColorGreen = getResources().getColor(R.color.green500);
 362		mPrimaryColor = getResources().getColor(R.color.primary);
 363		mPrimaryBackgroundColor = getResources().getColor(R.color.grey50);
 364		mSecondaryBackgroundColor = getResources().getColor(R.color.grey200);
 365		this.mTheme = findTheme();
 366		setTheme(this.mTheme);
 367		this.mUsingEnterKey = usingEnterKey();
 368		mUseSubject = getPreferences().getBoolean("use_subject", true);
 369		final ActionBar ab = getActionBar();
 370		if (ab!=null) {
 371			ab.setDisplayHomeAsUpEnabled(true);
 372		}
 373	}
 374
 375	protected boolean usingEnterKey() {
 376		return getPreferences().getBoolean("display_enter_key", false);
 377	}
 378
 379	protected SharedPreferences getPreferences() {
 380		return PreferenceManager
 381			.getDefaultSharedPreferences(getApplicationContext());
 382	}
 383
 384	public boolean useSubjectToIdentifyConference() {
 385		return mUseSubject;
 386	}
 387
 388	public void switchToConversation(Conversation conversation) {
 389		switchToConversation(conversation, null, false);
 390	}
 391
 392	public void switchToConversation(Conversation conversation, String text,
 393			boolean newTask) {
 394		switchToConversation(conversation,text,null,false,newTask);
 395	}
 396
 397	public void highlightInMuc(Conversation conversation, String nick) {
 398		switchToConversation(conversation, null, nick, false, false);
 399	}
 400
 401	public void privateMsgInMuc(Conversation conversation, String nick) {
 402		switchToConversation(conversation, null, nick, true, false);
 403	}
 404
 405	private void switchToConversation(Conversation conversation, String text, String nick, boolean pm, boolean newTask) {
 406		Intent viewConversationIntent = new Intent(this,
 407				ConversationActivity.class);
 408		viewConversationIntent.setAction(Intent.ACTION_VIEW);
 409		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
 410				conversation.getUuid());
 411		if (text != null) {
 412			viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
 413		}
 414		if (nick != null) {
 415			viewConversationIntent.putExtra(ConversationActivity.NICK, nick);
 416			viewConversationIntent.putExtra(ConversationActivity.PRIVATE_MESSAGE,pm);
 417		}
 418		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
 419		if (newTask) {
 420			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
 421					| Intent.FLAG_ACTIVITY_NEW_TASK
 422					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
 423		} else {
 424			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
 425					| Intent.FLAG_ACTIVITY_CLEAR_TOP);
 426		}
 427		startActivity(viewConversationIntent);
 428		finish();
 429	}
 430
 431	public void switchToContactDetails(Contact contact) {
 432		switchToContactDetails(contact, null);
 433	}
 434
 435	public void switchToContactDetails(Contact contact, String messageFingerprint) {
 436		Intent intent = new Intent(this, ContactDetailsActivity.class);
 437		intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
 438		intent.putExtra("account", contact.getAccount().getJid().toBareJid().toString());
 439		intent.putExtra("contact", contact.getJid().toString());
 440		intent.putExtra("fingerprint", messageFingerprint);
 441		startActivity(intent);
 442	}
 443
 444	public void switchToAccount(Account account) {
 445		switchToAccount(account, false);
 446	}
 447
 448	public void switchToAccount(Account account, boolean init) {
 449		Intent intent = new Intent(this, EditAccountActivity.class);
 450		intent.putExtra("jid", account.getJid().toBareJid().toString());
 451		intent.putExtra("init", init);
 452		startActivity(intent);
 453	}
 454
 455	protected void inviteToConversation(Conversation conversation) {
 456		Intent intent = new Intent(getApplicationContext(),
 457				ChooseContactActivity.class);
 458		List<String> contacts = new ArrayList<>();
 459		if (conversation.getMode() == Conversation.MODE_MULTI) {
 460			for (MucOptions.User user : conversation.getMucOptions().getUsers()) {
 461				Jid jid = user.getJid();
 462				if (jid != null) {
 463					contacts.add(jid.toBareJid().toString());
 464				}
 465			}
 466		} else {
 467			contacts.add(conversation.getJid().toBareJid().toString());
 468		}
 469		intent.putExtra("filter_contacts", contacts.toArray(new String[contacts.size()]));
 470		intent.putExtra("conversation", conversation.getUuid());
 471		intent.putExtra("multiple", true);
 472		startActivityForResult(intent, REQUEST_INVITE_TO_CONVERSATION);
 473	}
 474
 475	protected void announcePgp(Account account, final Conversation conversation) {
 476		if (account.getPgpId() == -1) {
 477			choosePgpSignId(account);
 478		} else {
 479			xmppConnectionService.getPgpEngine().generateSignature(account, "", new UiCallback<Account>() {
 480
 481				@Override
 482				public void userInputRequried(PendingIntent pi,
 483											  Account account) {
 484					try {
 485						startIntentSenderForResult(pi.getIntentSender(),
 486								REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
 487					} catch (final SendIntentException ignored) {
 488					}
 489				}
 490
 491				@Override
 492				public void success(Account account) {
 493					xmppConnectionService.databaseBackend.updateAccount(account);
 494					xmppConnectionService.sendPresence(account);
 495					if (conversation != null) {
 496						conversation.setNextEncryption(Message.ENCRYPTION_PGP);
 497						xmppConnectionService.databaseBackend.updateConversation(conversation);
 498					}
 499				}
 500
 501				@Override
 502				public void error(int error, Account account) {
 503					displayErrorDialog(error);
 504				}
 505			});
 506		}
 507	}
 508
 509	protected void choosePgpSignId(Account account) {
 510		xmppConnectionService.getPgpEngine().chooseKey(account, new UiCallback<Account>() {
 511			@Override
 512			public void success(Account account1) {
 513			}
 514
 515			@Override
 516			public void error(int errorCode, Account object) {
 517
 518			}
 519
 520			@Override
 521			public void userInputRequried(PendingIntent pi, Account object) {
 522				try {
 523					startIntentSenderForResult(pi.getIntentSender(),
 524							REQUEST_CHOOSE_PGP_ID, null, 0, 0, 0);
 525				} catch (final SendIntentException ignored) {
 526				}
 527			}
 528		});
 529	}
 530
 531	protected void displayErrorDialog(final int errorCode) {
 532		runOnUiThread(new Runnable() {
 533
 534			@Override
 535			public void run() {
 536				AlertDialog.Builder builder = new AlertDialog.Builder(
 537						XmppActivity.this);
 538				builder.setIconAttribute(android.R.attr.alertDialogIcon);
 539				builder.setTitle(getString(R.string.error));
 540				builder.setMessage(errorCode);
 541				builder.setNeutralButton(R.string.accept, null);
 542				builder.create().show();
 543			}
 544		});
 545
 546	}
 547
 548	protected void showAddToRosterDialog(final Conversation conversation) {
 549		showAddToRosterDialog(conversation.getContact());
 550	}
 551
 552	protected void showAddToRosterDialog(final Contact contact) {
 553		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 554		builder.setTitle(contact.getJid().toString());
 555		builder.setMessage(getString(R.string.not_in_roster));
 556		builder.setNegativeButton(getString(R.string.cancel), null);
 557		builder.setPositiveButton(getString(R.string.add_contact),
 558				new DialogInterface.OnClickListener() {
 559
 560					@Override
 561					public void onClick(DialogInterface dialog, int which) {
 562						final Jid jid = contact.getJid();
 563						Account account = contact.getAccount();
 564						Contact contact = account.getRoster().getContact(jid);
 565						xmppConnectionService.createContact(contact);
 566					}
 567				});
 568		builder.create().show();
 569	}
 570
 571	private void showAskForPresenceDialog(final Contact contact) {
 572		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 573		builder.setTitle(contact.getJid().toString());
 574		builder.setMessage(R.string.request_presence_updates);
 575		builder.setNegativeButton(R.string.cancel, null);
 576		builder.setPositiveButton(R.string.request_now,
 577				new DialogInterface.OnClickListener() {
 578
 579					@Override
 580					public void onClick(DialogInterface dialog, int which) {
 581						if (xmppConnectionServiceBound) {
 582							xmppConnectionService.sendPresencePacket(contact
 583									.getAccount(), xmppConnectionService
 584									.getPresenceGenerator()
 585									.requestPresenceUpdatesFrom(contact));
 586						}
 587					}
 588				});
 589		builder.create().show();
 590	}
 591
 592	private void warnMutalPresenceSubscription(final Conversation conversation,
 593			final OnPresenceSelected listener) {
 594		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 595		builder.setTitle(conversation.getContact().getJid().toString());
 596		builder.setMessage(R.string.without_mutual_presence_updates);
 597		builder.setNegativeButton(R.string.cancel, null);
 598		builder.setPositiveButton(R.string.ignore, new OnClickListener() {
 599
 600			@Override
 601			public void onClick(DialogInterface dialog, int which) {
 602				conversation.setNextCounterpart(null);
 603				if (listener != null) {
 604					listener.onPresenceSelected();
 605				}
 606			}
 607		});
 608		builder.create().show();
 609	}
 610
 611	protected void quickEdit(String previousValue, OnValueEdited callback) {
 612		quickEdit(previousValue, callback, false);
 613	}
 614
 615	protected void quickPasswordEdit(String previousValue,
 616			OnValueEdited callback) {
 617		quickEdit(previousValue, callback, true);
 618	}
 619
 620	@SuppressLint("InflateParams")
 621	private void quickEdit(final String previousValue,
 622			final OnValueEdited callback, boolean password) {
 623		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 624		View view = getLayoutInflater().inflate(R.layout.quickedit, null);
 625		final EditText editor = (EditText) view.findViewById(R.id.editor);
 626		OnClickListener mClickListener = new OnClickListener() {
 627
 628			@Override
 629			public void onClick(DialogInterface dialog, int which) {
 630				String value = editor.getText().toString();
 631				if (!previousValue.equals(value) && value.trim().length() > 0) {
 632					callback.onValueEdited(value);
 633				}
 634			}
 635		};
 636		if (password) {
 637			editor.setInputType(InputType.TYPE_CLASS_TEXT
 638					| InputType.TYPE_TEXT_VARIATION_PASSWORD);
 639			editor.setHint(R.string.password);
 640			builder.setPositiveButton(R.string.accept, mClickListener);
 641		} else {
 642			builder.setPositiveButton(R.string.edit, mClickListener);
 643		}
 644		editor.requestFocus();
 645		editor.setText(previousValue);
 646		builder.setView(view);
 647		builder.setNegativeButton(R.string.cancel, null);
 648		builder.create().show();
 649	}
 650
 651	protected boolean addFingerprintRow(LinearLayout keys, final Account account, final String fingerprint, boolean highlight) {
 652		final XmppAxolotlSession.Trust trust = account.getAxolotlService()
 653				.getFingerprintTrust(fingerprint);
 654		if (trust == null) {
 655			return false;
 656		}
 657		return addFingerprintRowWithListeners(keys, account, fingerprint, highlight, trust, true,
 658				new CompoundButton.OnCheckedChangeListener() {
 659					@Override
 660					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 661						account.getAxolotlService().setFingerprintTrust(fingerprint,
 662								(isChecked) ? XmppAxolotlSession.Trust.TRUSTED :
 663										XmppAxolotlSession.Trust.UNTRUSTED);
 664					}
 665				},
 666				new View.OnClickListener() {
 667					@Override
 668					public void onClick(View v) {
 669						account.getAxolotlService().setFingerprintTrust(fingerprint,
 670								XmppAxolotlSession.Trust.UNTRUSTED);
 671						v.setEnabled(true);
 672					}
 673				}
 674
 675		);
 676	}
 677
 678	protected boolean addFingerprintRowWithListeners(LinearLayout keys, final Account account,
 679	                                                 final String fingerprint,
 680	                                                 boolean highlight,
 681	                                                 XmppAxolotlSession.Trust trust,
 682	                                                 boolean showTag,
 683	                                                 CompoundButton.OnCheckedChangeListener
 684			                                                 onCheckedChangeListener,
 685	                                                 View.OnClickListener onClickListener) {
 686		if (trust == XmppAxolotlSession.Trust.COMPROMISED) {
 687			return false;
 688		}
 689		View view = getLayoutInflater().inflate(R.layout.contact_key, keys, false);
 690		TextView key = (TextView) view.findViewById(R.id.key);
 691		TextView keyType = (TextView) view.findViewById(R.id.key_type);
 692		Switch trustToggle = (Switch) view.findViewById(R.id.tgl_trust);
 693		trustToggle.setVisibility(View.VISIBLE);
 694		trustToggle.setOnCheckedChangeListener(onCheckedChangeListener);
 695		trustToggle.setOnClickListener(onClickListener);
 696		view.setOnLongClickListener(new View.OnLongClickListener() {
 697			@Override
 698			public boolean onLongClick(View v) {
 699				showPurgeKeyDialog(account, fingerprint);
 700				return true;
 701			}
 702		});
 703		boolean x509 = trust == XmppAxolotlSession.Trust.TRUSTED_X509 || trust == XmppAxolotlSession.Trust.INACTIVE_TRUSTED_X509;
 704		switch (trust) {
 705			case UNTRUSTED:
 706			case TRUSTED:
 707			case TRUSTED_X509:
 708				trustToggle.setChecked(trust.trusted(), false);
 709				trustToggle.setEnabled(trust != XmppAxolotlSession.Trust.TRUSTED_X509);
 710				if (trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
 711					trustToggle.setOnClickListener(null);
 712				}
 713				key.setTextColor(getPrimaryTextColor());
 714				keyType.setTextColor(getSecondaryTextColor());
 715				break;
 716			case UNDECIDED:
 717				trustToggle.setChecked(false, false);
 718				trustToggle.setEnabled(false);
 719				key.setTextColor(getPrimaryTextColor());
 720				keyType.setTextColor(getSecondaryTextColor());
 721				break;
 722			case INACTIVE_UNTRUSTED:
 723			case INACTIVE_UNDECIDED:
 724				trustToggle.setOnClickListener(null);
 725				trustToggle.setChecked(false, false);
 726				trustToggle.setEnabled(false);
 727				key.setTextColor(getTertiaryTextColor());
 728				keyType.setTextColor(getTertiaryTextColor());
 729				break;
 730			case INACTIVE_TRUSTED:
 731			case INACTIVE_TRUSTED_X509:
 732				trustToggle.setOnClickListener(null);
 733				trustToggle.setChecked(true, false);
 734				trustToggle.setEnabled(false);
 735				key.setTextColor(getTertiaryTextColor());
 736				keyType.setTextColor(getTertiaryTextColor());
 737				break;
 738		}
 739
 740		if (showTag) {
 741			keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
 742		} else {
 743			keyType.setVisibility(View.GONE);
 744		}
 745		if (highlight) {
 746			keyType.setTextColor(getResources().getColor(R.color.accent));
 747			keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509_selected_message : R.string.omemo_fingerprint_selected_message));
 748		} else {
 749			keyType.setText(getString(x509 ? R.string.omemo_fingerprint_x509 : R.string.omemo_fingerprint));
 750		}
 751
 752		key.setText(CryptoHelper.prettifyFingerprint(fingerprint));
 753		keys.addView(view);
 754		return true;
 755	}
 756
 757	public void showPurgeKeyDialog(final Account account, final String fingerprint) {
 758		Builder builder = new Builder(this);
 759		builder.setTitle(getString(R.string.purge_key));
 760		builder.setIconAttribute(android.R.attr.alertDialogIcon);
 761		builder.setMessage(getString(R.string.purge_key_desc_part1)
 762				+ "\n\n" + CryptoHelper.prettifyFingerprint(fingerprint)
 763				+ "\n\n" + getString(R.string.purge_key_desc_part2));
 764		builder.setNegativeButton(getString(R.string.cancel), null);
 765		builder.setPositiveButton(getString(R.string.accept),
 766				new DialogInterface.OnClickListener() {
 767					@Override
 768					public void onClick(DialogInterface dialog, int which) {
 769						account.getAxolotlService().purgeKey(fingerprint);
 770						refreshUi();
 771					}
 772				});
 773		builder.create().show();
 774	}
 775
 776	public void selectPresence(final Conversation conversation,
 777			final OnPresenceSelected listener) {
 778		final Contact contact = conversation.getContact();
 779		if (conversation.hasValidOtrSession()) {
 780			SessionID id = conversation.getOtrSession().getSessionID();
 781			Jid jid;
 782			try {
 783				jid = Jid.fromString(id.getAccountID() + "/" + id.getUserID());
 784			} catch (InvalidJidException e) {
 785				jid = null;
 786			}
 787			conversation.setNextCounterpart(jid);
 788			listener.onPresenceSelected();
 789		} else 	if (!contact.showInRoster()) {
 790			showAddToRosterDialog(conversation);
 791		} else {
 792			Presences presences = contact.getPresences();
 793			if (presences.size() == 0) {
 794				if (!contact.getOption(Contact.Options.TO)
 795						&& !contact.getOption(Contact.Options.ASKING)
 796						&& contact.getAccount().getStatus() == Account.State.ONLINE) {
 797					showAskForPresenceDialog(contact);
 798				} else if (!contact.getOption(Contact.Options.TO)
 799						|| !contact.getOption(Contact.Options.FROM)) {
 800					warnMutalPresenceSubscription(conversation, listener);
 801				} else {
 802					conversation.setNextCounterpart(null);
 803					listener.onPresenceSelected();
 804				}
 805			} else if (presences.size() == 1) {
 806				String presence = presences.asStringArray()[0];
 807				try {
 808					conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence));
 809				} catch (InvalidJidException e) {
 810					conversation.setNextCounterpart(null);
 811				}
 812				listener.onPresenceSelected();
 813			} else {
 814				final StringBuilder presence = new StringBuilder();
 815				AlertDialog.Builder builder = new AlertDialog.Builder(this);
 816				builder.setTitle(getString(R.string.choose_presence));
 817				final String[] presencesArray = presences.asStringArray();
 818				int preselectedPresence = 0;
 819				for (int i = 0; i < presencesArray.length; ++i) {
 820					if (presencesArray[i].equals(contact.lastseen.presence)) {
 821						preselectedPresence = i;
 822						break;
 823					}
 824				}
 825				presence.append(presencesArray[preselectedPresence]);
 826				builder.setSingleChoiceItems(presencesArray,
 827						preselectedPresence,
 828						new DialogInterface.OnClickListener() {
 829
 830							@Override
 831							public void onClick(DialogInterface dialog,
 832									int which) {
 833								presence.delete(0, presence.length());
 834								presence.append(presencesArray[which]);
 835							}
 836						});
 837				builder.setNegativeButton(R.string.cancel, null);
 838				builder.setPositiveButton(R.string.ok, new OnClickListener() {
 839
 840					@Override
 841					public void onClick(DialogInterface dialog, int which) {
 842						try {
 843							conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence.toString()));
 844						} catch (InvalidJidException e) {
 845							conversation.setNextCounterpart(null);
 846						}
 847						listener.onPresenceSelected();
 848					}
 849				});
 850				builder.create().show();
 851			}
 852		}
 853	}
 854
 855	protected void onActivityResult(int requestCode, int resultCode,
 856			final Intent data) {
 857		super.onActivityResult(requestCode, resultCode, data);
 858		if (requestCode == REQUEST_INVITE_TO_CONVERSATION && resultCode == RESULT_OK) {
 859			mPendingConferenceInvite = ConferenceInvite.parse(data);
 860			if (xmppConnectionServiceBound && mPendingConferenceInvite != null) {
 861				mPendingConferenceInvite.execute(this);
 862				mPendingConferenceInvite = null;
 863			}
 864		}
 865	}
 866
 867	private UiCallback<Conversation> adhocCallback = new UiCallback<Conversation>() {
 868		@Override
 869		public void success(final Conversation conversation) {
 870			switchToConversation(conversation);
 871			runOnUiThread(new Runnable() {
 872				@Override
 873				public void run() {
 874					Toast.makeText(XmppActivity.this,R.string.conference_created,Toast.LENGTH_LONG).show();
 875				}
 876			});
 877		}
 878
 879		@Override
 880		public void error(final int errorCode, Conversation object) {
 881			runOnUiThread(new Runnable() {
 882				@Override
 883				public void run() {
 884					Toast.makeText(XmppActivity.this,errorCode,Toast.LENGTH_LONG).show();
 885				}
 886			});
 887		}
 888
 889		@Override
 890		public void userInputRequried(PendingIntent pi, Conversation object) {
 891
 892		}
 893	};
 894
 895	public int getTertiaryTextColor() {
 896		return this.mTertiaryTextColor;
 897	}
 898
 899	public int getSecondaryTextColor() {
 900		return this.mSecondaryTextColor;
 901	}
 902
 903	public int getPrimaryTextColor() {
 904		return this.mPrimaryTextColor;
 905	}
 906
 907	public int getWarningTextColor() {
 908		return this.mColorRed;
 909	}
 910
 911	public int getOnlineColor() {
 912		return this.mColorGreen;
 913	}
 914
 915	public int getPrimaryBackgroundColor() {
 916		return this.mPrimaryBackgroundColor;
 917	}
 918
 919	public int getSecondaryBackgroundColor() {
 920		return this.mSecondaryBackgroundColor;
 921	}
 922
 923	public int getPixel(int dp) {
 924		DisplayMetrics metrics = getResources().getDisplayMetrics();
 925		return ((int) (dp * metrics.density));
 926	}
 927
 928	public boolean copyTextToClipboard(String text, int labelResId) {
 929		ClipboardManager mClipBoardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
 930		String label = getResources().getString(labelResId);
 931		if (mClipBoardManager != null) {
 932			ClipData mClipData = ClipData.newPlainText(label, text);
 933			mClipBoardManager.setPrimaryClip(mClipData);
 934			return true;
 935		}
 936		return false;
 937	}
 938
 939	protected void registerNdefPushMessageCallback() {
 940		NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
 941		if (nfcAdapter != null && nfcAdapter.isEnabled()) {
 942			nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
 943				@Override
 944				public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
 945					return new NdefMessage(new NdefRecord[]{
 946						NdefRecord.createUri(getShareableUri()),
 947							NdefRecord.createApplicationRecord("eu.siacs.conversations")
 948					});
 949				}
 950			}, this);
 951		}
 952	}
 953
 954	protected void unregisterNdefPushMessageCallback() {
 955		NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
 956		if (nfcAdapter != null && nfcAdapter.isEnabled()) {
 957			nfcAdapter.setNdefPushMessageCallback(null,this);
 958		}
 959	}
 960
 961	protected String getShareableUri() {
 962		return null;
 963	}
 964
 965	@Override
 966	public void onResume() {
 967		super.onResume();
 968		if (this.getShareableUri()!=null) {
 969			this.registerNdefPushMessageCallback();
 970		}
 971	}
 972
 973	protected int findTheme() {
 974		if (getPreferences().getBoolean("use_larger_font", false)) {
 975			return R.style.ConversationsTheme_LargerText;
 976		} else {
 977			return R.style.ConversationsTheme;
 978		}
 979	}
 980
 981	@Override
 982	public void onPause() {
 983		super.onPause();
 984		this.unregisterNdefPushMessageCallback();
 985	}
 986
 987	protected void showQrCode() {
 988		String uri = getShareableUri();
 989		if (uri!=null) {
 990			Point size = new Point();
 991			getWindowManager().getDefaultDisplay().getSize(size);
 992			final int width = (size.x < size.y ? size.x : size.y);
 993			Bitmap bitmap = createQrCodeBitmap(uri, width);
 994			ImageView view = new ImageView(this);
 995			view.setImageBitmap(bitmap);
 996			AlertDialog.Builder builder = new AlertDialog.Builder(this);
 997			builder.setView(view);
 998			builder.create().show();
 999		}
1000	}
1001
1002	protected Bitmap createQrCodeBitmap(String input, int size) {
1003		Log.d(Config.LOGTAG,"qr code requested size: "+size);
1004		try {
1005			final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
1006			final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
1007			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
1008			final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
1009			final int width = result.getWidth();
1010			final int height = result.getHeight();
1011			final int[] pixels = new int[width * height];
1012			for (int y = 0; y < height; y++) {
1013				final int offset = y * width;
1014				for (int x = 0; x < width; x++) {
1015					pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
1016				}
1017			}
1018			final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
1019			Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
1020			bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
1021			return bitmap;
1022		} catch (final WriterException e) {
1023			return null;
1024		}
1025	}
1026
1027	public static class ConferenceInvite {
1028		private String uuid;
1029		private List<Jid> jids = new ArrayList<>();
1030
1031		public static ConferenceInvite parse(Intent data) {
1032			ConferenceInvite invite = new ConferenceInvite();
1033			invite.uuid = data.getStringExtra("conversation");
1034			if (invite.uuid == null) {
1035				return null;
1036			}
1037			try {
1038				if (data.getBooleanExtra("multiple", false)) {
1039					String[] toAdd = data.getStringArrayExtra("contacts");
1040					for (String item : toAdd) {
1041						invite.jids.add(Jid.fromString(item));
1042					}
1043				} else {
1044					invite.jids.add(Jid.fromString(data.getStringExtra("contact")));
1045				}
1046			} catch (final InvalidJidException ignored) {
1047				return null;
1048			}
1049			return invite;
1050		}
1051
1052		public void execute(XmppActivity activity) {
1053			XmppConnectionService service = activity.xmppConnectionService;
1054			Conversation conversation = service.findConversationByUuid(this.uuid);
1055			if (conversation == null) {
1056				return;
1057			}
1058			if (conversation.getMode() == Conversation.MODE_MULTI) {
1059				for (Jid jid : jids) {
1060					service.invite(conversation, jid);
1061				}
1062			} else {
1063				jids.add(conversation.getJid().toBareJid());
1064				service.createAdhocConference(conversation.getAccount(), jids, activity.adhocCallback);
1065			}
1066		}
1067	}
1068
1069	public AvatarService avatarService() {
1070		return xmppConnectionService.getAvatarService();
1071	}
1072
1073	class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
1074		private final WeakReference<ImageView> imageViewReference;
1075		private Message message = null;
1076
1077		public BitmapWorkerTask(ImageView imageView) {
1078			imageViewReference = new WeakReference<>(imageView);
1079		}
1080
1081		@Override
1082		protected Bitmap doInBackground(Message... params) {
1083			message = params[0];
1084			try {
1085				return xmppConnectionService.getFileBackend().getThumbnail(
1086						message, (int) (metrics.density * 288), false);
1087			} catch (FileNotFoundException e) {
1088				return null;
1089			}
1090		}
1091
1092		@Override
1093		protected void onPostExecute(Bitmap bitmap) {
1094			if (bitmap != null) {
1095				final ImageView imageView = imageViewReference.get();
1096				if (imageView != null) {
1097					imageView.setImageBitmap(bitmap);
1098					imageView.setBackgroundColor(0x00000000);
1099				}
1100			}
1101		}
1102	}
1103
1104	public void loadBitmap(Message message, ImageView imageView) {
1105		Bitmap bm;
1106		try {
1107			bm = xmppConnectionService.getFileBackend().getThumbnail(message,
1108					(int) (metrics.density * 288), true);
1109		} catch (FileNotFoundException e) {
1110			bm = null;
1111		}
1112		if (bm != null) {
1113			imageView.setImageBitmap(bm);
1114			imageView.setBackgroundColor(0x00000000);
1115		} else {
1116			if (cancelPotentialWork(message, imageView)) {
1117				imageView.setBackgroundColor(0xff333333);
1118				final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
1119				final AsyncDrawable asyncDrawable = new AsyncDrawable(
1120						getResources(), null, task);
1121				imageView.setImageDrawable(asyncDrawable);
1122				try {
1123					task.execute(message);
1124				} catch (final RejectedExecutionException ignored) {
1125				}
1126			}
1127		}
1128	}
1129
1130	public static boolean cancelPotentialWork(Message message,
1131			ImageView imageView) {
1132		final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
1133
1134		if (bitmapWorkerTask != null) {
1135			final Message oldMessage = bitmapWorkerTask.message;
1136			if (oldMessage == null || message != oldMessage) {
1137				bitmapWorkerTask.cancel(true);
1138			} else {
1139				return false;
1140			}
1141		}
1142		return true;
1143	}
1144
1145	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
1146		if (imageView != null) {
1147			final Drawable drawable = imageView.getDrawable();
1148			if (drawable instanceof AsyncDrawable) {
1149				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
1150				return asyncDrawable.getBitmapWorkerTask();
1151			}
1152		}
1153		return null;
1154	}
1155
1156	static class AsyncDrawable extends BitmapDrawable {
1157		private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
1158
1159		public AsyncDrawable(Resources res, Bitmap bitmap,
1160				BitmapWorkerTask bitmapWorkerTask) {
1161			super(res, bitmap);
1162			bitmapWorkerTaskReference = new WeakReference<>(
1163					bitmapWorkerTask);
1164		}
1165
1166		public BitmapWorkerTask getBitmapWorkerTask() {
1167			return bitmapWorkerTaskReference.get();
1168		}
1169	}
1170}