Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ plugins {
id("dev.flutter.flutter-gradle-plugin")
}

extra["code"] = 1
extra["name"] = "1.0.0"


android {
namespace = "com.example.cpu_z_copy"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileSdk = 35
ndkVersion = "25.2.9519653"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.cpu_z_copy"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
minSdk = 25
targetSdk = 35
versionCode = extra["code"] as Int
versionName = extra["name"] as String
}

buildTypes {
Expand All @@ -37,6 +37,9 @@ android {
signingConfig = signingConfigs.getByName("debug")
}
}
kotlinOptions {
jvmTarget = "17"
}
}

flutter {
Expand Down
87 changes: 87 additions & 0 deletions android/app/src/main/kotlin/com/example/cpu_z_copy/Battery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.example.cpu_z_copy

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import io.flutter.plugin.common.EventChannel

class Battery(context: Context) {

val batteryHandler = object : EventChannel.StreamHandler {
lateinit var receiver: BroadcastReceiver
val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager

override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null || events == null) return
val health = getHealth(intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1))
val status = getStatus(intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1))
val level =
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
val temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10
val powerSource = getPowerSource(intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1))
val voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)
val technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY) ?: "error"

val result = mapOf<String, Any>(
"type" to "battery",
"health" to health,
"status" to status,
"level" to level,
"technology" to technology,
"temperature" to temperature,
"powerSource" to powerSource,
"voltage" to voltage
)
events.success(result)
}
}
context.registerReceiver(receiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
}

override fun onCancel(arguments: Any?) {
receiver.let { context.unregisterReceiver(it) }
}

}


fun getHealth(health: Int): String {
return when (health) {
BatteryManager.BATTERY_HEALTH_GOOD -> "Good"
BatteryManager.BATTERY_HEALTH_OVERHEAT -> "Over heat"
BatteryManager.BATTERY_HEALTH_COLD -> "Cold"
BatteryManager.BATTERY_HEALTH_DEAD -> "Dead"
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> "Over voltage"
BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> "Unspecified failure"
BatteryManager.BATTERY_HEALTH_UNKNOWN -> "Unknown"
else -> "Error"
}
}

fun getStatus(status: Int): String {
return when (status) {
BatteryManager.BATTERY_STATUS_CHARGING -> "Charging"
BatteryManager.BATTERY_STATUS_FULL -> "Full"
BatteryManager.BATTERY_STATUS_DISCHARGING -> "Discharging"
BatteryManager.BATTERY_STATUS_NOT_CHARGING -> "Not charging"
BatteryManager.BATTERY_STATUS_UNKNOWN -> "Unknown"
else -> "Error"
}
}

fun getPowerSource(status: Int): String {
return when (status) {
BatteryManager.BATTERY_PLUGGED_AC -> "Ac"
BatteryManager.BATTERY_PLUGGED_USB -> "Usb"
BatteryManager.BATTERY_PLUGGED_DOCK -> "Dock"
BatteryManager.BATTERY_PLUGGED_WIRELESS -> "Wireless"
else -> "Battery"
}
}


}
71 changes: 71 additions & 0 deletions android/app/src/main/kotlin/com/example/cpu_z_copy/Device.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.cpu_z_copy

import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.os.StatFs
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlin.math.pow
import kotlin.math.sqrt

class Device(context: Context) {

val storageStat = StatFs(context.filesDir.path)
val runtime = Runtime.getRuntime()
val metrics = Resources.getSystem().displayMetrics

val handler = object : MethodChannel.MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
val resultData = mapOf(
"type" to "device",
"content" to when (call.method) {
DeviceRequest.MANUFACTURER.title -> Build.MANUFACTURER
DeviceRequest.HARDWARE.title -> Build.HARDWARE
DeviceRequest.BOARD.title -> Build.BOARD
DeviceRequest.MODEL.title -> Build.MODEL
DeviceRequest.SCREEN.title -> {
val wInch = metrics.widthPixels / metrics.xdpi
val hInch = metrics.heightPixels / metrics.ydpi
val size = sqrt(wInch.pow(2) + hInch.pow(2))
mapOf(
"size" to size,
"resolution" to "${metrics.heightPixels} x ${metrics.widthPixels} pixels",
"density" to metrics.densityDpi,
)
}

DeviceRequest.RAM.title -> mapOf(
"total" to runtime.totalMemory(),
"available" to runtime.freeMemory()
)

DeviceRequest.STORAGE.title -> {
var size = storageStat.blockSizeLong
var total = size * storageStat.blockCountLong
mapOf(
"total" to total,
"available" to total - (size * storageStat.availableBlocksLong)
)
}

else -> {
result.notImplemented()
return
}
}
)

result.success(resultData)
}
}

}


