remove NFC + light refactoring of XmppActivity

This commit is contained in:
Daniel Gultsch 2017-12-15 16:14:04 +01:00
parent df37e34e02
commit 8d339b6521
2 changed files with 232 additions and 332 deletions

View File

@ -12,7 +12,6 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission <uses-permission

View File

@ -7,7 +7,6 @@ import android.app.ActionBar;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.ClipData; import android.content.ClipData;
@ -15,7 +14,6 @@ import android.content.ClipboardManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender.SendIntentException; import android.content.IntentSender.SendIntentException;
import android.content.ServiceConnection; import android.content.ServiceConnection;
@ -31,10 +29,6 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Uri; 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.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -88,13 +82,11 @@ import eu.siacs.conversations.xmpp.jid.Jid;
public abstract class XmppActivity extends Activity { public abstract class XmppActivity extends Activity {
public static final String EXTRA_ACCOUNT = "account";
protected static final int REQUEST_ANNOUNCE_PGP = 0x0101; protected static final int REQUEST_ANNOUNCE_PGP = 0x0101;
protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102; protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102;
protected static final int REQUEST_CHOOSE_PGP_ID = 0x0103; protected static final int REQUEST_CHOOSE_PGP_ID = 0x0103;
protected static final int REQUEST_BATTERY_OP = 0x13849ff; protected static final int REQUEST_BATTERY_OP = 0x13849ff;
public static final String EXTRA_ACCOUNT = "account";
public XmppConnectionService xmppConnectionService; public XmppConnectionService xmppConnectionService;
public boolean xmppConnectionServiceBound = false; public boolean xmppConnectionServiceBound = false;
protected boolean registeredListeners = false; protected boolean registeredListeners = false;
@ -110,71 +102,11 @@ public abstract class XmppActivity extends Activity {
protected int mPrimaryColor; protected int mPrimaryColor;
protected boolean mUseSubject = true; protected boolean mUseSubject = true;
private DisplayMetrics metrics;
protected int mTheme; protected int mTheme;
protected boolean mUsingEnterKey = false; protected boolean mUsingEnterKey = false;
protected Toast mToast; protected Toast mToast;
protected Runnable onOpenPGPKeyPublished = () -> Toast.makeText(XmppActivity.this, R.string.openpgp_has_been_published, Toast.LENGTH_SHORT).show();
protected void hideToast() {
if (mToast != null) {
mToast.cancel();
}
}
protected void replaceToast(String msg) {
replaceToast(msg, true);
}
protected void replaceToast(String msg, boolean showlong) {
hideToast();
mToast = Toast.makeText(this, msg ,showlong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
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 ConferenceInvite mPendingConferenceInvite = null;
protected final void refreshUi() {
final long diff = SystemClock.elapsedRealtime() - mLastUiRefresh;
if (diff > Config.REFRESH_UI_INTERVAL) {
mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
runOnUiThread(mRefreshUiRunnable);
} else {
final long next = Config.REFRESH_UI_INTERVAL - diff;
mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
mRefreshUiHandler.postDelayed(mRefreshUiRunnable,next);
}
}
abstract protected void refreshUiReal();
protected interface OnValueEdited {
String onValueEdited(String value);
}
public interface OnPresenceSelected {
void onPresenceSelected();
}
protected ServiceConnection mConnection = new ServiceConnection() { protected ServiceConnection mConnection = new ServiceConnection() {
@Override @Override
@ -194,6 +126,87 @@ public abstract class XmppActivity extends Activity {
xmppConnectionServiceBound = false; 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() {
if (mToast != null) {
mToast.cancel();
}
}
protected void replaceToast(String msg) {
replaceToast(msg, true);
}
protected void replaceToast(String msg, boolean showlong) {
hideToast();
mToast = Toast.makeText(this, msg, showlong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
mToast.show();
}
protected final void refreshUi() {
final long diff = SystemClock.elapsedRealtime() - mLastUiRefresh;
if (diff > Config.REFRESH_UI_INTERVAL) {
mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
runOnUiThread(mRefreshUiRunnable);
} else {
final long next = Config.REFRESH_UI_INTERVAL - diff;
mRefreshUiHandler.removeCallbacks(mRefreshUiRunnable);
mRefreshUiHandler.postDelayed(mRefreshUiRunnable, next);
}
}
abstract protected void refreshUiReal();
@Override @Override
protected void onStart() { protected void onStart() {
@ -211,7 +224,7 @@ public abstract class XmppActivity extends Activity {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
protected boolean shouldRegisterListeners() { protected boolean shouldRegisterListeners() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return !isDestroyed() && !isFinishing(); return !isDestroyed() && !isFinishing();
} else { } else {
return !isFinishing(); return !isFinishing();
@ -239,14 +252,10 @@ public abstract class XmppActivity extends Activity {
} }
protected void hideKeyboard() { protected void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View focus = getCurrentFocus(); View focus = getCurrentFocus();
if (focus != null && inputManager != null) {
if (focus != null) { inputManager.hideSoftInputFromWindow(focus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} }
} }
@ -261,42 +270,34 @@ public abstract class XmppActivity extends Activity {
builder.setMessage(getText(R.string.openkeychain_required_long)); builder.setMessage(getText(R.string.openkeychain_required_long));
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.setNeutralButton(getString(R.string.restart), builder.setNeutralButton(getString(R.string.restart),
new OnClickListener() { (dialog, which) -> {
if (xmppConnectionServiceBound) {
@Override unbindService(mConnection);
public void onClick(DialogInterface dialog, int which) { xmppConnectionServiceBound = false;
if (xmppConnectionServiceBound) {
unbindService(mConnection);
xmppConnectionServiceBound = false;
}
stopService(new Intent(XmppActivity.this,
XmppConnectionService.class));
finish();
} }
stopService(new Intent(XmppActivity.this,
XmppConnectionService.class));
finish();
}); });
builder.setPositiveButton(getString(R.string.install), builder.setPositiveButton(getString(R.string.install),
new OnClickListener() { (dialog, which) -> {
Uri uri = Uri
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri
.parse("market://details?id=org.sufficientlysecure.keychain"); .parse("market://details?id=org.sufficientlysecure.keychain");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Intent marketIntent = new Intent(Intent.ACTION_VIEW,
uri); uri);
PackageManager manager = getApplicationContext() PackageManager manager = getApplicationContext()
.getPackageManager(); .getPackageManager();
List<ResolveInfo> infos = manager List<ResolveInfo> infos = manager
.queryIntentActivities(marketIntent, 0); .queryIntentActivities(marketIntent, 0);
if (infos.size() > 0) { if (infos.size() > 0) {
startActivity(marketIntent); startActivity(marketIntent);
} else { } else {
uri = Uri.parse("http://www.openkeychain.org/"); uri = Uri.parse("http://www.openkeychain.org/");
Intent browserIntent = new Intent( Intent browserIntent = new Intent(
Intent.ACTION_VIEW, uri); Intent.ACTION_VIEW, uri);
startActivity(browserIntent); startActivity(browserIntent);
}
finish();
} }
finish();
}); });
builder.create().show(); builder.create().show();
} }
@ -393,7 +394,7 @@ public abstract class XmppActivity extends Activity {
mSecondaryBackgroundColor = ContextCompat.getColor(this, R.color.grey200); mSecondaryBackgroundColor = ContextCompat.getColor(this, R.color.grey200);
this.mTheme = findTheme(); this.mTheme = findTheme();
if(isDarkTheme()) { if (isDarkTheme()) {
mPrimaryTextColor = ContextCompat.getColor(this, R.color.white); mPrimaryTextColor = ContextCompat.getColor(this, R.color.white);
mSecondaryTextColor = ContextCompat.getColor(this, R.color.white70); mSecondaryTextColor = ContextCompat.getColor(this, R.color.white70);
mTertiaryTextColor = ContextCompat.getColor(this, R.color.white12); mTertiaryTextColor = ContextCompat.getColor(this, R.color.white12);
@ -405,7 +406,7 @@ public abstract class XmppActivity extends Activity {
this.mUsingEnterKey = usingEnterKey(); this.mUsingEnterKey = usingEnterKey();
mUseSubject = getPreferences().getBoolean("use_subject", getResources().getBoolean(R.bool.use_subject)); mUseSubject = getPreferences().getBoolean("use_subject", getResources().getBoolean(R.bool.use_subject));
final ActionBar ab = getActionBar(); final ActionBar ab = getActionBar();
if (ab!=null) { if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true); ab.setDisplayHomeAsUpEnabled(true);
} }
} }
@ -415,7 +416,7 @@ public abstract class XmppActivity extends Activity {
} }
public int getThemeResource(int r_attr_name, int r_drawable_def) { public int getThemeResource(int r_attr_name, int r_drawable_def) {
int[] attrs = { r_attr_name }; int[] attrs = {r_attr_name};
TypedArray ta = this.getTheme().obtainStyledAttributes(attrs); TypedArray ta = this.getTheme().obtainStyledAttributes(attrs);
int res = ta.getResourceId(0, r_drawable_def); int res = ta.getResourceId(0, r_drawable_def);
@ -427,7 +428,7 @@ public abstract class XmppActivity extends Activity {
protected boolean isOptimizingBattery() { protected boolean isOptimizingBattery() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); final PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
return pm == null return pm != null
&& !pm.isIgnoringBatteryOptimizations(getPackageName()); && !pm.isIgnoringBatteryOptimizations(getPackageName());
} else { } else {
return false; return false;
@ -451,7 +452,7 @@ public abstract class XmppActivity extends Activity {
protected SharedPreferences getPreferences() { protected SharedPreferences getPreferences() {
return PreferenceManager return PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); .getDefaultSharedPreferences(getApplicationContext());
} }
public boolean useSubjectToIdentifyConference() { public boolean useSubjectToIdentifyConference() {
@ -463,8 +464,8 @@ public abstract class XmppActivity extends Activity {
} }
public void switchToConversation(Conversation conversation, String text, public void switchToConversation(Conversation conversation, String text,
boolean newTask) { boolean newTask) {
switchToConversation(conversation,text,null,false,newTask); switchToConversation(conversation, text, null, false, newTask);
} }
public void highlightInMuc(Conversation conversation, String nick) { public void highlightInMuc(Conversation conversation, String nick) {
@ -486,7 +487,7 @@ public abstract class XmppActivity extends Activity {
} }
if (nick != null) { if (nick != null) {
viewConversationIntent.putExtra(ConversationActivity.NICK, nick); viewConversationIntent.putExtra(ConversationActivity.NICK, nick);
viewConversationIntent.putExtra(ConversationActivity.PRIVATE_MESSAGE,pm); viewConversationIntent.putExtra(ConversationActivity.PRIVATE_MESSAGE, pm);
} }
if (newTask) { if (newTask) {
viewConversationIntent.setFlags(viewConversationIntent.getFlags() viewConversationIntent.setFlags(viewConversationIntent.getFlags()
@ -596,11 +597,11 @@ public abstract class XmppActivity extends Activity {
} }
} }
protected boolean noAccountUsesPgp() { protected boolean noAccountUsesPgp() {
if (!hasPgp()) { if (!hasPgp()) {
return true; return true;
} }
for(Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
if (account.getPgpId() != 0) { if (account.getPgpId() != 0) {
return false; return false;
} }
@ -642,18 +643,13 @@ public abstract class XmppActivity extends Activity {
} }
protected void displayErrorDialog(final int errorCode) { protected void displayErrorDialog(final int errorCode) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
Builder builder = new Builder(XmppActivity.this);
@Override builder.setIconAttribute(android.R.attr.alertDialogIcon);
public void run() { builder.setTitle(getString(R.string.error));
AlertDialog.Builder builder = new AlertDialog.Builder( builder.setMessage(errorCode);
XmppActivity.this); builder.setNeutralButton(R.string.accept, null);
builder.setIconAttribute(android.R.attr.alertDialogIcon); builder.create().show();
builder.setTitle(getString(R.string.error));
builder.setMessage(errorCode);
builder.setNeutralButton(R.string.accept, null);
builder.create().show();
}
}); });
} }
@ -668,15 +664,11 @@ public abstract class XmppActivity extends Activity {
builder.setMessage(getString(R.string.not_in_roster)); builder.setMessage(getString(R.string.not_in_roster));
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.add_contact), builder.setPositiveButton(getString(R.string.add_contact),
new DialogInterface.OnClickListener() { (dialog, which) -> {
final Jid jid = contact.getJid();
@Override Account account = contact.getAccount();
public void onClick(DialogInterface dialog, int which) { Contact contact1 = account.getRoster().getContact(jid);
final Jid jid = contact.getJid(); xmppConnectionService.createContact(contact1);
Account account = contact.getAccount();
Contact contact = account.getRoster().getContact(jid);
xmppConnectionService.createContact(contact);
}
}); });
builder.create().show(); builder.create().show();
} }
@ -687,35 +679,27 @@ public abstract class XmppActivity extends Activity {
builder.setMessage(R.string.request_presence_updates); builder.setMessage(R.string.request_presence_updates);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.request_now, builder.setPositiveButton(R.string.request_now,
new DialogInterface.OnClickListener() { (dialog, which) -> {
if (xmppConnectionServiceBound) {
@Override xmppConnectionService.sendPresencePacket(contact
public void onClick(DialogInterface dialog, int which) { .getAccount(), xmppConnectionService
if (xmppConnectionServiceBound) { .getPresenceGenerator()
xmppConnectionService.sendPresencePacket(contact .requestPresenceUpdatesFrom(contact));
.getAccount(), xmppConnectionService
.getPresenceGenerator()
.requestPresenceUpdatesFrom(contact));
}
} }
}); });
builder.create().show(); builder.create().show();
} }
private void warnMutalPresenceSubscription(final Conversation conversation, private void warnMutalPresenceSubscription(final Conversation conversation,
final OnPresenceSelected listener) { final OnPresenceSelected listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(conversation.getContact().getJid().toString()); builder.setTitle(conversation.getContact().getJid().toString());
builder.setMessage(R.string.without_mutual_presence_updates); builder.setMessage(R.string.without_mutual_presence_updates);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ignore, new OnClickListener() { builder.setPositiveButton(R.string.ignore, (dialog, which) -> {
conversation.setNextCounterpart(null);
@Override if (listener != null) {
public void onClick(DialogInterface dialog, int which) { listener.onPresenceSelected();
conversation.setNextCounterpart(null);
if (listener != null) {
listener.onPresenceSelected();
}
} }
}); });
builder.create().show(); builder.create().show();
@ -731,16 +715,16 @@ public abstract class XmppActivity extends Activity {
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
private void quickEdit(final String previousValue, private void quickEdit(final String previousValue,
final OnValueEdited callback, final OnValueEdited callback,
final int hint, final int hint,
boolean password) { boolean password) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.quickedit, null); View view = getLayoutInflater().inflate(R.layout.quickedit, null);
final EditText editor = view.findViewById(R.id.editor); final EditText editor = view.findViewById(R.id.editor);
if (password) { if (password) {
editor.setInputType(InputType.TYPE_CLASS_TEXT editor.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD); | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setPositiveButton(R.string.accept,null); builder.setPositiveButton(R.string.accept, null);
} else { } else {
builder.setPositiveButton(R.string.edit, null); builder.setPositiveButton(R.string.edit, null);
} }
@ -756,20 +740,16 @@ public abstract class XmppActivity extends Activity {
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
final AlertDialog dialog = builder.create(); final AlertDialog dialog = builder.create();
dialog.show(); dialog.show();
View.OnClickListener clickListener = new View.OnClickListener() { View.OnClickListener clickListener = v -> {
String value = editor.getText().toString();
@Override if (!value.equals(previousValue) && value.trim().length() > 0) {
public void onClick(View v) { String error = callback.onValueEdited(value);
String value = editor.getText().toString(); if (error != null) {
if (!value.equals(previousValue) && value.trim().length() > 0) { editor.setError(error);
String error = callback.onValueEdited(value); return;
if (error != null) {
editor.setError(error);
return;
}
} }
dialog.dismiss();
} }
dialog.dismiss();
}; };
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener); dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(clickListener);
} }
@ -788,7 +768,7 @@ public abstract class XmppActivity extends Activity {
} }
public void selectPresence(final Conversation conversation, public void selectPresence(final Conversation conversation,
final OnPresenceSelected listener) { final OnPresenceSelected listener) {
final Contact contact = conversation.getContact(); final Contact contact = conversation.getContact();
if (conversation.hasValidOtrSession()) { if (conversation.hasValidOtrSession()) {
SessionID id = conversation.getOtrSession().getSessionID(); SessionID id = conversation.getOtrSession().getSessionID();
@ -800,7 +780,7 @@ public abstract class XmppActivity extends Activity {
} }
conversation.setNextCounterpart(jid); conversation.setNextCounterpart(jid);
listener.onPresenceSelected(); listener.onPresenceSelected();
} else if (!contact.showInRoster()) { } else if (!contact.showInRoster()) {
showAddToRosterDialog(conversation); showAddToRosterDialog(conversation);
} else { } else {
final Presences presences = contact.getPresences(); final Presences presences = contact.getPresences();
@ -819,13 +799,13 @@ public abstract class XmppActivity extends Activity {
} else if (presences.size() == 1) { } else if (presences.size() == 1) {
String presence = presences.toResourceArray()[0]; String presence = presences.toResourceArray()[0];
try { try {
conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence)); conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
} catch (InvalidJidException e) { } catch (InvalidJidException e) {
conversation.setNextCounterpart(null); conversation.setNextCounterpart(null);
} }
listener.onPresenceSelected(); listener.onPresenceSelected();
} else { } else {
showPresenceSelectionDialog(presences,conversation,listener); showPresenceSelectionDialog(presences, conversation, listener);
} }
} }
} }
@ -836,8 +816,8 @@ public abstract class XmppActivity extends Activity {
builder.setTitle(getString(R.string.choose_presence)); builder.setTitle(getString(R.string.choose_presence));
final String[] resourceArray = presences.toResourceArray(); final String[] resourceArray = presences.toResourceArray();
Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap(); Pair<Map<String, String>, Map<String, String>> typeAndName = presences.toTypeAndNameMap();
final Map<String,String> resourceTypeMap = typeAndName.first; final Map<String, String> resourceTypeMap = typeAndName.first;
final Map<String,String> resourceNameMap = typeAndName.second; final Map<String, String> resourceNameMap = typeAndName.second;
final String[] readableIdentities = new String[resourceArray.length]; final String[] readableIdentities = new String[resourceArray.length];
final AtomicInteger selectedResource = new AtomicInteger(0); final AtomicInteger selectedResource = new AtomicInteger(0);
for (int i = 0; i < resourceArray.length; ++i) { for (int i = 0; i < resourceArray.length; ++i) {
@ -848,17 +828,17 @@ public abstract class XmppActivity extends Activity {
String type = resourceTypeMap.get(resource); String type = resourceTypeMap.get(resource);
String name = resourceNameMap.get(resource); String name = resourceNameMap.get(resource);
if (type != null) { if (type != null) {
if (Collections.frequency(resourceTypeMap.values(),type) == 1) { if (Collections.frequency(resourceTypeMap.values(), type) == 1) {
readableIdentities[i] = UIHelper.tranlasteType(this,type); readableIdentities[i] = UIHelper.tranlasteType(this, type);
} else if (name != null) { } else if (name != null) {
if (Collections.frequency(resourceNameMap.values(), name) == 1 if (Collections.frequency(resourceNameMap.values(), name) == 1
|| CryptoHelper.UUID_PATTERN.matcher(resource).matches()) { || CryptoHelper.UUID_PATTERN.matcher(resource).matches()) {
readableIdentities[i] = UIHelper.tranlasteType(this,type) + " (" + name+")"; readableIdentities[i] = UIHelper.tranlasteType(this, type) + " (" + name + ")";
} else { } else {
readableIdentities[i] = UIHelper.tranlasteType(this,type) + " (" + name +" / " + resource+")"; readableIdentities[i] = UIHelper.tranlasteType(this, type) + " (" + name + " / " + resource + ")";
} }
} else { } else {
readableIdentities[i] = UIHelper.tranlasteType(this,type) + " (" + resource+")"; readableIdentities[i] = UIHelper.tranlasteType(this, type) + " (" + resource + ")";
} }
} else { } else {
readableIdentities[i] = resource; readableIdentities[i] = resource;
@ -866,26 +846,16 @@ public abstract class XmppActivity extends Activity {
} }
builder.setSingleChoiceItems(readableIdentities, builder.setSingleChoiceItems(readableIdentities,
selectedResource.get(), selectedResource.get(),
new DialogInterface.OnClickListener() { (dialog, which) -> selectedResource.set(which));
@Override
public void onClick(DialogInterface dialog, int which) {
selectedResource.set(which);
}
});
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ok, new OnClickListener() { builder.setPositiveButton(R.string.ok, (dialog, which) -> {
try {
@Override Jid next = Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), resourceArray[selectedResource.get()]);
public void onClick(DialogInterface dialog, int which) { conversation.setNextCounterpart(next);
try { } catch (InvalidJidException e) {
Jid next = Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),resourceArray[selectedResource.get()]); conversation.setNextCounterpart(null);
conversation.setNextCounterpart(next);
} catch (InvalidJidException e) {
conversation.setNextCounterpart(null);
}
listener.onPresenceSelected();
} }
listener.onPresenceSelected();
}); });
builder.create().show(); builder.create().show();
} }
@ -904,35 +874,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() { public int getTertiaryTextColor() {
return this.mTertiaryTextColor; return this.mTertiaryTextColor;
} }
@ -977,21 +918,6 @@ public abstract class XmppActivity extends Activity {
return false; 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("eu.siacs.conversations")
});
}
}, this);
}
}
protected boolean neverCompressPictures() { protected boolean neverCompressPictures() {
return getPreferences().getString("picture_compression", getResources().getString(R.string.picture_compression)).equals("never"); return getPreferences().getString("picture_compression", getResources().getString(R.string.picture_compression)).equals("never");
} }
@ -1000,13 +926,6 @@ public abstract class XmppActivity extends Activity {
return getPreferences().getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, getResources().getBoolean(R.bool.manually_change_presence)); 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() { protected String getShareableUri() {
return getShareableUri(false); return getShareableUri(false);
} }
@ -1022,7 +941,7 @@ public abstract class XmppActivity extends Activity {
} }
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,getShareableUri(http)); intent.putExtra(Intent.EXTRA_TEXT, getShareableUri(http));
try { try {
startActivity(Intent.createChooser(intent, getText(R.string.share_uri_with))); startActivity(Intent.createChooser(intent, getText(R.string.share_uri_with)));
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
@ -1037,24 +956,21 @@ public abstract class XmppActivity extends Activity {
pgp.getIntentForKey(keyId).getIntentSender(), 0, null, 0, pgp.getIntentForKey(keyId).getIntentSender(), 0, null, 0,
0, 0); 0, 0);
} catch (Throwable e) { } catch (Throwable e) {
Toast.makeText(XmppActivity.this,R.string.openpgp_error,Toast.LENGTH_SHORT).show(); Toast.makeText(XmppActivity.this, R.string.openpgp_error, Toast.LENGTH_SHORT).show();
} }
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (this.getShareableUri() != null) {
this.registerNdefPushMessageCallback();
}
} }
protected int findTheme() { protected int findTheme() {
Boolean dark = getPreferences().getString(SettingsActivity.THEME, getResources().getString(R.string.theme)).equals("dark"); Boolean dark = getPreferences().getString(SettingsActivity.THEME, getResources().getString(R.string.theme)).equals("dark");
Boolean larger = getPreferences().getBoolean("use_larger_font", getResources().getBoolean(R.bool.use_larger_font)); Boolean larger = getPreferences().getBoolean("use_larger_font", getResources().getBoolean(R.bool.use_larger_font));
if(dark) { if (dark) {
if(larger) if (larger)
return R.style.ConversationsTheme_Dark_LargerText; return R.style.ConversationsTheme_Dark_LargerText;
else else
return R.style.ConversationsTheme_Dark; return R.style.ConversationsTheme_Dark;
@ -1069,7 +985,6 @@ public abstract class XmppActivity extends Activity {
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
this.unregisterNdefPushMessageCallback();
} }
protected void showQrCode() { protected void showQrCode() {
@ -1098,6 +1013,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 { public static class ConferenceInvite {
private String uuid; private String uuid;
private List<Jid> jids = new ArrayList<>(); private List<Jid> jids = new ArrayList<>();
@ -1141,16 +1096,14 @@ public abstract class XmppActivity extends Activity {
} }
} }
public AvatarService avatarService() { static class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
return xmppConnectionService.getAvatarService();
}
class BitmapWorkerTask extends AsyncTask<Message, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
private final WeakReference<XmppActivity> activity;
private Message message = null; private Message message = null;
public BitmapWorkerTask(ImageView imageView) { private BitmapWorkerTask(XmppActivity activity, ImageView imageView) {
imageViewReference = new WeakReference<>(imageView); this.activity = new WeakReference<>(activity);
this.imageViewReference = new WeakReference<>(imageView);
} }
@Override @Override
@ -1160,8 +1113,12 @@ public abstract class XmppActivity extends Activity {
} }
message = params[0]; message = params[0];
try { try {
return xmppConnectionService.getFileBackend().getThumbnail( XmppActivity activity = this.activity.get();
message, (int) (metrics.density * 288), false); if (activity != null && activity.xmppConnectionService != null) {
return activity.xmppConnectionService.getFileBackend().getThumbnail(message, (int) (activity.metrics.density * 288), false);
} else {
return null;
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return null; return null;
} }
@ -1179,71 +1136,15 @@ public abstract class XmppActivity extends Activity {
} }
} }
public void loadBitmap(Message message, ImageView imageView) { private static class AsyncDrawable extends BitmapDrawable {
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.execute(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 final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, private AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap); super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>( bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
bitmapWorkerTask);
} }
public BitmapWorkerTask getBitmapWorkerTask() { private BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get(); return bitmapWorkerTaskReference.get();
} }
} }