OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.base; | 5 package org.chromium.ui.base; |
6 | 6 |
7 import android.animation.Animator; | 7 import android.animation.Animator; |
8 import android.animation.AnimatorListenerAdapter; | 8 import android.animation.AnimatorListenerAdapter; |
9 import android.annotation.SuppressLint; | 9 import android.annotation.SuppressLint; |
10 import android.annotation.TargetApi; | 10 import android.annotation.TargetApi; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 protected SparseArray<IntentCallback> mOutstandingIntents; | 84 protected SparseArray<IntentCallback> mOutstandingIntents; |
85 // We use a weak reference here to prevent this from leaking in WebView. | 85 // We use a weak reference here to prevent this from leaking in WebView. |
86 private WeakReference<Context> mContextRef; | 86 private WeakReference<Context> mContextRef; |
87 | 87 |
88 // Ideally, this would be a SparseArray<String>, but there's no easy way to
store a | 88 // Ideally, this would be a SparseArray<String>, but there's no easy way to
store a |
89 // SparseArray<String> in a bundle during saveInstanceState(). So we use a H
ashMap and suppress | 89 // SparseArray<String> in a bundle during saveInstanceState(). So we use a H
ashMap and suppress |
90 // the Android lint warning "UseSparseArrays". | 90 // the Android lint warning "UseSparseArrays". |
91 protected HashMap<Integer, String> mIntentErrors; | 91 protected HashMap<Integer, String> mIntentErrors; |
92 | 92 |
93 // We track all animations over content and provide a drawing placeholder fo
r them. | 93 // We track all animations over content and provide a drawing placeholder fo
r them. |
94 private HashSet<Animator> mAnimationsOverContent = new HashSet<Animator>(); | 94 private HashSet<Animator> mAnimationsOverContent = new HashSet<>(); |
95 private View mAnimationPlaceholderView; | 95 private View mAnimationPlaceholderView; |
96 | 96 |
97 private ViewGroup mKeyboardAccessoryView; | 97 private ViewGroup mKeyboardAccessoryView; |
98 | 98 |
99 protected boolean mIsKeyboardShowing = false; | 99 protected boolean mIsKeyboardShowing = false; |
100 | 100 |
101 // System accessibility service. | 101 // System accessibility service. |
102 private final AccessibilityManager mAccessibilityManager; | 102 private final AccessibilityManager mAccessibilityManager; |
103 | 103 |
104 // Whether touch exploration is enabled. | 104 // Whether touch exploration is enabled. |
105 private boolean mIsTouchExplorationEnabled; | 105 private boolean mIsTouchExplorationEnabled; |
106 | 106 |
107 // On KitKat and higher, a class that monitors the touch exploration state. | 107 // On KitKat and higher, a class that monitors the touch exploration state. |
108 private TouchExplorationMonitor mTouchExplorationMonitor; | 108 private TouchExplorationMonitor mTouchExplorationMonitor; |
109 | 109 |
110 private AndroidPermissionDelegate mPermissionDelegate; | 110 private AndroidPermissionDelegate mPermissionDelegate; |
111 | 111 |
112 /** | 112 /** |
113 * An interface to notify listeners of changes in the soft keyboard's visibi
lity. | 113 * An interface to notify listeners of changes in the soft keyboard's visibi
lity. |
114 */ | 114 */ |
115 public interface KeyboardVisibilityListener { | 115 public interface KeyboardVisibilityListener { |
116 public void keyboardVisibilityChanged(boolean isShowing); | 116 public void keyboardVisibilityChanged(boolean isShowing); |
117 } | 117 } |
118 private LinkedList<KeyboardVisibilityListener> mKeyboardVisibilityListeners
= | 118 private LinkedList<KeyboardVisibilityListener> mKeyboardVisibilityListeners
= |
119 new LinkedList<KeyboardVisibilityListener>(); | 119 new LinkedList<>(); |
120 | 120 |
121 private final VSyncMonitor.Listener mVSyncListener = new VSyncMonitor.Listen
er() { | 121 private final VSyncMonitor.Listener mVSyncListener = new VSyncMonitor.Listen
er() { |
122 @Override | 122 @Override |
123 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) { | 123 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) { |
124 if (mNativeWindowAndroid != 0) { | 124 if (mNativeWindowAndroid != 0) { |
125 nativeOnVSync(mNativeWindowAndroid, | 125 nativeOnVSync(mNativeWindowAndroid, |
126 vsyncTimeMicros, | 126 vsyncTimeMicros, |
127 mVSyncMonitor.getVSyncPeriodInMicroseconds()); | 127 mVSyncMonitor.getVSyncPeriodInMicroseconds()); |
128 } | 128 } |
129 } | 129 } |
(...skipping 29 matching lines...) Expand all Loading... |
159 /** | 159 /** |
160 * @return The time interval between two consecutive vsync pulses in millise
conds. | 160 * @return The time interval between two consecutive vsync pulses in millise
conds. |
161 */ | 161 */ |
162 public long getVsyncPeriodInMillis() { | 162 public long getVsyncPeriodInMillis() { |
163 return mVSyncMonitor.getVSyncPeriodInMicroseconds() / 1000; | 163 return mVSyncMonitor.getVSyncPeriodInMicroseconds() / 1000; |
164 } | 164 } |
165 | 165 |
166 /** | 166 /** |
167 * @param context The application context. | 167 * @param context The application context. |
168 */ | 168 */ |
| 169 public WindowAndroid(Context context) { |
| 170 this(context, DisplayAndroid.getNonMultiDisplay(context)); |
| 171 } |
| 172 |
| 173 /** |
| 174 * @param context The application context. |
| 175 * @param display |
| 176 */ |
169 @SuppressLint("UseSparseArrays") | 177 @SuppressLint("UseSparseArrays") |
170 public WindowAndroid(Context context) { | 178 protected WindowAndroid(Context context, DisplayAndroid display) { |
171 mApplicationContext = context.getApplicationContext(); | 179 mApplicationContext = context.getApplicationContext(); |
172 // context does not have the same lifetime guarantees as an application
context so we can't | 180 // context does not have the same lifetime guarantees as an application
context so we can't |
173 // hold a strong reference to it. | 181 // hold a strong reference to it. |
174 mContextRef = new WeakReference<Context>(context); | 182 mContextRef = new WeakReference<>(context); |
175 mOutstandingIntents = new SparseArray<IntentCallback>(); | 183 mOutstandingIntents = new SparseArray<>(); |
176 mIntentErrors = new HashMap<Integer, String>(); | 184 mIntentErrors = new HashMap<>(); |
177 mVSyncMonitor = new VSyncMonitor(context, mVSyncListener); | 185 mVSyncMonitor = new VSyncMonitor(context, mVSyncListener); |
178 mAccessibilityManager = (AccessibilityManager) mApplicationContext.getSy
stemService( | 186 mAccessibilityManager = (AccessibilityManager) mApplicationContext.getSy
stemService( |
179 Context.ACCESSIBILITY_SERVICE); | 187 Context.ACCESSIBILITY_SERVICE); |
180 mDisplayAndroid = DisplayAndroid.getNonMultiDisplay(context); | 188 mDisplayAndroid = display; |
181 } | 189 } |
182 | 190 |
183 @CalledByNative | 191 @CalledByNative |
184 private static long createForTesting(Context context) { | 192 private static long createForTesting(Context context) { |
185 WindowAndroid windowAndroid = new WindowAndroid(context); | 193 WindowAndroid windowAndroid = new WindowAndroid(context); |
186 // |windowAndroid.getNativePointer()| creates native WindowAndroid objec
t | 194 // |windowAndroid.getNativePointer()| creates native WindowAndroid objec
t |
187 // which stores a global ref to |windowAndroid|. Therefore |windowAndroi
d| | 195 // which stores a global ref to |windowAndroid|. Therefore |windowAndroi
d| |
188 // is not immediately eligible for gc. | 196 // is not immediately eligible for gc. |
189 return windowAndroid.getNativePointer(); | 197 return windowAndroid.getNativePointer(); |
190 } | 198 } |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 public DisplayAndroid getDisplay() { | 412 public DisplayAndroid getDisplay() { |
405 return mDisplayAndroid; | 413 return mDisplayAndroid; |
406 } | 414 } |
407 | 415 |
408 /** | 416 /** |
409 * @return A reference to owning Activity. The returned WeakReference will
never be null, but | 417 * @return A reference to owning Activity. The returned WeakReference will
never be null, but |
410 * the contained Activity can be null (either if it has been garbage
collected or if | 418 * the contained Activity can be null (either if it has been garbage
collected or if |
411 * this is in the context of a WebView that was not created using an
Activity). | 419 * this is in the context of a WebView that was not created using an
Activity). |
412 */ | 420 */ |
413 public WeakReference<Activity> getActivity() { | 421 public WeakReference<Activity> getActivity() { |
414 return new WeakReference<Activity>(null); | 422 return new WeakReference<>(null); |
415 } | 423 } |
416 | 424 |
417 /** | 425 /** |
418 * @return The application context for this activity. | 426 * @return The application context for this activity. |
419 */ | 427 */ |
420 public Context getApplicationContext() { | 428 public Context getApplicationContext() { |
421 return mApplicationContext; | 429 return mApplicationContext; |
422 } | 430 } |
423 | 431 |
424 /** | 432 /** |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 * To be called when the keyboard visibility state might have changed. Infor
ms listeners of the | 615 * To be called when the keyboard visibility state might have changed. Infor
ms listeners of the |
608 * state change IFF there actually was a change. | 616 * state change IFF there actually was a change. |
609 * @param isShowing The current (guesstimated) state of the keyboard. | 617 * @param isShowing The current (guesstimated) state of the keyboard. |
610 */ | 618 */ |
611 protected void keyboardVisibilityPossiblyChanged(boolean isShowing) { | 619 protected void keyboardVisibilityPossiblyChanged(boolean isShowing) { |
612 if (mIsKeyboardShowing == isShowing) return; | 620 if (mIsKeyboardShowing == isShowing) return; |
613 mIsKeyboardShowing = isShowing; | 621 mIsKeyboardShowing = isShowing; |
614 | 622 |
615 // Clone the list in case a listener tries to remove itself during the c
allback. | 623 // Clone the list in case a listener tries to remove itself during the c
allback. |
616 LinkedList<KeyboardVisibilityListener> listeners = | 624 LinkedList<KeyboardVisibilityListener> listeners = |
617 new LinkedList<KeyboardVisibilityListener>(mKeyboardVisibilityLi
steners); | 625 new LinkedList<>(mKeyboardVisibilityListeners); |
618 for (KeyboardVisibilityListener listener : listeners) { | 626 for (KeyboardVisibilityListener listener : listeners) { |
619 listener.keyboardVisibilityChanged(isShowing); | 627 listener.keyboardVisibilityChanged(isShowing); |
620 } | 628 } |
621 } | 629 } |
622 | 630 |
623 /** | 631 /** |
624 * Start a post-layout animation on top of web content. | 632 * Start a post-layout animation on top of web content. |
625 * | 633 * |
626 * By default, Android optimizes what it shows on top of SurfaceViews (saves
power). | 634 * By default, Android optimizes what it shows on top of SurfaceViews (saves
power). |
627 * Effectively, layouts determine what gets drawn and post-layout animations
outside | 635 * Effectively, layouts determine what gets drawn and post-layout animations
outside |
(...skipping 28 matching lines...) Expand all Loading... |
656 }); | 664 }); |
657 } | 665 } |
658 | 666 |
659 /** | 667 /** |
660 * Getter for the current context (not necessarily the application context). | 668 * Getter for the current context (not necessarily the application context). |
661 * Make no assumptions regarding what type of Context is returned here, it c
ould be for example | 669 * Make no assumptions regarding what type of Context is returned here, it c
ould be for example |
662 * an Activity or a Context created specifically to target an external displ
ay. | 670 * an Activity or a Context created specifically to target an external displ
ay. |
663 */ | 671 */ |
664 public WeakReference<Context> getContext() { | 672 public WeakReference<Context> getContext() { |
665 // Return a new WeakReference to prevent clients from releasing our inte
rnal WeakReference. | 673 // Return a new WeakReference to prevent clients from releasing our inte
rnal WeakReference. |
666 return new WeakReference<Context>(mContextRef.get()); | 674 return new WeakReference<>(mContextRef.get()); |
667 } | 675 } |
668 | 676 |
669 /** | 677 /** |
670 * Update whether the placeholder is 'drawn' based on whether an animation i
s running | 678 * Update whether the placeholder is 'drawn' based on whether an animation i
s running |
671 * or touch exploration is enabled - if either of those are true, we call | 679 * or touch exploration is enabled - if either of those are true, we call |
672 * setWillNotDraw(false) to ensure that the animation is drawn over the Surf
aceView, | 680 * setWillNotDraw(false) to ensure that the animation is drawn over the Surf
aceView, |
673 * and otherwise we call setWillNotDraw(true). | 681 * and otherwise we call setWillNotDraw(true). |
674 */ | 682 */ |
675 private void refreshWillNotDraw() { | 683 private void refreshWillNotDraw() { |
676 boolean willNotDraw = !mIsTouchExplorationEnabled && mAnimationsOverCont
ent.isEmpty(); | 684 boolean willNotDraw = !mIsTouchExplorationEnabled && mAnimationsOverCont
ent.isEmpty(); |
677 if (mAnimationPlaceholderView.willNotDraw() != willNotDraw) { | 685 if (mAnimationPlaceholderView.willNotDraw() != willNotDraw) { |
678 mAnimationPlaceholderView.setWillNotDraw(willNotDraw); | 686 mAnimationPlaceholderView.setWillNotDraw(willNotDraw); |
679 } | 687 } |
680 } | 688 } |
681 | 689 |
682 private native long nativeInit(int displayId); | 690 private native long nativeInit(int displayId); |
683 private native void nativeOnVSync(long nativeWindowAndroid, | 691 private native void nativeOnVSync(long nativeWindowAndroid, |
684 long vsyncTimeMicros, | 692 long vsyncTimeMicros, |
685 long vsyncPeriodMicros); | 693 long vsyncPeriodMicros); |
686 private native void nativeOnVisibilityChanged(long nativeWindowAndroid, bool
ean visible); | 694 private native void nativeOnVisibilityChanged(long nativeWindowAndroid, bool
ean visible); |
687 private native void nativeOnActivityStopped(long nativeWindowAndroid); | 695 private native void nativeOnActivityStopped(long nativeWindowAndroid); |
688 private native void nativeOnActivityStarted(long nativeWindowAndroid); | 696 private native void nativeOnActivityStarted(long nativeWindowAndroid); |
689 private native void nativeDestroy(long nativeWindowAndroid); | 697 private native void nativeDestroy(long nativeWindowAndroid); |
690 | 698 |
691 } | 699 } |
OLD | NEW |