Merge pull request #2849 from dlvoy/fix-prefs-import-crash

Fix preferences import/export related crashes
This commit is contained in:
Milos Kozak 2020-07-24 10:06:46 +02:00 committed by GitHub
commit fc69e851d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions

View file

@ -257,6 +257,7 @@ dependencies {
implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation "androidx.preference:preference-ktx:1.1.1"
implementation "androidx.activity:activity-ktx:${activityVersion}"
implementation "androidx.fragment:fragment:${fragmentVersion}"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'

View file

@ -209,18 +209,24 @@ class ImportExportPrefs @Inject constructor(
} catch (e: IOException) {
ToastUtils.errorToast(activity, e.message)
log.error(TAG, "Unhandled exception", e)
} catch (e: PrefFileNotFoundError) {
ToastUtils.Long.errorToast(activity, resourceHelper.gs(R.string.preferences_export_canceled)
+ "\n\n" + resourceHelper.gs(R.string.filenotfound)
+ ": " + e.message
+ "\n\n" + resourceHelper.gs(R.string.needstoragepermission))
log.error(TAG, "File system exception", e)
} catch (e: PrefIOError) {
ToastUtils.Long.errorToast(activity, resourceHelper.gs(R.string.preferences_export_canceled)
+ "\n\n" + resourceHelper.gs(R.string.needstoragepermission)
+ ": " + e.message)
log.error(TAG, "File system exception", e)
}
}
}
fun importSharedPreferences(fragment: Fragment) {
fragment.activity?.let { fragmentAct ->
val callForPrefFile = fragmentAct.registerForActivityResult(PrefsFileContract()) {
it?.let {
importSharedPreferences(fragmentAct, it)
}
}
callForPrefFile.launch(null)
importSharedPreferences(fragmentAct)
}
}
@ -230,7 +236,15 @@ class ImportExportPrefs @Inject constructor(
importSharedPreferences(activity, it)
}
}
callForPrefFile.launch(null)
try {
callForPrefFile.launch(null)
} catch (e: IllegalArgumentException) {
// this exception happens on some early implementations of ActivityResult contracts
// when registered and called for the second time
ToastUtils.errorToast(activity, resourceHelper.gs(R.string.goto_main_try_again))
log.error(TAG, "Internal android framework exception", e)
}
}
private fun importSharedPreferences(activity: Activity, importFile: PrefsFile) {
@ -267,7 +281,7 @@ class ImportExportPrefs @Inject constructor(
restartAppAfterImport(activity)
} else {
// for impossible imports it should not be called
ToastUtils.errorToast(activity, "Cannot import preferences!")
ToastUtils.errorToast(activity, resourceHelper.gs(R.string.preferences_import_impossible))
}
})

View file

@ -206,6 +206,8 @@
<string name="password_preferences_decrypt_prompt">You will be asked for master password, which is needed to decrypt imported preferences.</string>
<string name="preferences_export_canceled">Export canceled! Preferences were NOT exported!</string>
<string name="preferences_import_canceled">Import canceled! Preferences were NOT imported!</string>
<string name="preferences_import_impossible">Cannot import preferences!</string>
<string name="goto_main_try_again">Please go back to main screen and try again.</string>
<string name="preferences_import_list_title">Select file to import</string>
@ -950,7 +952,7 @@
<string name="askforpermission">Ask for permission</string>
<string name="needsystemwindowpermission">Application needs system window permission for notifications</string>
<string name="needlocationpermission">Application needs location permission for BT scan and WiFi identification</string>
<string name="needstoragepermission">Application needs storage permission to be able store log files</string>
<string name="needstoragepermission">Application needs storage permission to be able store log files and export settings</string>
<string name="request">Request</string>
<string name="insulinsourcesetup">Configure Insulin plugin</string>
<string name="exit">Exit</string>

View file

@ -12,6 +12,7 @@ buildscript {
dagger_version = '2.28.1'
coroutinesVersion = '1.3.7'
activityVersion = '1.2.0-alpha06'
fragmentVersion = '1.3.0-alpha07'
ormLiteVersion = "4.46"
}
repositories {