Skip to content

Commit b98a81f

Browse files
committed
Add support for optional titles in action modes
Optional titles will only be displayed in the CAB if they entirely fit instead of ellipsizing. Fixes bug 5821883 Change-Id: I0cfd6d4fd34a4fa9f520499d577706da30606811
1 parent 8657477 commit b98a81f

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

api/current.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21935,12 +21935,14 @@ package android.view {
2193521935
method public java.lang.Object getTag();
2193621936
method public abstract java.lang.CharSequence getTitle();
2193721937
method public abstract void invalidate();
21938+
method public boolean isTitleOptional();
2193821939
method public abstract void setCustomView(android.view.View);
2193921940
method public abstract void setSubtitle(java.lang.CharSequence);
2194021941
method public abstract void setSubtitle(int);
2194121942
method public void setTag(java.lang.Object);
2194221943
method public abstract void setTitle(java.lang.CharSequence);
2194321944
method public abstract void setTitle(int);
21945+
method public void setTitleOptionalHint(boolean);
2194421946
}
2194521947

2194621948
public static abstract interface ActionMode.Callback {

core/java/android/inputmethodservice/ExtractEditLayout.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ public void setSubtitle(int resId) {
123123
// Subtitle will not be shown.
124124
}
125125

126+
@Override
127+
public boolean isTitleOptional() {
128+
// Not only is it optional, it will *never* be shown.
129+
return true;
130+
}
131+
126132
@Override
127133
public void setCustomView(View view) {
128134
// Custom view is not supported here.

core/java/android/view/ActionMode.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,32 @@ public Object getTag() {
103103
*/
104104
public abstract void setSubtitle(int resId);
105105

106+
/**
107+
* Set whether or not the title/subtitle display for this action mode
108+
* is optional.
109+
*
110+
* <p>In many cases the supplied title for an action mode is merely
111+
* meant to add context and is not strictly required for the action
112+
* mode to be useful. If the title is optional, the system may choose
113+
* to hide the title entirely rather than truncate it due to a lack
114+
* of available space.</p>
115+
*
116+
* <p>Note that this is merely a hint; the underlying implementation
117+
* may choose to ignore this setting under some circumstances.</p>
118+
*
119+
* @param titleOptional true if the title only presents optional information.
120+
*/
121+
public void setTitleOptionalHint(boolean titleOptional) {
122+
}
123+
124+
/**
125+
* @return true if this action mode considers the title and subtitle fields
126+
* as optional. Optional titles may not be displayed to the user.
127+
*/
128+
public boolean isTitleOptional() {
129+
return false;
130+
}
131+
106132
/**
107133
* Set a custom view for this action mode. The custom view will take the place of
108134
* the title and subtitle. Useful for things like search boxes.

core/java/android/webkit/SelectActionModeCallback.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
5353
mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_copy, menu);
5454

5555
final Context context = mWebView.getContext();
56-
boolean allowText = context.getResources().getBoolean(
57-
com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon);
58-
mode.setTitle(allowText ?
59-
context.getString(com.android.internal.R.string.textSelectionCABTitle) : null);
56+
mode.setTitle(context.getString(com.android.internal.R.string.textSelectionCABTitle));
57+
mode.setTitleOptionalHint(true);
6058

6159
// If the action mode UI we're running in isn't capable of taking window focus
6260
// the user won't be able to type into the find on page UI. Disable this functionality.

core/java/android/widget/TextView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10363,9 +10363,9 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1036310363
boolean allowText = getContext().getResources().getBoolean(
1036410364
com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon);
1036510365

10366-
mode.setTitle(allowText ?
10367-
mContext.getString(com.android.internal.R.string.textSelectionCABTitle) : null);
10366+
mode.setTitle(mContext.getString(com.android.internal.R.string.textSelectionCABTitle));
1036810367
mode.setSubtitle(null);
10368+
mode.setTitleOptionalHint(true);
1036910369

1037010370
int selectAllIconId = 0; // No icon by default
1037110371
if (!allowText) {

core/java/com/android/internal/app/ActionBarImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,16 @@ public CharSequence getSubtitle() {
747747
return mContextView.getSubtitle();
748748
}
749749

750+
@Override
751+
public void setTitleOptionalHint(boolean titleOptional) {
752+
mContextView.setTitleOptional(titleOptional);
753+
}
754+
755+
@Override
756+
public boolean isTitleOptional() {
757+
return mContextView.isTitleOptional();
758+
}
759+
750760
@Override
751761
public View getCustomView() {
752762
return mCustomView != null ? mCustomView.get() : null;

core/java/com/android/internal/view/StandaloneActionMode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public void setSubtitle(int resId) {
7171
setSubtitle(mContext.getString(resId));
7272
}
7373

74+
@Override
75+
public void setTitleOptionalHint(boolean titleOptional) {
76+
mContextView.setTitleOptional(titleOptional);
77+
}
78+
79+
@Override
80+
public boolean isTitleOptional() {
81+
return mContextView.isTitleOptional();
82+
}
83+
7484
@Override
7585
public void setCustomView(View view) {
7686
mContextView.setCustomView(view);

core/java/com/android/internal/widget/ActionBarContextView.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class ActionBarContextView extends AbsActionBarView implements AnimatorLi
5656
private int mTitleStyleRes;
5757
private int mSubtitleStyleRes;
5858
private Drawable mSplitBackground;
59+
private boolean mTitleOptional;
5960

6061
private Animator mCurrentAnimation;
6162
private boolean mAnimateInOnLayout;
@@ -354,7 +355,18 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
354355
}
355356

356357
if (mTitleLayout != null && mCustomView == null) {
357-
availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
358+
if (mTitleOptional) {
359+
final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
360+
mTitleLayout.measure(titleWidthSpec, childSpecHeight);
361+
final int titleWidth = mTitleLayout.getMeasuredWidth();
362+
final boolean titleFits = titleWidth <= availableWidth;
363+
if (titleFits) {
364+
availableWidth -= titleWidth;
365+
}
366+
mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE);
367+
} else {
368+
availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
369+
}
358370
}
359371

360372
if (mCustomView != null) {
@@ -460,7 +472,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
460472
}
461473
}
462474

463-
if (mTitleLayout != null && mCustomView == null) {
475+
if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) {
464476
x += positionChild(mTitleLayout, x, y, contentHeight);
465477
}
466478

@@ -512,4 +524,15 @@ public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
512524
super.onInitializeAccessibilityEvent(event);
513525
}
514526
}
527+
528+
public void setTitleOptional(boolean titleOptional) {
529+
if (titleOptional != mTitleOptional) {
530+
requestLayout();
531+
}
532+
mTitleOptional = titleOptional;
533+
}
534+
535+
public boolean isTitleOptional() {
536+
return mTitleOptional;
537+
}
515538
}

0 commit comments

Comments
 (0)