diff --git a/android/build.gradle b/android/build.gradle index f91b92f..07036e8 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,7 +2,7 @@ group 'de.florianweinaug.system_settings' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.3.50' + ext.kotlin_version = '1.7.20' repositories { google() jcenter() @@ -25,7 +25,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 28 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -37,6 +37,13 @@ android { lintOptions { disable 'InvalidPackage' } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + + namespace 'de.florianweinaug.system_settings' } dependencies { diff --git a/android/src/main/kotlin/de/florianweinaug/system_settings/SystemSettingsPlugin.kt b/android/src/main/kotlin/de/florianweinaug/system_settings/SystemSettingsPlugin.kt index 7a92ac3..332ca97 100644 --- a/android/src/main/kotlin/de/florianweinaug/system_settings/SystemSettingsPlugin.kt +++ b/android/src/main/kotlin/de/florianweinaug/system_settings/SystemSettingsPlugin.kt @@ -1,5 +1,6 @@ package de.florianweinaug.system_settings +import android.content.Context import android.content.Intent import android.net.Uri import android.os.Build @@ -10,25 +11,24 @@ import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result -import io.flutter.plugin.common.PluginRegistry.Registrar import java.lang.Exception public class SystemSettingsPlugin: MethodCallHandler,FlutterPlugin { - constructor() { - } - constructor(binding: FlutterPlugin.FlutterPluginBinding, methodChannel: MethodChannel) { - mPluginBinding = binding - channel = methodChannel - } + // Instance variables, not static + private var channel: MethodChannel? = null + private var applicationContext: Context? = null - companion object { - lateinit var mPluginBinding: FlutterPlugin.FlutterPluginBinding - lateinit var channel: MethodChannel - @JvmStatic - fun registerWith(registrar: Registrar) { - val channel = MethodChannel(registrar.messenger(), "system_settings") - channel.setMethodCallHandler(SystemSettingsPlugin()) - } + // Default constructor is fine if no initial setup is needed before onAttachedToEngine + // constructor() {} // You can keep or remove this if not strictly needed + + override fun onAttachedToEngine(@androidx.annotation.NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { + // Store the application context from the binding + this.applicationContext = flutterPluginBinding.applicationContext + + // Setup the method channel + // The channel name must match what is used in the Dart code + this.channel = MethodChannel(flutterPluginBinding.binaryMessenger, "system_settings") + this.channel?.setMethodCallHandler(this) // 'this' instance will handle method calls } override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { @@ -57,6 +57,7 @@ public class SystemSettingsPlugin: MethodCallHandler,FlutterPlugin { "internal-storage" -> openSetting(Settings.ACTION_INTERNAL_STORAGE_SETTINGS) "notification-policy" -> openSetting(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS) "power-options" -> openSetting(Settings.ACTION_BATTERY_SAVER_SETTINGS) + "power-usage" -> openSetting(Intent.ACTION_POWER_USAGE_SUMMARY) else -> result.notImplemented() } } @@ -64,39 +65,33 @@ public class SystemSettingsPlugin: MethodCallHandler,FlutterPlugin { private fun openAppSettings() { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - val uri = Uri.fromParts("package", mPluginBinding.applicationContext.packageName, null) + val uri = Uri.fromParts("package", applicationContext!!.packageName, null) intent.data = uri - mPluginBinding.applicationContext.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + applicationContext!!.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } private fun openAppNotificationSettings() { val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) - .putExtra(Settings.EXTRA_APP_PACKAGE, mPluginBinding.applicationContext.packageName) + Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, applicationContext!!.packageName) } else { Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(Uri.parse("package:${mPluginBinding.applicationContext.packageName}")) + .setData(Uri.parse("package:${applicationContext!!.packageName}")) } - mPluginBinding.applicationContext.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + applicationContext!!.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } private fun openSetting(name: String) { try { - mPluginBinding.applicationContext.startActivity(Intent(name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + applicationContext!!.startActivity(Intent(name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } catch (e: Exception) { openSystemSettings() } } private fun openSystemSettings() { - mPluginBinding.applicationContext.startActivity(Intent(Settings.ACTION_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) - } - - override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { - val channel = MethodChannel(binding.binaryMessenger, "system_settings") - channel.setMethodCallHandler(SystemSettingsPlugin(binding,channel)) + applicationContext!!.startActivity(Intent(Settings.ACTION_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 4733702..730492c 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -11,7 +11,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.2 + cupertino_icons: ^1.0.2 dev_dependencies: system_settings: diff --git a/lib/system_settings.dart b/lib/system_settings.dart index 261dc28..97ac4e5 100644 --- a/lib/system_settings.dart +++ b/lib/system_settings.dart @@ -21,6 +21,10 @@ class SystemSettings { return await _channel.invokeMethod('system'); } + static Future powerUsage() async { + return await _channel.invokeMethod('power-usage'); + } + static Future location() async { return await _channel.invokeMethod('location'); } diff --git a/pubspec.yaml b/pubspec.yaml index e245b63..64f968a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: system_settings description: Flutter plugin to open system and app settings on iOS and Android. -version: 2.1.0 +version: 2.1.1 homepage: https://github.com/fweinaug/system_settings repository: https://github.com/fweinaug/system_settings diff --git a/system_settings.iml b/system_settings.iml index 0ada17d..ec02a8c 100644 --- a/system_settings.iml +++ b/system_settings.iml @@ -2,17 +2,9 @@ - - - - - - - - - + + - \ No newline at end of file