diff --git a/.gitignore b/.gitignore index 14c7d4c..8eac3b0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,42 @@ build/ ios/.generated/ packages pubspec.lock +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.pub-cache/ +/build/ + +# Web related +lib/generated_plugin_registrant.dart + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Exceptions to above rules. +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/android/src/main/java/flutter/plugins/screen/screen/ScreenPlugin.java b/android/src/main/java/flutter/plugins/screen/screen/ScreenPlugin.java index e98a2a7..1eb66c7 100644 --- a/android/src/main/java/flutter/plugins/screen/screen/ScreenPlugin.java +++ b/android/src/main/java/flutter/plugins/screen/screen/ScreenPlugin.java @@ -1,76 +1,114 @@ package flutter.plugins.screen.screen; +import android.app.Activity; +import android.content.res.Resources; import android.provider.Settings; import android.view.WindowManager; +import androidx.annotation.NonNull; + +import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; 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.MethodCall; -import io.flutter.plugin.common.PluginRegistry.Registrar; /** * ScreenPlugin */ -public class ScreenPlugin implements MethodCallHandler { +public class ScreenPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware { - private ScreenPlugin(Registrar registrar){ - this._registrar = registrar; - } - private Registrar _registrar; + private Activity _activity; - public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "github.com/clovisnicolas/flutter_screen"); - channel.setMethodCallHandler(new ScreenPlugin(registrar)); - } + public ScreenPlugin() { } - @Override - public void onMethodCall(MethodCall call, Result result) { - switch(call.method){ - case "brightness": - result.success(getBrightness()); - break; - case "setBrightness": - double brightness = call.argument("brightness"); - WindowManager.LayoutParams layoutParams = _registrar.activity().getWindow().getAttributes(); - layoutParams.screenBrightness = (float)brightness; - _registrar.activity().getWindow().setAttributes(layoutParams); - result.success(null); - break; - case "isKeptOn": - int flags = _registrar.activity().getWindow().getAttributes().flags; - result.success((flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0) ; - break; - case "keepOn": - Boolean on = call.argument("on"); - if (on) { - System.out.println("Keeping screen on "); - _registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - else{ - System.out.println("Not keeping screen on"); - _registrar.activity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - result.success(null); - break; + @Override + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { + final MethodChannel channel = new MethodChannel(binding.getBinaryMessenger(), "github.com/clovisnicolas/flutter_screen"); + channel.setMethodCallHandler(this); + } + + @Override + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { + } + + @Override + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { + this._activity = binding.getActivity(); + } + + @Override + public void onDetachedFromActivityForConfigChanges() { + } - default: - result.notImplemented(); - break; + @Override + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { + this._activity = binding.getActivity(); } - } - private float getBrightness(){ - float result = _registrar.activity().getWindow().getAttributes().screenBrightness; - if (result < 0) { // the application is using the system brightness - try { - result = Settings.System.getInt(_registrar.context().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) / (float)255; - } catch (Settings.SettingNotFoundException e) { - result = 1.0f; - e.printStackTrace(); - } + @Override + public void onDetachedFromActivity() { + this._activity = null; } - return result; - } + @Override + public void onMethodCall(MethodCall call, Result result) { + switch (call.method) { + case "brightness": + result.success(getBrightness()); + break; + case "setBrightness": + double brightness = call.argument("brightness"); + WindowManager.LayoutParams layoutParams = _activity.getWindow().getAttributes(); + layoutParams.screenBrightness = (float) brightness; + _activity.getWindow().setAttributes(layoutParams); + result.success(null); + break; + case "isKeptOn": + int flags = _activity.getWindow().getAttributes().flags; + result.success((flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0); + break; + case "keepOn": + Boolean on = call.argument("on"); + if (on) { + System.out.println("Keeping screen on "); + _activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } else { + System.out.println("Not keeping screen on"); + _activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + result.success(null); + break; + default: + result.notImplemented(); + break; + } + } + + private float getBrightness() { + float result = _activity.getWindow().getAttributes().screenBrightness; + if (result < 0) { // the application is using the system brightness + try { + result = Settings.System.getInt(_activity.getApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) / (float) getBrightnessMax(); + } catch (Settings.SettingNotFoundException e) { + result = 1.0f; + e.printStackTrace(); + } + } + return result; + } + + private int getBrightnessMax() { + try { + Resources system = Resources.getSystem(); + int resId = system.getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); + if (resId != 0) { + return system.getInteger(resId); + } + } catch (Exception ignore) { + } + return 255; + } }