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;
import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
import net.ktnx.mobileledger.utils.Colors;
+import org.jetbrains.annotations.NonNls;
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;
/**
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
+ @NonNls
+ private static final String HTTPS_URL_START = "https://";
/**
* The dummy content this fragment is presenting.
private TextView preferredAccountsFilter;
private TextInputLayout preferredAccountsFilterLayout;
private View huePickerView;
+ private View insecureWarningText;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
mProfile.removeFromDB();
ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
if (oldList == null) throw new AssertionError();
- ArrayList<MobileLedgerProfile> newList =
- (ArrayList<MobileLedgerProfile>) oldList.clone();
+ ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
newList.remove(mProfile);
Data.profiles.setValue(newList);
if (mProfile.equals(Data.profile.getValue())) {
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
mProfile.storeInDB();
final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
if (profiles == null) throw new AssertionError();
- ArrayList<MobileLedgerProfile> newList =
- (ArrayList<MobileLedgerProfile>) profiles.clone();
+ ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
newList.add(mProfile);
Data.profiles.setValue(newList);
MobileLedgerProfile.storeProfilesOrder();
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(
}
else {
profileName.setText("");
- url.setText("https://");
+ url.setText(HTTPS_URL_START);
postingPermitted.setChecked(true);
useAuthentication.setChecked(false);
authParams.setVisibility(View.GONE);
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);
});
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);
profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
}
- val = String.valueOf(url.getText());
- if (val.trim().isEmpty()) {
- valid = false;
- urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
- }
+ if (!checkUrlValidity()) valid = false;
+
if (useAuthentication.isChecked()) {
val = String.valueOf(userName.getText());
if (val.trim().isEmpty()) {