2020-04-26 15:36:33 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
|
|
|
|
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;
|
|
|
|
|
2021-03-09 22:15:42 +01:00
|
|
|
import android.app.KeyguardManager;
|
2020-04-26 15:36:33 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
2021-03-09 22:15:42 +01:00
|
|
|
import androidx.biometric.BiometricManager;
|
2022-08-07 21:34:02 +02:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-05-09 16:50:45 +02:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2020-04-26 15:36:33 +02:00
|
|
|
import org.mian.gitnex.R;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.ActivitySettingsSecurityBinding;
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2024-02-11 14:20:23 +01:00
|
|
|
import org.mian.gitnex.helpers.SnackBar;
|
2020-04-26 15:36:33 +02:00
|
|
|
import org.mian.gitnex.helpers.ssl.MemorizingTrustManager;
|
|
|
|
|
|
|
|
/**
|
2022-07-29 14:08:44 +02:00
|
|
|
* @author M M Arif
|
2020-04-26 15:36:33 +02:00
|
|
|
*/
|
|
|
|
public class SettingsSecurityActivity extends BaseActivity {
|
|
|
|
|
2021-06-10 18:03:52 +02:00
|
|
|
private static String[] cacheSizeDataList;
|
2020-05-09 16:50:45 +02:00
|
|
|
private static int cacheSizeDataSelectedChoice = 0;
|
2021-06-10 18:03:52 +02:00
|
|
|
private static String[] cacheSizeImagesList;
|
2020-05-09 16:50:45 +02:00
|
|
|
private static int cacheSizeImagesSelectedChoice = 0;
|
|
|
|
|
2020-04-26 15:36:33 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
2020-04-28 15:47:25 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
ActivitySettingsSecurityBinding activitySettingsSecurityBinding =
|
|
|
|
ActivitySettingsSecurityBinding.inflate(getLayoutInflater());
|
2021-01-26 18:43:25 +01:00
|
|
|
setContentView(activitySettingsSecurityBinding.getRoot());
|
2021-01-26 16:10:31 +01:00
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.topAppBar.setNavigationOnClickListener(v -> finish());
|
2020-05-09 16:50:45 +02:00
|
|
|
|
2021-06-10 18:03:52 +02:00
|
|
|
cacheSizeDataList = getResources().getStringArray(R.array.cacheSizeList);
|
|
|
|
cacheSizeImagesList = getResources().getStringArray(R.array.cacheSizeList);
|
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.cacheSizeDataSelected.setText(
|
2022-09-21 07:43:00 +02:00
|
|
|
tinyDB.getString(
|
|
|
|
"cacheSizeStr", getString(R.string.cacheSizeDataSelectionSelectedText)));
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.cacheSizeImagesSelected.setText(
|
2022-09-21 07:43:00 +02:00
|
|
|
tinyDB.getString(
|
|
|
|
"cacheSizeImagesStr",
|
|
|
|
getString(R.string.cacheSizeImagesSelectionSelectedText)));
|
2020-05-09 16:50:45 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (cacheSizeDataSelectedChoice == 0) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
cacheSizeDataSelectedChoice = tinyDB.getInt("cacheSizeId");
|
2020-05-09 16:50:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (cacheSizeImagesSelectedChoice == 0) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
cacheSizeImagesSelectedChoice = tinyDB.getInt("cacheSizeImagesId");
|
2020-05-09 16:50:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 12:55:19 +01:00
|
|
|
activitySettingsSecurityBinding.switchBiometric.setChecked(
|
|
|
|
tinyDB.getBoolean("biometricStatus", false));
|
2021-03-09 22:15:42 +01:00
|
|
|
|
|
|
|
// biometric switcher
|
2023-01-19 12:55:19 +01:00
|
|
|
activitySettingsSecurityBinding.switchBiometric.setOnCheckedChangeListener(
|
2022-09-21 07:43:00 +02:00
|
|
|
(buttonView, isChecked) -> {
|
|
|
|
if (isChecked) {
|
|
|
|
|
|
|
|
BiometricManager biometricManager = BiometricManager.from(ctx);
|
|
|
|
KeyguardManager keyguardManager =
|
|
|
|
(KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE);
|
|
|
|
|
|
|
|
if (!keyguardManager.isDeviceSecure()) {
|
|
|
|
|
|
|
|
switch (biometricManager.canAuthenticate(
|
|
|
|
BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) {
|
|
|
|
case BiometricManager.BIOMETRIC_SUCCESS:
|
|
|
|
tinyDB.putBoolean("biometricStatus", true);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.success(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.settingsSave));
|
2022-09-21 07:43:00 +02:00
|
|
|
break;
|
|
|
|
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
|
|
|
|
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
|
|
|
|
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
|
|
|
|
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
|
|
|
|
tinyDB.putBoolean("biometricStatus", false);
|
2023-01-19 12:55:19 +01:00
|
|
|
activitySettingsSecurityBinding.switchBiometric.setChecked(
|
|
|
|
false);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.error(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.biometricNotSupported));
|
2022-09-21 07:43:00 +02:00
|
|
|
break;
|
|
|
|
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
|
|
|
|
tinyDB.putBoolean("biometricStatus", false);
|
2023-01-19 12:55:19 +01:00
|
|
|
activitySettingsSecurityBinding.switchBiometric.setChecked(
|
|
|
|
false);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.error(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.biometricNotAvailable));
|
2022-09-21 07:43:00 +02:00
|
|
|
break;
|
|
|
|
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
|
|
|
|
tinyDB.putBoolean("biometricStatus", false);
|
2023-01-19 12:55:19 +01:00
|
|
|
activitySettingsSecurityBinding.switchBiometric.setChecked(
|
|
|
|
false);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.info(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.enrollBiometric));
|
2022-09-21 07:43:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-09 22:15:42 +01:00
|
|
|
|
2022-08-12 04:02:04 +02:00
|
|
|
tinyDB.putBoolean("biometricStatus", true);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.success(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.settingsSave));
|
2022-09-21 07:43:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-09 22:15:42 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
tinyDB.putBoolean("biometricStatus", false);
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.success(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.settingsSave));
|
2022-09-21 07:43:00 +02:00
|
|
|
}
|
|
|
|
});
|
2021-03-09 22:15:42 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
activitySettingsSecurityBinding.biometricFrame.setOnClickListener(
|
2023-01-19 12:55:19 +01:00
|
|
|
v ->
|
|
|
|
activitySettingsSecurityBinding.switchBiometric.setChecked(
|
|
|
|
!activitySettingsSecurityBinding.switchBiometric.isChecked()));
|
2021-12-17 20:56:12 +01:00
|
|
|
|
2020-05-09 16:50:45 +02:00
|
|
|
// clear cache setter
|
|
|
|
File cacheDir = appCtx.getCacheDir();
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.clearCacheSelected.setText(
|
2022-09-21 07:43:00 +02:00
|
|
|
FileUtils.byteCountToDisplaySize((int) FileUtils.sizeOfDirectory(cacheDir)));
|
2020-05-09 16:50:45 +02:00
|
|
|
|
|
|
|
// clear cache
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.clearCacheSelectionFrame.setOnClickListener(
|
2022-09-21 07:43:00 +02:00
|
|
|
v1 -> {
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(ctx)
|
|
|
|
.setTitle(R.string.clearCacheDialogHeader)
|
|
|
|
.setMessage(
|
|
|
|
getResources()
|
|
|
|
.getString(R.string.clearCacheDialogMessage))
|
|
|
|
.setNeutralButton(
|
|
|
|
R.string.cancelButton,
|
|
|
|
(dialog, which) -> dialog.dismiss())
|
|
|
|
.setPositiveButton(
|
|
|
|
R.string.menuDeleteText,
|
|
|
|
(dialog, which) -> {
|
|
|
|
try {
|
|
|
|
|
|
|
|
FileUtils.deleteDirectory(cacheDir);
|
|
|
|
FileUtils.forceMkdir(cacheDir);
|
|
|
|
this.recreate();
|
|
|
|
this.overridePendingTransition(0, 0);
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
2024-02-11 14:20:23 +01:00
|
|
|
// Log.e("SettingsSecurity", e.toString());
|
2022-09-21 07:43:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
2022-08-07 21:34:02 +02:00
|
|
|
});
|
2020-05-09 16:50:45 +02:00
|
|
|
|
|
|
|
// cache size images selection dialog
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.cacheSizeImagesSelectionFrame.setOnClickListener(
|
2022-09-21 07:43:00 +02:00
|
|
|
view -> {
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(ctx)
|
|
|
|
.setTitle(R.string.cacheSizeImagesDialogHeader)
|
|
|
|
.setCancelable(cacheSizeImagesSelectedChoice != -1)
|
|
|
|
.setSingleChoiceItems(
|
|
|
|
cacheSizeImagesList,
|
|
|
|
cacheSizeImagesSelectedChoice,
|
|
|
|
(dialogInterfaceTheme, i) -> {
|
|
|
|
cacheSizeImagesSelectedChoice = i;
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding
|
|
|
|
.cacheSizeImagesSelected.setText(
|
2022-09-21 07:43:00 +02:00
|
|
|
cacheSizeImagesList[i]);
|
|
|
|
tinyDB.putString(
|
|
|
|
"cacheSizeImagesStr",
|
|
|
|
cacheSizeImagesList[i]);
|
|
|
|
tinyDB.putInt("cacheSizeImagesId", i);
|
|
|
|
|
|
|
|
dialogInterfaceTheme.dismiss();
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.success(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.settingsSave));
|
2022-09-21 07:43:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
2022-08-07 21:34:02 +02:00
|
|
|
});
|
2020-05-09 16:50:45 +02:00
|
|
|
|
|
|
|
// cache size data selection dialog
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.cacheSizeDataSelectionFrame.setOnClickListener(
|
2022-09-21 07:43:00 +02:00
|
|
|
view -> {
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(ctx)
|
|
|
|
.setTitle(R.string.cacheSizeDataDialogHeader)
|
|
|
|
.setCancelable(cacheSizeDataSelectedChoice != -1)
|
|
|
|
.setSingleChoiceItems(
|
|
|
|
cacheSizeDataList,
|
|
|
|
cacheSizeDataSelectedChoice,
|
|
|
|
(dialogInterfaceTheme, i) -> {
|
|
|
|
cacheSizeDataSelectedChoice = i;
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding
|
|
|
|
.cacheSizeDataSelected.setText(
|
|
|
|
cacheSizeDataList[i]);
|
2022-09-21 07:43:00 +02:00
|
|
|
tinyDB.putString(
|
|
|
|
"cacheSizeStr", cacheSizeDataList[i]);
|
|
|
|
tinyDB.putInt("cacheSizeId", i);
|
|
|
|
|
|
|
|
dialogInterfaceTheme.dismiss();
|
2024-02-11 14:20:23 +01:00
|
|
|
SnackBar.success(
|
|
|
|
ctx,
|
|
|
|
findViewById(android.R.id.content),
|
|
|
|
getString(R.string.settingsSave));
|
2022-09-21 07:43:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
2022-08-07 21:34:02 +02:00
|
|
|
});
|
2020-05-09 16:50:45 +02:00
|
|
|
|
2020-04-26 15:36:33 +02:00
|
|
|
// certs deletion
|
2024-02-11 14:20:23 +01:00
|
|
|
activitySettingsSecurityBinding.certsFrame.setOnClickListener(
|
2022-09-21 07:43:00 +02:00
|
|
|
v1 -> {
|
|
|
|
MaterialAlertDialogBuilder materialAlertDialogBuilder =
|
|
|
|
new MaterialAlertDialogBuilder(ctx)
|
|
|
|
.setTitle(R.string.settingsCertsPopupTitle)
|
|
|
|
.setMessage(
|
|
|
|
getResources()
|
|
|
|
.getString(R.string.settingsCertsPopupMessage))
|
|
|
|
.setNeutralButton(
|
|
|
|
R.string.cancelButton,
|
|
|
|
(dialog, which) -> dialog.dismiss())
|
|
|
|
.setPositiveButton(
|
|
|
|
R.string.menuDeleteText,
|
|
|
|
(dialog, which) -> {
|
|
|
|
appCtx.getSharedPreferences(
|
|
|
|
MemorizingTrustManager
|
|
|
|
.KEYSTORE_NAME,
|
|
|
|
Context.MODE_PRIVATE)
|
|
|
|
.edit()
|
|
|
|
.remove(MemorizingTrustManager.KEYSTORE_KEY)
|
|
|
|
.apply();
|
|
|
|
AppUtil.logout(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
materialAlertDialogBuilder.create().show();
|
2022-08-07 21:34:02 +02:00
|
|
|
});
|
2020-04-26 15:36:33 +02:00
|
|
|
}
|
|
|
|
}
|