Commit ff649b03 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download location: limit download location dialog width.

Currently ModalDialogView uses dialog_max_width(600dp) as maximum
width. The download location dialog's custom view only wraps a much
smaller area, so it looks bad in landscape view.

This CL makes width limitation to use type value, which can be dp or
fraction of the screen.


Bug: 847575
Change-Id: I946423bdda2eb052cb02f72d5ed36447e58bcc6d
Reviewed-on: https://chromium-review.googlesource.com/1082930Reviewed-by: default avatarBecky Zhou <huayinz@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564936}
parent 4d87a4a7
......@@ -10,7 +10,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
chrome:maxWidth="@dimen/dialog_max_width">
chrome:maxWidthLandscape="@dimen/abc_dialog_min_width_major"
chrome:maxWidthPortrait="@dimen/abc_dialog_min_width_minor" >
<android.support.v7.widget.DialogTitle
android:id="@+id/title"
......@@ -71,4 +72,4 @@
</android.support.v7.widget.ButtonBarLayout>
</org.chromium.chrome.browser.widget.BoundedLinearLayout>
\ No newline at end of file
</org.chromium.chrome.browser.widget.BoundedLinearLayout>
......@@ -49,6 +49,8 @@
<declare-styleable name="BoundedView">
<attr name="maxWidth" format="dimension" />
<attr name="maxWidthLandscape" format="dimension" />
<attr name="maxWidthPortrait" format="dimension" />
<attr name="maxHeight" format="dimension" />
</declare-styleable>
......
......@@ -7,12 +7,14 @@ package org.chromium.chrome.browser.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.LinearLayout;
import org.chromium.chrome.R;
/**
* A LinearLayout that can be constrained to a maximum size.
* A LinearLayout that can be constrained to a maximum size or percentage of the screen size.
*
* Example:
* <org.chromium.chrome.browser.widget.BoundedLinearLayout
......@@ -20,14 +22,19 @@ import org.chromium.chrome.R;
* xmlns:chrome="http://schemas.android.com/apk/res-auto"
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* chrome:maxWidth="692dp" >
* chrome:maxWidthLandscape="@dimen/modal_dialog_landscape_max_width"
chrome:maxWidthPortrait="@dimen/modal_dialog_portrait_max_width">
* ...
*/
public class BoundedLinearLayout extends LinearLayout {
private static final int NOT_SPECIFIED = -1;
// TODO(xingliu): Get rid of mMaxWidth.
private final int mMaxWidth;
private TypedValue mMaxWidthLandscape = new TypedValue();
private TypedValue mMaxWidthPortrait = new TypedValue();
private final int mMaxHeight;
/**
......@@ -39,28 +46,59 @@ public class BoundedLinearLayout extends LinearLayout {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoundedView);
int maxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_maxWidth, NOT_SPECIFIED);
int maxHeight = a.getDimensionPixelSize(R.styleable.BoundedView_maxHeight, NOT_SPECIFIED);
assert(maxWidth == NOT_SPECIFIED
|| (mMaxWidthLandscape.type == TypedValue.TYPE_NULL
&& mMaxWidthPortrait.type == TypedValue.TYPE_NULL));
a.getValue(R.styleable.BoundedView_maxWidthLandscape, mMaxWidthLandscape);
a.getValue(R.styleable.BoundedView_maxWidthPortrait, mMaxWidthPortrait);
a.recycle();
// Treat 0 or below as being unconstrained.
mMaxWidth = maxWidth <= 0 ? NOT_SPECIFIED : maxWidth;
mMaxHeight = maxHeight <= 0 ? NOT_SPECIFIED : maxHeight;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Limit width to mMaxWidth.
final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
// Limit the width with fixed value.
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (mMaxWidth != NOT_SPECIFIED && widthSize > mMaxWidth) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED) widthMode = MeasureSpec.AT_MOST;
widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode);
widthMeasureSpec = makeMeasureSpec(widthMeasureSpec, mMaxWidth);
}
// Limit the width with fraction of the screen if fixed width value is not specified.
if (mMaxWidth == NOT_SPECIFIED) {
final TypedValue tv = isPortrait ? mMaxWidthPortrait : mMaxWidthLandscape;
if (tv.type != TypedValue.TYPE_NULL) {
int maxWidthPixel = NOT_SPECIFIED;
if (tv.type == TypedValue.TYPE_DIMENSION) {
maxWidthPixel = (int) tv.getDimension(metrics);
} else if (tv.type == TypedValue.TYPE_FRACTION) {
maxWidthPixel = (int) tv.getFraction(metrics.widthPixels, metrics.widthPixels);
}
if (widthSize > maxWidthPixel && maxWidthPixel != NOT_SPECIFIED) {
widthMeasureSpec = makeMeasureSpec(widthMeasureSpec, maxWidthPixel);
}
}
}
// Limit the height.
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (mMaxHeight != NOT_SPECIFIED && heightSize > mMaxHeight) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode == MeasureSpec.UNSPECIFIED) heightMode = MeasureSpec.AT_MOST;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, heightMode);
heightMeasureSpec = makeMeasureSpec(heightMeasureSpec, mMaxHeight);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private int makeMeasureSpec(int measureSpec, int maxPixel) {
int mode = MeasureSpec.getMode(measureSpec);
if (mode == MeasureSpec.UNSPECIFIED) mode = MeasureSpec.AT_MOST;
return MeasureSpec.makeMeasureSpec(maxPixel, mode);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment