diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger')
7 files changed, 116 insertions, 261 deletions
diff --git a/src/main/java/de/pixart/messenger/crypto/PgpEngine.java b/src/main/java/de/pixart/messenger/crypto/PgpEngine.java index 9228c1c84..cc1ee41dc 100644 --- a/src/main/java/de/pixart/messenger/crypto/PgpEngine.java +++ b/src/main/java/de/pixart/messenger/crypto/PgpEngine.java @@ -197,7 +197,7 @@ public class PgpEngine { }); } - public void generateSignature(Intent intent, final Account account, String status, final UiCallback<Account> callback) { + public void generateSignature(Intent intent, final Account account, String status, final UiCallback<String> callback) { if (account.getPgpId() == 0) { return; } @@ -231,19 +231,18 @@ public class PgpEngine { } } } catch (IOException e) { - callback.error(R.string.openpgp_error, account); + callback.error(R.string.openpgp_error, null); return; } - account.setPgpSignature(signatureBuilder.toString()); - callback.success(account); + callback.success(signatureBuilder.toString()); return; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: - callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account); + callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), status); return; case OpenPgpApi.RESULT_CODE_ERROR: OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); if (error != null && "signing subkey not found!".equals(error.getMessage())) { - callback.error(0, account); + callback.error(0, null); } else { logError(account, error); callback.error(R.string.unable_to_connect_to_keychain, null); diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index c0c74d78b..1a6129a49 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -654,6 +654,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { public void insertPresenceTemplate(PresenceTemplate template) { SQLiteDatabase db = this.getWritableDatabase(); + String whereToDelete = PresenceTemplate.MESSAGE + "=?"; + String[] whereToDeleteArgs = {template.getStatusMessage()}; + db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs); + db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null); db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues()); } @@ -668,14 +672,6 @@ public class DatabaseBackend extends SQLiteOpenHelper { return templates; } - public void deletePresenceTemplate(PresenceTemplate template) { - Log.d(Config.LOGTAG, "deleting presence template with uuid " + template.getUuid()); - SQLiteDatabase db = this.getWritableDatabase(); - String where = PresenceTemplate.UUID + "=?"; - String[] whereArgs = {template.getUuid()}; - db.delete(PresenceTemplate.TABELNAME, where, whereArgs); - } - public CopyOnWriteArrayList<Conversation> getConversations(int status) { CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 06311c441..716a39434 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -3833,15 +3833,16 @@ public class XmppConnectionService extends Service { } public void sendPresence(final Account account, final boolean includeIdleTimestamp) { - PresencePacket packet; + Presence.Status status; if (manuallyChangePresence()) { - packet = mPresenceGenerator.selfPresence(account, account.getPresenceStatus()); - String message = account.getPresenceStatusMessage(); - if (message != null && !message.isEmpty()) { - packet.addChild(new Element("status").setContent(message)); - } + status = account.getPresenceStatus(); } else { - packet = mPresenceGenerator.selfPresence(account, getTargetPresence()); + status = getTargetPresence(); + } + PresencePacket packet = mPresenceGenerator.selfPresence(account, status); + String message = account.getPresenceStatusMessage(); + if (message != null && !message.isEmpty()) { + packet.addChild(new Element("status").setContent(message)); } if (mLastActivity > 0 && includeIdleTimestamp) { long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates @@ -4139,29 +4140,15 @@ public class XmppConnectionService extends Service { return pending; } - public void changeStatus(Account account, Presence.Status status, String statusMessage, boolean send) { - if (!statusMessage.isEmpty()) { - databaseBackend.insertPresenceTemplate(new PresenceTemplate(status, statusMessage)); + public void changeStatus(Account account, PresenceTemplate template, String signature) { + if (!template.getStatusMessage().isEmpty()) { + databaseBackend.insertPresenceTemplate(template); } - changeStatusReal(account, status, statusMessage, send); - } - - private void changeStatusReal(Account account, Presence.Status status, String statusMessage, boolean send) { - account.setPresenceStatus(status); - account.setPresenceStatusMessage(statusMessage); + account.setPgpSignature(signature); + account.setPresenceStatus(template.getStatus()); + account.setPresenceStatusMessage(template.getStatusMessage()); databaseBackend.updateAccount(account); - if (account.isEnabled() && send) { - sendPresence(account); - } - } - - public void changeStatus(Presence.Status status, String statusMessage) { - if (!statusMessage.isEmpty()) { - databaseBackend.insertPresenceTemplate(new PresenceTemplate(status, statusMessage)); - } - for (Account account : getAccounts()) { - changeStatusReal(account, status, statusMessage, true); - } + sendPresence(account); } public List<PresenceTemplate> getPresenceTemplates(Account account) { diff --git a/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java index a24dd3691..eddbc44b6 100644 --- a/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java +++ b/src/main/java/de/pixart/messenger/ui/EditAccountActivity.java @@ -1,9 +1,11 @@ package de.pixart.messenger.ui; +import android.app.Activity; import android.app.AlertDialog.Builder; import android.app.PendingIntent; import android.content.ActivityNotFoundException; import android.content.Intent; +import android.content.IntentSender; import android.content.SharedPreferences; import android.databinding.DataBindingUtil; import android.graphics.Bitmap; @@ -54,6 +56,7 @@ import de.pixart.messenger.crypto.axolotl.XmppAxolotlSession; import de.pixart.messenger.databinding.ActivityEditAccountBinding; import de.pixart.messenger.databinding.DialogPresenceBinding; import de.pixart.messenger.entities.Account; +import de.pixart.messenger.entities.Presence; import de.pixart.messenger.entities.PresenceTemplate; import de.pixart.messenger.services.BarcodeProvider; import de.pixart.messenger.services.XmppConnectionService; @@ -61,6 +64,7 @@ import de.pixart.messenger.services.XmppConnectionService.OnAccountUpdate; import de.pixart.messenger.services.XmppConnectionService.OnCaptchaRequested; import de.pixart.messenger.ui.adapter.KnownHostsAdapter; import de.pixart.messenger.ui.adapter.PresenceTemplateAdapter; +import de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.utils.XmppUri; @@ -77,6 +81,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched { private static final int REQUEST_DATA_SAVER = 0xf244; + private static final int REQUEST_CHANGE_STATUS = 0xee11; private TextInputLayout mAccountJidLayout; private EditText mPassword; private TextInputLayout mPasswordLayout; @@ -125,6 +130,8 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat private Account mAccount; private String messageFingerprint; + private final PendingItem<PresenceTemplate> mPendingPresenceTemplate = new PendingItem<>(); + private boolean mFetchingAvatar = false; private final OnClickListener mSaveButtonClickListener = new OnClickListener() { @@ -436,6 +443,14 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) { updateAccountInformation(mAccount == null); } + if (requestCode == REQUEST_CHANGE_STATUS) { + PresenceTemplate template = mPendingPresenceTemplate.pop(); + if (template != null && resultCode == Activity.RESULT_OK) { + generateSignature(data, template); + } else { + Log.d(Config.LOGTAG, "pgp result not ok"); + } + } } @Override @@ -887,21 +902,88 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat boolean manualStatus = sharedPreferences.getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, getResources().getBoolean(R.bool.manually_change_presence)); AlertDialog.Builder builder = new AlertDialog.Builder(this); final DialogPresenceBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_presence, null, false); + String current = mAccount.getPresenceStatusMessage(); + if (current != null && !current.trim().isEmpty()) { + binding.statusMessage.append(current); + } + setAvailabilityRadioButton(mAccount.getPresenceStatus(), binding); binding.show.setVisibility(manualStatus ? View.VISIBLE : View.GONE); List<PresenceTemplate> templates = xmppConnectionService.getPresenceTemplates(mAccount); PresenceTemplateAdapter presenceTemplateAdapter = new PresenceTemplateAdapter(this, R.layout.simple_list_item, templates); binding.statusMessage.setAdapter(presenceTemplateAdapter); binding.statusMessage.setOnItemClickListener((parent, view, position, id) -> { PresenceTemplate template = (PresenceTemplate) parent.getItemAtPosition(position); - Log.d(Config.LOGTAG, "selected: " + template.getStatusMessage()); + setAvailabilityRadioButton(template.getStatus(), binding); }); - builder.setTitle(R.string.change_presence); + builder.setTitle(R.string.edit_status_message_title); builder.setView(binding.getRoot()); builder.setNegativeButton(R.string.cancel, null); - builder.setPositiveButton(R.string.confirm, null); + builder.setPositiveButton(R.string.confirm, (dialog, which) -> { + PresenceTemplate template = new PresenceTemplate(getAvailabilityRadioButton(binding), binding.statusMessage.getText().toString().trim()); + if (mAccount.getPgpId() != 0 && hasPgp()) { + generateSignature(null, template); + } else { + xmppConnectionService.changeStatus(mAccount, template, null); + } + }); builder.create().show(); } + private void generateSignature(Intent intent, PresenceTemplate template) { + xmppConnectionService.getPgpEngine().generateSignature(intent, mAccount, template.getStatusMessage(), new UiCallback<String>() { + @Override + public void success(String signature) { + xmppConnectionService.changeStatus(mAccount, template, signature); + } + + @Override + public void error(int errorCode, String object) { + + } + + @Override + public void userInputRequried(PendingIntent pi, String object) { + mPendingPresenceTemplate.push(template); + try { + startIntentSenderForResult(pi.getIntentSender(), REQUEST_CHANGE_STATUS, null, 0, 0, 0); + } catch (final IntentSender.SendIntentException ignored) { + } + } + }); + } + + private static void setAvailabilityRadioButton(Presence.Status status, DialogPresenceBinding binding) { + if (status == null) { + binding.online.setChecked(true); + return; + } + switch (status) { + case DND: + binding.dnd.setChecked(true); + break; + case XA: + binding.xa.setChecked(true); + break; + case AWAY: + binding.xa.setChecked(true); + break; + default: + binding.online.setChecked(true); + } + } + + private static Presence.Status getAvailabilityRadioButton(DialogPresenceBinding binding) { + if (binding.dnd.isChecked()) { + return Presence.Status.DND; + } else if (binding.xa.isChecked()) { + return Presence.Status.XA; + } else if (binding.away.isChecked()) { + return Presence.Status.AWAY; + } else { + return Presence.Status.ONLINE; + } + } + @Override public void alias(String alias) { if (alias != null) { diff --git a/src/main/java/de/pixart/messenger/ui/SetPresenceActivity.java b/src/main/java/de/pixart/messenger/ui/SetPresenceActivity.java deleted file mode 100644 index 383763402..000000000 --- a/src/main/java/de/pixart/messenger/ui/SetPresenceActivity.java +++ /dev/null @@ -1,207 +0,0 @@ -package de.pixart.messenger.ui; - -import android.content.Intent; -import android.databinding.DataBindingUtil; -import android.os.Bundle; -import android.os.Handler; -import android.util.Pair; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.ImageButton; -import android.widget.TextView; - -import java.util.List; - -import de.pixart.messenger.R; -import de.pixart.messenger.databinding.ActivitySetPresenceBinding; -import de.pixart.messenger.entities.Account; -import de.pixart.messenger.entities.ListItem; -import de.pixart.messenger.entities.Presence; -import de.pixart.messenger.entities.PresenceTemplate; -import de.pixart.messenger.utils.UIHelper; - -public class SetPresenceActivity extends XmppActivity implements View.OnClickListener { - - //data - protected Account mAccount; - private List<PresenceTemplate> mTemplates; - - private ActivitySetPresenceBinding binding; - - private Pair<Integer, Intent> mPostponedActivityResult; - - private Runnable onPresenceChanged = new Runnable() { - @Override - public void run() { - finish(); - } - }; - - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - this.binding = DataBindingUtil.setContentView(this, R.layout.activity_set_presence); - ArrayAdapter adapter = ArrayAdapter.createFromResource(this, - R.array.presence_show_options, - R.layout.simple_list_item); - this.binding.presenceShow.setAdapter(adapter); - this.binding.presenceShow.setSelection(1); - this.binding.changePresence.setOnClickListener(v -> executeChangePresence()); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.change_presence, menu); - return super.onCreateOptionsMenu(menu); - } - - @Override - public boolean onOptionsItemSelected(final MenuItem item) { - if (item.getItemId() == R.id.action_account_details) { - if (mAccount != null) { - switchToAccount(mAccount); - } - return true; - } else { - return super.onOptionsItemSelected(item); - } - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (resultCode == RESULT_OK) { - if (xmppConnectionServiceBound && mAccount != null) { - if (requestCode == REQUEST_ANNOUNCE_PGP) { - announcePgp(mAccount, null, data, onPresenceChanged); - } - this.mPostponedActivityResult = null; - } else { - this.mPostponedActivityResult = new Pair<>(requestCode, data); - } - } - } - - private void executeChangePresence() { - Presence.Status status = getStatusFromSpinner(); - boolean allAccounts = this.binding.allAccounts.isChecked(); - String statusMessage = this.binding.presenceStatusMessage.getText().toString().trim(); - if (allAccounts && noAccountUsesPgp()) { - xmppConnectionService.changeStatus(status, statusMessage); - finish(); - } else if (mAccount != null) { - if (mAccount.getPgpId() == 0 || !hasPgp()) { - xmppConnectionService.changeStatus(mAccount, status, statusMessage, true); - finish(); - } else { - xmppConnectionService.changeStatus(mAccount, status, statusMessage, false); - announcePgp(mAccount, null, null, onPresenceChanged); - } - } - } - - private Presence.Status getStatusFromSpinner() { - switch (this.binding.presenceShow.getSelectedItemPosition()) { - case 0: - return Presence.Status.CHAT; - case 2: - return Presence.Status.AWAY; - case 3: - return Presence.Status.XA; - case 4: - return Presence.Status.DND; - default: - return Presence.Status.ONLINE; - } - } - - private void setStatusInSpinner(Presence.Status status) { - switch (status) { - case AWAY: - this.binding.presenceShow.setSelection(2); - break; - case XA: - this.binding.presenceShow.setSelection(3); - break; - case CHAT: - this.binding.presenceShow.setSelection(0); - break; - case DND: - this.binding.presenceShow.setSelection(4); - break; - default: - this.binding.presenceShow.setSelection(1); - break; - } - } - - @Override - protected void refreshUiReal() { - - } - - @Override - void onBackendConnected() { - mAccount = extractAccount(getIntent()); - if (mAccount != null) { - setStatusInSpinner(mAccount.getPresenceStatus()); - String message = mAccount.getPresenceStatusMessage(); - if (this.binding.presenceStatusMessage.getText().length() == 0 && message != null) { - this.binding.presenceStatusMessage.append(message); - } - mTemplates = xmppConnectionService.getPresenceTemplates(mAccount); - if (this.mPostponedActivityResult != null) { - this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second); - } - boolean e = noAccountUsesPgp(); - this.binding.allAccounts.setEnabled(e); - } - redrawTemplates(); - } - - private void redrawTemplates() { - if (mTemplates == null || mTemplates.size() == 0) { - this.binding.templates.setVisibility(View.GONE); - } else { - this.binding.templates.removeAllViews(); - this.binding.templates.setVisibility(View.VISIBLE); - LayoutInflater inflater = getLayoutInflater(); - for (PresenceTemplate template : mTemplates) { - View templateLayout = inflater.inflate(R.layout.presence_template, this.binding.templates, false); - templateLayout.setTag(template); - setListItemBackgroundOnView(templateLayout); - templateLayout.setOnClickListener(this); - TextView message = templateLayout.findViewById(R.id.presence_status_message); - TextView status = templateLayout.findViewById(R.id.status); - ImageButton button = templateLayout.findViewById(R.id.delete_button); - button.setTag(template); - button.setOnClickListener(this); - ListItem.Tag tag = UIHelper.getTagForStatus(this, template.getStatus()); - status.setText(tag.getName()); - status.setBackgroundColor(tag.getColor()); - message.setText(template.getStatusMessage()); - this.binding.templates.addView(templateLayout); - } - } - } - - @Override - public void onClick(View v) { - PresenceTemplate template = (PresenceTemplate) v.getTag(); - if (template == null) { - return; - } - if (v.getId() == R.id.presence_template) { - setStatusInSpinner(template.getStatus()); - this.binding.presenceStatusMessage.getEditableText().clear(); - this.binding.presenceStatusMessage.getEditableText().append(template.getStatusMessage()); - new Handler().post(() -> this.binding.scrollView.smoothScrollTo(0,0)); - } else if (v.getId() == R.id.delete_button) { - xmppConnectionService.databaseBackend.deletePresenceTemplate(template); - mTemplates.remove(template); - redrawTemplates(); - } - } -} diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index 733642579..54bfaa854 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -425,9 +425,6 @@ public class SettingsActivity extends XmppActivity implements || name.equals(MANUALLY_CHANGE_PRESENCE)) { xmppConnectionService.toggleScreenEventReceiver(); } - if (name.equals(MANUALLY_CHANGE_PRESENCE) && !noAccountUsesPgp()) { - Toast.makeText(this, R.string.republish_pgp_keys, Toast.LENGTH_LONG).show(); - } xmppConnectionService.refreshAllPresences(); } } else if (name.equals("dont_trust_system_cas")) { diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index d38563d04..ba03663f3 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -556,7 +556,7 @@ public abstract class XmppActivity extends AppCompatActivity { startActivityForResult(ChooseContactActivity.create(this,conversation), REQUEST_INVITE_TO_CONVERSATION); } - protected void announcePgp(Account account, final Conversation conversation, Intent intent, final Runnable onSuccess) { + protected void announcePgp(final Account account, final Conversation conversation, Intent intent, final Runnable onSuccess) { if (account.getPgpId() == 0) { choosePgpSignId(account); } else { @@ -567,10 +567,10 @@ public abstract class XmppActivity extends AppCompatActivity { if (status == null) { status = ""; } - xmppConnectionService.getPgpEngine().generateSignature(intent, account, status, new UiCallback<Account>() { + xmppConnectionService.getPgpEngine().generateSignature(intent, account, status, new UiCallback<String>() { @Override - public void userInputRequried(PendingIntent pi, Account account) { + public void userInputRequried(PendingIntent pi, String signature) { try { startIntentSenderForResult(pi.getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0); } catch (final SendIntentException ignored) { @@ -578,7 +578,8 @@ public abstract class XmppActivity extends AppCompatActivity { } @Override - public void success(Account account) { + public void success(String signature) { + account.setPgpSignature(signature); xmppConnectionService.databaseBackend.updateAccount(account); xmppConnectionService.sendPresence(account); if (conversation != null) { @@ -592,8 +593,8 @@ public abstract class XmppActivity extends AppCompatActivity { } @Override - public void error(int error, Account account) { - if (error == 0 && account != null) { + public void error(int error, String signature) { + if (error == 0) { account.setPgpSignId(0); account.unsetPgpSignature(); xmppConnectionService.databaseBackend.updateAccount(account); |