X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailFragment.java;h=f4cdae1d097b63f342ed4e313d0a5669e440dd6c;hb=d857b003248bf4b3e8af018f83253700470ca41f;hp=c6dbc6a587c1799244b6656de63d95c4bba9cf5f;hpb=a4295cb43de54dd0428563d46e66a89320f6afef;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index c6dbc6a5..f4cdae1d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -32,6 +32,11 @@ import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; + import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.textfield.TextInputLayout; @@ -51,11 +56,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.Objects; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; - import static net.ktnx.mobileledger.utils.Logger.debug; /** @@ -88,6 +88,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue private TextView preferredAccountsFilter; private TextInputLayout preferredAccountsFilterLayout; private View huePickerView; + private View insecureWarningText; /** * Mandatory empty constructor for the fragment manager to instantiate the @@ -111,8 +112,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue mProfile.removeFromDB(); ArrayList oldList = Data.profiles.getValue(); if (oldList == null) throw new AssertionError(); - ArrayList newList = - (ArrayList) oldList.clone(); + ArrayList newList = new ArrayList<>(oldList); newList.remove(mProfile); Data.profiles.setValue(newList); if (mProfile.equals(Data.profile.getValue())) { @@ -200,8 +200,7 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue mProfile.storeInDB(); final ArrayList profiles = Data.profiles.getValue(); if (profiles == null) throw new AssertionError(); - ArrayList newList = - (ArrayList) profiles.clone(); + ArrayList newList = new ArrayList<>(profiles); newList.add(mProfile); Data.profiles.setValue(newList); MobileLedgerProfile.storeProfilesOrder(); @@ -243,11 +242,13 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue preferredAccountsFilter = rootView.findViewById(R.id.preferred_accounts_filter_filter); preferredAccountsFilterLayout = rootView.findViewById(R.id.preferred_accounts_accounts_filter_layout); + insecureWarningText = rootView.findViewById(R.id.insecure_scheme_text); useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> { debug("profiles", isChecked ? "auth enabled " : "auth disabled"); authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE); if (isChecked) userName.requestFocus(); + checkInsecureSchemeWithAuth(); }); postingPermitted.setOnCheckedChangeListener( @@ -283,6 +284,23 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue profileThemeId = -1; } + checkInsecureSchemeWithAuth(); + + url.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + @Override + public void afterTextChanged(Editable s) { + checkInsecureSchemeWithAuth(); + } + }); + final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId; final int profileColor = Colors.getPrimaryColorForHue(hue); @@ -297,6 +315,42 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue }); return rootView; } + private boolean checkUrlValidity() { + boolean valid = true; + + String val = String.valueOf(url.getText()).trim(); + if (val.isEmpty()) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_profile_url_empty)); + } + try { + URL url = new URL(val); + String host = url.getHost(); + if (host == null || host.isEmpty()) throw new MalformedURLException("Missing host"); + String protocol = url.getProtocol().toUpperCase(); + if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_invalid_url)); + } + } + catch (MalformedURLException e) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_invalid_url)); + } + + return valid; + } + private void checkInsecureSchemeWithAuth() { + boolean showWarning = false; + + if (useAuthentication.isChecked()) { + String urlText = url.getText().toString(); + if (urlText.startsWith("http") && !urlText.startsWith("https")) showWarning = true; + } + + if (showWarning) insecureWarningText.setVisibility(View.VISIBLE); + else insecureWarningText.setVisibility(View.GONE); + } private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) { view.setOnFocusChangeListener((v, hasFocus) -> { if (hasFocus) layout.setError(null); @@ -323,20 +377,8 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty)); } - val = String.valueOf(url.getText()).trim(); - if (val.isEmpty()) { - valid = false; - urlLayout.setError(getResources().getText(R.string.err_profile_url_empty)); - } - try { - URL url = new URL(val); - String host = url.getHost(); - if (host == null || host.isEmpty()) throw new MalformedURLException("Missing host"); - } - catch (MalformedURLException e) { - valid = false; - urlLayout.setError(getResources().getText(R.string.err_invalid_url)); - } + if (!checkUrlValidity()) valid = false; + if (useAuthentication.isChecked()) { val = String.valueOf(userName.getText()); if (val.trim().isEmpty()) {