From 4f8c3131f4d24ae43c79686a79e432205e6f10a5 Mon Sep 17 00:00:00 2001 From: Miranda Kephart Date: Mon, 17 May 2021 11:44:33 -0400 Subject: [PATCH] Fix repeated screenshots in landscape Currently, if a screenshot is taken, then the phone is rotated while the UI is still up, the UI will correctly rotate but successive screenshots will fail. This change alters the way we handle rotations so that we update dynamically rarther than needing to reload the UI. Bug: 187238867 Fix: 187238867 Test: manual Change-Id: I3ebfd6a06fb5d4653ba4d267dcfb11c381eda105 --- .../layout-land/global_screenshot_preview.xml | 33 ------------------- .../screenshot/ScreenshotController.java | 29 +++++++++++----- .../systemui/screenshot/ScreenshotView.java | 18 ++++++++++ 3 files changed, 38 insertions(+), 42 deletions(-) delete mode 100644 packages/SystemUI/res/layout-land/global_screenshot_preview.xml diff --git a/packages/SystemUI/res/layout-land/global_screenshot_preview.xml b/packages/SystemUI/res/layout-land/global_screenshot_preview.xml deleted file mode 100644 index 93664da997175..0000000000000 --- a/packages/SystemUI/res/layout-land/global_screenshot_preview.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - \ No newline at end of file diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java index 7c2d4768fe8ff..1f9221c2956f4 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java @@ -401,9 +401,6 @@ public class ScreenshotController { if (DEBUG_UI) { Log.d(TAG, "reloadAssets()"); } - if (mScreenshotView != null && mScreenshotView.isAttachedToWindow()) { - mWindow.clearContentView(); // Is there a simpler way to say "remove screenshotView?" - } // respect the display cutout in landscape (since we'd otherwise overlap) but not portrait int orientation = mContext.getResources().getConfiguration().orientation; @@ -497,6 +494,17 @@ public class ScreenshotController { saveScreenshot(screenshot, finisher, screenRect, Insets.NONE, true); } + private void updateDisplayCutout() { + // respect the display cutout in landscape (since we'd otherwise overlap) but not portrait + int orientation = mContext.getResources().getConfiguration().orientation; + mWindowLayoutParams.setFitInsetsTypes( + orientation == ORIENTATION_PORTRAIT ? 0 : WindowInsets.Type.displayCutout()); + final View decorView = mWindow.peekDecorView(); + if (decorView != null && decorView.isAttachedToWindow()) { + mWindowManager.updateViewLayout(decorView, mWindowLayoutParams); + } + } + private void saveScreenshot(Bitmap screenshot, Consumer finisher, Rect screenRect, Insets screenInsets, boolean showFlash) { if (mAccessibilityManager.isEnabled()) { @@ -507,12 +515,6 @@ public class ScreenshotController { mAccessibilityManager.sendAccessibilityEvent(event); } - if (mConfigChanges.applyNewConfig(mContext.getResources())) { - if (DEBUG_UI) { - Log.d(TAG, "saveScreenshot: reloading assets"); - } - reloadAssets(); - } if (mScreenshotView.isAttachedToWindow()) { // if we didn't already dismiss for another reason @@ -526,6 +528,9 @@ public class ScreenshotController { mScreenshotView.reset(); } + int orientation = mContext.getResources().getConfiguration().orientation; + mScreenshotView.updateOrientation(orientation == ORIENTATION_PORTRAIT); + mScreenBitmap = screenshot; if (!isUserSetupComplete()) { @@ -556,6 +561,12 @@ public class ScreenshotController { mLastScrollCaptureRequest = mScrollCaptureClient.request(DEFAULT_DISPLAY); mLastScrollCaptureRequest.addListener(() -> onScrollCaptureResponseReady(mLastScrollCaptureRequest), mMainExecutor); + mWindow.peekDecorView().getViewRootImpl().setActivityConfigCallback( + (overrideConfig, newDisplayId) -> { + if (mConfigChanges.applyNewConfig(mContext.getResources())) { + updateDisplayCutout(); + } + }); }); attachWindow(); diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java index 70b11337db934..71a152dd961e7 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java @@ -60,6 +60,7 @@ import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.TouchDelegate; import android.view.View; +import android.view.ViewGroup; import android.view.ViewOutlineProvider; import android.view.ViewTreeObserver; import android.view.WindowInsets; @@ -350,6 +351,23 @@ public class ScreenshotView extends FrameLayout implements mScreenshotPreview.setImageDrawable(createScreenDrawable(mResources, bitmap, screenInsets)); } + void updateOrientation(boolean portrait) { + mOrientationPortrait = portrait; + int screenshotFixedSize = + mContext.getResources().getDimensionPixelSize(R.dimen.global_screenshot_x_scale); + ViewGroup.LayoutParams params = mScreenshotPreview.getLayoutParams(); + if (portrait) { + params.width = screenshotFixedSize; + params.height = LayoutParams.WRAP_CONTENT; + mScreenshotPreview.setScaleType(ImageView.ScaleType.FIT_START); + } else { + params.width = LayoutParams.WRAP_CONTENT; + params.height = screenshotFixedSize; + mScreenshotPreview.setScaleType(ImageView.ScaleType.FIT_END); + } + mScreenshotPreview.setLayoutParams(params); + } + AnimatorSet createScreenshotDropInAnimation(Rect bounds, boolean showFlash) { if (DEBUG_ANIM) { Log.d(TAG, "createAnim: bounds=" + bounds + " showFlash=" + showFlash);