From 9b360639cdad5709323adf0c4fe40ccd6daaff53 Mon Sep 17 00:00:00 2001 From: Jacqueline Bronger Date: Mon, 2 Aug 2021 11:32:20 +0000 Subject: [PATCH] Set privacy chip bounds in WindowInsets for tv. Now also listening to layout direction changes so the chip location as well as the chip bounds are updated immediately. Test: atest PrivacyIndicatorBoundsTests + manual: change languages between English and Arabic with and without the indicator present Bug: 185932128 Change-Id: Ie321982394fc63ccbd77c625cc6a51053562ea3c --- .../res/layout/tv_ongoing_privacy_chip.xml | 2 +- .../SystemUI/res/values-television/dimens.xml | 1 + .../television/TvOngoingPrivacyChip.java | 76 ++++++++++++++++--- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/res/layout/tv_ongoing_privacy_chip.xml b/packages/SystemUI/res/layout/tv_ongoing_privacy_chip.xml index 6218a5e6ea820..45b9f25e68e11 100644 --- a/packages/SystemUI/res/layout/tv_ongoing_privacy_chip.xml +++ b/packages/SystemUI/res/layout/tv_ongoing_privacy_chip.xml @@ -26,7 +26,7 @@ 9dp 9dp 12dp + 51dp 24dp 30dp 12dp diff --git a/packages/SystemUI/src/com/android/systemui/privacy/television/TvOngoingPrivacyChip.java b/packages/SystemUI/src/com/android/systemui/privacy/television/TvOngoingPrivacyChip.java index d94cd28c3bc65..875097625a6f0 100644 --- a/packages/SystemUI/src/com/android/systemui/privacy/television/TvOngoingPrivacyChip.java +++ b/packages/SystemUI/src/com/android/systemui/privacy/television/TvOngoingPrivacyChip.java @@ -24,13 +24,17 @@ import android.animation.ObjectAnimator; import android.annotation.IntDef; import android.annotation.UiThread; import android.content.Context; +import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.PixelFormat; +import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Looper; +import android.os.RemoteException; import android.util.Log; import android.view.Gravity; +import android.view.IWindowManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -98,6 +102,10 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl private final Context mContext; private final PrivacyItemController mPrivacyItemController; + private final IWindowManager mIWindowManager; + private final Rect[] mBounds = new Rect[4]; + private boolean mIsRtl; + private ViewGroup mIndicatorView; private boolean mViewAndWindowAdded; private ObjectAnimator mAnimator; @@ -124,17 +132,23 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl private int mState = STATE_NOT_SHOWN; @Inject - public TvOngoingPrivacyChip(Context context, PrivacyItemController privacyItemController) { + public TvOngoingPrivacyChip(Context context, PrivacyItemController privacyItemController, + IWindowManager iWindowManager) { super(context); if (DEBUG) Log.d(TAG, "Privacy chip running"); mContext = context; mPrivacyItemController = privacyItemController; + mIWindowManager = iWindowManager; Resources res = mContext.getResources(); mIconMarginStart = Math.round( res.getDimension(R.dimen.privacy_chip_icon_margin_in_between)); mIconSize = res.getDimensionPixelSize(R.dimen.privacy_chip_icon_size); + mIsRtl = mContext.getResources().getConfiguration().getLayoutDirection() + == View.LAYOUT_DIRECTION_RTL; + updateStaticPrivacyIndicatorBounds(); + mAnimationDurationMs = res.getInteger(R.integer.privacy_chip_animation_millis); mMicCameraIndicatorFlagEnabled = privacyItemController.getMicCameraAvailable(); @@ -146,6 +160,24 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl } } + @Override + public void onConfigurationChanged(Configuration config) { + boolean updatedRtl = config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; + if (mIsRtl == updatedRtl) { + return; + } + mIsRtl = updatedRtl; + + updateStaticPrivacyIndicatorBounds(); + + // Update privacy chip location. + if (mState == STATE_NOT_SHOWN || mIndicatorView == null) { + return; + } + fadeOutIndicator(); + createAndShowIndicator(); + } + @Override public void start() { mPrivacyItemController.addCallback(this); @@ -181,6 +213,32 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl updateChip(); } + private void updateStaticPrivacyIndicatorBounds() { + Resources res = mContext.getResources(); + int mMaxExpandedWidth = res.getDimensionPixelSize(R.dimen.privacy_chip_max_width); + int mMaxExpandedHeight = res.getDimensionPixelSize(R.dimen.privacy_chip_height); + int mChipMarginTotal = 2 * res.getDimensionPixelSize(R.dimen.privacy_chip_margin); + + final WindowManager windowManager = mContext.getSystemService(WindowManager.class); + Rect screenBounds = windowManager.getCurrentWindowMetrics().getBounds(); + mBounds[0] = new Rect( + mIsRtl ? screenBounds.left + : screenBounds.right - mChipMarginTotal - mMaxExpandedWidth, + screenBounds.top, + mIsRtl ? screenBounds.left + mChipMarginTotal + mMaxExpandedWidth + : screenBounds.right, + screenBounds.top + mChipMarginTotal + mMaxExpandedHeight + ); + + if (DEBUG) Log.v(TAG, "privacy indicator bounds: " + mBounds[0].toShortString()); + + try { + mIWindowManager.updateStaticPrivacyIndicatorBounds(mContext.getDisplayId(), mBounds); + } catch (RemoteException e) { + Log.w(TAG, "could not update privacy indicator bounds"); + } + } + private void updateChip() { if (DEBUG) Log.d(TAG, mPrivacyItems.size() + " privacy items"); @@ -316,13 +374,9 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl } }); - final boolean isRtl = mContext.getResources().getConfiguration().getLayoutDirection() - == View.LAYOUT_DIRECTION_RTL; - if (DEBUG) Log.d(TAG, "is RTL: " + isRtl); - mChipDrawable = new PrivacyChipDrawable(mContext); mChipDrawable.setListener(this); - mChipDrawable.setRtl(isRtl); + mChipDrawable.setRtl(mIsRtl); ImageView chipBackground = mIndicatorView.findViewById(R.id.chip_drawable); if (chipBackground != null) { chipBackground.setImageDrawable(mChipDrawable); @@ -332,17 +386,21 @@ public class TvOngoingPrivacyChip extends SystemUI implements PrivacyItemControl mIconsContainer.setAlpha(0f); updateIcons(); + final WindowManager windowManager = mContext.getSystemService(WindowManager.class); + windowManager.addView(mIndicatorView, getWindowLayoutParams()); + } + + private WindowManager.LayoutParams getWindowLayoutParams() { final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WRAP_CONTENT, WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); - layoutParams.gravity = Gravity.TOP | (isRtl ? Gravity.LEFT : Gravity.RIGHT); + layoutParams.gravity = Gravity.TOP | (mIsRtl ? Gravity.LEFT : Gravity.RIGHT); layoutParams.setTitle(LAYOUT_PARAMS_TITLE); layoutParams.packageName = mContext.getPackageName(); - final WindowManager windowManager = mContext.getSystemService(WindowManager.class); - windowManager.addView(mIndicatorView, layoutParams); + return layoutParams; } private void updateIcons() {