diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-12-23 22:17:59 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-12-23 22:26:05 +0100 |
commit | b2fade624678549c61dd69dd4ddd64bace0f91da (patch) | |
tree | 67880018169ca98431e4c72346e5215382167a4f /src/main/java/de/pixart | |
parent | f87067131339803abac51b2c6b2bc5007be7542a (diff) |
remove NFC + light refactoring of XmppActivity
Diffstat (limited to 'src/main/java/de/pixart')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/XmppActivity.java | 477 |
1 files changed, 194 insertions, 283 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index d12d3aa01..7aca6a708 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -14,7 +14,6 @@ import android.content.ClipboardManager; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.IntentSender.SendIntentException; import android.content.ServiceConnection; @@ -30,10 +29,6 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.ConnectivityManager; import android.net.Uri; -import android.nfc.NdefMessage; -import android.nfc.NdefRecord; -import android.nfc.NfcAdapter; -import android.nfc.NfcEvent; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; @@ -115,13 +110,84 @@ public abstract class XmppActivity extends Activity { protected boolean mUseSubject = true; - private DisplayMetrics metrics; protected int mTheme; protected boolean mUsingEnterKey = false; protected Toast mToast; + protected Runnable onOpenPGPKeyPublished = () -> Toast.makeText(XmppActivity.this, R.string.openpgp_has_been_published, Toast.LENGTH_SHORT).show(); + protected ConferenceInvite mPendingConferenceInvite = null; + protected ServiceConnection mConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + XmppConnectionBinder binder = (XmppConnectionBinder) service; + xmppConnectionService = binder.getService(); + xmppConnectionServiceBound = true; + if (!registeredListeners && shouldRegisterListeners()) { + registerListeners(); + registeredListeners = true; + } + onBackendConnected(); + } + + @Override + public void onServiceDisconnected(ComponentName arg0) { + xmppConnectionServiceBound = false; + } + }; + private DisplayMetrics metrics; + private long mLastUiRefresh = 0; + private Handler mRefreshUiHandler = new Handler(); + private Runnable mRefreshUiRunnable = () -> { + mLastUiRefresh = SystemClock.elapsedRealtime(); + refreshUiReal(); + }; + private UiCallback<Conversation> adhocCallback = new UiCallback<Conversation>() { + @Override + public void success(final Conversation conversation) { + runOnUiThread(() -> { + switchToConversation(conversation); + hideToast(); + }); + } + + @Override + public void error(final int errorCode, Conversation object) { + runOnUiThread(() -> replaceToast(getString(errorCode))); + } + + @Override + public void userInputRequried(PendingIntent pi, Conversation object) { + + } + }; + + public static boolean cancelPotentialWork(Message message, ImageView imageView) { + final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); + + if (bitmapWorkerTask != null) { + final Message oldMessage = bitmapWorkerTask.message; + if (oldMessage == null || message != oldMessage) { + bitmapWorkerTask.cancel(true); + } else { + return false; + } + } + return true; + } + + private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) { + if (imageView != null) { + final Drawable drawable = imageView.getDrawable(); + if (drawable instanceof AsyncDrawable) { + final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; + return asyncDrawable.getBitmapWorkerTask(); + } + } + return null; + } - protected void hideToast() { + protected void hideToast() { if (mToast != null) { mToast.cancel(); } @@ -137,26 +203,6 @@ public abstract class XmppActivity extends Activity { mToast.show(); } - protected Runnable onOpenPGPKeyPublished = new Runnable() { - @Override - public void run() { - Toast.makeText(XmppActivity.this, R.string.openpgp_has_been_published, Toast.LENGTH_SHORT).show(); - } - }; - - private long mLastUiRefresh = 0; - private Handler mRefreshUiHandler = new Handler(); - private Runnable mRefreshUiRunnable = new Runnable() { - @Override - public void run() { - mLastUiRefresh = SystemClock.elapsedRealtime(); - refreshUiReal(); - } - }; - - protected ConferenceInvite mPendingConferenceInvite = null; - - protected final void refreshUi() { final long diff = SystemClock.elapsedRealtime() - mLastUiRefresh; if (diff > Config.REFRESH_UI_INTERVAL) { @@ -171,34 +217,6 @@ public abstract class XmppActivity extends Activity { abstract protected void refreshUiReal(); - protected interface OnValueEdited { - String onValueEdited(String value); - } - - public interface OnPresenceSelected { - void onPresenceSelected(); - } - - protected ServiceConnection mConnection = new ServiceConnection() { - - @Override - public void onServiceConnected(ComponentName className, IBinder service) { - XmppConnectionBinder binder = (XmppConnectionBinder) service; - xmppConnectionService = binder.getService(); - xmppConnectionServiceBound = true; - if (!registeredListeners && shouldRegisterListeners()) { - registerListeners(); - registeredListeners = true; - } - onBackendConnected(); - } - - @Override - public void onServiceDisconnected(ComponentName arg0) { - xmppConnectionServiceBound = false; - } - }; - @Override protected void onStart() { super.onStart(); @@ -243,14 +261,12 @@ public abstract class XmppActivity extends Activity { } protected void hideKeyboard() { - InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); View focus = getCurrentFocus(); - if (focus != null) { - - inputManager.hideSoftInputFromWindow(focus.getWindowToken(), - InputMethodManager.HIDE_NOT_ALWAYS); + if (focus != null && inputManager != null) { + inputManager.hideSoftInputFromWindow(focus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } @@ -265,42 +281,34 @@ public abstract class XmppActivity extends Activity { builder.setMessage(getText(R.string.openkeychain_required_long)); builder.setNegativeButton(getString(R.string.cancel), null); builder.setNeutralButton(getString(R.string.restart), - new OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (xmppConnectionServiceBound) { - unbindService(mConnection); - xmppConnectionServiceBound = false; - } - stopService(new Intent(XmppActivity.this, - XmppConnectionService.class)); - finish(); + (dialog, which) -> { + if (xmppConnectionServiceBound) { + unbindService(mConnection); + xmppConnectionServiceBound = false; } + stopService(new Intent(XmppActivity.this, + XmppConnectionService.class)); + finish(); }); builder.setPositiveButton(getString(R.string.install), - new OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - Uri uri = Uri - .parse("market://details?id=org.sufficientlysecure.keychain"); - Intent marketIntent = new Intent(Intent.ACTION_VIEW, - uri); - PackageManager manager = getApplicationContext() - .getPackageManager(); - List<ResolveInfo> infos = manager - .queryIntentActivities(marketIntent, 0); - if (infos.size() > 0) { - startActivity(marketIntent); - } else { - uri = Uri.parse("http://www.openkeychain.org/"); - Intent browserIntent = new Intent( - Intent.ACTION_VIEW, uri); - startActivity(browserIntent); - } - finish(); + (dialog, which) -> { + Uri uri = Uri + .parse("market://details?id=org.sufficientlysecure.keychain"); + Intent marketIntent = new Intent(Intent.ACTION_VIEW, + uri); + PackageManager manager = getApplicationContext() + .getPackageManager(); + List<ResolveInfo> infos = manager + .queryIntentActivities(marketIntent, 0); + if (infos.size() > 0) { + startActivity(marketIntent); + } else { + uri = Uri.parse("http://www.openkeychain.org/"); + Intent browserIntent = new Intent( + Intent.ACTION_VIEW, uri); + startActivity(browserIntent); } + finish(); }); builder.create().show(); } @@ -649,18 +657,14 @@ public abstract class XmppActivity extends Activity { } protected void displayErrorDialog(final int errorCode) { - runOnUiThread(new Runnable() { - - @Override - public void run() { - AlertDialog.Builder builder = new AlertDialog.Builder( - XmppActivity.this); - builder.setIconAttribute(android.R.attr.alertDialogIcon); - builder.setTitle(getString(R.string.error)); - builder.setMessage(errorCode); - builder.setNeutralButton(R.string.accept, null); - builder.create().show(); - } + runOnUiThread(() -> { + Builder builder = new Builder( + XmppActivity.this); + builder.setIconAttribute(android.R.attr.alertDialogIcon); + builder.setTitle(getString(R.string.error)); + builder.setMessage(errorCode); + builder.setNeutralButton(R.string.accept, null); + builder.create().show(); }); } @@ -675,15 +679,11 @@ public abstract class XmppActivity extends Activity { builder.setMessage(getString(R.string.not_in_roster)); builder.setNegativeButton(getString(R.string.cancel), null); builder.setPositiveButton(getString(R.string.add_contact), - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - final Jid jid = contact.getJid(); - Account account = contact.getAccount(); - Contact contact = account.getRoster().getContact(jid); - xmppConnectionService.createContact(contact); - } + (dialog, which) -> { + final Jid jid = contact.getJid(); + Account account = contact.getAccount(); + Contact contact1 = account.getRoster().getContact(jid); + xmppConnectionService.createContact(contact1); }); builder.create().show(); } @@ -694,16 +694,12 @@ public abstract class XmppActivity extends Activity { builder.setMessage(R.string.request_presence_updates); builder.setNegativeButton(R.string.cancel, null); builder.setPositiveButton(R.string.request_now, - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (xmppConnectionServiceBound) { - xmppConnectionService.sendPresencePacket(contact - .getAccount(), xmppConnectionService - .getPresenceGenerator() - .requestPresenceUpdatesFrom(contact)); - } + (dialog, which) -> { + if (xmppConnectionServiceBound) { + xmppConnectionService.sendPresencePacket(contact + .getAccount(), xmppConnectionService + .getPresenceGenerator() + .requestPresenceUpdatesFrom(contact)); } }); builder.create().show(); @@ -715,14 +711,10 @@ public abstract class XmppActivity extends Activity { builder.setTitle(conversation.getContact().getJid().toString()); builder.setMessage(R.string.without_mutual_presence_updates); builder.setNegativeButton(R.string.cancel, null); - builder.setPositiveButton(R.string.ignore, new OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - conversation.setNextCounterpart(null); - if (listener != null) { - listener.onPresenceSelected(); - } + builder.setPositiveButton(R.string.ignore, (dialog, which) -> { + conversation.setNextCounterpart(null); + if (listener != null) { + listener.onPresenceSelected(); } }); builder.create().show(); @@ -763,20 +755,16 @@ public abstract class XmppActivity extends Activity { builder.setNegativeButton(R.string.cancel, null); final AlertDialog dialog = builder.create(); dialog.show(); - View.OnClickListener clickListener = new View.OnClickListener() { - - @Override - public void onClick(View v) { - String value = editor.getText().toString(); - if (!value.equals(previousValue) && value.trim().length() > 0) { - String error = callback.onValueEdited(value); - if (error != null) { - editor.setError(error); - return; - } + View.OnClickListener clickListener = v -> { + String value = editor.getText().toString(); + if (!value.equals(previousValue) && value.trim().length() > 0) { + String error = callback.onValueEdited(value); + if (error != null) { + editor.setError(error); + return; } - dialog.dismiss(); } + dialog.dismiss(); }; dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener); } @@ -900,26 +888,16 @@ public abstract class XmppActivity extends Activity { } builder.setSingleChoiceItems(readableIdentities, selectedResource.get(), - new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - selectedResource.set(which); - } - }); + (dialog, which) -> selectedResource.set(which)); builder.setNegativeButton(R.string.cancel, null); - builder.setPositiveButton(R.string.ok, new OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - try { - Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]); - conversation.setNextCounterpart(next); - } catch (InvalidJidException e) { - conversation.setNextCounterpart(null); - } - listener.onPresenceSelected(); + builder.setPositiveButton(R.string.ok, (dialog, which) -> { + try { + Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]); + conversation.setNextCounterpart(next); + } catch (InvalidJidException e) { + conversation.setNextCounterpart(null); } + listener.onPresenceSelected(); }); builder.create().show(); } @@ -938,35 +916,6 @@ public abstract class XmppActivity extends Activity { } } - - private UiCallback<Conversation> adhocCallback = new UiCallback<Conversation>() { - @Override - public void success(final Conversation conversation) { - runOnUiThread(new Runnable() { - @Override - public void run() { - switchToConversation(conversation); - hideToast(); - } - }); - } - - @Override - public void error(final int errorCode, Conversation object) { - runOnUiThread(new Runnable() { - @Override - public void run() { - replaceToast(getString(errorCode)); - } - }); - } - - @Override - public void userInputRequried(PendingIntent pi, Conversation object) { - - } - }; - public int getTertiaryTextColor() { return this.mTertiaryTextColor; } @@ -1015,21 +964,6 @@ public abstract class XmppActivity extends Activity { return false; } - protected void registerNdefPushMessageCallback() { - NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); - if (nfcAdapter != null && nfcAdapter.isEnabled()) { - nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { - @Override - public NdefMessage createNdefMessage(NfcEvent nfcEvent) { - return new NdefMessage(new NdefRecord[]{ - NdefRecord.createUri(getShareableUri()), - NdefRecord.createApplicationRecord("de.pixart.messenger") - }); - } - }, this); - } - } - protected boolean neverCompressPictures() { return getPreferences().getString("picture_compression", getResources().getString(R.string.picture_compression)).equals("never"); } @@ -1038,13 +972,6 @@ public abstract class XmppActivity extends Activity { return getPreferences().getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, getResources().getBoolean(R.bool.manually_change_presence)); } - protected void unregisterNdefPushMessageCallback() { - NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); - if (nfcAdapter != null && nfcAdapter.isEnabled()) { - nfcAdapter.setNdefPushMessageCallback(null, this); - } - } - protected String getShareableUri() { return getShareableUri(false); } @@ -1102,9 +1029,6 @@ public abstract class XmppActivity extends Activity { @Override public void onResume() { super.onResume(); - if (this.getShareableUri() != null) { - this.registerNdefPushMessageCallback(); - } } protected int findTheme() { @@ -1120,7 +1044,6 @@ public abstract class XmppActivity extends Activity { @Override public void onPause() { super.onPause(); - this.unregisterNdefPushMessageCallback(); } protected void showQrCode() { @@ -1149,6 +1072,46 @@ public abstract class XmppActivity extends Activity { } } + public AvatarService avatarService() { + return xmppConnectionService.getAvatarService(); + } + + public void loadBitmap(Message message, ImageView imageView) { + Bitmap bm; + try { + bm = xmppConnectionService.getFileBackend().getThumbnail(message, (int) (metrics.density * 288), true); + } catch (FileNotFoundException e) { + bm = null; + } + if (bm != null) { + cancelPotentialWork(message, imageView); + imageView.setImageBitmap(bm); + imageView.setBackgroundColor(0x00000000); + } else { + if (cancelPotentialWork(message, imageView)) { + imageView.setBackgroundColor(0xff333333); + imageView.setImageDrawable(null); + final BitmapWorkerTask task = new BitmapWorkerTask(this, imageView); + final AsyncDrawable asyncDrawable = new AsyncDrawable( + getResources(), null, task); + imageView.setImageDrawable(asyncDrawable); + try { + task.execute(message); + } catch (final RejectedExecutionException ignored) { + ignored.printStackTrace(); + } + } + } + } + + protected interface OnValueEdited { + String onValueEdited(String value); + } + + public interface OnPresenceSelected { + void onPresenceSelected(); + } + public static class ConferenceInvite { private String uuid; private List<Jid> jids = new ArrayList<>(); @@ -1192,16 +1155,14 @@ public abstract class XmppActivity extends Activity { } } - public AvatarService avatarService() { - return xmppConnectionService.getAvatarService(); - } - - class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { + static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; + private final WeakReference<XmppActivity> activity; private Message message = null; - public BitmapWorkerTask(ImageView imageView) { - imageViewReference = new WeakReference<>(imageView); + private BitmapWorkerTask(XmppActivity activity, ImageView imageView) { + this.activity = new WeakReference<>(activity); + this.imageViewReference = new WeakReference<>(imageView); } @Override @@ -1211,8 +1172,12 @@ public abstract class XmppActivity extends Activity { } message = params[0]; try { - return xmppConnectionService.getFileBackend().getThumbnail( - message, (int) (metrics.density * 288), false); + XmppActivity activity = this.activity.get(); + if (activity != null && activity.xmppConnectionService != null) { + return activity.xmppConnectionService.getFileBackend().getThumbnail(message, (int) (activity.metrics.density * 288), false); + } else { + return null; + } } catch (FileNotFoundException e) { return null; } @@ -1230,71 +1195,17 @@ public abstract class XmppActivity extends Activity { } } - public void loadBitmap(Message message, ImageView imageView) { - Bitmap bm; - try { - bm = xmppConnectionService.getFileBackend().getThumbnail(message, - (int) (metrics.density * 288), true); - } catch (FileNotFoundException e) { - bm = null; - } - if (bm != null) { - cancelPotentialWork(message, imageView); - imageView.setImageBitmap(bm); - imageView.setBackgroundColor(0x00000000); - } else { - if (cancelPotentialWork(message, imageView)) { - imageView.setBackgroundColor(0xff333333); - imageView.setImageDrawable(null); - final BitmapWorkerTask task = new BitmapWorkerTask(imageView); - final AsyncDrawable asyncDrawable = new AsyncDrawable( - getResources(), null, task); - imageView.setImageDrawable(asyncDrawable); - try { - task.executeOnExecutor(BitmapWorkerTask.THREAD_POOL_EXECUTOR, message); - } catch (final RejectedExecutionException ignored) { - ignored.printStackTrace(); - } - } - } - } - - public static boolean cancelPotentialWork(Message message, ImageView imageView) { - final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - - if (bitmapWorkerTask != null) { - final Message oldMessage = bitmapWorkerTask.message; - if (oldMessage == null || message != oldMessage) { - bitmapWorkerTask.cancel(true); - } else { - return false; - } - } - return true; - } - - private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) { - if (imageView != null) { - final Drawable drawable = imageView.getDrawable(); - if (drawable instanceof AsyncDrawable) { - final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; - return asyncDrawable.getBitmapWorkerTask(); - } - } - return null; - } - - static class AsyncDrawable extends BitmapDrawable { + private static class AsyncDrawable extends BitmapDrawable { private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; - public AsyncDrawable(Resources res, Bitmap bitmap, - BitmapWorkerTask bitmapWorkerTask) { + AsyncDrawable(Resources res, Bitmap bitmap, + BitmapWorkerTask bitmapWorkerTask) { super(res, bitmap); bitmapWorkerTaskReference = new WeakReference<>( bitmapWorkerTask); } - public BitmapWorkerTask getBitmapWorkerTask() { + BitmapWorkerTask getBitmapWorkerTask() { return bitmapWorkerTaskReference.get(); } } |