尝试解决二次启动时文件读取问题
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.os.BatteryManager
|
||||
import android.os.Build
|
||||
@@ -20,18 +21,17 @@ import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
|
||||
class MainActivity : FlutterActivity() {
|
||||
// ---------- 充电状态 ----------
|
||||
private val CHARGE_CHANNEL = "com.lxh.battery_health/charge"
|
||||
private val EVENT_CHANNEL = "com.lxh.battery_health/charge_events"
|
||||
private var eventSink: EventChannel.EventSink? = null
|
||||
|
||||
// ---------- SAF 相关 ----------
|
||||
private val SAF_CHANNEL = "com.lxh.battery_health/saf"
|
||||
private var safResult: MethodChannel.Result? = null
|
||||
private var safUri: Uri? = null
|
||||
private val FOLDER_PICKER_REQUEST = 1001
|
||||
private val PREF_NAME = "battery_health_prefs"
|
||||
private val KEY_SAF_URI = "saf_uri"
|
||||
|
||||
// ---------- 充电广播接收器 ----------
|
||||
private val chargeReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
intent ?: return
|
||||
@@ -42,6 +42,46 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
android.util.Log.d("BatteryHealth", "onCreate 开始")
|
||||
|
||||
try {
|
||||
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
val uriString = prefs.getString(KEY_SAF_URI, null)
|
||||
android.util.Log.d("BatteryHealth", "SharedPreferences URI: $uriString")
|
||||
if (uriString != null) {
|
||||
safUri = Uri.parse(uriString)
|
||||
android.util.Log.d("BatteryHealth", "恢复 SAF URI 成功: $safUri")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("BatteryHealth", "恢复 URI 异常: ${e.message}")
|
||||
}
|
||||
android.util.Log.d("BatteryHealth", "onCreate 结束,safUri = $safUri")
|
||||
}
|
||||
|
||||
private fun saveUri(uri: Uri) {
|
||||
try {
|
||||
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
prefs.edit().putString(KEY_SAF_URI, uri.toString()).apply()
|
||||
android.util.Log.d("BatteryHealth", "保存 SAF URI: $uri")
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("BatteryHealth", "保存 URI 失败: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun restoreUri(uriString: String) {
|
||||
try {
|
||||
safUri = Uri.parse(uriString)
|
||||
android.util.Log.d("BatteryHealth", "通过 MethodChannel 恢复 URI: $safUri")
|
||||
// 同时保存到 SharedPreferences
|
||||
val prefs: SharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
prefs.edit().putString(KEY_SAF_URI, uriString).apply()
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("BatteryHealth", "restoreUri 失败: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendChargeState(isCharging: Boolean) {
|
||||
val chargeType = if (isCharging) {
|
||||
val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||
@@ -66,7 +106,6 @@ class MainActivity : FlutterActivity() {
|
||||
status == BatteryManager.BATTERY_STATUS_FULL
|
||||
}
|
||||
|
||||
// ---------- SAF 方法 ----------
|
||||
private fun openFolderPicker() {
|
||||
try {
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
|
||||
@@ -100,7 +139,8 @@ class MainActivity : FlutterActivity() {
|
||||
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
||||
safUri = uri
|
||||
android.util.Log.d("BatteryHealth", "SAF URI: $uri")
|
||||
saveUri(uri)
|
||||
android.util.Log.d("BatteryHealth", "SAF 授权成功 URI: $uri")
|
||||
safResult?.success(uri.toString())
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("BatteryHealth", "SAF 授权失败: ${e.message}")
|
||||
@@ -118,9 +158,13 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
|
||||
private fun listFiles(): List<String> {
|
||||
android.util.Log.d("BatteryHealth", "listFiles 被调用,safUri = $safUri")
|
||||
val result = mutableListOf<String>()
|
||||
val docId = getTreeDocId()
|
||||
if (docId == null || docId.isEmpty()) return result
|
||||
if (docId == null || docId.isEmpty()) {
|
||||
android.util.Log.e("BatteryHealth", "docId 为空")
|
||||
return result
|
||||
}
|
||||
|
||||
try {
|
||||
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(safUri!!, docId)
|
||||
@@ -134,7 +178,7 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("BatteryHealth", "列出文件失败: ${e.message}")
|
||||
android.util.Log.e("BatteryHealth", "列出文件失败: ${e.message}", e)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -178,11 +222,9 @@ class MainActivity : FlutterActivity() {
|
||||
return fileUri?.let { readFile(it) }
|
||||
}
|
||||
|
||||
// ---------- Flutter 通道配置 ----------
|
||||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
|
||||
// 充电状态通道
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHARGE_CHANNEL)
|
||||
.setMethodCallHandler { call, result ->
|
||||
if (call.method == "getChargeState") {
|
||||
@@ -205,10 +247,18 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
// SAF 通道
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SAF_CHANNEL)
|
||||
.setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"restoreUri" -> {
|
||||
val uriString = call.argument<String>("uri")
|
||||
if (uriString != null) {
|
||||
restoreUri(uriString)
|
||||
result.success(true)
|
||||
} else {
|
||||
result.error("INVALID_ARGUMENT", "uri is required", null)
|
||||
}
|
||||
}
|
||||
"selectFolder" -> {
|
||||
safResult = result
|
||||
openFolderPicker()
|
||||
@@ -233,7 +283,6 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
// 充电状态事件通道
|
||||
EventChannel(flutterEngine.dartExecutor.binaryMessenger, EVENT_CHANNEL)
|
||||
.setStreamHandler(
|
||||
object : EventChannel.StreamHandler {
|
||||
|
||||
Reference in New Issue
Block a user