Skip to content
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

update the plugin native android code to make it compatible with V2 #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,66 @@
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.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.MethodCall;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry;

/**
* ScreenPlugin
*/
public class ScreenPlugin implements MethodCallHandler {
public class ScreenPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

private ScreenPlugin(Registrar registrar){
this._registrar = registrar;
}
private Registrar _registrar;
// public ScreenPlugin(PluginRegistry.Registrar registrar){
// this._registrar = registrar;
// }

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(){
}
// private PluginRegistry.Registrar _registrar;
private MethodChannel channel;
private FlutterPluginBinding flutterPluginBinding;
private ActivityPluginBinding activityPluginBinding;

// public static void registerWith(@NonNull PluginRegistry.Registrar registrar) {
// final MethodChannel channel = new MethodChannel(registrar.messenger(), "github.com/clovisnicolas/flutter_screen");
// channel.setMethodCallHandler(new ScreenPlugin(registrar));
// }

@Override
public void onMethodCall(MethodCall call, Result result) {
public void onMethodCall(@NonNull 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();
WindowManager.LayoutParams layoutParams = activityPluginBinding.getActivity().getWindow().getAttributes();
layoutParams.screenBrightness = (float)brightness;
_registrar.activity().getWindow().setAttributes(layoutParams);
activityPluginBinding.getActivity().getWindow().setAttributes(layoutParams);
result.success(null);
break;
case "isKeptOn":
int flags = _registrar.activity().getWindow().getAttributes().flags;
int flags = activityPluginBinding.getActivity().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);
activityPluginBinding.getActivity().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);
activityPluginBinding.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
result.success(null);
break;
Expand All @@ -61,10 +74,10 @@ public void onMethodCall(MethodCall call, Result result) {
}

private float getBrightness(){
float result = _registrar.activity().getWindow().getAttributes().screenBrightness;
float result = activityPluginBinding.getActivity().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(flutterPluginBinding.getApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) / (float)255;
} catch (Settings.SettingNotFoundException e) {
result = 1.0f;
e.printStackTrace();
Expand All @@ -73,4 +86,35 @@ private float getBrightness(){
return result;
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
flutterPluginBinding=binding;
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) {
activityPluginBinding = binding;
}

@Override
public void onDetachedFromActivityForConfigChanges() {

}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

}

@Override
public void onDetachedFromActivity() {

}
}
10 changes: 5 additions & 5 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:label="screen_example"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:allowBackup="false">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
Expand All @@ -29,9 +30,8 @@
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<meta-data android:name="io.flutter.embedding.android.FlutterSplashView" android:resource="@drawable/launch_background"/>
<meta-data android:name="flutterEmbedding" android:value="2"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import androidx.annotation.NonNull;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
}
}
12 changes: 12 additions & 0 deletions example/android/app/src/main/res/drawable/normal_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_blue_bright" />

<!-- You can insert your own image assets here -->
<!-- <item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" />
</item> -->
</layer-list>
4 changes: 4 additions & 0 deletions example/android/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- You can name this style whatever you'd like -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@drawable/normal_background</item>
</style>
</resources>
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:7.0.2'
}
}

Expand Down
1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
3 changes: 3 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: screen_example
description: Demonstrates how to use the screen plugin.

environment:
sdk: ">=2.10.0 <3.0.0"
flutter: ">=2.0.0"
dependencies:
flutter:
sdk: flutter
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ author: Clovis Nicolas <[email protected]>
homepage: https://github.com/clovisnicolas/flutter_screen

environment:
sdk: '>=2.0.0 <3.0.0'
sdk: ">=2.10.0 <3.0.0"
flutter: ">=2.0.0"

dependencies:
flutter:
Expand Down