| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.autofill; |
| 18 |
|
| 19 |
import static android.view.autofill.Helper.sVerbose; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.graphics.drawable.Drawable; |
| 24 |
import android.os.IBinder; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.transition.Transition; |
| 27 |
import android.util.Log; |
| 28 |
import android.view.View; |
| 29 |
import android.view.View.OnTouchListener; |
| 30 |
import android.view.ViewTreeObserver; |
| 31 |
import android.view.WindowManager; |
| 32 |
import android.view.WindowManager.LayoutParams; |
| 33 |
import android.widget.PopupWindow; |
| 34 |
import android.window.WindowMetricsHelper; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class AutofillPopupWindow extends PopupWindow { |
| 45 |
|
| 46 |
private static final String TAG = "AutofillPopupWindow"; |
| 47 |
|
| 48 |
private final WindowPresenter mWindowPresenter; |
| 49 |
private WindowManager.LayoutParams mWindowLayoutParams; |
| 50 |
private boolean mFullScreen; |
| 51 |
|
| 52 |
private final View.OnAttachStateChangeListener mOnAttachStateChangeListener = |
| 53 |
new View.OnAttachStateChangeListener() { |
| 54 |
@Override |
| 55 |
public void onViewAttachedToWindow(View v) { |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onViewDetachedFromWindow(View v) { |
| 61 |
dismiss(); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public AutofillPopupWindow(@NonNull IAutofillWindowPresenter presenter) { |
| 81 |
mWindowPresenter = new WindowPresenter(presenter); |
| 82 |
|
| 83 |
setTouchModal(false); |
| 84 |
setOutsideTouchable(true); |
| 85 |
setInputMethodMode(INPUT_METHOD_NOT_NEEDED); |
| 86 |
setFocusable(true); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected boolean hasContentView() { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
protected boolean hasDecorView() { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
protected LayoutParams getDecorViewLayoutParams() { |
| 101 |
return mWindowLayoutParams; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public void update(View anchor, int offsetX, int offsetY, int width, int height, |
| 108 |
Rect virtualBounds) { |
| 109 |
mFullScreen = width == LayoutParams.MATCH_PARENT; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
setWindowLayoutType(mFullScreen ? WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG |
| 118 |
: WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
final View actualAnchor; |
| 124 |
if (mFullScreen) { |
| 125 |
offsetX = 0; |
| 126 |
offsetY = 0; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
final WindowManager windowManager = anchor.getContext() |
| 131 |
.getSystemService(WindowManager.class); |
| 132 |
final Rect windowBounds = WindowMetricsHelper.getBoundsExcludingNavigationBarAndCutout( |
| 133 |
windowManager.getCurrentWindowMetrics()); |
| 134 |
width = windowBounds.width(); |
| 135 |
if (height != LayoutParams.MATCH_PARENT) { |
| 136 |
offsetY = windowBounds.height() - height; |
| 137 |
} |
| 138 |
actualAnchor = anchor; |
| 139 |
} else if (virtualBounds != null) { |
| 140 |
final int[] mLocationOnScreen = new int[] {virtualBounds.left, virtualBounds.top}; |
| 141 |
actualAnchor = new View(anchor.getContext()) { |
| 142 |
@Override |
| 143 |
public void getLocationOnScreen(int[] location) { |
| 144 |
location[0] = mLocationOnScreen[0]; |
| 145 |
location[1] = mLocationOnScreen[1]; |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public int getAccessibilityViewId() { |
| 150 |
return anchor.getAccessibilityViewId(); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public ViewTreeObserver getViewTreeObserver() { |
| 155 |
return anchor.getViewTreeObserver(); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public IBinder getApplicationWindowToken() { |
| 160 |
return anchor.getApplicationWindowToken(); |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public View getRootView() { |
| 165 |
return anchor.getRootView(); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public int getLayoutDirection() { |
| 170 |
return anchor.getLayoutDirection(); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void getWindowDisplayFrame(Rect outRect) { |
| 175 |
anchor.getWindowDisplayFrame(outRect); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void addOnAttachStateChangeListener( |
| 180 |
OnAttachStateChangeListener listener) { |
| 181 |
anchor.addOnAttachStateChangeListener(listener); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void removeOnAttachStateChangeListener( |
| 186 |
OnAttachStateChangeListener listener) { |
| 187 |
anchor.removeOnAttachStateChangeListener(listener); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public boolean isAttachedToWindow() { |
| 192 |
return anchor.isAttachedToWindow(); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) { |
| 197 |
return anchor.requestRectangleOnScreen(rectangle, immediate); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public IBinder getWindowToken() { |
| 202 |
return anchor.getWindowToken(); |
| 203 |
} |
| 204 |
}; |
| 205 |
|
| 206 |
actualAnchor.setLeftTopRightBottom( |
| 207 |
virtualBounds.left, virtualBounds.top, |
| 208 |
virtualBounds.right, virtualBounds.bottom); |
| 209 |
actualAnchor.setScrollX(anchor.getScrollX()); |
| 210 |
actualAnchor.setScrollY(anchor.getScrollY()); |
| 211 |
|
| 212 |
anchor.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> { |
| 213 |
mLocationOnScreen[0] = mLocationOnScreen[0] - (scrollX - oldScrollX); |
| 214 |
mLocationOnScreen[1] = mLocationOnScreen[1] - (scrollY - oldScrollY); |
| 215 |
}); |
| 216 |
actualAnchor.setWillNotDraw(true); |
| 217 |
} else { |
| 218 |
actualAnchor = anchor; |
| 219 |
} |
| 220 |
|
| 221 |
if (!mFullScreen) { |
| 222 |
|
| 223 |
setAnimationStyle(-1); |
| 224 |
} else if (height == LayoutParams.MATCH_PARENT) { |
| 225 |
|
| 226 |
setAnimationStyle(0); |
| 227 |
} else { |
| 228 |
|
| 229 |
setAnimationStyle(com.android.internal.R.style.AutofillHalfScreenAnimation); |
| 230 |
} |
| 231 |
if (!isShowing()) { |
| 232 |
setWidth(width); |
| 233 |
setHeight(height); |
| 234 |
showAsDropDown(actualAnchor, offsetX, offsetY); |
| 235 |
} else { |
| 236 |
update(actualAnchor, offsetX, offsetY, width, height); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
protected void update(View anchor, WindowManager.LayoutParams params) { |
| 242 |
final int layoutDirection = anchor != null ? anchor.getLayoutDirection() |
| 243 |
: View.LAYOUT_DIRECTION_LOCALE; |
| 244 |
mWindowPresenter.show(params, getTransitionEpicenter(), isLayoutInsetDecor(), |
| 245 |
layoutDirection); |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
protected boolean findDropDownPosition(View anchor, LayoutParams outParams, |
| 250 |
int xOffset, int yOffset, int width, int height, int gravity, boolean allowScroll) { |
| 251 |
if (mFullScreen) { |
| 252 |
|
| 253 |
outParams.x = xOffset; |
| 254 |
outParams.y = yOffset; |
| 255 |
outParams.width = width; |
| 256 |
outParams.height = height; |
| 257 |
outParams.gravity = gravity; |
| 258 |
return false; |
| 259 |
} |
| 260 |
return super.findDropDownPosition(anchor, outParams, xOffset, yOffset, |
| 261 |
width, height, gravity, allowScroll); |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) { |
| 266 |
if (sVerbose) { |
| 267 |
Log.v(TAG, "showAsDropDown(): anchor=" + anchor + ", xoff=" + xoff + ", yoff=" + yoff |
| 268 |
+ ", isShowing(): " + isShowing()); |
| 269 |
} |
| 270 |
if (isShowing()) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
setShowing(true); |
| 275 |
setDropDown(true); |
| 276 |
attachToAnchor(anchor, xoff, yoff, gravity); |
| 277 |
final WindowManager.LayoutParams p = mWindowLayoutParams = createPopupLayoutParams( |
| 278 |
anchor.getWindowToken()); |
| 279 |
final boolean aboveAnchor = findDropDownPosition(anchor, p, xoff, yoff, |
| 280 |
p.width, p.height, gravity, getAllowScrollingAnchorParent()); |
| 281 |
updateAboveAnchor(aboveAnchor); |
| 282 |
p.accessibilityIdOfAnchor = anchor.getAccessibilityViewId(); |
| 283 |
p.packageName = anchor.getContext().getPackageName(); |
| 284 |
mWindowPresenter.show(p, getTransitionEpicenter(), isLayoutInsetDecor(), |
| 285 |
anchor.getLayoutDirection()); |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
protected void attachToAnchor(View anchor, int xoff, int yoff, int gravity) { |
| 290 |
super.attachToAnchor(anchor, xoff, yoff, gravity); |
| 291 |
anchor.addOnAttachStateChangeListener(mOnAttachStateChangeListener); |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
protected void detachFromAnchor() { |
| 296 |
final View anchor = getAnchor(); |
| 297 |
if (anchor != null) { |
| 298 |
anchor.removeOnAttachStateChangeListener(mOnAttachStateChangeListener); |
| 299 |
} |
| 300 |
super.detachFromAnchor(); |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void dismiss() { |
| 305 |
if (!isShowing() || isTransitioningToDismiss()) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
setShowing(false); |
| 310 |
setTransitioningToDismiss(true); |
| 311 |
|
| 312 |
mWindowPresenter.hide(getTransitionEpicenter()); |
| 313 |
detachFromAnchor(); |
| 314 |
if (getOnDismissListener() != null) { |
| 315 |
getOnDismissListener().onDismiss(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public int getAnimationStyle() { |
| 321 |
throw new IllegalStateException("You can't call this!"); |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
public Drawable getBackground() { |
| 326 |
throw new IllegalStateException("You can't call this!"); |
| 327 |
} |
| 328 |
|
| 329 |
@Override |
| 330 |
public View getContentView() { |
| 331 |
throw new IllegalStateException("You can't call this!"); |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public float getElevation() { |
| 336 |
throw new IllegalStateException("You can't call this!"); |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public Transition getEnterTransition() { |
| 341 |
throw new IllegalStateException("You can't call this!"); |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public Transition getExitTransition() { |
| 346 |
throw new IllegalStateException("You can't call this!"); |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void setBackgroundDrawable(Drawable background) { |
| 351 |
throw new IllegalStateException("You can't call this!"); |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public void setContentView(View contentView) { |
| 356 |
if (contentView != null) { |
| 357 |
throw new IllegalStateException("You can't call this!"); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public void setElevation(float elevation) { |
| 363 |
throw new IllegalStateException("You can't call this!"); |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public void setEnterTransition(Transition enterTransition) { |
| 368 |
throw new IllegalStateException("You can't call this!"); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public void setExitTransition(Transition exitTransition) { |
| 373 |
throw new IllegalStateException("You can't call this!"); |
| 374 |
} |
| 375 |
|
| 376 |
@Override |
| 377 |
public void setTouchInterceptor(OnTouchListener l) { |
| 378 |
throw new IllegalStateException("You can't call this!"); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
private class WindowPresenter { |
| 389 |
final IAutofillWindowPresenter mPresenter; |
| 390 |
|
| 391 |
WindowPresenter(IAutofillWindowPresenter presenter) { |
| 392 |
mPresenter = presenter; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
void show(WindowManager.LayoutParams p, Rect transitionEpicenter, boolean fitsSystemWindows, |
| 404 |
int layoutDirection) { |
| 405 |
try { |
| 406 |
mPresenter.show(p, transitionEpicenter, fitsSystemWindows, layoutDirection); |
| 407 |
} catch (RemoteException e) { |
| 408 |
Log.w(TAG, "Error showing fill window", e); |
| 409 |
e.rethrowFromSystemServer(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
void hide(Rect transitionEpicenter) { |
| 419 |
try { |
| 420 |
mPresenter.hide(transitionEpicenter); |
| 421 |
} catch (RemoteException e) { |
| 422 |
Log.w(TAG, "Error hiding fill window", e); |
| 423 |
e.rethrowFromSystemServer(); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |