Skip to content

Commit c55fa0d

Browse files
committed
Update
1 parent ef822d3 commit c55fa0d

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

app/src/main/java/com/tool/tree/SplashActivity.kt

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import kotlinx.coroutines.withContext
2828
import java.io.BufferedReader
2929
import java.io.DataOutputStream
3030
import java.io.File
31+
import android.content.res.Configuration
32+
import java.util.Locale
3133

3234
class SplashActivity : AppCompatActivity() {
3335

@@ -63,14 +65,11 @@ class SplashActivity : AppCompatActivity() {
6365
showAgreementDialog()
6466
}
6567

66-
val rotateAnim = AnimationUtils.loadAnimation(this, R.anim.ic_settings_rotate)
67-
binding.startLogoXml.startAnimation(rotateAnim)
68+
binding.startLogoXml.startAnimation(AnimationUtils.loadAnimation(this, R.anim.ic_settings_rotate))
6869

6970
applyTheme()
7071
}
7172

72-
// =================== PERMISSIONS & STORAGE ===================
73-
7473
private fun getRequiredPermissions(): Array<String> {
7574
return when {
7675
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> {
@@ -81,37 +80,31 @@ class SplashActivity : AppCompatActivity() {
8180
}
8281

8382
private fun hasPermissions(): Boolean {
84-
// 1. Kiểm tra các quyền Runtime cơ bản
8583
val basicPermissions = getRequiredPermissions().all {
8684
ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
8785
}
8886

89-
// 2. Kiểm tra quyền MANAGE_EXTERNAL_STORAGE (Android 11+)
9087
val manageStoragePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
9188
Environment.isExternalStorageManager()
9289
} else {
93-
true // Các bản cũ không có quyền này
90+
true
9491
}
9592

9693
return basicPermissions && manageStoragePermission
9794
}
9895

9996
private fun requestAppPermissions() {
10097
saveAgreement()
101-
102-
// Bước 1: Xin các quyền Runtime cơ bản trước
10398
val missingBasic = getRequiredPermissions().filter {
10499
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
105100
}
106101

107102
if (missingBasic.isNotEmpty()) {
108103
ActivityCompat.requestPermissions(this, missingBasic.toTypedArray(), REQUEST_CODE_PERMISSIONS)
109-
}
110-
// Bước 2: Nếu quyền cơ bản có rồi nhưng thiếu MANAGE_EXTERNAL_STORAGE trên A11+
104+
}
111105
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
112106
requestManageStoragePermission()
113-
}
114-
// Bước 3: Đã đủ hết quyền
107+
}
115108
else {
116109
startLogic()
117110
}
@@ -133,7 +126,6 @@ class SplashActivity : AppCompatActivity() {
133126
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
134127
if (requestCode == REQUEST_CODE_PERMISSIONS) {
135128
if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
136-
// Sau khi có quyền cơ bản, kiểm tra tiếp MANAGE_STORAGE nếu cần
137129
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
138130
requestManageStoragePermission()
139131
} else {
@@ -152,7 +144,7 @@ class SplashActivity : AppCompatActivity() {
152144
if (Environment.isExternalStorageManager()) {
153145
startLogic()
154146
} else {
155-
finish() // Người dùng từ chối cấp quyền All Files Access
147+
finish()
156148
}
157149
}
158150
}
@@ -167,14 +159,11 @@ class SplashActivity : AppCompatActivity() {
167159

168160
override fun onResume() {
169161
super.onResume()
170-
// Tự động kiểm tra nếu đã đồng nhất thỏa thuận nhưng chưa khởi chạy
171162
if (hasAgreed() && !started && hasPermissions()) {
172163
startLogic()
173164
}
174165
}
175166

176-
// =================== SHELL & LOGS ===================
177-
178167
private fun runBeforeStartSh(config: KrScriptConfig, hasRoot: Boolean) {
179168
lifecycleScope.launch(Dispatchers.IO) {
180169
try {
@@ -187,10 +176,8 @@ class SplashActivity : AppCompatActivity() {
187176
)
188177
}
189178
}
190-
191179
val outJob = launch { readStreamAsync(p.inputStream.bufferedReader()) }
192180
val errJob = launch { readStreamAsync(p.errorStream.bufferedReader()) }
193-
194181
p.waitFor()
195182
shellJob.join()
196183
outJob.join()
@@ -229,14 +216,21 @@ class SplashActivity : AppCompatActivity() {
229216
}
230217
}
231218

232-
// =================== HELPERS ===================
233-
234219
private fun applyAppLanguage() {
235220
runCatching {
236221
val langFile = File(filesDir, "home/log/language")
237222
val lang = langFile.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() } ?: return
238-
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(lang.replace("_", "-"))
239-
AppCompatDelegate.setApplicationLocales(appLocale)
223+
val locale = Locale.forLanguageTag(lang.replace("_", "-"))
224+
if (Locale.getDefault() != locale) {
225+
Locale.setDefault(locale)
226+
val config = Configuration(resources.configuration).apply { setLocale(locale) }
227+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
228+
resources.updateConfiguration(config, resources.displayMetrics)
229+
} else {
230+
@Suppress("DEPRECATION")
231+
resources.updateConfiguration(config, resources.displayMetrics)
232+
}
233+
}
240234
}
241235
}
242236

0 commit comments

Comments
 (0)