diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java | 187 |
1 files changed, 129 insertions, 58 deletions
diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index d63516bc..42dc01cd 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -3,6 +3,7 @@ package eu.siacs.conversations.ui; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.PendingIntent; +import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -33,6 +34,8 @@ import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; +import android.util.Log; + import java.util.Arrays; import java.util.List; import java.util.Set; @@ -105,6 +108,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate private Jid jidToEdit; private boolean mInitMode = false; + private boolean mUsernameMode = Config.DOMAIN_LOCK != null; private boolean mShowOptions = false; private Account mAccount; private String messageFingerprint; @@ -115,6 +119,13 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate @Override public void onClick(final View v) { + final String password = mPassword.getText().toString(); + final String passwordConfirm = mPasswordConfirm.getText().toString(); + + if (!mInitMode && passwordChangedInMagicCreateMode()) { + gotoChangePassword(password); + return; + } if (mInitMode && mAccount != null) { mAccount.setOption(Account.OPTION_DISABLED, false); } @@ -125,20 +136,20 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate return; } final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI; - if (Config.DOMAIN_LOCK != null && mAccountJid.getText().toString().contains("@")) { + if (mUsernameMode && mAccountJid.getText().toString().contains("@")) { mAccountJid.setError(getString(R.string.invalid_username)); mAccountJid.requestFocus(); return; } final Jid jid; try { - if (Config.DOMAIN_LOCK != null) { - jid = Jid.fromParts(mAccountJid.getText().toString(), Config.DOMAIN_LOCK, null); + if (mUsernameMode) { + jid = Jid.fromParts(mAccountJid.getText().toString(), getUserModeDomain(), null); } else { jid = Jid.fromString(mAccountJid.getText().toString()); } } catch (final InvalidJidException e) { - if (Config.DOMAIN_LOCK != null) { + if (mUsernameMode) { mAccountJid.setError(getString(R.string.invalid_username)); } else { mAccountJid.setError(getString(R.string.invalid_jid)); @@ -172,7 +183,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } if (jid.isDomainJid()) { - if (Config.DOMAIN_LOCK != null) { + if (mUsernameMode) { mAccountJid.setError(getString(R.string.invalid_username)); } else { mAccountJid.setError(getString(R.string.invalid_jid)); @@ -180,8 +191,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate mAccountJid.requestFocus(); return; } - final String password = mPassword.getText().toString(); - final String passwordConfirm = mPasswordConfirm.getText().toString(); if (registerNewAccount) { if (!password.equals(passwordConfirm)) { mPasswordConfirm.setError(getString(R.string.passwords_do_not_match)); @@ -190,6 +199,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } } if (mAccount != null) { + if (mInitMode && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) { + mAccount.setOption(Account.OPTION_MAGIC_CREATE, mAccount.getPassword().contains(password)); + } mAccount.setJid(jid); mAccount.setPort(numericPort); mAccount.setHostname(hostname); @@ -312,9 +324,10 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate public void run() { final Intent intent; final XmppConnection connection = mAccount.getXmppConnection(); + final boolean wasFirstAccount = xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1; if (avatar != null || (connection != null && !connection.getFeatures().pep())) { intent = new Intent(getApplicationContext(), StartConversationActivity.class); - if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) { + if (wasFirstAccount) { intent.putExtra("init", true); } } else { @@ -322,6 +335,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString()); intent.putExtra("setup", true); } + if (wasFirstAccount) { + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + } startActivity(intent); finish(); } @@ -337,9 +353,14 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } protected void updateSaveButton() { - if (accountInfoEdited() && !mInitMode) { + boolean accountInfoEdited = accountInfoEdited(); + + if (!mInitMode && passwordChangedInMagicCreateMode()) { + TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText(), R.string.change_password); + } else if (accountInfoEdited && !mInitMode) { TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText(), R.string.save); - } else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) { + } else if (mAccount != null + && (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL|| mFetchingAvatar)) { TextViewUtil.disable(mSaveButton, ConversationsPlusColors.secondaryText(), R.string.account_status_connecting); } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) { TextViewUtil.enable(mSaveButton, ConversationsPlusColors.primaryText(), R.string.enable); @@ -348,7 +369,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate if (!mInitMode) { if (mAccount != null && mAccount.isOnlineAndConnected()) { this.mSaveButton.setText(R.string.save); - if (!accountInfoEdited()) { + if (!accountInfoEdited) { TextViewUtil.disable(mSaveButton, ConversationsPlusColors.secondaryText()); } } else { @@ -364,16 +385,28 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate if (this.mAccount == null) { return false; } + return jidEdited() || + !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) || + !this.mAccount.getHostname().equals(this.mHostname.getText().toString()) || + !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString()); + } + + protected boolean jidEdited() { final String unmodified; - if (Config.DOMAIN_LOCK != null) { + if (mUsernameMode) { unmodified = this.mAccount.getJid().getLocalpart(); } else { unmodified = this.mAccount.getJid().toBareJid().toString(); } - return !unmodified.equals(this.mAccountJid.getText().toString()) || - !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) || - !this.mAccount.getHostname().equals(this.mHostname.getText().toString()) || - !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString()); + return !unmodified.equals(this.mAccountJid.getText().toString()); + } + + protected boolean passwordChangedInMagicCreateMode() { + return mAccount != null + && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) + && !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) + && !this.jidEdited() + && mAccount.isOnlineAndConnected(); } @Override @@ -392,10 +425,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid); this.mAccountJid.addTextChangedListener(this.mTextWatcher); this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label); - if (Config.DOMAIN_LOCK != null) { - this.mAccountJidLabel.setText(R.string.username); - this.mAccountJid.setHint(R.string.username_hint); - } this.mPassword = (EditText) findViewById(R.id.account_password); this.mPassword.addTextChangedListener(this.mTextWatcher); this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm); @@ -411,7 +440,11 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); Uri uri = Uri.parse("package:"+getPackageName()); intent.setData(uri); + try { startActivityForResult(intent, REQUEST_BATTERY_OP); + } catch (ActivityNotFoundException e) { + Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show(); + } } }); this.mSessionEst = (TextView) findViewById(R.id.session_est); @@ -471,10 +504,11 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list); final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more); final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server); + final MenuItem showPassword = menu.findItem(R.id.action_show_password); final MenuItem clearDevices = menu.findItem(R.id.action_clear_devices); final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate); final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs); - + final MenuItem changePresence = menu.findItem(R.id.action_change_presence); renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null); if (mAccount != null && mAccount.isOnlineAndConnected()) { @@ -489,6 +523,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate if (otherDevices == null || otherDevices.isEmpty()) { clearDevices.setVisible(false); } + changePresence.setVisible(manuallyChangePresence()); } else { showQrCode.setVisible(false); showBlocklist.setVisible(false); @@ -496,6 +531,14 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate changePassword.setVisible(false); clearDevices.setVisible(false); mamPrefs.setVisible(false); + changePresence.setVisible(false); + } + + if (mAccount != null) { + showPassword.setVisible(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) + && !mAccount.isOptionSet(Account.OPTION_REGISTER)); + } else { + showPassword.setVisible(false); } return super.onCreateOptionsMenu(menu); } @@ -531,8 +574,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate protected void onBackendConnected() { if (this.jidToEdit != null) { this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit); - this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER); if (this.mAccount != null) { + this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER); + this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER); if (this.mAccount.getPrivateKeyAlias() != null) { this.mPassword.setHint(R.string.authenticate_with_certificate); if (this.mInitMode) { @@ -541,7 +585,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } updateAccountInformation(true); } - } else if (this.xmppConnectionService.getAccounts().size() == 0) { + } + if ((Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) + || (this.mAccount != null && this.mAccount == xmppConnectionService.getPendingAccount())) { if (getActionBar() != null) { getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setDisplayShowHomeEnabled(false); @@ -549,9 +595,12 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } TextViewUtil.disable(mCancelButton, ConversationsPlusColors.secondaryText()); } - if (Config.DOMAIN_LOCK == null) { + if (mUsernameMode) { + this.mAccountJidLabel.setText(R.string.username); + this.mAccountJid.setHint(R.string.username_hint); + } else { final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, - android.R.layout.simple_list_item_1, + R.layout.simple_list_item, xmppConnectionService.getKnownHosts()); this.mAccountJid.setAdapter(mKnownHostsAdapter); } @@ -559,6 +608,14 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate invalidateOptionsMenu(); } + private String getUserModeDomain() { + if (mAccount != null) { + return mAccount.getJid().getDomainpart(); + } else { + return Config.DOMAIN_LOCK; + } + } + @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { @@ -572,9 +629,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate item.setChecked(!item.isChecked()); break; case R.id.action_change_password_on_server: - final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class); - changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString()); - startActivity(changePasswordIntent); + gotoChangePassword(null); break; case R.id.action_mam_prefs: editMamPrefs(); @@ -585,14 +640,35 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate case R.id.action_renew_certificate: renewCertificate(); break; + case R.id.action_change_presence: + changePresence(); + break; + case R.id.action_show_password: + showPassword(); + break; } return super.onOptionsItemSelected(item); } + private void gotoChangePassword(String newPassword) { + final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class); + changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString()); + if (newPassword != null) { + changePasswordIntent.putExtra("password", newPassword); + } + startActivity(changePasswordIntent); + } + private void renewCertificate() { KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null); } + private void changePresence() { + Intent intent = new Intent(this, SetPresenceActivity.class); + intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT,mAccount.getJid().toBareJid().toString()); + startActivity(intent); + } + @Override public void alias(String alias) { if (alias != null) { @@ -603,7 +679,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate private void updateAccountInformation(boolean init) { if (init) { this.mAccountJid.getEditableText().clear(); - if (Config.DOMAIN_LOCK != null) { + if (mUsernameMode) { this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart()); } else { this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString()); @@ -616,12 +692,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE); } - mPassword.setEnabled(!Config.LOCK_SETTINGS); - mAccountJid.setEnabled(!Config.LOCK_SETTINGS); - mHostname.setEnabled(!Config.LOCK_SETTINGS); - mPort.setEnabled(!Config.LOCK_SETTINGS); - mPasswordConfirm.setEnabled(!Config.LOCK_SETTINGS); - mRegisterNew.setEnabled(!Config.LOCK_SETTINGS); if (!mInitMode) { this.mAvatar.setVisibility(View.VISIBLE); @@ -843,30 +913,38 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate xmppConnectionService.fetchMamPreferences(mAccount, this); } + private void showPassword() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + View view = getLayoutInflater().inflate(R.layout.dialog_show_password, null); + TextView password = (TextView) view.findViewById(R.id.password); + password.setText(mAccount.getPassword()); + builder.setTitle(R.string.password); + builder.setView(view); + builder.setPositiveButton(R.string.cancel, null); + builder.create().show(); + } + @Override public void onKeyStatusUpdated(AxolotlService.FetchStatus report) { refreshUi(); } @Override - public void onCaptchaRequested(final Account account, final String id, final Data data, - final Bitmap captcha) { - final AlertDialog.Builder builder = new AlertDialog.Builder(this); - final ImageView view = new ImageView(this); - final LinearLayout layout = new LinearLayout(this); - final EditText input = new EditText(this); - - view.setImageBitmap(captcha); - view.setScaleType(ImageView.ScaleType.FIT_CENTER); - - input.setHint(getString(R.string.captcha_hint)); - - layout.setOrientation(LinearLayout.VERTICAL); - layout.addView(view); - layout.addView(input); + public void onCaptchaRequested(final Account account, final String id, final Data data, final Bitmap captcha) { + runOnUiThread(new Runnable() { + @Override + public void run() { + if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) { + mCaptchaDialog.dismiss(); + } + final AlertDialog.Builder builder = new AlertDialog.Builder(EditAccountActivity.this); + final View view = getLayoutInflater().inflate(R.layout.captcha, null); + final ImageView imageView = (ImageView) view.findViewById(R.id.captcha); + final EditText input = (EditText) view.findViewById(R.id.input); + imageView.setImageBitmap(captcha); builder.setTitle(getString(R.string.captcha_required)); - builder.setView(layout); + builder.setView(view); builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { @@ -901,13 +979,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } } }); - - runOnUiThread(new Runnable() { - @Override - public void run() { - if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) { - mCaptchaDialog.dismiss(); - } mCaptchaDialog = builder.create(); mCaptchaDialog.show(); } |