Skip to content

add miui support #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
146 changes: 92 additions & 54 deletions android/src/main/java/flutter/plugins/screen/screen/ScreenPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
}
}