Index: content/public/android/java/src/org/chromium/content/browser/PowerSaveBlocker.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/PowerSaveBlocker.java b/content/public/android/java/src/org/chromium/content/browser/PowerSaveBlocker.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..915d668da16f01c7d09df3a7cc9c3c323a99434f |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/PowerSaveBlocker.java |
@@ -0,0 +1,46 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.content.browser; |
+ |
+import android.view.View; |
+ |
+import org.chromium.base.annotations.CalledByNative; |
+import org.chromium.base.annotations.JNINamespace; |
+import org.chromium.ui.base.ViewAndroidDelegate; |
+ |
+import java.lang.ref.WeakReference; |
+ |
+@JNINamespace("content") |
+class PowerSaveBlocker { |
+ // WeakReference to prevent leaks in Android WebView. |
+ private WeakReference<View> mKeepScreenOnView; |
+ |
+ @CalledByNative |
+ private static PowerSaveBlocker create() { |
+ return new PowerSaveBlocker(); |
+ } |
+ |
+ private PowerSaveBlocker() {} |
+ |
+ @CalledByNative |
+ private void applyBlock(ViewAndroidDelegate delegate) { |
+ assert mKeepScreenOnView == null; |
+ View anchorView = delegate.acquireAnchorView(); |
+ mKeepScreenOnView = new WeakReference<>(anchorView); |
+ delegate.setAnchorViewPosition(anchorView, 0, 0, 0, 0); |
+ anchorView.setKeepScreenOn(true); |
+ } |
+ |
+ @CalledByNative |
+ private void removeBlock(ViewAndroidDelegate delegate) { |
+ assert mKeepScreenOnView != null; |
+ View anchorView = mKeepScreenOnView.get(); |
+ mKeepScreenOnView = null; |
+ if (anchorView == null) return; |
+ |
+ anchorView.setKeepScreenOn(false); |
+ delegate.releaseAnchorView(anchorView); |
+ } |
+} |