From f532341cccad2f52e1d6c4b9f71b4f143bb6dc95 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Wed, 1 Apr 2020 20:34:05 +0200 Subject: [PATCH] Post review fixes --- .../general/maintenance/ImportExportPrefs.kt | 37 +++++++++---------- .../alertDialogs/PrefImportSummaryDialog.kt | 2 - .../alertDialogs/TwoMessagesAlertDialog.kt | 2 - 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt index f5ad429641..d7a5b63949 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.kt @@ -82,7 +82,7 @@ class ImportExportPrefs @Inject constructor( } fun exportSharedPreferences(f: Fragment) { - exportSharedPreferences(f.activity) + f.activity?.let { exportSharedPreferences(it) } } fun verifyStoragePermissions(fragment: Fragment) { @@ -94,14 +94,11 @@ class ImportExportPrefs @Inject constructor( } } - private fun prepareMetadata(context: Context?): Map { + private fun prepareMetadata(context: Context): Map { val metadata: MutableMap = mutableMapOf() - if (context != null) { - metadata[PrefsMetadataKey.DEVICE_NAME] = PrefMetadata(detectUserName(context), PrefsStatus.OK) - } - + metadata[PrefsMetadataKey.DEVICE_NAME] = PrefMetadata(detectUserName(context), PrefsStatus.OK) metadata[PrefsMetadataKey.CREATED_AT] = PrefMetadata(DateUtil.toISOString(Date()), PrefsStatus.OK) metadata[PrefsMetadataKey.AAPS_VERSION] = PrefMetadata(BuildConfig.VERSION_NAME, PrefsStatus.OK) metadata[PrefsMetadataKey.AAPS_FLAVOUR] = PrefMetadata(BuildConfig.FLAVOR, PrefsStatus.OK) @@ -141,15 +138,15 @@ class ImportExportPrefs @Inject constructor( private fun prefsEncryptionIsDisabled() = buildHelper.isEngineeringMode() && !sp.getBoolean(resourceHelper.gs(R.string.key_maintenance_encrypt_exported_prefs), true) - private fun askForMasterPass(activity: Activity?, @StringRes canceledMsg: Int, then: ((password: String) -> Unit)) { - passwordCheck.queryPassword(activity!!, R.string.master_password, R.string.key_master_password, { password -> + private fun askForMasterPass(activity: Activity, @StringRes canceledMsg: Int, then: ((password: String) -> Unit)) { + passwordCheck.queryPassword(activity, R.string.master_password, R.string.key_master_password, { password -> then(password) }, { ToastUtils.warnToast(activity, resourceHelper.gs(canceledMsg)) }) } - private fun askForMasterPassIfNeeded(activity: Activity?, @StringRes canceledMsg: Int, then: ((password: String) -> Unit)) { + private fun askForMasterPassIfNeeded(activity: Activity, @StringRes canceledMsg: Int, then: ((password: String) -> Unit)) { if (prefsEncryptionIsDisabled()) { then("") } else { @@ -157,9 +154,9 @@ class ImportExportPrefs @Inject constructor( } } - private fun assureMasterPasswordSet(activity: Activity?, @StringRes wrongPwdTitle: Int): Boolean { + private fun assureMasterPasswordSet(activity: Activity, @StringRes wrongPwdTitle: Int): Boolean { if (!sp.contains(R.string.key_master_password) || (sp.getString(R.string.key_master_password, "") == "")) { - WarningDialog.showWarning(activity!!, + WarningDialog.showWarning(activity, resourceHelper.gs(wrongPwdTitle), resourceHelper.gs(R.string.master_password_missing, resourceHelper.gs(R.string.configbuilder_general), resourceHelper.gs(R.string.protection)), R.string.nav_preferences, { @@ -173,35 +170,35 @@ class ImportExportPrefs @Inject constructor( return true } - private fun askToConfirmExport(activity: Activity?, then: ((password: String) -> Unit)) { + private fun askToConfirmExport(activity: Activity, then: ((password: String) -> Unit)) { if (!prefsEncryptionIsDisabled() && !assureMasterPasswordSet(activity, R.string.nav_export)) return - TwoMessagesAlertDialog.showAlert(activity!!, resourceHelper.gs(R.string.nav_export), + TwoMessagesAlertDialog.showAlert(activity, resourceHelper.gs(R.string.nav_export), resourceHelper.gs(R.string.export_to) + " " + encFile + " ?", resourceHelper.gs(R.string.password_preferences_encrypt_prompt), { askForMasterPassIfNeeded(activity, R.string.preferences_export_canceled, then) }, null, R.drawable.ic_header_export) } - private fun askToConfirmImport(activity: Activity?, fileToImport: File, then: ((password: String) -> Unit)) { + private fun askToConfirmImport(activity: Activity, fileToImport: File, then: ((password: String) -> Unit)) { if (encFile.exists()) { if (!assureMasterPasswordSet(activity, R.string.nav_import)) return - TwoMessagesAlertDialog.showAlert(activity!!, resourceHelper.gs(R.string.nav_import), + TwoMessagesAlertDialog.showAlert(activity, resourceHelper.gs(R.string.nav_import), resourceHelper.gs(R.string.import_from) + " " + fileToImport + " ?", resourceHelper.gs(R.string.password_preferences_decrypt_prompt), { askForMasterPass(activity, R.string.preferences_import_canceled, then) }, null, R.drawable.ic_header_import) } else { - OKDialog.showConfirmation(activity!!, resourceHelper.gs(R.string.nav_import), + OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.nav_import), resourceHelper.gs(R.string.import_from) + " " + fileToImport + " ?", Runnable { then("") }) } } - private fun exportSharedPreferences(activity: Activity?) { + private fun exportSharedPreferences(activity: Activity) { askToConfirmExport(activity) { password -> try { val entries: MutableMap = mutableMapOf() @@ -226,10 +223,10 @@ class ImportExportPrefs @Inject constructor( } fun importSharedPreferences(fragment: Fragment) { - importSharedPreferences(fragment.activity) + fragment.activity?.let { importSharedPreferences(it) } } - fun importSharedPreferences(activity: Activity?) { + fun importSharedPreferences(activity: Activity) { val importFile = prefsImportFile() @@ -325,7 +322,7 @@ class ImportExportPrefs @Inject constructor( return importOk } - private fun restartAppAfterImport(context: Context?) { + private fun restartAppAfterImport(context: Context) { sp.putBoolean(R.string.key_setupwizard_processed, true) show(context!!, resourceHelper.gs(R.string.setting_imported), resourceHelper.gs(R.string.restartingapp), Runnable { log.debug(TAG, "Exiting") diff --git a/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/PrefImportSummaryDialog.kt b/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/PrefImportSummaryDialog.kt index db5731f8fc..d5b6900386 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/PrefImportSummaryDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/PrefImportSummaryDialog.kt @@ -26,8 +26,6 @@ import java.util.* object PrefImportSummaryDialog { @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun showSummary(context: Context, importOk: Boolean, importPossible: Boolean, prefs: Prefs, ok: (() -> Unit)?, cancel: (() -> Unit)? = null) { @StyleRes val theme: Int = if (importOk) R.style.AppTheme else { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/TwoMessagesAlertDialog.kt b/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/TwoMessagesAlertDialog.kt index 7e8a4d47a6..9c1fbf37aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/TwoMessagesAlertDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/alertDialogs/TwoMessagesAlertDialog.kt @@ -14,8 +14,6 @@ import info.nightscout.androidaps.utils.runOnUiThread object TwoMessagesAlertDialog { @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun showAlert(context: Context, title: String, message: String, secondMessage: String, ok: (() -> Unit)?, cancel: (() -> Unit)? = null, @DrawableRes icon: Int? = null) { val secondMessageLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_two_messages, null)