Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java

Issue 2779033004: [Android] Focus/Blur contents when window focus changes (Closed)
Patch Set: rebase + fix nit Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java
index 3493dfef590fc0fe5740ccf545029b8efa968368..91ea2954f7e6e2d62aa9dc9942abcf4af4626453 100644
--- a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java
+++ b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java
@@ -360,6 +360,11 @@ public class ContentViewCore
// ResultReceiver in the InputMethodService (IME app) gets gc'ed.
private ShowKeyboardResultReceiver mShowKeyboardResultReceiver;
+ // Whether this ContentViewCore has view focus.
+ private boolean mHasViewFocus;
+ // Whether this ContentViewCore has window focus.
+ private boolean mHasWindowFocus;
+
// The list of observers that are notified when ContentViewCore changes its WindowAndroid.
private final ObserverList<WindowAndroidChangedObserver> mWindowAndroidChangedObservers;
@@ -665,6 +670,8 @@ public class ContentViewCore
}
mContainerView = containerView;
+ onFocusChangedInternal(getRealWindowFocusedState(), containerView.hasFocus(),
+ false /* hideKeyboardOnBlur */);
mContainerView.setClickable(true);
if (mSelectionPopupController != null) {
mSelectionPopupController.setContainerView(containerView);
@@ -680,6 +687,19 @@ public class ContentViewCore
}
}
+ /**
+ * Note: We don't use View#hasWindowFocus() because it returns incorrect values when the View
+ * doesn't have View focus.
+ * @return Whether the containerView actually has Window focus.
+ */
+ private boolean getRealWindowFocusedState() {
+ int[] states = mContainerView.getDrawableState();
+ for (int state : states) {
+ if ((state & android.R.attr.state_window_focused) != 0) return true;
+ }
+ return false;
+ }
+
@CalledByNative
private void onNativeContentViewCoreDestroyed(long nativeContentViewCore) {
assert nativeContentViewCore == mNativeContentViewCore;
@@ -1329,22 +1349,35 @@ public class ContentViewCore
* @see View#onWindowFocusChanged(boolean)
*/
public void onWindowFocusChanged(boolean hasWindowFocus) {
+ if (mHasWindowFocus == hasWindowFocus) return;
mImeAdapter.onWindowFocusChanged(hasWindowFocus);
if (!hasWindowFocus) resetGestureDetection();
mSelectionPopupController.onWindowFocusChanged(hasWindowFocus);
for (mGestureStateListenersIterator.rewind(); mGestureStateListenersIterator.hasNext();) {
mGestureStateListenersIterator.next().onWindowFocusChanged(hasWindowFocus);
}
+ onFocusChangedInternal(hasWindowFocus, mHasViewFocus, true /* hideKeyboardOnBlur */);
+ }
+
+ public void onFocusChanged(boolean hasViewFocus, boolean hideKeyboardOnBlur) {
+ if (mHasViewFocus == hasViewFocus) return;
+ onFocusChangedInternal(mHasWindowFocus, hasViewFocus, hideKeyboardOnBlur);
}
- public void onFocusChanged(boolean gainFocus, boolean hideKeyboardOnBlur) {
- mImeAdapter.onViewFocusChanged(gainFocus, hideKeyboardOnBlur);
+ private void onFocusChangedInternal(
+ boolean hasWindowFocus, boolean hasViewFocus, boolean hideKeyboardOnBlur) {
+ boolean hadInputFocus = mHasWindowFocus && mHasViewFocus;
+ boolean hasInputFocus = hasWindowFocus && hasViewFocus;
+ mHasWindowFocus = hasWindowFocus;
+ mHasViewFocus = hasViewFocus;
+ if (hasInputFocus == hadInputFocus) return;
+ mImeAdapter.onViewFocusChanged(hasInputFocus, hideKeyboardOnBlur);
if (mJoystickScrollProvider != null) {
- mJoystickScrollProvider.setEnabled(gainFocus && !isFocusedNodeEditable());
+ mJoystickScrollProvider.setEnabled(hasInputFocus && !isFocusedNodeEditable());
}
- if (gainFocus) {
+ if (hasInputFocus) {
restoreSelectionPopupsIfNecessary();
} else {
cancelRequestToScrollFocusedEditableNodeIntoView();
@@ -1359,7 +1392,7 @@ public class ContentViewCore
clearSelection();
}
}
- if (mNativeContentViewCore != 0) nativeSetFocus(mNativeContentViewCore, gainFocus);
+ if (mNativeContentViewCore != 0) nativeSetFocus(mNativeContentViewCore, hasInputFocus);
}
/**

Powered by Google App Engine
This is Rietveld 408576698