+ mProfile = new MobileLedgerProfile(String.valueOf(UUID.randomUUID()));
+ model.updateProfile(mProfile);
+ mProfile.storeInDB();
+ final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+ if (profiles == null)
+ throw new AssertionError();
+ ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
+ newList.add(mProfile);
+ Data.profiles.setValue(newList);
+ MobileLedgerProfile.storeProfilesOrder();
+
+ // first profile ever?
+ if (newList.size() == 1)
+ Data.setCurrentProfile(mProfile);
+ }
+
+ Activity activity = getActivity();
+ if (activity != null)
+ activity.finish();
+ }
+ private boolean checkUrlValidity() {
+ boolean valid = true;
+
+ ProfileDetailModel model = getModel();
+
+ String val = model.getUrl()
+ .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;
+
+ final ProfileDetailModel model = getModel();
+
+ if (model.getUseAuthentication()) {
+ String urlText = model.getUrl();
+ 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);
+ });
+ view.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) {
+ layout.setError(null);
+ }
+ @Override
+ public void afterTextChanged(Editable s) {
+ }
+ });
+ }
+ private void syncModelFromUI() {
+ if (syncingModelFromUI)
+ return;
+
+ syncingModelFromUI = true;
+
+ try {
+ ProfileDetailModel model = getModel();
+
+ model.setProfileName(profileName.getText());
+ model.setUrl(url.getText());
+ model.setPreferredAccountsFilter(preferredAccountsFilter.getText());
+ model.setAuthUserName(userName.getText());
+ model.setAuthPassword(password.getText());
+ }
+ finally {
+ syncingModelFromUI = false;
+ }
+ }
+ private boolean checkValidity() {
+ boolean valid = true;
+
+ String val = String.valueOf(profileName.getText());
+ if (val.trim()
+ .isEmpty())
+ {
+ valid = false;
+ profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
+ }
+
+ if (!checkUrlValidity())
+ valid = false;
+
+ if (useAuthentication.isChecked()) {
+ val = String.valueOf(userName.getText());
+ if (val.trim()
+ .isEmpty())
+ {
+ valid = false;
+ userNameLayout.setError(
+ getResources().getText(R.string.err_profile_user_name_empty));
+ }
+
+ val = String.valueOf(password.getText());
+ if (val.trim()
+ .isEmpty())
+ {
+ valid = false;
+ passwordLayout.setError(
+ getResources().getText(R.string.err_profile_password_empty));
+ }