diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 433c59f..de40f3f 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -11,6 +11,7 @@ android { ndkVersion = flutter.ndkVersion compileOptions { + isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } @@ -42,3 +43,7 @@ android { flutter { source = "../.." } + +dependencies { + coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4") +} diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index d962e2f..65a1ae9 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,9 +1,29 @@ - + + + + + + + + android:icon="@mipmap/ic_launcher" + android:requestLegacyExternalStorage="true"> + + + + + - - + + + - - - - - - - diff --git a/android/app/src/main/kotlin/xyz/nuark/update_forge_companion/MainActivity.kt b/android/app/src/main/kotlin/xyz/nuark/update_forge_companion/MainActivity.kt index 9a955db..269e403 100644 --- a/android/app/src/main/kotlin/xyz/nuark/update_forge_companion/MainActivity.kt +++ b/android/app/src/main/kotlin/xyz/nuark/update_forge_companion/MainActivity.kt @@ -1,5 +1,100 @@ package xyz.nuark.update_forge_companion +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import androidx.core.content.FileProvider import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodCall +import io.flutter.plugin.common.MethodChannel +import java.io.File -class MainActivity : FlutterActivity() +class MainActivity : FlutterActivity() { + private val CHANNEL = "xyz.nuark.update_forge_companion/device" + + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + + MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL) + .setMethodCallHandler { call, result -> handleMethod(call, result) } + } + + private fun handleMethod(call: MethodCall, result: MethodChannel.Result) { + when (call.method) { + "getInstalledVersion" -> { + val packageName = call.argument("packageName") + if (packageName == null) { + result.success(null) + return + } + try { + val pInfo = packageManager.getPackageInfo(packageName, 0) + result.success(pInfo.versionName) + } catch (e: PackageManager.NameNotFoundException) { + result.success(null) + } + } + "installApk" -> { + val filePath = call.argument("filePath") + if (filePath == null) { + result.success(false) + return + } + installApk(filePath, result) + } + "openApp" -> { + val packageName = call.argument("packageName") + if (packageName == null) { + result.success(false) + return + } + try { + val intent = packageManager.getLaunchIntentForPackage(packageName) + if (intent != null) { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(intent) + result.success(true) + } else { + result.success(false) + } + } catch (e: Exception) { + result.success(false) + } + } + else -> result.notImplemented() + } + } + + private fun installApk(filePath: String, result: MethodChannel.Result) { + try { + val file = File(filePath) + if (!file.exists()) { + result.success(false) + return + } + + val uri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + FileProvider.getUriForFile( + this, + "${packageName}.fileprovider", + file + ) + } else { + Uri.fromFile(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) + result.success(true) + } catch (e: Exception) { + result.success(false) + } + } +} diff --git a/android/app/src/main/res/values/colors.xml b/android/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..658a5ca --- /dev/null +++ b/android/app/src/main/res/values/colors.xml @@ -0,0 +1,4 @@ + + + #FF0D0E11 + \ No newline at end of file diff --git a/android/app/src/main/res/xml/file_paths.xml b/android/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..d225479 --- /dev/null +++ b/android/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,6 @@ + + + + + +