enum class DeviceRequest(val title: String) {
MODEL("model"), MANUFACTURER("manufacturer"), BOARD("board"), HARDWARE("hardware"), SCREEN("screen"), RAM(
"ram"
),
STORAGE("storage");
}
48 changes: 25 additions & 23 deletions android/app/src/main/kotlin/com/example/cpu_z_copy/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,50 @@ package com.example.cpu_z_copy

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {

lateinit var _eventChannel: EventChannel
lateinit var _methodChannel: MethodChannel

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

/*
MethodChannel(
EventChannel(
flutterEngine.dartExecutor.binaryMessenger,
Define.METHOD_CHANNEL_NAME
).setMethodCallHandler { call, result -> {

if(call.method == "") {


result.success();
}
Channel.EVENT_CHANNEL_NAME.title + RequestApp.BATTERY.title
).setStreamHandler(Battery(context).batteryHandler)

EventChannel(
flutterEngine.dartExecutor.binaryMessenger,
Channel.EVENT_CHANNEL_NAME.title + RequestApp.SENSOR.title
).setStreamHandler(Sensor(context).sensorHandler)

MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
Channel.METHOD_CHANNEL_NAME.title + RequestApp.DEVICE.title
).setMethodCallHandler(Device(context).handler)

} }
*/
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
Channel.METHOD_CHANNEL_NAME.title + RequestApp.SYSTEM.title
).setMethodCallHandler(System(context).handler)


}

/*

fun getBattery (): Int {



enum class RequestApp(val title: String) {
BATTERY("_battery"), THERMAL("_thermal"), SENSOR("_sensor"), SYSTEM("_system"), SOC("_soc"), DEVICE(
"_device"
);
}
*/

object Define {

const val METHOD_CHANNEL_NAME: String = "com.example.spu_z_copy_method_channel"
const val EVENT_CHANNEL_NAME: String = "com.example.spu_z_copy_event_channel"

enum class Channel(val title: String) {
METHOD_CHANNEL_NAME("com.example.spu_z_copy_method_channel"),
EVENT_CHANNEL_NAME("com.example.spu_z_copy_event_channel");
}

}
104 changes: 104 additions & 0 deletions android/app/src/main/kotlin/com/example/cpu_z_copy/Sensor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.example.cpu_z_copy

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import io.flutter.plugin.common.EventChannel


class Sensor(context: Context) {
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
lateinit var listener: SensorEventListener

val sensorHandler = object : EventChannel.StreamHandler {

override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
if (events == null) return

listener = object : SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
if (event == null) return
val result = mapOf(
"type" to "sensor",
"sensor_type" to event.sensor.stringType,
"id" to event.sensor.id,
"name" to event.sensor.name,
"content" to getValue(event)
)
events.success(result)
}

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
}
sensorList.forEach { type ->
sensorManager.registerListener(
listener, sensorManager.getDefaultSensor(type),
SensorManager.SENSOR_DELAY_NORMAL
)
}
}

override fun onCancel(arguments: Any?) {
sensorManager.unregisterListener(listener)
}

val sensorList = listOf<Int>(
Sensor.TYPE_ACCELEROMETER,
Sensor.TYPE_GYROSCOPE_UNCALIBRATED,
Sensor.TYPE_GYROSCOPE,
Sensor.TYPE_PRESSURE,
Sensor.TYPE_GRAVITY,
Sensor.TYPE_ROTATION_VECTOR,
Sensor.TYPE_PROXIMITY,
Sensor.TYPE_MAGNETIC_FIELD,
Sensor.TYPE_LIGHT,
Sensor.TYPE_AMBIENT_TEMPERATURE
)
}

fun getValue(event: SensorEvent): Map<String, Any> {
val sensor = event.sensor
return when (sensor.type) {
Sensor.TYPE_LIGHT -> mapOf(
"lux" to event.values[0]
)
Sensor.TYPE_GRAVITY, Sensor.TYPE_ACCELEROMETER, Sensor.TYPE_MAGNETIC_FIELD, Sensor.TYPE_GYROSCOPE, Sensor.TYPE_ROTATION_VECTOR -> mapOf(
"x=" to event.values[0],
"y=" to event.values[1],
"z=" to event.values[2],
"unit" to when (sensor.type) {
Sensor.TYPE_GRAVITY, Sensor.TYPE_ACCELEROMETER -> "m/s²"
Sensor.TYPE_MAGNETIC_FIELD -> "μT"
Sensor.TYPE_GYROSCOPE -> "rad/s"
Sensor.TYPE_ROTATION_VECTOR -> ""
else -> "error"
}
)
Sensor.TYPE_AMBIENT_TEMPERATURE -> mapOf(
"°C" to event.values[0]
)
Sensor.TYPE_PRESSURE -> mapOf(
"hPa" to event.values[0]
)
Sensor.TYPE_PROXIMITY -> mapOf(
"cm" to event.values[0]
)
else -> mapOf()
}
}

fun getSensorList(): Map<String, Any> {
val sensorList = sensorManager.getSensorList(SensorManager.SENSOR_ALL)
if (sensorList == null) return mapOf<String, Any>("type" to "error")
var result: Map<String, Any>
result = mapOf(
"type" to "sensor",
"content" to sensorList.map { sensor -> sensor.name }
)
return result
}


}
Loading