Skip to content

feat: Support the v2 Android embedding. #26

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 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package flutter.plugins.screen.screen;

import android.app.Activity;
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;
Expand All @@ -12,18 +18,47 @@
/**
* ScreenPlugin
*/
public class ScreenPlugin implements MethodCallHandler {
public class ScreenPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {

private ScreenPlugin(Registrar registrar){
this._registrar = registrar;
this._activity = registrar.activity();
}
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 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() { }

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
this._activity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
this._activity = null;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
switch(call.method){
Expand All @@ -32,24 +67,24 @@ public void onMethodCall(MethodCall call, Result result) {
break;
case "setBrightness":
double brightness = call.argument("brightness");
WindowManager.LayoutParams layoutParams = _registrar.activity().getWindow().getAttributes();
WindowManager.LayoutParams layoutParams = _activity.getWindow().getAttributes();
layoutParams.screenBrightness = (float)brightness;
_registrar.activity().getWindow().setAttributes(layoutParams);
_activity.getWindow().setAttributes(layoutParams);
result.success(null);
break;
case "isKeptOn":
int flags = _registrar.activity().getWindow().getAttributes().flags;
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 ");
_registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_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);
_activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
result.success(null);
break;
Expand All @@ -61,16 +96,15 @@ public void onMethodCall(MethodCall call, Result result) {
}

private float getBrightness(){
float result = _registrar.activity().getWindow().getAttributes().screenBrightness;
float result = _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;
result = Settings.System.getInt(_activity.getApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) / (float)255;
} catch (Settings.SettingNotFoundException e) {
result = 1.0f;
e.printStackTrace();
}
}
return result;
}

}