Request storage rw for saving logs

Fix #2993
This commit is contained in:
topjohnwu
2020-07-17 01:27:35 -07:00
parent f7abc03dac
commit 113eec59f9
26 changed files with 77 additions and 86 deletions

View File

@@ -53,7 +53,7 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
// ---
interface Callback {
fun onItemPressed(view: View, item: SettingsItem)
fun onItemPressed(view: View, item: SettingsItem, method: () -> Unit)
fun onItemChanged(view: View, item: SettingsItem)
}
@@ -80,9 +80,10 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
override val layoutRes = R.layout.item_settings_toggle
override fun onPressed(view: View, callback: Callback) {
callback.onItemPressed(view, this)
value = !value
super.onPressed(view, callback)
callback.onItemPressed(view, this) {
value = !value
super.onPressed(view, callback)
}
}
fun onTouched(view: View, callback: Callback, event: MotionEvent): Boolean {
@@ -103,27 +104,28 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
protected abstract val intermediate: String?
override fun onPressed(view: View, callback: Callback) {
callback.onItemPressed(view, this)
MagiskDialog(view.context)
.applyTitle(title.getText(resources))
.applyView(getView(view.context))
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = android.R.string.ok
onClick {
intermediate?.let { result ->
preventDismiss = false
value = result
it.dismiss()
super.onPressed(view, callback)
return@onClick
callback.onItemPressed(view, this) {
MagiskDialog(view.context)
.applyTitle(title.getText(resources))
.applyView(getView(view.context))
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = android.R.string.ok
onClick {
intermediate?.let { result ->
preventDismiss = false
value = result
it.dismiss()
super.onPressed(view, callback)
return@onClick
}
preventDismiss = true
}
preventDismiss = true
}
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.reveal()
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.reveal()
}
}
abstract fun getView(context: Context): View
@@ -156,18 +158,19 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
override fun onPressed(view: View, callback: Callback) {
if (entries.isEmpty() || entryValues.isEmpty()) return
callback.onItemPressed(view, this)
MagiskDialog(view.context)
.applyTitle(title.getText(resources))
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.applyAdapter(entries) {
value = it
notifyPropertyChanged(BR.selectedEntry)
super.onPressed(view, callback)
}
.reveal()
callback.onItemPressed(view, this) {
MagiskDialog(view.context)
.applyTitle(title.getText(resources))
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.applyAdapter(entries) {
value = it
notifyPropertyChanged(BR.selectedEntry)
super.onPressed(view, callback)
}
.reveal()
}
}
}
@@ -177,8 +180,9 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
override val layoutRes = R.layout.item_settings_blank
override fun onPressed(view: View, callback: Callback) {
callback.onItemPressed(view, this)
super.onPressed(view, callback)
callback.onItemPressed(view, this) {
super.onPressed(view, callback)
}
}
}

View File

@@ -12,6 +12,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavDirections
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.model.events.*
@@ -85,8 +86,14 @@ abstract class BaseViewModel(
PermissionEvent(permissions.toList(), callback).publish()
}
fun withExternalRW(callback: (Boolean) -> Unit) {
withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, callback = callback)
fun withExternalRW(callback: () -> Unit) {
withPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE) {
if (!it) {
SnackbarEvent(R.string.external_rw_permission_denied).publish()
} else {
callback()
}
}
}
fun back() = BackPressEvent().publish()

View File

@@ -107,8 +107,6 @@ class FlashViewModel(
}
private fun savePressed() = withExternalRW {
if (!it)
return@withExternalRW
viewModelScope.launch {
val name = Const.MAGISK_INSTALL_LOG_FILENAME.format(now.toTime(timeFormatStandard))
val file = File(Config.downloadDirectory, name)

View File

@@ -131,9 +131,7 @@ class HomeViewModel(
fun onManagerPressed() = ManagerInstallDialog().publish()
fun onMagiskPressed() = withExternalRW {
if (it) {
HomeFragmentDirections.actionHomeFragmentToInstallFragment().publish()
}
HomeFragmentDirections.actionHomeFragmentToInstallFragment().publish()
}
fun hideNotice() {

View File

@@ -57,7 +57,7 @@ class LogViewModel(
items.lastOrNull()?.isBottom = true
}
fun saveMagiskLog() {
fun saveMagiskLog() = withExternalRW {
val now = Calendar.getInstance()
val filename = "magisk_log_%04d%02d%02d_%02d%02d%02d.log".format(
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
@@ -70,7 +70,7 @@ class LogViewModel(
logFile.createNewFile()
} catch (e: IOException) {
Timber.e(e)
return
return@withExternalRW
}
Shell.su("cat ${Const.MAGISK_LOG} > $logFile").submit {

View File

@@ -19,7 +19,6 @@ import com.topjohnwu.magisk.model.entity.internal.DownloadSubject
import com.topjohnwu.magisk.model.entity.recycler.*
import com.topjohnwu.magisk.model.events.InstallExternalModuleEvent
import com.topjohnwu.magisk.model.events.OpenChangelogEvent
import com.topjohnwu.magisk.model.events.SnackbarEvent
import com.topjohnwu.magisk.model.events.dialog.ModuleInstallDialog
import com.topjohnwu.magisk.ui.base.*
import com.topjohnwu.magisk.utils.EndlessRecyclerScrollListener
@@ -323,26 +322,15 @@ class ModuleViewModel(
}
fun downloadPressed(item: RepoItem) = withExternalRW {
if (it)
ModuleInstallDialog(item.item).publish()
else
permissionDenied()
ModuleInstallDialog(item.item).publish()
}
fun installPressed() = withExternalRW {
if (it)
InstallExternalModuleEvent().publish()
else
permissionDenied()
InstallExternalModuleEvent().publish()
}
fun infoPressed(item: RepoItem) = OpenChangelogEvent(item.item).publish()
fun infoPressed(item: ModuleItem) {
OpenChangelogEvent(item.repo ?: return).publish()
}
private fun permissionDenied() {
SnackbarEvent(R.string.module_permission_declined).publish()
}
}

View File

@@ -87,9 +87,9 @@ class SettingsViewModel(
return list
}
override fun onItemPressed(view: View, item: SettingsItem) = when (item) {
is DownloadPath -> requireRWPermission()
else -> Unit
override fun onItemPressed(view: View, item: SettingsItem, method: () -> Unit) = when (item) {
is DownloadPath -> withExternalRW(method)
else -> method()
}
override fun onItemChanged(view: View, item: SettingsItem) = when (item) {
@@ -136,10 +136,6 @@ class SettingsViewModel(
}
}
private fun requireRWPermission() {
withExternalRW { if (!it) requireRWPermission() }
}
private fun updateManager(hide: Boolean) {
if (hide) {
PatchAPK.hideManager(get(), Hide.value)