From 7590a6ffaeef6b9f0131cd7ceff49767234dee6c Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 26 Dec 2018 10:54:48 +0000 Subject: [PATCH] Revise identity layout --- .../eu/faircode/email/EntityIdentity.java | 96 +++--- .../eu/faircode/email/FragmentIdentity.java | 313 ++++++++++-------- app/src/main/res/layout/fragment_identity.xml | 270 ++++++++------- 3 files changed, 358 insertions(+), 321 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityIdentity.java b/app/src/main/java/eu/faircode/email/EntityIdentity.java index b439714081..8fa4779ee0 100644 --- a/app/src/main/java/eu/faircode/email/EntityIdentity.java +++ b/app/src/main/java/eu/faircode/email/EntityIdentity.java @@ -49,15 +49,13 @@ public class EntityIdentity { public String name; @NonNull public String email; - public String display; - public String replyto; - public String bcc; - @NonNull - public Boolean delivery_receipt; - @NonNull - public Boolean read_receipt; @NonNull public Long account; + public String display; + public Integer color; + public String signature; + @NonNull + public Integer auth_type; @NonNull public String host; // SMTP @NonNull @@ -71,15 +69,17 @@ public class EntityIdentity { @NonNull public String password; @NonNull - public Integer auth_type; - @NonNull - public Boolean primary; - public Integer color; - public String signature; - @NonNull public Boolean synchronize; @NonNull - public Boolean store_sent; + public Boolean primary; + public String replyto; + public String bcc; + @NonNull + public Boolean delivery_receipt; + @NonNull + public Boolean read_receipt; + @NonNull + public Boolean store_sent = false; // obsolete public Long sent_folder; public Boolean tbd; public String state; @@ -88,25 +88,26 @@ public class EntityIdentity { public JSONObject toJSON() throws JSONException { JSONObject json = new JSONObject(); json.put("name", name); - json.put("display", display); json.put("email", email); - json.put("replyto", replyto); - json.put("bcc", bcc); - json.put("delivery_receipt", delivery_receipt); - json.put("read_receipt", read_receipt); + json.put("display", display); + if (color != null) + json.put("color", color); + json.put("signature", signature); // not account + json.put("auth_type", auth_type); json.put("host", host); json.put("starttls", starttls); json.put("insecure", insecure); json.put("port", port); json.put("user", user); json.put("password", password); - json.put("auth_type", auth_type); - json.put("primary", primary); - if (color != null) - json.put("color", color); - json.put("signature", signature); json.put("synchronize", synchronize); + json.put("primary", primary); + + json.put("replyto", replyto); + json.put("bcc", bcc); + json.put("delivery_receipt", delivery_receipt); + json.put("read_receipt", read_receipt); json.put("store_sent", store_sent); if (sent_folder != null) json.put("sent_folder", sent_folder); @@ -118,11 +119,26 @@ public class EntityIdentity { public static EntityIdentity fromJSON(JSONObject json) throws JSONException { EntityIdentity identity = new EntityIdentity(); identity.name = json.getString("name"); + identity.email = json.getString("email"); if (json.has("display")) identity.display = json.getString("display"); - identity.email = json.getString("email"); + if (json.has("color")) + identity.color = json.getInt("color"); + if (json.has("signature")) + identity.signature = json.getString("signature"); + + identity.auth_type = json.getInt("auth_type"); + identity.host = json.getString("host"); + identity.starttls = json.getBoolean("starttls"); + identity.insecure = (json.has("insecure") && json.getBoolean("insecure")); + identity.port = json.getInt("port"); + identity.user = json.getString("user"); + identity.password = json.getString("password"); + + identity.synchronize = json.getBoolean("synchronize"); + identity.primary = json.getBoolean("primary"); if (json.has("replyto")) identity.replyto = json.getString("replyto"); @@ -140,22 +156,8 @@ public class EntityIdentity { else identity.read_receipt = false; - identity.host = json.getString("host"); - identity.starttls = json.getBoolean("starttls"); - identity.insecure = (json.has("insecure") && json.getBoolean("insecure")); - identity.port = json.getInt("port"); - identity.user = json.getString("user"); - identity.password = json.getString("password"); - identity.auth_type = json.getInt("auth_type"); - identity.primary = json.getBoolean("primary"); - - if (json.has("color")) - identity.color = json.getInt("color"); - if (json.has("signature")) - identity.signature = json.getString("signature"); - - identity.synchronize = json.getBoolean("synchronize"); - identity.store_sent = json.getBoolean("store_sent"); + if (json.has("store_sent")) + identity.store_sent = json.getBoolean("store_sent"); if (json.has("sent_folder")) identity.sent_folder = json.getLong("sent_folder"); @@ -169,19 +171,21 @@ public class EntityIdentity { EntityIdentity other = (EntityIdentity) obj; return (this.name.equals(other.name) && this.email.equals(other.email) && - (this.replyto == null ? other.replyto == null : this.replyto.equals(other.replyto)) && this.account.equals(other.account) && + (this.display == null ? other.display == null : this.display.equals(other.display)) && + (this.color == null ? other.color == null : this.color.equals(other.color)) && + (this.signature == null ? other.signature == null : this.signature.equals(other.signature)) && this.host.equals(other.host) && this.starttls.equals(other.starttls) && this.insecure.equals(other.insecure) && this.port.equals(other.port) && this.user.equals(other.user) && this.password.equals(other.password) && - this.primary.equals(other.primary) && - (this.color == null ? other.color == null : this.color.equals(other.color)) && - (this.signature == null ? other.signature == null : this.signature.equals(other.signature)) && this.synchronize.equals(other.synchronize) && - this.store_sent.equals(other.store_sent) && + this.primary.equals(other.primary) && + (this.replyto == null ? other.replyto == null : this.replyto.equals(other.replyto)) && + this.delivery_receipt.equals(other.delivery_receipt) && + this.read_receipt.equals(other.read_receipt) && (this.sent_folder == null ? other.sent_folder == null : this.sent_folder.equals(other.sent_folder)) && (this.tbd == null ? other.tbd == null : this.tbd.equals(other.tbd)) && (this.state == null ? other.state == null : this.state.equals(other.state)) && diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 2c3506611a..2655b72337 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -27,8 +27,10 @@ import android.graphics.drawable.GradientDrawable; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; +import android.text.Editable; import android.text.Html; import android.text.TextUtils; +import android.text.TextWatcher; import android.util.Patterns; import android.view.LayoutInflater; import android.view.Menu; @@ -47,6 +49,7 @@ import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.Spinner; +import android.widget.TextView; import com.android.colorpicker.ColorPickerDialog; import com.android.colorpicker.ColorPickerSwatch; @@ -73,15 +76,21 @@ import androidx.fragment.app.FragmentTransaction; public class FragmentIdentity extends FragmentEx { private ViewGroup view; + private EditText etName; private EditText etEmail; - private EditText etDisplay; + private Spinner spAccount; + + private EditText etDisplay; + private Button btnColor; + private View vwColor; + private ImageView ibColorDefault; + private EditText etSignature; + private ImageButton ibPro; + private Button btnAdvanced; - private EditText etReplyTo; - private EditText etBcc; - private CheckBox cbDeliveryReceipt; - private CheckBox cbReadReceipt; + private TextView tvProvider; private Spinner spProvider; private EditText etDomain; private Button btnAutoConfig; @@ -92,19 +101,19 @@ public class FragmentIdentity extends FragmentEx { private EditText etUser; private TextInputLayout tilPassword; - private Button btnColor; - private View vwColor; - private ImageView ibColorDefault; - private EditText etSignature; - private ImageButton ibPro; - private CheckBox cbSynchronize; private CheckBox cbPrimary; + + private EditText etReplyTo; + private EditText etBcc; + private CheckBox cbDeliveryReceipt; + private CheckBox cbReadReceipt; private Spinner spSent; private Button btnSave; private ProgressBar pbSave; private ProgressBar pbWait; + private Group grpAdvanced; private long id = -1; @@ -135,19 +144,18 @@ public class FragmentIdentity extends FragmentEx { etName = view.findViewById(R.id.etName); etEmail = view.findViewById(R.id.etEmail); spAccount = view.findViewById(R.id.spAccount); + etDisplay = view.findViewById(R.id.etDisplay); + btnColor = view.findViewById(R.id.btnColor); + vwColor = view.findViewById(R.id.vwColor); + ibColorDefault = view.findViewById(R.id.ibColorDefault); + etSignature = view.findViewById(R.id.etSignature); + ibPro = view.findViewById(R.id.ibPro); btnAdvanced = view.findViewById(R.id.btnAdvanced); - etDisplay = view.findViewById(R.id.etDisplay); - etReplyTo = view.findViewById(R.id.etReplyTo); - etBcc = view.findViewById(R.id.etBcc); - cbDeliveryReceipt = view.findViewById(R.id.cbDeliveryReceipt); - cbReadReceipt = view.findViewById(R.id.cbReadReceipt); - + tvProvider = view.findViewById(R.id.tvProvider); spProvider = view.findViewById(R.id.spProvider); - etDomain = view.findViewById(R.id.etDomain); btnAutoConfig = view.findViewById(R.id.btnAutoConfig); - etHost = view.findViewById(R.id.etHost); cbStartTls = view.findViewById(R.id.cbStartTls); cbInsecure = view.findViewById(R.id.cbInsecure); @@ -155,19 +163,19 @@ public class FragmentIdentity extends FragmentEx { etUser = view.findViewById(R.id.etUser); tilPassword = view.findViewById(R.id.tilPassword); - btnColor = view.findViewById(R.id.btnColor); - vwColor = view.findViewById(R.id.vwColor); - ibColorDefault = view.findViewById(R.id.ibColorDefault); - etSignature = view.findViewById(R.id.etSignature); - ibPro = view.findViewById(R.id.ibPro); - cbSynchronize = view.findViewById(R.id.cbSynchronize); cbPrimary = view.findViewById(R.id.cbPrimary); + + etReplyTo = view.findViewById(R.id.etReplyTo); + etBcc = view.findViewById(R.id.etBcc); + cbDeliveryReceipt = view.findViewById(R.id.cbDeliveryReceipt); + cbReadReceipt = view.findViewById(R.id.cbReadReceipt); spSent = view.findViewById(R.id.spSent); btnSave = view.findViewById(R.id.btnSave); pbSave = view.findViewById(R.id.pbSave); pbWait = view.findViewById(R.id.pbWait); + grpAdvanced = view.findViewById(R.id.grpAdvanced); // Wire controls @@ -229,98 +237,6 @@ public class FragmentIdentity extends FragmentEx { } }); - spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView adapterView, View view, int position, long id) { - Integer tag = (Integer) adapterView.getTag(); - if (tag != null && tag.equals(position)) - return; - adapterView.setTag(position); - - Provider provider = (Provider) adapterView.getSelectedItem(); - - // Set associated host/port/starttls - etHost.setText(provider.smtp_host); - etPort.setText(position == 0 ? null : Integer.toString(provider.smtp_port)); - cbStartTls.setChecked(provider.smtp_starttls); - } - - @Override - public void onNothingSelected(AdapterView adapterView) { - } - }); - - btnAutoConfig.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - etDomain.setEnabled(false); - btnAutoConfig.setEnabled(false); - - Bundle args = new Bundle(); - args.putString("domain", etDomain.getText().toString()); - - new SimpleTask() { - @Override - protected SRVRecord onLoad(Context context, Bundle args) throws Throwable { - String domain = args.getString("domain"); - Record[] records = new Lookup("_submission._tcp." + domain, Type.SRV).run(); - if (records != null) - for (int i = 0; i < records.length; i++) { - SRVRecord srv = (SRVRecord) records[i]; - Log.i("SRV=" + srv); - return srv; - } - - throw new IllegalArgumentException(getString(R.string.title_no_settings)); - } - - @Override - protected void onLoaded(Bundle args, SRVRecord srv) { - etDomain.setEnabled(true); - btnAutoConfig.setEnabled(true); - if (srv != null) { - etHost.setText(srv.getTarget().toString(true)); - etPort.setText(Integer.toString(srv.getPort())); - cbStartTls.setChecked(srv.getPort() == 587); - } - } - - @Override - protected void onException(Bundle args, Throwable ex) { - etDomain.setEnabled(true); - btnAutoConfig.setEnabled(true); - if (ex instanceof IllegalArgumentException) - Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); - else - Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); - } - }.load(FragmentIdentity.this, args); - } - }); - - btnAdvanced.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - int visibility = (grpAdvanced.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); - grpAdvanced.setVisibility(visibility); - cbInsecure.setVisibility(insecure ? visibility : View.GONE); - if (visibility == View.VISIBLE) - new Handler().post(new Runnable() { - @Override - public void run() { - ((ScrollView) view).smoothScrollTo(0, etDisplay.getTop()); - } - }); - } - }); - - cbStartTls.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { - etPort.setHint(checked ? "587" : "465"); - } - }); - vwColor.setBackgroundColor(color); btnColor.setOnClickListener(new View.OnClickListener() { @Override @@ -362,6 +278,117 @@ public class FragmentIdentity extends FragmentEx { } }); + btnAdvanced.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + int visibility = (grpAdvanced.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); + grpAdvanced.setVisibility(visibility); + cbInsecure.setVisibility(insecure ? visibility : View.GONE); + if (visibility == View.VISIBLE) + new Handler().post(new Runnable() { + @Override + public void run() { + ((ScrollView) view).smoothScrollTo(0, tvProvider.getTop()); + } + }); + } + }); + + spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int position, long id) { + Integer tag = (Integer) adapterView.getTag(); + if (tag != null && tag.equals(position)) + return; + adapterView.setTag(position); + + Provider provider = (Provider) adapterView.getSelectedItem(); + + // Set associated host/port/starttls + etHost.setText(provider.smtp_host); + etPort.setText(position == 0 ? null : Integer.toString(provider.smtp_port)); + cbStartTls.setChecked(provider.smtp_starttls); + } + + @Override + public void onNothingSelected(AdapterView adapterView) { + } + }); + + etDomain.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence text, int start, int before, int count) { + btnAutoConfig.setEnabled(text.length() > 0); + } + + @Override + public void afterTextChanged(Editable s) { + } + }); + + btnAutoConfig.setEnabled(false); + + btnAutoConfig.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + etDomain.setEnabled(false); + btnAutoConfig.setEnabled(false); + + Bundle args = new Bundle(); + args.putString("domain", etDomain.getText().toString()); + + new SimpleTask() { + @Override + protected SRVRecord onLoad(Context context, Bundle args) throws Throwable { + String dns = "_submission._tcp." + args.getString("domain"); + Log.i("Lookup dns=" + dns); + Record[] records = new Lookup(dns, Type.SRV).run(); + Log.i("Found dns=" + (records == null ? -1 : records.length)); + if (records != null) + for (int i = 0; i < records.length; i++) { + SRVRecord srv = (SRVRecord) records[i]; + Log.i("SRV=" + srv); + return srv; + } + + throw new IllegalArgumentException(getString(R.string.title_no_settings)); + } + + @Override + protected void onLoaded(Bundle args, SRVRecord srv) { + etDomain.setEnabled(true); + btnAutoConfig.setEnabled(true); + if (srv != null) { + etHost.setText(srv.getTarget().toString(true)); + etPort.setText(Integer.toString(srv.getPort())); + cbStartTls.setChecked(srv.getPort() == 587); + } + } + + @Override + protected void onException(Bundle args, Throwable ex) { + etDomain.setEnabled(true); + btnAutoConfig.setEnabled(true); + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); + } + }.load(FragmentIdentity.this, args); + } + }); + + cbStartTls.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + etPort.setHint(checked ? "587" : "465"); + } + }); + cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -406,24 +433,27 @@ public class FragmentIdentity extends FragmentEx { protected Void onLoad(Context context, Bundle args) throws Throwable { long id = args.getLong("id"); String name = args.getString("name"); - long account = args.getLong("account"); - String display = args.getString("display"); String email = args.getString("email"); - String replyto = args.getString("replyto"); - String bcc = args.getString("bcc"); - boolean delivery_receipt = args.getBoolean("delivery_receipt"); - boolean read_receipt = args.getBoolean("read_receipt"); + long account = args.getLong("account"); + + String display = args.getString("display"); + Integer color = args.getInt("color"); + String signature = args.getString("signature"); + + int auth_type = args.getInt("auth_type"); String host = args.getString("host"); boolean starttls = args.getBoolean("starttls"); boolean insecure = args.getBoolean("insecure"); String port = args.getString("port"); String user = args.getString("user"); String password = args.getString("password"); - Integer color = args.getInt("color"); - String signature = args.getString("signature"); - int auth_type = args.getInt("auth_type"); boolean synchronize = args.getBoolean("synchronize"); boolean primary = args.getBoolean("primary"); + + String replyto = args.getString("replyto"); + String bcc = args.getString("bcc"); + boolean delivery_receipt = args.getBoolean("delivery_receipt"); + boolean read_receipt = args.getBoolean("read_receipt"); EntityFolder sent = (EntityFolder) args.getSerializable("sent"); if (TextUtils.isEmpty(name)) @@ -495,24 +525,26 @@ public class FragmentIdentity extends FragmentEx { if (identity == null) identity = new EntityIdentity(); identity.name = name; + identity.email = email; identity.account = account; identity.display = display; - identity.email = email; - identity.replyto = replyto; - identity.bcc = bcc; - identity.delivery_receipt = delivery_receipt; - identity.read_receipt = read_receipt; + identity.color = color; + identity.signature = signature; + + identity.auth_type = auth_type; identity.host = host; identity.starttls = starttls; identity.insecure = insecure; identity.port = Integer.parseInt(port); identity.user = user; identity.password = password; - identity.color = color; - identity.signature = signature; - identity.auth_type = auth_type; identity.synchronize = synchronize; identity.primary = (identity.synchronize && primary); + + identity.replyto = replyto; + identity.bcc = bcc; + identity.delivery_receipt = delivery_receipt; + identity.read_receipt = read_receipt; identity.store_sent = false; identity.sent_folder = (sent == null ? null : sent.id); identity.error = null; @@ -602,21 +634,24 @@ public class FragmentIdentity extends FragmentEx { if (savedInstanceState == null) { etName.setText(identity == null ? null : identity.name); etEmail.setText(identity == null ? null : identity.email); + etDisplay.setText(identity == null ? null : identity.display); - etReplyTo.setText(identity == null ? null : identity.replyto); - etBcc.setText(identity == null ? null : identity.bcc); - cbDeliveryReceipt.setChecked(identity == null ? false : identity.delivery_receipt); - cbReadReceipt.setChecked(identity == null ? false : identity.read_receipt); + etSignature.setText(identity == null || identity.signature == null ? null : Html.fromHtml(identity.signature)); + etHost.setText(identity == null ? null : identity.host); cbStartTls.setChecked(identity == null ? false : identity.starttls); cbInsecure.setChecked(identity == null ? false : identity.insecure); etPort.setText(identity == null ? null : Long.toString(identity.port)); etUser.setText(identity == null ? null : identity.user); tilPassword.getEditText().setText(identity == null ? null : identity.password); - etSignature.setText(identity == null || identity.signature == null ? null : Html.fromHtml(identity.signature)); cbSynchronize.setChecked(identity == null ? true : identity.synchronize); cbPrimary.setChecked(identity == null ? true : identity.primary); + etReplyTo.setText(identity == null ? null : identity.replyto); + etBcc.setText(identity == null ? null : identity.bcc); + cbDeliveryReceipt.setChecked(identity == null ? false : identity.delivery_receipt); + cbReadReceipt.setChecked(identity == null ? false : identity.read_receipt); + color = (identity == null || identity.color == null ? Color.TRANSPARENT : identity.color); etName.requestFocus(); diff --git a/app/src/main/res/layout/fragment_identity.xml b/app/src/main/res/layout/fragment_identity.xml index 3f977d5012..b8715aa75a 100644 --- a/app/src/main/res/layout/fragment_identity.xml +++ b/app/src/main/res/layout/fragment_identity.xml @@ -72,20 +72,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvAccount" /> -