From 4e763be6ec84fe411860ef6f466de6cc39b0d56b Mon Sep 17 00:00:00 2001 From: Daniel Dominguez Date: Wed, 20 Jun 2018 17:39:29 -0300 Subject: [PATCH] Add Java module to keep the screen awake when needed. --- app/src/page/file/view.js | 6 +++ .../browser/reactmodules/UtilityModule.java | 51 +++++++++++++++++++ .../reactpackages/LbryReactPackage.java | 4 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/lbry/browser/reactmodules/UtilityModule.java diff --git a/app/src/page/file/view.js b/app/src/page/file/view.js index 00f6944..f063e64 100644 --- a/app/src/page/file/view.js +++ b/app/src/page/file/view.js @@ -56,6 +56,9 @@ class FilePage extends React.PureComponent { if (NativeModules.Mixpanel) { NativeModules.Mixpanel.track('Open File Page', { Uri: uri }); } + if (NativeModules.UtilityModule) { + NativeModules.UtilityModule.keepAwakeOn(); + } } componentDidUpdate() { @@ -125,6 +128,9 @@ class FilePage extends React.PureComponent { StatusBar.setHidden(false); if (NativeModules.ScreenOrientation) { NativeModules.ScreenOrientation.unlockOrientation(); + } + if (NativeModules.UtilityModule) { + NativeModules.UtilityModule.keepAwakeOff(); } } diff --git a/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java new file mode 100644 index 0000000..083d8c8 --- /dev/null +++ b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java @@ -0,0 +1,51 @@ +package io.lbry.browser.reactmodules; + +import android.app.Activity; +import android.content.Context; +import android.view.WindowManager; + +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; + +public class UtilityModule extends ReactContextBaseJavaModule { + private Context context; + + public UtilityModule(ReactApplicationContext reactContext) { + super(reactContext); + this.context = reactContext; + } + + @Override + public String getName() { + return "UtilityModule"; + } + + @ReactMethod + public void keepAwakeOn() { + final Activity activity = getCurrentActivity(); + + if (activity != null) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + } + } + + @ReactMethod + public void keepAwakeOff() { + final Activity activity = getCurrentActivity(); + + if (activity != null) { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + } + } +} diff --git a/src/main/java/io/lbry/browser/reactpackages/LbryReactPackage.java b/src/main/java/io/lbry/browser/reactpackages/LbryReactPackage.java index 6fa4c9f..a1c7eba 100644 --- a/src/main/java/io/lbry/browser/reactpackages/LbryReactPackage.java +++ b/src/main/java/io/lbry/browser/reactpackages/LbryReactPackage.java @@ -11,6 +11,7 @@ import io.lbry.browser.reactmodules.FirstRunModule; import io.lbry.browser.reactmodules.MixpanelModule; import io.lbry.browser.reactmodules.ScreenOrientationModule; import io.lbry.browser.reactmodules.VersionInfoModule; +import io.lbry.browser.reactmodules.UtilityModule;; import java.util.ArrayList; import java.util.Collections; @@ -31,8 +32,9 @@ public class LbryReactPackage implements ReactPackage { modules.add(new FirstRunModule(reactContext)); modules.add(new MixpanelModule(reactContext)); modules.add(new ScreenOrientationModule(reactContext)); + modules.add(new UtilityModule(reactContext)); modules.add(new VersionInfoModule(reactContext)); return modules; } -} \ No newline at end of file +}