first commit

This commit is contained in:
ZV committed 2026-08-27 15:41:11 +08:00
commit 90f6d120eb
47 files changed
+3766

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<application
android:label="MCServer Manager"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- 提供 APK 文件给系统安装器访问 -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
@@ -0,0 +1,145 @@
package com.mcserver_manager
import android.content.ActivityNotFoundException
import android.content.Intent
import androidx.core.content.FileProvider
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
class MainActivity : FlutterActivity() {
private companion object {
const val CHANNEL = "mc_server_manager/update"
const val PROGRESS_CHANNEL = "mc_server_manager/down_progress"
const val ARG_URL = "url"
const val ARG_FILENAME = "filename"
/** 是否正在下载,防止并发下载到同一文件 */
val downloading = AtomicBoolean(false)
/** 下载进度推送事件流(由 Flutter 订阅) */
val progressSink = AtomicReference<EventChannel.EventSink?>(null)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
CHANNEL,
).setMethodCallHandler { call, result ->
when (call.method) {
"downloadAndInstall" -> {
val url = call.argument<String>(ARG_URL)
val filename = call.argument<String>(ARG_FILENAME)
if (url.isNullOrBlank()) {
result.error("BAD_ARGS", "下载地址为空", null)
} else if (!downloading.compareAndSet(false, true)) {
// 已有下载正在进行,拒绝并发,避免文件写坏
result.error("BUSY", "正在下载,请稍候", null)
} else {
downloadAndInstall(url, filename ?: "latest.apk", result)
}
}
else -> result.notImplemented()
}
}
EventChannel(
flutterEngine.dartExecutor.binaryMessenger,
PROGRESS_CHANNEL,
).setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
progressSink.set(events)
}
override fun onCancel(arguments: Any?) {
progressSink.set(null)
}
})
}
private fun sendProgress(downloaded: Long, total: Long) {
val sink = progressSink.get() ?: return
val percent = if (total > 0) downloaded.toDouble() / total else 0.0
val payload = HashMap<String, Any>()
payload["downloaded"] = downloaded
payload["total"] = total
payload["percent"] = percent
android.os.Handler(android.os.Looper.getMainLooper()).post {
runCatching { sink.success(payload) }
}
}
private fun downloadAndInstall(
url: String,
filename: String,
result: MethodChannel.Result,
) {
Thread {
try {
val dir = File(cacheDir, "apk").apply { mkdirs() }
val file = File(dir, filename)
// 删除旧的半成品/残留文件,保证安装的是最新完整文件
file.delete()
val connection = java.net.URL(url).openConnection()
connection.setRequestProperty("User-Agent", "MCServerManager")
connection.connectTimeout = 15_000
connection.readTimeout = 30_000
val total = connection.contentLengthLong
val buffer = ByteArray(8 * 1024)
var downloaded = 0L
var lastSent = 0L
connection.inputStream.use { input ->
file.outputStream().use { output ->
while (true) {
val read = input.read(buffer)
if (read < 0) break
output.write(buffer, 0, read)
downloaded += read
// 每约 256KB 推送一次进度,避免高频回调卡顿
if (downloaded - lastSent >= 256 * 1024 || downloaded >= total) {
sendProgress(downloaded, total)
lastSent = downloaded
}
}
}
}
// 确保最终 100% 进度已推送
if (lastSent < downloaded) {
sendProgress(downloaded, total)
}
if (file.length() == 0L) {
file.delete()
runOnUiThread { result.error("DOWNLOAD_FAILED", "下载文件为空", null) }
return@Thread
}
val uri = FileProvider.getUriForFile(
this,
"$packageName.fileprovider",
file,
)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(intent)
runOnUiThread { result.success(null) }
} catch (e: ActivityNotFoundException) {
runOnUiThread { result.error("INSTALL_NOT_FOUND", "未找到可用的安装器", e.message) }
} catch (e: Exception) {
runOnUiThread { result.error("DOWNLOAD_FAILED", "下载失败:${e.message}", null) }
} finally {
downloading.set(false)
}
}.start()
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is on -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
the Flutter engine draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
the Flutter engine draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- APK 下载缓存目录:context.getCacheDir()/apk/latest.apk -->
<cache-path name="apk_cache" path="apk/" />
</paths>