]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
require URL protocol to be HTTP or HTTPS
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.java
index c0944f9a9261bd0aa1355a36fd2b7c9ba570df12..2e964d1c0977ab864f486b7bf836f97b492e4c45 100644 (file)
@@ -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;
@@ -46,14 +51,11 @@ import net.ktnx.mobileledger.utils.Colors;
 
 import org.jetbrains.annotations.NotNull;
 
+import java.net.MalformedURLException;
+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;
 
 /**
@@ -86,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
@@ -146,6 +149,10 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
         if (profiles == null) throw new AssertionError();
         profiles.set(index, newProfile);
+
+        ProfilesRecyclerViewAdapter prva = ProfilesRecyclerViewAdapter.getInstance();
+        if (prva != null) prva.notifyItemChanged(index);
+
         if (mProfile.equals(Data.profile.getValue())) Data.profile.setValue(newProfile);
     }
     @Override
@@ -237,11 +244,29 @@ 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);
+
+        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) {
+                checkValidity();
+                checkInsecureSchemeWithAuth();
+            }
+        });
 
         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(
@@ -291,6 +316,17 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
         });
         return rootView;
     }
+    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);
@@ -317,11 +353,25 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue
             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
         }
 
-        val = String.valueOf(url.getText());
-        if (val.trim().isEmpty()) {
+        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));
+        }
         if (useAuthentication.isChecked()) {
             val = String.valueOf(userName.getText());
             if (val.trim().isEmpty()) {