From 1f8884b379ffbff000d7fad7549f8c3598e16fb5 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 27 Sep 2023 19:48:32 +0300 Subject: [PATCH] login: stable realm while settings dialog (fixes #2532) (#2537) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/ui/sync/LoginActivity.java | 175 ++++++++++-------- .../planet/myplanet/ui/sync/SyncActivity.java | 54 ++++-- 3 files changed, 133 insertions(+), 100 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e4953fbda..3dd9a4417 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1057 - versionName "0.10.57" + versionCode 1058 + versionName "0.10.58" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java index 696fec7d6..c0fe9e0ac 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java @@ -204,7 +204,7 @@ private void becomeAMember() { private void showGuestLoginDialog() { try { - mRealm = Realm.getDefaultInstance(); // Open a new Realm instance + mRealm = Realm.getDefaultInstance(); editor = settings.edit(); View v = LayoutInflater.from(this).inflate(R.layout.alert_guest_login, null); @@ -263,58 +263,66 @@ public void notAvailable() { } public void declareMoreElements() { - syncIcon = findViewById(R.id.syncIcon); - syncIcon.setImageDrawable(getResources().getDrawable(R.drawable.login_file_upload_animation)); - syncIcon.getScaleType(); - syncIconDrawable = (AnimationDrawable) syncIcon.getDrawable(); - syncIcon.setOnClickListener(v -> { - syncIconDrawable.start(); - isSync = false; - forceSync = true; - service.checkVersion(this, settings); - }); - declareHideKeyboardElements(); - TextView txtVersion = findViewById(R.id.lblVersion); - txtVersion.setText(getResources().getText(R.string.version) + " " + getResources().getText(R.string.app_version)); - inputName = findViewById(R.id.input_name); - inputPassword = findViewById(R.id.input_password); - inputName.addTextChangedListener(new MyTextWatcher(inputName)); - inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword)); - inputPassword.setOnEditorActionListener((v, actionId, event) -> { - if (actionId == EditorInfo.IME_ACTION_DONE || - (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { - btnSignIn.performClick(); - return true; + try { + mRealm = Realm.getDefaultInstance(); + syncIcon = findViewById(R.id.syncIcon); + syncIcon.setImageDrawable(getResources().getDrawable(R.drawable.login_file_upload_animation)); + syncIcon.getScaleType(); + syncIconDrawable = (AnimationDrawable) syncIcon.getDrawable(); + syncIcon.setOnClickListener(v -> { + syncIconDrawable.start(); + isSync = false; + forceSync = true; + service.checkVersion(this, settings); + }); + declareHideKeyboardElements(); + TextView txtVersion = findViewById(R.id.lblVersion); + txtVersion.setText(getResources().getText(R.string.version) + " " + getResources().getText(R.string.app_version)); + inputName = findViewById(R.id.input_name); + inputPassword = findViewById(R.id.input_password); + inputName.addTextChangedListener(new MyTextWatcher(inputName)); + inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword)); + inputPassword.setOnEditorActionListener((v, actionId, event) -> { + if (actionId == EditorInfo.IME_ACTION_DONE || + (event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { + btnSignIn.performClick(); + return true; + } + return false; + }); + setUplanguageButton(); + if (defaultPref.getBoolean("saveUsernameAndPassword", false)) { + inputName.setText(settings.getString(getString(R.string.login_user), "")); + inputPassword.setText(settings.getString(getString(R.string.login_password), "")); } - return false; - }); - setUplanguageButton(); - if (defaultPref.getBoolean("saveUsernameAndPassword", false)) { - inputName.setText(settings.getString(getString(R.string.login_user), "")); - inputPassword.setText(settings.getString(getString(R.string.login_password), "")); - } - if (NetworkUtils.isNetworkConnected()) { - service.syncPlanetServers(mRealm, success -> Utilities.toast(LoginActivity.this, success)); - } - inputName.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { + if (NetworkUtils.isNetworkConnected()) { + service.syncPlanetServers(mRealm, success -> Utilities.toast(LoginActivity.this, success)); } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - String lowercaseText = s.toString().toLowerCase(Locale.ROOT); - if (!s.toString().equals(lowercaseText)) { - inputName.setText(lowercaseText); - inputName.setSelection(lowercaseText.length()); + inputName.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } - } - @Override - public void afterTextChanged(Editable s) { + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + String lowercaseText = s.toString().toLowerCase(Locale.ROOT); + if (!s.toString().equals(lowercaseText)) { + inputName.setText(lowercaseText); + inputName.setSelection(lowercaseText.length()); + } + } + + @Override + public void afterTextChanged(Editable s) { + } + }); + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); } - }); + } } private void setUplanguageButton() { @@ -400,43 +408,50 @@ private void onLogin() { } public void settingDialog() { - MaterialDialog.Builder builder = new MaterialDialog.Builder(LoginActivity.this); - builder.title(R.string.action_settings).customView(R.layout.dialog_server_url_, true).positiveText(R.string.btn_sync).negativeText(R.string.btn_sync_cancel).neutralText(R.string.btn_sync_save).onPositive((dialog, which) -> continueSync(dialog)).onNeutral((dialog, which) -> saveConfigAndContinue(dialog)); - - MaterialDialog dialog = builder.build(); - positiveAction = dialog.getActionButton(DialogAction.POSITIVE); - spnCloud = dialog.getCustomView().findViewById(R.id.spn_cloud); - List communities = mRealm.where(RealmCommunity.class).sort("weight", Sort.ASCENDING).findAll(); - spnCloud.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, communities)); - spnCloud.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView adapterView, View view, int i, long l) { - onChangeServerUrl(); - } + try { + mRealm = Realm.getDefaultInstance(); + MaterialDialog.Builder builder = new MaterialDialog.Builder(LoginActivity.this); + builder.title(R.string.action_settings).customView(R.layout.dialog_server_url_, true).positiveText(R.string.btn_sync).negativeText(R.string.btn_sync_cancel).neutralText(R.string.btn_sync_save).onPositive((dialog, which) -> continueSync(dialog)).onNeutral((dialog, which) -> saveConfigAndContinue(dialog)); + + MaterialDialog dialog = builder.build(); + positiveAction = dialog.getActionButton(DialogAction.POSITIVE); + spnCloud = dialog.getCustomView().findViewById(R.id.spn_cloud); + List communities = mRealm.where(RealmCommunity.class).sort("weight", Sort.ASCENDING).findAll(); + spnCloud.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, communities)); + spnCloud.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView adapterView, View view, int i, long l) { + onChangeServerUrl(); + } - @Override - public void onNothingSelected(AdapterView adapterView) { + @Override + public void onNothingSelected(AdapterView adapterView) { + } + }); + protocol_checkin = dialog.getCustomView().findViewById(R.id.radio_protocol); + serverUrl = dialog.getCustomView().findViewById(R.id.input_server_url); + serverPassword = dialog.getCustomView().findViewById(R.id.input_server_Password); + switchServerUrl = dialog.getCustomView().findViewById(R.id.switch_server_url); + serverUrlProtocol = dialog.getCustomView().findViewById(R.id.input_server_url_protocol); + switchServerUrl.setOnCheckedChangeListener((compoundButton, b) -> { + settings.edit().putBoolean("switchCloudUrl", b).commit(); + spnCloud.setVisibility(b ? View.VISIBLE : View.GONE); + setUrlAndPin(switchServerUrl.isChecked()); + }); + serverUrl.addTextChangedListener(new MyTextWatcher(serverUrl)); + EditText customDeviceName = dialog.getCustomView().findViewById(R.id.deviceName); + customDeviceName.setText(getCustomDeviceName()); + switchServerUrl.setChecked(settings.getBoolean("switchCloudUrl", false)); + setUrlAndPin(settings.getBoolean("switchCloudUrl", false)); + protocol_semantics(); + dialog.show(); + sync(dialog); + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); } - }); - protocol_checkin = dialog.getCustomView().findViewById(R.id.radio_protocol); - serverUrl = dialog.getCustomView().findViewById(R.id.input_server_url); - serverPassword = dialog.getCustomView().findViewById(R.id.input_server_Password); - switchServerUrl = dialog.getCustomView().findViewById(R.id.switch_server_url); - serverUrlProtocol = dialog.getCustomView().findViewById(R.id.input_server_url_protocol); - switchServerUrl.setOnCheckedChangeListener((compoundButton, b) -> { - settings.edit().putBoolean("switchCloudUrl", b).commit(); - spnCloud.setVisibility(b ? View.VISIBLE : View.GONE); - setUrlAndPin(switchServerUrl.isChecked()); - }); - serverUrl.addTextChangedListener(new MyTextWatcher(serverUrl)); - EditText customDeviceName = dialog.getCustomView().findViewById(R.id.deviceName); - customDeviceName.setText(getCustomDeviceName()); - switchServerUrl.setChecked(settings.getBoolean("switchCloudUrl", false)); - setUrlAndPin(settings.getBoolean("switchCloudUrl", false)); - protocol_semantics(); - dialog.show(); - sync(dialog); + } } private void onChangeServerUrl() { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.java index fc98d2655..6adc6a5b5 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.java @@ -106,18 +106,25 @@ private void setSpinnerVisibility(boolean isChecked) { } public void setUpChildMode() { - if (!settings.getBoolean("isChild", false)) return; - RecyclerView rvTeams = findViewById(R.id.rv_teams); - TextView tvNodata = findViewById(R.id.tv_nodata); - - List teams = mRealm.where(RealmMyTeam.class).isEmpty("teamId").findAll(); - rvTeams.setLayoutManager(new GridLayoutManager(this, 3)); - rvTeams.setAdapter(new AdapterTeam(this, teams, mRealm)); - if (teams.size() > 0) { - tvNodata.setVisibility(View.GONE); - } else { - tvNodata.setText(R.string.no_team_available); - tvNodata.setVisibility(View.VISIBLE); + try { + mRealm = Realm.getDefaultInstance(); + if (!settings.getBoolean("isChild", false)) return; + RecyclerView rvTeams = findViewById(R.id.rv_teams); + TextView tvNodata = findViewById(R.id.tv_nodata); + + List teams = mRealm.where(RealmMyTeam.class).isEmpty("teamId").findAll(); + rvTeams.setLayoutManager(new GridLayoutManager(this, 3)); + rvTeams.setAdapter(new AdapterTeam(this, teams, mRealm)); + if (teams.size() > 0) { + tvNodata.setVisibility(View.GONE); + } else { + tvNodata.setText(R.string.no_team_available); + tvNodata.setVisibility(View.VISIBLE); + } + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); + } } } @@ -197,18 +204,26 @@ public void saveSyncInfoToPreference() { } public boolean authenticateUser(SharedPreferences settings, String username, String password, boolean isManagerMode) { - this.settings = settings; - if (mRealm.isEmpty()) { - alertDialogOkay(getString(R.string.server_not_configured_properly_connect_this_device_with_planet_server)); - return false; - } else { - return checkName(username, password, isManagerMode); + try { + mRealm = Realm.getDefaultInstance(); + this.settings = settings; + if (mRealm.isEmpty()) { + alertDialogOkay(getString(R.string.server_not_configured_properly_connect_this_device_with_planet_server)); + return false; + } else { + return checkName(username, password, isManagerMode); + } + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); + } } } @Nullable private Boolean checkName(String username, String password, boolean isManagerMode) { try { + mRealm = Realm.getDefaultInstance(); AndroidDecrypter decrypt = new AndroidDecrypter(); RealmResults db_users = mRealm.where(RealmUserModel.class).equalTo("name", username).findAll(); for (RealmUserModel user : db_users) { @@ -227,6 +242,9 @@ private Boolean checkName(String username, String password, boolean isManagerMod } } catch (Exception err) { err.printStackTrace(); + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); + } return false; } return false;