The window's KEEP_SCREEN_ON flag is more like controlling the screen from an...

The window's KEEP_SCREEN_ON flag is more like controlling the screen from an Activity level, and we shouldn't use it instead, view's KEEP_SCREEN_ON flag should be used.

This patch attaches a zero size view to ContentViewCore's mContainerView instead of view tree, so once the ContainerView is invisible, the screen can turned off even PowerSaveBlocker not removed.


BUG=270903

Review URL: https://chromiumcodereview.appspot.com/20125004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@216840 0039d316-1c4b-4281-b951-d872f2087c98
parent 24d83228
......@@ -5,23 +5,26 @@
#include "content/browser/power_save_blocker_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_helper.h"
#include "base/logging.h"
#include "content/browser/power_save_blocker_impl.h"
#include "content/public/browser/android/content_view_core.h"
#include "content/public/browser/browser_thread.h"
#include "jni/PowerSaveBlocker_jni.h"
#include "ui/android/window_android.h"
#include "ui/android/view_android.h"
using base::android::AttachCurrentThread;
using base::android::ScopedJavaLocalRef;
using gfx::NativeView;
namespace content {
class PowerSaveBlockerImpl::Delegate
: public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
public:
explicit Delegate(gfx::NativeWindow native_window) {
j_window_android_ = JavaObjectWeakGlobalRef(AttachCurrentThread(),
static_cast<ui::WindowAndroid*>(native_window)->GetJavaObject().obj());
explicit Delegate(NativeView view_android) {
j_view_android_ = JavaObjectWeakGlobalRef(
AttachCurrentThread(), view_android->GetJavaObject().obj());
}
// Does the actual work to apply or remove the desired power save block.
......@@ -32,7 +35,7 @@ class PowerSaveBlockerImpl::Delegate
friend class base::RefCountedThreadSafe<Delegate>;
~Delegate() {}
JavaObjectWeakGlobalRef j_window_android_;
JavaObjectWeakGlobalRef j_view_android_;
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
......@@ -40,7 +43,7 @@ class PowerSaveBlockerImpl::Delegate
void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_object = j_window_android_.get(env);
ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env);
if (j_object.obj())
Java_PowerSaveBlocker_applyBlock(env, j_object.obj());
}
......@@ -48,7 +51,7 @@ void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_object = j_window_android_.get(env);
ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env);
if (j_object.obj())
Java_PowerSaveBlocker_removeBlock(env, j_object.obj());
}
......@@ -66,9 +69,8 @@ PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
}
}
void PowerSaveBlockerImpl::InitDisplaySleepBlocker(
gfx::NativeWindow native_window) {
delegate_ = new Delegate(native_window);
void PowerSaveBlockerImpl::InitDisplaySleepBlocker(NativeView view_android) {
delegate_ = new Delegate(view_android);
// This may be called on any thread.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
......
......@@ -17,11 +17,10 @@ class PowerSaveBlockerImpl : public PowerSaveBlocker {
virtual ~PowerSaveBlockerImpl();
#if defined(OS_ANDROID)
// In Android platform, the |native_window| is needed to create the
// kPowerSaveBlockPreventDisplaySleep type of PowerSaveBlocker
// so the blocker could be removed by platform if the window isn't in the
// foreground.
void InitDisplaySleepBlocker(gfx::NativeWindow native_window);
// In Android platform, the kPowerSaveBlockPreventDisplaySleep type of
// PowerSaveBlocker should associated with the ViewAndroid,
// so the blocker could be removed by platform if the view isn't visble
void InitDisplaySleepBlocker(gfx::NativeView view_android);
#endif
private:
......
......@@ -2610,7 +2610,7 @@ void WebContentsImpl::OnMediaNotification(int64 player_cookie,
"Playing video");
#if defined(OS_ANDROID)
static_cast<PowerSaveBlockerImpl*>(blocker.get())->
InitDisplaySleepBlocker(GetView()->GetTopLevelNativeWindow());
InitDisplaySleepBlocker(GetView()->GetNativeView());
#endif
} else if (has_audio) {
blocker = PowerSaveBlocker::Create(
......
......@@ -5,16 +5,16 @@
package org.chromium.content.browser;
import org.chromium.base.CalledByNative;
import org.chromium.ui.WindowAndroid;
import org.chromium.ui.ViewAndroid;
class PowerSaveBlocker {
@CalledByNative
private static void applyBlock(WindowAndroid windowAndroid) {
windowAndroid.keepScreenOn(true);
private static void applyBlock(ViewAndroid view) {
view.incrementKeepScreenOnCount();
}
@CalledByNative
private static void removeBlock(WindowAndroid windowAndroid) {
windowAndroid.keepScreenOn(false);
private static void removeBlock(ViewAndroid view) {
view.decrementKeepScreenOnCount();
}
}
\ No newline at end of file
......@@ -4,6 +4,8 @@
package org.chromium.ui;
import android.view.View;
import org.chromium.base.JNINamespace;
import org.chromium.ui.ViewAndroidDelegate;
import org.chromium.ui.WindowAndroid;
......@@ -23,6 +25,8 @@ public class ViewAndroid {
private int mNativeViewAndroid = 0;
private final ViewAndroidDelegate mViewAndroidDelegate;
private final WindowAndroid mWindowAndroid;
private int mKeepScreenOnCount;
private View mKeepScreenOnView;
/**
* Constructs a View object.
......@@ -55,6 +59,29 @@ public class ViewAndroid {
return mNativeViewAndroid;
}
/**
* Set KeepScreenOn flag. If the flag already set, increase mKeepScreenOnCount.
*/
public void incrementKeepScreenOnCount() {
mKeepScreenOnCount++;
if (mKeepScreenOnCount == 1) {
mKeepScreenOnView = mViewAndroidDelegate.acquireAnchorView();
mKeepScreenOnView.setKeepScreenOn(true);
}
}
/**
* Decrease mKeepScreenOnCount, if it is decreased to 0, remove the flag.
*/
public void decrementKeepScreenOnCount() {
assert mKeepScreenOnCount > 0;
mKeepScreenOnCount--;
if (mKeepScreenOnCount == 0) {
mViewAndroidDelegate.releaseAnchorView(mKeepScreenOnView);
mKeepScreenOnView = null;
}
}
private native int nativeInit(int windowPtr);
private native void nativeDestroy(int nativeViewAndroid);
}
......@@ -11,7 +11,6 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.WindowManager;
import android.widget.Toast;
import java.util.HashMap;
......@@ -201,14 +200,6 @@ public class WindowAndroid {
return mNativeWindowAndroid;
}
public void keepScreenOn(boolean screenOn) {
if (screenOn) {
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
private native int nativeInit();
private native void nativeDestroy(int nativeWindowAndroid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment