|
| 1 | +package com.tool.tree |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.content.pm.PackageManager |
| 6 | +import android.content.res.Configuration |
| 7 | +import android.graphics.Color |
| 8 | +import android.net.Uri |
| 9 | +import android.os.Build |
| 10 | +import android.os.Bundle |
| 11 | +import android.os.Environment |
| 12 | +import android.os.Handler |
| 13 | +import android.provider.Settings |
| 14 | +import android.view.animation.AnimationUtils |
| 15 | +import android.widget.TextView |
| 16 | +import androidx.appcompat.app.AppCompatActivity |
| 17 | +import androidx.core.app.ActivityCompat |
| 18 | +import androidx.core.content.ContextCompat |
| 19 | +import androidx.core.view.WindowCompat |
| 20 | +import androidx.core.view.WindowInsetsControllerCompat |
| 21 | +import androidx.lifecycle.lifecycleScope |
| 22 | +import com.omarea.common.shell.KeepShellPublic |
| 23 | +import com.omarea.common.shell.ShellExecutor |
| 24 | +import com.omarea.common.ui.DialogHelper |
| 25 | +import com.omarea.krscript.executor.ScriptEnvironmen |
| 26 | +import com.tool.tree.databinding.ActivitySplashBinding |
| 27 | +import com.tool.tree.R |
| 28 | +import kotlinx.coroutines.Dispatchers |
| 29 | +import kotlinx.coroutines.launch |
| 30 | +import kotlinx.coroutines.withContext |
| 31 | +import java.io.BufferedReader |
| 32 | +import java.io.DataOutputStream |
| 33 | +import java.io.File |
| 34 | +import java.util.Locale |
| 35 | + |
| 36 | +class SplashActivity : AppCompatActivity() { |
| 37 | + |
| 38 | + private lateinit var binding: ActivitySplashBinding |
| 39 | + private val REQUEST_CODE_PERMISSIONS = 1001 |
| 40 | + |
| 41 | + private var hasRoot = false |
| 42 | + private var started = false |
| 43 | + private var starting = false |
| 44 | + |
| 45 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 46 | + applyAppLanguage() |
| 47 | + super.onCreate(savedInstanceState) |
| 48 | + ThemeModeState.switchTheme(this) |
| 49 | + |
| 50 | + if (ScriptEnvironmen.isInited() && isTaskRoot) { |
| 51 | + gotoHome() |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + if (!hasAgreed()) showAgreementDialog() |
| 56 | + |
| 57 | + binding = ActivitySplashBinding.inflate(layoutInflater) |
| 58 | + setContentView(binding.root) |
| 59 | + |
| 60 | + binding.startLogoXml.postDelayed({ |
| 61 | + binding.startLogoXml.startAnimation(AnimationUtils.loadAnimation(this, R.anim.blink)) |
| 62 | + }, 2000) |
| 63 | + |
| 64 | + applyTheme() |
| 65 | + } |
| 66 | + |
| 67 | + private fun applyAppLanguage() { |
| 68 | + runCatching { |
| 69 | + val langFile = File(filesDir, "home/log/language") |
| 70 | + val lang = langFile.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() } ?: return |
| 71 | + val locale = Locale.forLanguageTag(lang.replace("_", "-")) |
| 72 | + if (Locale.getDefault() != locale) { |
| 73 | + Locale.setDefault(locale) |
| 74 | + val config = Configuration(resources.configuration).apply { setLocale(locale) } |
| 75 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 76 | + resources.updateConfiguration(config, resources.displayMetrics) |
| 77 | + } else { |
| 78 | + @Suppress("DEPRECATION") |
| 79 | + resources.updateConfiguration(config, resources.displayMetrics) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun applyTheme() { |
| 86 | + WindowCompat.setDecorFitsSystemWindows(window, false) |
| 87 | + window.statusBarColor = Color.TRANSPARENT |
| 88 | + window.navigationBarColor = ContextCompat.getColor(this, R.color.splash_bg_color) |
| 89 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 90 | + WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = true |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // =================== AGREEMENT =================== |
| 95 | + private fun showAgreementDialog() { |
| 96 | + DialogHelper.warning( |
| 97 | + this, |
| 98 | + getString(R.string.permission_dialog_title), |
| 99 | + getString(R.string.permission_dialog_message), |
| 100 | + Runnable { requestAppPermissions() }, |
| 101 | + Runnable { finish() } |
| 102 | + ).setCancelable(false) |
| 103 | + } |
| 104 | + |
| 105 | + private fun hasAgreed(): Boolean = |
| 106 | + getSharedPreferences("kr-script-config", MODE_PRIVATE) |
| 107 | + .getBoolean("agreed_permissions", false) |
| 108 | + |
| 109 | + private fun saveAgreement() { |
| 110 | + getSharedPreferences("kr-script-config", MODE_PRIVATE) |
| 111 | + .edit() |
| 112 | + .putBoolean("agreed_permissions", true) |
| 113 | + .apply() |
| 114 | + } |
| 115 | + |
| 116 | + // =================== PERMISSION =================== |
| 117 | + private fun hasAllFilesPermission(): Boolean = |
| 118 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) |
| 119 | + Environment.isExternalStorageManager() |
| 120 | + else ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == |
| 121 | + PackageManager.PERMISSION_GRANTED |
| 122 | + |
| 123 | + private fun requestAllFilesPermission() { |
| 124 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 125 | + startActivity(Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION).apply { |
| 126 | + data = Uri.parse("package:$packageName") |
| 127 | + }) |
| 128 | + } else { |
| 129 | + // Legacy permission |
| 130 | + ActivityCompat.requestPermissions( |
| 131 | + this, |
| 132 | + arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE), |
| 133 | + REQUEST_CODE_PERMISSIONS |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private fun requestAppPermissions() { |
| 139 | + saveAgreement() |
| 140 | + if (!hasAllFilesPermission()) requestAllFilesPermission() |
| 141 | + else { |
| 142 | + started = true |
| 143 | + checkRootAndStart() |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + override fun onResume() { |
| 148 | + super.onResume() |
| 149 | + if (hasAgreed() && !started) { |
| 150 | + started = true |
| 151 | + checkRootAndStart() |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { |
| 156 | + if (requestCode == REQUEST_CODE_PERMISSIONS) { |
| 157 | + if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { |
| 158 | + started = true |
| 159 | + checkRootAndStart() |
| 160 | + } else finish() |
| 161 | + } |
| 162 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults) |
| 163 | + } |
| 164 | + |
| 165 | + // =================== ROOT =================== |
| 166 | + @Synchronized |
| 167 | + private fun checkRootAndStart() { |
| 168 | + if (!started || starting) return |
| 169 | + starting = true |
| 170 | + |
| 171 | + lifecycleScope.launch(Dispatchers.IO) { |
| 172 | + hasRoot = KeepShellPublic.checkRoot() |
| 173 | + withContext(Dispatchers.Main) { |
| 174 | + starting = false |
| 175 | + startToFinish() |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private fun startToFinish() { |
| 181 | + binding.startStateText.text = getString(R.string.pop_started) |
| 182 | + val config = KrScriptConfig().init(this) |
| 183 | + |
| 184 | + if (config.beforeStartSh.isNotEmpty()) { |
| 185 | + runBeforeStartSh(config, hasRoot) |
| 186 | + } else gotoHome() |
| 187 | + } |
| 188 | + |
| 189 | + private fun gotoHome() { |
| 190 | + startActivity( |
| 191 | + if (intent?.getBooleanExtra("JumpActionPage", false) == true) |
| 192 | + Intent(this, ActionPage::class.java).apply { putExtras(intent!!) } |
| 193 | + else Intent(this, MainActivity::class.java) |
| 194 | + ) |
| 195 | + finish() |
| 196 | + } |
| 197 | + |
| 198 | + private fun runBeforeStartSh(config: KrScriptConfig, hasRoot: Boolean) { |
| 199 | + // Coroutine IO |
| 200 | + lifecycleScope.launch(Dispatchers.IO) { |
| 201 | + try { |
| 202 | + val process = if (hasRoot) ShellExecutor.getSuperUserRuntime() else ShellExecutor.getRuntime() |
| 203 | + process?.let { |
| 204 | + DataOutputStream(it.outputStream).use { os -> |
| 205 | + ScriptEnvironmen.executeShell( |
| 206 | + this@SplashActivity, |
| 207 | + os, |
| 208 | + config.beforeStartSh, |
| 209 | + config.variables, |
| 210 | + null, |
| 211 | + "pio-splash" |
| 212 | + ) |
| 213 | + } |
| 214 | + |
| 215 | + // Đọc stdout và stderr bằng coroutine con |
| 216 | + launch { readStreamAsync(it.inputStream.bufferedReader()) } |
| 217 | + launch { readStreamAsync(it.errorStream.bufferedReader()) } |
| 218 | + |
| 219 | + it.waitFor() |
| 220 | + } |
| 221 | + } finally { |
| 222 | + withContext(Dispatchers.Main) { gotoHome() } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // Buffer lưu 4 dòng cuối |
| 228 | + private val rows = mutableListOf<String>() |
| 229 | + private var ignored = false |
| 230 | + private val maxLines = 5 |
| 231 | + private val handler = android.os.Handler(android.os.Looper.getMainLooper()) |
| 232 | + |
| 233 | + private fun readStreamAsync(reader: BufferedReader) { |
| 234 | + Thread { |
| 235 | + reader.forEachLine { line -> |
| 236 | + onLogOutput(line) |
| 237 | + } |
| 238 | + }.start() |
| 239 | + } |
| 240 | + |
| 241 | + private fun onLogOutput(log: String) { |
| 242 | + handler.post { |
| 243 | + synchronized(rows) { |
| 244 | + if (rows.size >= maxLines) { |
| 245 | + rows.removeAt(0) |
| 246 | + ignored = true |
| 247 | + } |
| 248 | + rows.add(log) |
| 249 | + binding.startStateText.text = rows.joinToString("\n", if (ignored) "……\n" else "") |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments