commit state

This commit is contained in:
niuhuan 2021-10-17 01:17:06 +08:00
parent d5df963020
commit 12e74d202c
3 changed files with 74 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import android.provider.MediaStore
import android.view.Display import android.view.Display
import android.view.KeyEvent import android.view.KeyEvent
import androidx.annotation.NonNull import androidx.annotation.NonNull
import androidx.annotation.RequiresApi
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel import io.flutter.plugin.common.EventChannel
@ -20,8 +21,10 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import mobile.Mobile import mobile.Mobile
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue
class MainActivity : FlutterActivity() { class MainActivity : FlutterActivity() {
@ -59,13 +62,10 @@ class MainActivity : FlutterActivity() {
} }
} }
private val resourceQueue: LinkedBlockingQueue<Any?> = LinkedBlockingQueue() @RequiresApi(Build.VERSION_CODES.KITKAT)
private var cacheDir: String? = null
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)
cacheDir = context!!.cacheDir.absolutePath Mobile.initApplication(androidDataLocal())
Mobile.initApplication(context!!.filesDir.absolutePath)
// Method Channel // Method Channel
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "method").setMethodCallHandler { call, result -> MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "method").setMethodCallHandler { call, result ->
result.withCoroutine { result.withCoroutine {
@ -89,6 +89,12 @@ class MainActivity : FlutterActivity() {
// "exportComicDownloadAndroidQ" -> { // "exportComicDownloadAndroidQ" -> {
// exportComicDownloadAndroidQ(call.argument("comicId")!!) // exportComicDownloadAndroidQ(call.argument("comicId")!!)
// } // }
// 现在的文件储存路径, 默认路径返回空字符串 ""
"androidDataLocal" -> androidDataLocal()
// 获取可以迁移数据地址
"androidGetExtendDirs" -> androidGetExtendDirs()
// 迁移到那个地方, 如果是空字符串则迁移会默认位置
"androidMigrate" -> androidMigrate(call.argument("path")!!)
else -> { else -> {
notImplementedToken notImplementedToken
} }
@ -140,6 +146,45 @@ class MainActivity : FlutterActivity() {
} }
private fun androidDataLocal(): String {
val localFile = File(context!!.filesDir.absolutePath, "data.local")
if (localFile.exists()) {
val path = String(FileInputStream(localFile).use { it.readBytes() })
if (File(path).isDirectory) {
return path
}
}
return context!!.filesDir.absolutePath
}
private fun androidGetExtendDirs(): String {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return context!!.getExternalFilesDirs("")?.joinToString(",") { it.toString() }
?: ""
}
throw Exception("System version too low")
}
private fun androidMigrate(path: String) {
val current = androidDataLocal()
if (current == path) {
return
}
Runtime.getRuntime().exec(
arrayOf(
"mv",
"$current/*",
"$path/"
)
).exitValue()
val localFile = File(context!!.filesDir.absolutePath, "data.local")
if (path == context!!.filesDir.absolutePath) {
localFile.delete()
} else {
FileOutputStream(localFile).use { it.write(path.toByteArray()) }
}
}
// save_image // save_image
private fun saveImage(path: String) { private fun saveImage(path: String) {
@ -250,7 +295,7 @@ class MainActivity : FlutterActivity() {
} }
// 安卓11以上使用了 MANAGE_EXTERNAL_STORAGE 权限来管理整个外置存储 (危险权限) // 安卓11以上使用了 MANAGE_EXTERNAL_STORAGE 权限来管理整个外置存储 (危险权限)
// private val resourceQueue: LinkedBlockingQueue<Any?> = LinkedBlockingQueue()
// private var tmpComicId: String? = null // private var tmpComicId: String? = null
// private val exportComicDownloadAndroidQRequestCode = 2 // private val exportComicDownloadAndroidQRequestCode = 2
// //

View File

@ -1,7 +1,5 @@
/// ///
import 'dart:io';
import 'package:event/event.dart'; import 'package:event/event.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';

View File

@ -1,6 +1,8 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pikapi/basic/Common.dart';
import 'package:pikapi/basic/config/AndroidDisplayMode.dart'; import 'package:pikapi/basic/config/AndroidDisplayMode.dart';
import 'package:pikapi/basic/config/AutoClean.dart'; import 'package:pikapi/basic/config/AutoClean.dart';
import 'package:pikapi/basic/config/AutoFullScreen.dart'; import 'package:pikapi/basic/config/AutoFullScreen.dart';
@ -140,7 +142,27 @@ class _SettingsScreenState extends State<SettingsScreen> {
), ),
fontSetting(), fontSetting(),
Divider(), Divider(),
migrate(),
], ],
), ),
); );
Widget migrate() {
if (Platform.isAndroid) {
return ListTile(
title: Text("文件迁移"),
subtitle: Text("更换您的数据文件夹"),
onTap: () async {
var f = confirmDialog(
context,
"文件迁移",
"为了手机数据存储空间不足, 且具有内存卡的手机设计, 可将数据迁移到内存卡上。"
" 您在迁移之前, 请确保您的下载处于暂停状态, 或下载均已完成, 已保证您的数据完整性。"
" 如果迁移中断或其他原因导致程序无法启动, 图片失效等问题, 您可在程序管理中清除本应用程序的数据, 以回复正常使用。",
);
},
);
}
return Container();
}
} }