Skip to content

Commit

Permalink
feat: request storage permission in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 29, 2024
1 parent dfdebd7 commit 66c7fc0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 44 deletions.
42 changes: 4 additions & 38 deletions app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.blankj.utilcode.util.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.osfans.trime.R
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.sound.SoundThemeManager
Expand All @@ -31,6 +27,7 @@ import com.osfans.trime.ime.core.RimeWrapper
import com.osfans.trime.ime.core.Status
import com.osfans.trime.ui.setup.SetupActivity
import com.osfans.trime.util.applyTranslucentSystemBars
import com.osfans.trime.util.isStorageAvailable
import com.osfans.trime.util.progressBarDialogIndeterminate
import com.osfans.trime.util.rimeActionWithResultDialog
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -161,45 +158,14 @@ class PrefMainActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()
requestExternalStoragePermission()
if (isStorageAvailable()) {
SoundThemeManager.init()
}
}

override fun onDestroy() {
super.onDestroy()
loadingDialog?.dismiss()
loadingDialog = null
}

private fun requestExternalStoragePermission() {
XXPermissions.with(this)
.permission(Permission.MANAGE_EXTERNAL_STORAGE)
.request(
object : OnPermissionCallback {
override fun onGranted(
permissions: List<String>,
all: Boolean,
) {
if (all) {
ToastUtils.showShort(R.string.external_storage_permission_granted)
SoundThemeManager.init()
}
}

override fun onDenied(
permissions: List<String>,
never: Boolean,
) {
if (never) {
ToastUtils.showShort(R.string.external_storage_permission_denied)
XXPermissions.startPermissionActivity(
this@PrefMainActivity,
permissions,
)
} else {
ToastUtils.showShort(R.string.external_storage_permission_denied)
}
}
},
)
}
}
21 changes: 15 additions & 6 deletions app/src/main/java/com/osfans/trime/ui/setup/SetupPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package com.osfans.trime.ui.setup
import android.content.Context
import com.osfans.trime.R
import com.osfans.trime.util.InputMethodUtils
import com.osfans.trime.util.appContext
import com.osfans.trime.util.isStorageAvailable
import com.osfans.trime.util.requestExternalStoragePermission

enum class SetupPage {
Permissions,
Enable,
Select,
;

fun getStepText(context: Context) =
context.getText(
when (this) {
Enable -> R.string.setup__step_one
Select -> R.string.setup__step_two
Permissions -> R.string.setup__step_one
Enable -> R.string.setup__step_two
Select -> R.string.setup__step_three
},
)

fun getHintText(context: Context) =
context.getText(
when (this) {
Permissions -> R.string.setup__request_permmision_hint
Enable -> R.string.setup__enable_ime_hint
Select -> R.string.setup__select_ime_hint
},
Expand All @@ -28,31 +34,34 @@ enum class SetupPage {
fun getButtonText(context: Context) =
context.getText(
when (this) {
Permissions -> R.string.setup__request_permmision
Enable -> R.string.setup__enable_ime
Select -> R.string.setup__select_ime
},
)

fun getButtonAction(context: Context) {
when (this) {
Permissions -> context.requestExternalStoragePermission()
Enable -> InputMethodUtils.showImeEnablerActivity(context)
Select -> InputMethodUtils.showImePicker()
}
}

fun isDone() =
when (this) {
Permissions -> appContext.isStorageAvailable()
Enable -> InputMethodUtils.checkIsTrimeEnabled()
Select -> InputMethodUtils.checkIsTrimeSelected()
}

companion object {
fun SetupPage.isLastPage() = this == values().last()
fun SetupPage.isLastPage() = this == entries.last()

fun Int.isLastPage() = this == values().size - 1
fun Int.isLastPage() = this == entries.size - 1

fun hasUndonePage() = values().any { !it.isDone() }
fun hasUndonePage() = entries.any { !it.isDone() }

fun firstUndonePage() = values().firstOrNull { !it.isDone() }
fun firstUndonePage() = entries.firstOrNull { !it.isDone() }
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/osfans/trime/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.fragment.app.Fragment
import androidx.preference.Preference
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.osfans.trime.R
Expand Down Expand Up @@ -147,3 +148,35 @@ inline fun Context.isStorageAvailable(): Boolean {
return XXPermissions.isGranted(this, Permission.MANAGE_EXTERNAL_STORAGE) &&
Environment.getExternalStorageDirectory().absolutePath.isNotEmpty()
}

fun Context.requestExternalStoragePermission() {
XXPermissions.with(this)
.permission(Permission.MANAGE_EXTERNAL_STORAGE)
.request(
object : OnPermissionCallback {
override fun onGranted(
permissions: List<String>,
all: Boolean,
) {
if (all) {
ToastUtils.showShort(R.string.external_storage_permission_granted)
}
}

override fun onDenied(
permissions: List<String>,
never: Boolean,
) {
if (never) {
ToastUtils.showShort(R.string.external_storage_permission_denied)
XXPermissions.startPermissionActivity(
this@requestExternalStoragePermission,
permissions,
)
} else {
ToastUtils.showShort(R.string.external_storage_permission_denied)
}
}
},
)
}
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,7 @@
<string name="pref_keyboard__landscape">横屏模式</string>
<string name="keyboard__split_title">转换到横屏键盘</string>
<string name="keyboard__split_space_title">自动分割键盘比例</string>
<string name="setup__step_three">第 3 步</string>
<string name="setup__request_permmision_hint">当前同文需要存储权限以访问配置、使得用户容易更改。</string>
<string name="setup__request_permmision">请求存储权限</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,7 @@
<string name="pref_keyboard__landscape">橫向模式</string>
<string name="keyboard__split_title">轉換到橫屏鍵盤</string>
<string name="keyboard__split_space_title">自動分割鍵盤比例</string>
<string name="setup__step_three">第 3 步</string>
<string name="setup__request_permmision_hint">當前同文須要存儲權限以訪問配置檔案、使得使用者容易修改。</string>
<string name="setup__request_permmision">请求存储权限</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,7 @@
<string name="pref_keyboard__landscape">Landscape Mode</string>
<string name="keyboard__split_title">Change to Landscape keyboard</string>
<string name="keyboard__split_space_title">Auto Split Keyboard Space Ratio</string>
<string name="setup__step_three">Setup 3</string>
<string name="setup__request_permmision_hint">Currently Trime requests storage permmissions to access its configuration\nand allow users to modify easily.</string>
<string name="setup__request_permmision">Request storage permmision</string>
</resources>
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ android.useAndroidX=true

# enlarge heap size to 2GB
org.gradle.jvmargs=-Xmx2048M -XX:+UseParallelGC

buildABI=x86,arm64-v8a

0 comments on commit 66c7fc0

Please sign in to comment.