1.0.0
This commit is contained in:
9
android/.gitignore
vendored
Normal file
9
android/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/workspace.xml
|
||||
/.idea/libraries
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.cxx
|
||||
80
android/build.gradle
Normal file
80
android/build.gradle
Normal file
@@ -0,0 +1,80 @@
|
||||
// The Android Gradle Plugin builds the native code with the Android NDK.
|
||||
|
||||
group = "org.swipelab.flutter.hardware"
|
||||
version = "1.0"
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = "1.9.22"
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// The Android Gradle Plugin knows how to build native code with the NDK.
|
||||
classpath("com.android.tools.build:gradle:8.11.1")
|
||||
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: "com.android.library"
|
||||
apply plugin: "kotlin-android"
|
||||
|
||||
android {
|
||||
namespace = "org.swipelab.flutter.hardware"
|
||||
|
||||
// Bumping the plugin compileSdk version requires all clients of this plugin
|
||||
// to bump the version in their app.
|
||||
compileSdk = 36
|
||||
|
||||
// Use the NDK version
|
||||
// declared in /android/app/build.gradle file of the Flutter project.
|
||||
// Replace it with a version number if this plugin requires a specific NDK version.
|
||||
// (e.g. ndkVersion "23.1.7779620")
|
||||
ndkVersion = android.ndkVersion
|
||||
|
||||
// Invoke the shared CMake build with the Android Gradle Plugin.
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path = "../src/CMakeLists.txt"
|
||||
|
||||
// The default CMake version for the Android Gradle Plugin is 3.10.2.
|
||||
// https://developer.android.com/studio/projects/install-ndk#vanilla_cmake
|
||||
//
|
||||
// The Flutter tooling requires that developers have CMake 3.10 or later
|
||||
// installed. You should not increase this version, as doing so will cause
|
||||
// the plugin to fail to compile for some customers of the plugin.
|
||||
// version "3.10.2"
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
kotlin.srcDirs += "src/main/kotlin"
|
||||
}
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 24
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
|
||||
}
|
||||
1
android/settings.gradle
Normal file
1
android/settings.gradle
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = 'hardware'
|
||||
3
android/src/main/AndroidManifest.xml
Normal file
3
android/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.swipelab.flutter.hardware">
|
||||
</manifest>
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.swipelab.flutter.hardware
|
||||
|
||||
data class DoubleOption(
|
||||
@JvmField val value: Double,
|
||||
@JvmField val hasValue: Boolean
|
||||
)
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.swipelab.flutter.hardware
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
object HardwareHelper {
|
||||
init {
|
||||
System.loadLibrary("hardware")
|
||||
}
|
||||
|
||||
private var activityRef: WeakReference<Activity>? = null
|
||||
|
||||
@JvmStatic
|
||||
fun setActivity(act: Activity?) {
|
||||
activityRef = act?.let { WeakReference(it) }
|
||||
}
|
||||
|
||||
// Called from C via JNI. Returns DoubleOption with hasValue=false if unavailable.
|
||||
@JvmStatic
|
||||
fun getScreenBorderRadius(): DoubleOption {
|
||||
val activity = activityRef?.get() ?: return DoubleOption(0.0, false)
|
||||
|
||||
val radiusPx = getCornerRadiusFromResources(activity)
|
||||
return if (radiusPx > 0) {
|
||||
val density = activity.resources.displayMetrics.density
|
||||
DoubleOption(radiusPx.toDouble() / density, true)
|
||||
} else {
|
||||
DoubleOption(0.0, false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCornerRadiusFromResources(context: Context): Int {
|
||||
// Try various resource names used by different Android versions/OEMs
|
||||
val resourceNames = listOf(
|
||||
"rounded_corner_radius",
|
||||
"config_roundedCornerRadius",
|
||||
"config_mainBuiltInDisplayCornerRadius"
|
||||
)
|
||||
|
||||
for (name in resourceNames) {
|
||||
val resId = context.resources.getIdentifier(name, "dimen", "android")
|
||||
if (resId > 0) {
|
||||
try {
|
||||
val radius = context.resources.getDimensionPixelSize(resId)
|
||||
if (radius > 0) return radius
|
||||
} catch (e: Exception) {
|
||||
// Continue to next resource name
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.swipelab.flutter.hardware
|
||||
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
||||
|
||||
class HardwarePlugin : FlutterPlugin, ActivityAware {
|
||||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {}
|
||||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {}
|
||||
|
||||
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
|
||||
HardwareHelper.setActivity(binding.activity)
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivity() {
|
||||
HardwareHelper.setActivity(null)
|
||||
}
|
||||
|
||||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
|
||||
HardwareHelper.setActivity(binding.activity)
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivityForConfigChanges() {
|
||||
HardwareHelper.setActivity(null)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user