1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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 * Custom {@link PopupWindow} used to isolate its content from the autofilled app - the 38 * UI is rendered in a framework process, but it's controlled by the app. 39 * 40 * TODO(b/34943932): use an app surface control solution. 41 * 42 * @hide 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 /* ignore - handled by the super class */ 57 } 58 59 @Override 60 public void onViewDetachedFromWindow(View v) { 61 dismiss(); 62 } 63 }; 64 65 /** 66 * Creates a popup window with a presenter owning the window and responsible for 67 * showing/hiding/updating the backing window. This can be useful of the window is 68 * being shown by another process while the popup logic is in the process hosting 69 * the anchor view. 70 * <p> 71 * Using this constructor means that the presenter completely owns the content of 72 * the window and the following methods manipulating the window content shouldn't 73 * be used: {@link #getEnterTransition()}, {@link #setEnterTransition(Transition)}, 74 * {@link #getExitTransition()}, {@link #setExitTransition(Transition)}, 75 * {@link #getContentView()}, {@link #setContentView(View)}, {@link #getBackground()}, 76 * {@link #setBackgroundDrawable(Drawable)}, {@link #getElevation()}, 77 * {@link #setElevation(float)}, ({@link #getAnimationStyle()}, 78 * {@link #setAnimationStyle(int)}, {@link #setTouchInterceptor(OnTouchListener)}.</p> 79 */ AutofillPopupWindow(@onNull IAutofillWindowPresenter presenter)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 hasContentView()90 protected boolean hasContentView() { 91 return true; 92 } 93 94 @Override hasDecorView()95 protected boolean hasDecorView() { 96 return true; 97 } 98 99 @Override getDecorViewLayoutParams()100 protected LayoutParams getDecorViewLayoutParams() { 101 return mWindowLayoutParams; 102 } 103 104 /** 105 * The effective {@code update} method that should be called by its clients. 106 */ update(View anchor, int offsetX, int offsetY, int width, int height, Rect virtualBounds)107 public void update(View anchor, int offsetX, int offsetY, int width, int height, 108 Rect virtualBounds) { 109 mFullScreen = width == LayoutParams.MATCH_PARENT; 110 // For no fullscreen autofill window, we want to show the window as system controlled one 111 // so it covers app windows, but it has to be an application type (so it's contained inside 112 // the application area). Hence, we set it to the application type with the highest z-order, 113 // which currently is TYPE_APPLICATION_ABOVE_SUB_PANEL. 114 // For fullscreen mode, autofill window is at the bottom of screen, it should not be 115 // clipped by app activity window. Fullscreen autofill window does not need to follow app 116 // anchor view position. 117 setWindowLayoutType(mFullScreen ? WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG 118 : WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL); 119 // If we are showing the popup for a virtual view we use a fake view which 120 // delegates to the anchor but present itself with the same bounds as the 121 // virtual view. This ensures that the location logic in popup works 122 // symmetrically when the dropdown is below and above the anchor. 123 final View actualAnchor; 124 if (mFullScreen) { 125 offsetX = 0; 126 offsetY = 0; 127 // If it is not fullscreen height, put window at bottom. Computes absolute position. 128 // Note that we cannot easily change default gravity from Gravity.TOP to 129 // Gravity.BOTTOM because PopupWindow base class does not expose computeGravity(). 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 // No fullscreen window animation is controlled by PopupWindow. 223 setAnimationStyle(-1); 224 } else if (height == LayoutParams.MATCH_PARENT) { 225 // Complete fullscreen autofill window has no animation. 226 setAnimationStyle(0); 227 } else { 228 // Slide half screen height autofill window from bottom. 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 update(View anchor, WindowManager.LayoutParams params)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 findDropDownPosition(View anchor, LayoutParams outParams, int xOffset, int yOffset, int width, int height, int gravity, boolean allowScroll)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 // In fullscreen mode, don't need consider the anchor view. 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 showAsDropDown(View anchor, int xoff, int yoff, int gravity)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 attachToAnchor(View anchor, int xoff, int yoff, int gravity)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 detachFromAnchor()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 dismiss()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 getAnimationStyle()320 public int getAnimationStyle() { 321 throw new IllegalStateException("You can't call this!"); 322 } 323 324 @Override getBackground()325 public Drawable getBackground() { 326 throw new IllegalStateException("You can't call this!"); 327 } 328 329 @Override getContentView()330 public View getContentView() { 331 throw new IllegalStateException("You can't call this!"); 332 } 333 334 @Override getElevation()335 public float getElevation() { 336 throw new IllegalStateException("You can't call this!"); 337 } 338 339 @Override getEnterTransition()340 public Transition getEnterTransition() { 341 throw new IllegalStateException("You can't call this!"); 342 } 343 344 @Override getExitTransition()345 public Transition getExitTransition() { 346 throw new IllegalStateException("You can't call this!"); 347 } 348 349 @Override setBackgroundDrawable(Drawable background)350 public void setBackgroundDrawable(Drawable background) { 351 throw new IllegalStateException("You can't call this!"); 352 } 353 354 @Override setContentView(View contentView)355 public void setContentView(View contentView) { 356 if (contentView != null) { 357 throw new IllegalStateException("You can't call this!"); 358 } 359 } 360 361 @Override setElevation(float elevation)362 public void setElevation(float elevation) { 363 throw new IllegalStateException("You can't call this!"); 364 } 365 366 @Override setEnterTransition(Transition enterTransition)367 public void setEnterTransition(Transition enterTransition) { 368 throw new IllegalStateException("You can't call this!"); 369 } 370 371 @Override setExitTransition(Transition exitTransition)372 public void setExitTransition(Transition exitTransition) { 373 throw new IllegalStateException("You can't call this!"); 374 } 375 376 @Override setTouchInterceptor(OnTouchListener l)377 public void setTouchInterceptor(OnTouchListener l) { 378 throw new IllegalStateException("You can't call this!"); 379 } 380 381 /** 382 * Contract between the popup window and a presenter that is responsible for 383 * showing/hiding/updating the actual window. 384 * 385 * <p>This can be useful if the anchor is in one process and the backing window is owned by 386 * another process. 387 */ 388 private class WindowPresenter { 389 final IAutofillWindowPresenter mPresenter; 390 WindowPresenter(IAutofillWindowPresenter presenter)391 WindowPresenter(IAutofillWindowPresenter presenter) { 392 mPresenter = presenter; 393 } 394 395 /** 396 * Shows the backing window. 397 * 398 * @param p The window layout params. 399 * @param transitionEpicenter The transition epicenter if animating. 400 * @param fitsSystemWindows Whether the content view should account for system decorations. 401 * @param layoutDirection The content layout direction to be consistent with the anchor. 402 */ show(WindowManager.LayoutParams p, Rect transitionEpicenter, boolean fitsSystemWindows, int layoutDirection)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 * Hides the backing window. 415 * 416 * @param transitionEpicenter The transition epicenter if animating. 417 */ hide(Rect transitionEpicenter)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 } 428