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
11 changes: 9 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'
Expand All @@ -37,6 +37,13 @@ android {
lintOptions {
disable 'InvalidPackage'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

namespace 'de.florianweinaug.system_settings'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -57,46 +57,41 @@ 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()
}
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions lib/system_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class SystemSettings {
return await _channel.invokeMethod('system');
}

static Future<void> powerUsage() async {
return await _channel.invokeMethod('power-usage');
}

static Future<void> location() async {
return await _channel.invokeMethod('location');
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 2 additions & 10 deletions system_settings.iml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/build" />
</content>
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
</component>
</module>