From 000163ad219b7ebb84b23dcd96e13868a2c919a7 Mon Sep 17 00:00:00 2001 From: Domingos Lopes Date: Thu, 30 Jun 2016 18:47:15 -0400 Subject: fix horizontal layout (MediaRouteControllerDialog) --- .../danoeh/antennapod/config/CastCallbackImpl.java | 12 +- .../dialog/CustomMRControllerDialog.java | 159 +++++++++++++++++---- .../fragment/CustomMRControllerDialogFragment.java | 15 ++ 3 files changed, 146 insertions(+), 40 deletions(-) create mode 100644 app/src/play/java/de/danoeh/antennapod/fragment/CustomMRControllerDialogFragment.java (limited to 'app/src/play/java') diff --git a/app/src/play/java/de/danoeh/antennapod/config/CastCallbackImpl.java b/app/src/play/java/de/danoeh/antennapod/config/CastCallbackImpl.java index 02720e42e..916b13a38 100644 --- a/app/src/play/java/de/danoeh/antennapod/config/CastCallbackImpl.java +++ b/app/src/play/java/de/danoeh/antennapod/config/CastCallbackImpl.java @@ -1,14 +1,11 @@ package de.danoeh.antennapod.config; -import android.content.Context; -import android.os.Bundle; import android.support.annotation.NonNull; -import android.support.v7.app.MediaRouteControllerDialog; import android.support.v7.app.MediaRouteControllerDialogFragment; import android.support.v7.app.MediaRouteDialogFactory; -import de.danoeh.antennapod.dialog.CustomMRControllerDialog; import de.danoeh.antennapod.core.CastCallbacks; +import de.danoeh.antennapod.fragment.CustomMRControllerDialogFragment; public class CastCallbackImpl implements CastCallbacks { @Override @@ -17,12 +14,7 @@ public class CastCallbackImpl implements CastCallbacks { @NonNull @Override public MediaRouteControllerDialogFragment onCreateControllerDialogFragment() { - return new MediaRouteControllerDialogFragment() { - @Override - public MediaRouteControllerDialog onCreateControllerDialog(Context context, Bundle savedInstanceState) { - return new CustomMRControllerDialog(context); - } - }; + return new CustomMRControllerDialogFragment(); } }; } diff --git a/app/src/play/java/de/danoeh/antennapod/dialog/CustomMRControllerDialog.java b/app/src/play/java/de/danoeh/antennapod/dialog/CustomMRControllerDialog.java index 009a8c254..f33af8823 100644 --- a/app/src/play/java/de/danoeh/antennapod/dialog/CustomMRControllerDialog.java +++ b/app/src/play/java/de/danoeh/antennapod/dialog/CustomMRControllerDialog.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.dialog; import android.app.PendingIntent; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -14,6 +15,7 @@ import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.util.Pair; +import android.support.v4.view.MarginLayoutParamsCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v7.app.MediaRouteControllerDialog; import android.support.v7.graphics.Palette; @@ -104,10 +106,49 @@ public class CustomMRControllerDialog extends MediaRouteControllerDialog { @Override public View onCreateMediaControlView(Bundle savedInstanceState) { - rootView = new LinearLayout(getContext()); - rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT)); - rootView.setOrientation(LinearLayout.VERTICAL); + boolean landscape = getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; + if (landscape) { + rootView = new LinearLayout(getContext()) { + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // We'd like to find the overall height before adjusting the widths within the LinearLayout + int maxHeight = Integer.MIN_VALUE; + if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) { + for (int i = 0; i < getChildCount(); i++) { + int height = Integer.MIN_VALUE; + View child = getChildAt(i); + ViewGroup.LayoutParams lp = child.getLayoutParams(); + if (lp.height >= 0) { + height = lp.height; + } else if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) { + child.measure(widthMeasureSpec, heightMeasureSpec); + height = child.getMeasuredHeight(); + } + maxHeight = Math.max(maxHeight, height); + } + } + if (maxHeight > 0) { + super.onMeasure(widthMeasureSpec, + MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY)); + } else { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + }; + } else { + rootView = new LinearLayout(getContext()); + } + FrameLayout.LayoutParams rootParams = new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + rootParams.setMargins(0, 0, 0, + getContext().getResources().getDimensionPixelSize(R.dimen.media_router_controller_bottom_margin)); + rootView.setLayoutParams(rootParams); + if (landscape) { + rootView.setOrientation(LinearLayout.HORIZONTAL); + } else { + rootView.setOrientation(LinearLayout.VERTICAL); + } // Start the session activity when a content item (album art, title or subtitle) is clicked. View.OnClickListener onClickListener = v -> { @@ -124,38 +165,89 @@ public class CustomMRControllerDialog extends MediaRouteControllerDialog { } }; - artView = new ImageView(getContext()) { - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - int desiredHeight = heightMeasureSpec; - if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) { - Drawable drawable = getDrawable(); - if (drawable != null) { - int originalWidth = MeasureSpec.getSize(widthMeasureSpec); - int intrHeight = drawable.getIntrinsicHeight(); - int intrWidth = drawable.getIntrinsicWidth(); - float scale; - if (intrHeight*16 > intrWidth*9) { - // image is taller than 16:9 - scale = (float) originalWidth * 9 / 16 / intrHeight; - } else { - // image is more horizontal than 16:9 - scale = (float) originalWidth / intrWidth; + LinearLayout.LayoutParams artParams; + if (landscape) { + artView = new ImageView(getContext()) { + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int desiredWidth = widthMeasureSpec; + int desiredMeasureMode = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY ? + MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; + if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY) { + Drawable drawable = getDrawable(); + if (drawable != null) { + int intrHeight = drawable.getIntrinsicHeight(); + int intrWidth = drawable.getIntrinsicWidth(); + int originalHeight = MeasureSpec.getSize(heightMeasureSpec); + if (intrHeight < intrWidth) { + desiredWidth = MeasureSpec.makeMeasureSpec( + originalHeight, desiredMeasureMode); + } else { + desiredWidth = MeasureSpec.makeMeasureSpec( + Math.round((float) originalHeight * intrWidth / intrHeight), + desiredMeasureMode); + } } - desiredHeight = MeasureSpec.makeMeasureSpec((int) (intrHeight * scale + 0.5f), MeasureSpec.EXACTLY); } + super.onMeasure(desiredWidth, heightMeasureSpec); } + }; + artParams = new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.MATCH_PARENT); + MarginLayoutParamsCompat.setMarginStart(artParams, + getContext().getResources().getDimensionPixelSize(R.dimen.media_router_controller_playback_control_horizontal_spacing)); + } else { + artView = new ImageView(getContext()) { + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int desiredHeight = heightMeasureSpec; + if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) { + Drawable drawable = getDrawable(); + if (drawable != null) { + int originalWidth = MeasureSpec.getSize(widthMeasureSpec); + int intrHeight = drawable.getIntrinsicHeight(); + int intrWidth = drawable.getIntrinsicWidth(); + float scale; + if (intrHeight*16 > intrWidth*9) { + // image is taller than 16:9 + scale = (float) originalWidth * 9 / 16 / intrHeight; + } else { + // image is more horizontal than 16:9 + scale = (float) originalWidth / intrWidth; + } + desiredHeight = MeasureSpec.makeMeasureSpec( + Math.round(intrHeight * scale), + MeasureSpec.EXACTLY); + } + } + super.onMeasure(widthMeasureSpec, desiredHeight); + } + }; + artParams = new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } + artView.setTag(landscape); - super.onMeasure(widthMeasureSpec, desiredHeight); - } - }; - artView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT)); artView.setScaleType(ImageView.ScaleType.FIT_CENTER); artView.setOnClickListener(onClickListener); + artView.setLayoutParams(artParams); rootView.addView(artView); - View playbackControlLayout = View.inflate(getContext(), R.layout.media_router_controller, rootView); + + ViewGroup wrapper = rootView; + + if (landscape) { + wrapper = new FrameLayout(getContext()); + wrapper.setLayoutParams(new LinearLayout.LayoutParams( + 0, + ViewGroup.LayoutParams.WRAP_CONTENT, 1f)); + rootView.addView(wrapper); + rootView.setWeightSum(1f); + } + + View playbackControlLayout = View.inflate(getContext(), R.layout.media_router_controller, wrapper); titleView = (TextView) playbackControlLayout.findViewById(R.id.mrc_control_title); subtitleView = (TextView) playbackControlLayout.findViewById(R.id.mrc_control_subtitle); @@ -179,7 +271,8 @@ public class CustomMRControllerDialog extends MediaRouteControllerDialog { event.setPackageName(getContext().getPackageName()); event.setClassName(getClass().getName()); int resId = isPlaying ? - android.support.v7.mediarouter.R.string.mr_controller_pause : android.support.v7.mediarouter.R.string.mr_controller_play; + android.support.v7.mediarouter.R.string.mr_controller_pause : + android.support.v7.mediarouter.R.string.mr_controller_play; event.getText().add(getContext().getString(resId)); accessibilityManager.sendAccessibilityEvent(event); } @@ -270,14 +363,20 @@ public class CustomMRControllerDialog extends MediaRouteControllerDialog { .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { fetchArtSubscription = null; + if (artView == null) { + return; + } if (result.first != null) { - artView.setBackgroundColor(result.second); + if (!((Boolean) artView.getTag())) { + artView.setBackgroundColor(result.second); + } artView.setImageBitmap(result.first); artView.setVisibility(View.VISIBLE); } else { artView.setVisibility(View.GONE); } }, error -> Log.e(TAG, Log.getStackTraceString(error))); + } private void updateState() { diff --git a/app/src/play/java/de/danoeh/antennapod/fragment/CustomMRControllerDialogFragment.java b/app/src/play/java/de/danoeh/antennapod/fragment/CustomMRControllerDialogFragment.java new file mode 100644 index 000000000..a960ec998 --- /dev/null +++ b/app/src/play/java/de/danoeh/antennapod/fragment/CustomMRControllerDialogFragment.java @@ -0,0 +1,15 @@ +package de.danoeh.antennapod.fragment; + +import android.content.Context; +import android.os.Bundle; +import android.support.v7.app.MediaRouteControllerDialog; +import android.support.v7.app.MediaRouteControllerDialogFragment; + +import de.danoeh.antennapod.dialog.CustomMRControllerDialog; + +public class CustomMRControllerDialogFragment extends MediaRouteControllerDialogFragment { + @Override + public MediaRouteControllerDialog onCreateControllerDialog(Context context, Bundle savedInstanceState) { + return new CustomMRControllerDialog(context); + } +} -- cgit v1.2.3