| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.internal.widget; |
| 17 |
|
| 18 |
import android.compat.annotation.UnsupportedAppUsage; |
| 19 |
import android.content.Context; |
| 20 |
import android.content.res.TypedArray; |
| 21 |
import android.graphics.drawable.Drawable; |
| 22 |
import android.os.Build; |
| 23 |
import android.text.TextUtils; |
| 24 |
import android.util.AttributeSet; |
| 25 |
import android.view.ActionMode; |
| 26 |
import android.view.LayoutInflater; |
| 27 |
import android.view.View; |
| 28 |
import android.view.ViewGroup; |
| 29 |
import android.view.accessibility.AccessibilityEvent; |
| 30 |
import android.widget.ActionMenuPresenter; |
| 31 |
import android.widget.ActionMenuView; |
| 32 |
import android.widget.LinearLayout; |
| 33 |
import android.widget.TextView; |
| 34 |
|
| 35 |
import com.android.internal.R; |
| 36 |
import com.android.internal.view.menu.MenuBuilder; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class ActionBarContextView extends AbsActionBarView { |
| 42 |
private static final String TAG = "ActionBarContextView"; |
| 43 |
|
| 44 |
private CharSequence mTitle; |
| 45 |
private CharSequence mSubtitle; |
| 46 |
|
| 47 |
private View mClose; |
| 48 |
private View mCustomView; |
| 49 |
private LinearLayout mTitleLayout; |
| 50 |
private TextView mTitleView; |
| 51 |
private TextView mSubtitleView; |
| 52 |
private int mTitleStyleRes; |
| 53 |
private int mSubtitleStyleRes; |
| 54 |
private Drawable mSplitBackground; |
| 55 |
private boolean mTitleOptional; |
| 56 |
private int mCloseItemLayout; |
| 57 |
|
| 58 |
public ActionBarContextView(Context context) { |
| 59 |
this(context, null); |
| 60 |
} |
| 61 |
|
| 62 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 63 |
public ActionBarContextView(Context context, AttributeSet attrs) { |
| 64 |
this(context, attrs, com.android.internal.R.attr.actionModeStyle); |
| 65 |
} |
| 66 |
|
| 67 |
public ActionBarContextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 68 |
this(context, attrs, defStyleAttr, 0); |
| 69 |
} |
| 70 |
|
| 71 |
public ActionBarContextView( |
| 72 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 73 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 74 |
|
| 75 |
final TypedArray a = context.obtainStyledAttributes( |
| 76 |
attrs, R.styleable.ActionMode, defStyleAttr, defStyleRes); |
| 77 |
setBackground(a.getDrawable( |
| 78 |
com.android.internal.R.styleable.ActionMode_background)); |
| 79 |
mTitleStyleRes = a.getResourceId( |
| 80 |
com.android.internal.R.styleable.ActionMode_titleTextStyle, 0); |
| 81 |
mSubtitleStyleRes = a.getResourceId( |
| 82 |
com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0); |
| 83 |
|
| 84 |
mContentHeight = a.getLayoutDimension( |
| 85 |
com.android.internal.R.styleable.ActionMode_height, 0); |
| 86 |
|
| 87 |
mSplitBackground = a.getDrawable( |
| 88 |
com.android.internal.R.styleable.ActionMode_backgroundSplit); |
| 89 |
|
| 90 |
mCloseItemLayout = a.getResourceId( |
| 91 |
com.android.internal.R.styleable.ActionMode_closeItemLayout, |
| 92 |
R.layout.action_mode_close_item); |
| 93 |
|
| 94 |
a.recycle(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onDetachedFromWindow() { |
| 99 |
super.onDetachedFromWindow(); |
| 100 |
if (mActionMenuPresenter != null) { |
| 101 |
mActionMenuPresenter.hideOverflowMenu(); |
| 102 |
mActionMenuPresenter.hideSubMenus(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void setSplitToolbar(boolean split) { |
| 108 |
if (mSplitActionBar != split) { |
| 109 |
if (mActionMenuPresenter != null) { |
| 110 |
|
| 111 |
final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 112 |
LayoutParams.MATCH_PARENT); |
| 113 |
if (!split) { |
| 114 |
mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 115 |
mMenuView.setBackground(null); |
| 116 |
final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 117 |
if (oldParent != null) oldParent.removeView(mMenuView); |
| 118 |
addView(mMenuView, layoutParams); |
| 119 |
} else { |
| 120 |
|
| 121 |
mActionMenuPresenter.setWidthLimit( |
| 122 |
getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 123 |
|
| 124 |
mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 125 |
|
| 126 |
layoutParams.width = LayoutParams.MATCH_PARENT; |
| 127 |
layoutParams.height = mContentHeight; |
| 128 |
mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 129 |
mMenuView.setBackground(mSplitBackground); |
| 130 |
final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 131 |
if (oldParent != null) oldParent.removeView(mMenuView); |
| 132 |
mSplitView.addView(mMenuView, layoutParams); |
| 133 |
} |
| 134 |
} |
| 135 |
super.setSplitToolbar(split); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public void setContentHeight(int height) { |
| 140 |
mContentHeight = height; |
| 141 |
} |
| 142 |
|
| 143 |
public void setCustomView(View view) { |
| 144 |
if (mCustomView != null) { |
| 145 |
removeView(mCustomView); |
| 146 |
} |
| 147 |
mCustomView = view; |
| 148 |
if (view != null && mTitleLayout != null) { |
| 149 |
removeView(mTitleLayout); |
| 150 |
mTitleLayout = null; |
| 151 |
} |
| 152 |
if (view != null) { |
| 153 |
addView(view); |
| 154 |
} |
| 155 |
requestLayout(); |
| 156 |
} |
| 157 |
|
| 158 |
public void setTitle(CharSequence title) { |
| 159 |
mTitle = title; |
| 160 |
initTitle(); |
| 161 |
} |
| 162 |
|
| 163 |
public void setSubtitle(CharSequence subtitle) { |
| 164 |
mSubtitle = subtitle; |
| 165 |
initTitle(); |
| 166 |
} |
| 167 |
|
| 168 |
public CharSequence getTitle() { |
| 169 |
return mTitle; |
| 170 |
} |
| 171 |
|
| 172 |
public CharSequence getSubtitle() { |
| 173 |
return mSubtitle; |
| 174 |
} |
| 175 |
|
| 176 |
private void initTitle() { |
| 177 |
if (mTitleLayout == null) { |
| 178 |
LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 179 |
inflater.inflate(R.layout.action_bar_title_item, this); |
| 180 |
mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1); |
| 181 |
mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 182 |
mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
| 183 |
if (mTitleStyleRes != 0) { |
| 184 |
mTitleView.setTextAppearance(mTitleStyleRes); |
| 185 |
} |
| 186 |
if (mSubtitleStyleRes != 0) { |
| 187 |
mSubtitleView.setTextAppearance(mSubtitleStyleRes); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
mTitleView.setText(mTitle); |
| 192 |
mSubtitleView.setText(mSubtitle); |
| 193 |
|
| 194 |
final boolean hasTitle = !TextUtils.isEmpty(mTitle); |
| 195 |
final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle); |
| 196 |
mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE); |
| 197 |
mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE); |
| 198 |
if (mTitleLayout.getParent() == null) { |
| 199 |
addView(mTitleLayout); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public void initForMode(final ActionMode mode) { |
| 204 |
if (mClose == null) { |
| 205 |
LayoutInflater inflater = LayoutInflater.from(mContext); |
| 206 |
mClose = inflater.inflate(mCloseItemLayout, this, false); |
| 207 |
addView(mClose); |
| 208 |
} else if (mClose.getParent() == null) { |
| 209 |
addView(mClose); |
| 210 |
} |
| 211 |
|
| 212 |
View closeButton = mClose.findViewById(R.id.action_mode_close_button); |
| 213 |
closeButton.setOnClickListener(new OnClickListener() { |
| 214 |
public void onClick(View v) { |
| 215 |
mode.finish(); |
| 216 |
} |
| 217 |
}); |
| 218 |
|
| 219 |
final MenuBuilder menu = (MenuBuilder) mode.getMenu(); |
| 220 |
if (mActionMenuPresenter != null) { |
| 221 |
mActionMenuPresenter.dismissPopupMenus(); |
| 222 |
} |
| 223 |
mActionMenuPresenter = new ActionMenuPresenter(mContext); |
| 224 |
mActionMenuPresenter.setReserveOverflow(true); |
| 225 |
|
| 226 |
final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 227 |
LayoutParams.MATCH_PARENT); |
| 228 |
if (!mSplitActionBar) { |
| 229 |
menu.addMenuPresenter(mActionMenuPresenter, mPopupContext); |
| 230 |
mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 231 |
mMenuView.setBackground(null); |
| 232 |
addView(mMenuView, layoutParams); |
| 233 |
} else { |
| 234 |
|
| 235 |
mActionMenuPresenter.setWidthLimit( |
| 236 |
getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 237 |
|
| 238 |
mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 239 |
|
| 240 |
layoutParams.width = LayoutParams.MATCH_PARENT; |
| 241 |
layoutParams.height = mContentHeight; |
| 242 |
menu.addMenuPresenter(mActionMenuPresenter, mPopupContext); |
| 243 |
mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 244 |
mMenuView.setBackgroundDrawable(mSplitBackground); |
| 245 |
mSplitView.addView(mMenuView, layoutParams); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public void closeMode() { |
| 250 |
if (mClose == null) { |
| 251 |
killMode(); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
public void killMode() { |
| 258 |
removeAllViews(); |
| 259 |
if (mSplitView != null) { |
| 260 |
mSplitView.removeView(mMenuView); |
| 261 |
} |
| 262 |
mCustomView = null; |
| 263 |
mMenuView = null; |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public boolean showOverflowMenu() { |
| 268 |
if (mActionMenuPresenter != null) { |
| 269 |
return mActionMenuPresenter.showOverflowMenu(); |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
@Override |
| 275 |
public boolean hideOverflowMenu() { |
| 276 |
if (mActionMenuPresenter != null) { |
| 277 |
return mActionMenuPresenter.hideOverflowMenu(); |
| 278 |
} |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public boolean isOverflowMenuShowing() { |
| 284 |
if (mActionMenuPresenter != null) { |
| 285 |
return mActionMenuPresenter.isOverflowMenuShowing(); |
| 286 |
} |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
| 292 |
|
| 293 |
|
| 294 |
return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 299 |
return new MarginLayoutParams(getContext(), attrs); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 304 |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 305 |
if (widthMode != MeasureSpec.EXACTLY) { |
| 306 |
throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 307 |
"with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 308 |
} |
| 309 |
|
| 310 |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 311 |
if (heightMode == MeasureSpec.UNSPECIFIED) { |
| 312 |
throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 313 |
"with android:layout_height=\"wrap_content\""); |
| 314 |
} |
| 315 |
|
| 316 |
final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 317 |
|
| 318 |
int maxHeight = mContentHeight > 0 ? |
| 319 |
mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
| 320 |
|
| 321 |
final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 322 |
int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); |
| 323 |
final int height = maxHeight - verticalPadding; |
| 324 |
final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
| 325 |
|
| 326 |
if (mClose != null) { |
| 327 |
availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); |
| 328 |
MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
| 329 |
availableWidth -= lp.leftMargin + lp.rightMargin; |
| 330 |
} |
| 331 |
|
| 332 |
if (mMenuView != null && mMenuView.getParent() == this) { |
| 333 |
availableWidth = measureChildView(mMenuView, availableWidth, |
| 334 |
childSpecHeight, 0); |
| 335 |
} |
| 336 |
|
| 337 |
if (mTitleLayout != null && mCustomView == null) { |
| 338 |
if (mTitleOptional) { |
| 339 |
final int titleWidthSpec = MeasureSpec.makeSafeMeasureSpec(contentWidth, |
| 340 |
MeasureSpec.UNSPECIFIED); |
| 341 |
mTitleLayout.measure(titleWidthSpec, childSpecHeight); |
| 342 |
final int titleWidth = mTitleLayout.getMeasuredWidth(); |
| 343 |
final boolean titleFits = titleWidth <= availableWidth; |
| 344 |
if (titleFits) { |
| 345 |
availableWidth -= titleWidth; |
| 346 |
} |
| 347 |
mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE); |
| 348 |
} else { |
| 349 |
availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (mCustomView != null) { |
| 354 |
ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); |
| 355 |
final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 356 |
MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 357 |
final int customWidth = lp.width >= 0 ? |
| 358 |
Math.min(lp.width, availableWidth) : availableWidth; |
| 359 |
final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 360 |
MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 361 |
final int customHeight = lp.height >= 0 ? |
| 362 |
Math.min(lp.height, height) : height; |
| 363 |
mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), |
| 364 |
MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); |
| 365 |
} |
| 366 |
|
| 367 |
if (mContentHeight <= 0) { |
| 368 |
int measuredHeight = 0; |
| 369 |
final int count = getChildCount(); |
| 370 |
for (int i = 0; i < count; i++) { |
| 371 |
View v = getChildAt(i); |
| 372 |
int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 373 |
if (paddedViewHeight > measuredHeight) { |
| 374 |
measuredHeight = paddedViewHeight; |
| 375 |
} |
| 376 |
} |
| 377 |
setMeasuredDimension(contentWidth, measuredHeight); |
| 378 |
} else { |
| 379 |
setMeasuredDimension(contentWidth, maxHeight); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 385 |
final boolean isLayoutRtl = isLayoutRtl(); |
| 386 |
int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft(); |
| 387 |
final int y = getPaddingTop(); |
| 388 |
final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
| 389 |
|
| 390 |
if (mClose != null && mClose.getVisibility() != GONE) { |
| 391 |
MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
| 392 |
final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin); |
| 393 |
final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin); |
| 394 |
x = next(x, startMargin, isLayoutRtl); |
| 395 |
x += positionChild(mClose, x, y, contentHeight, isLayoutRtl); |
| 396 |
x = next(x, endMargin, isLayoutRtl); |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) { |
| 401 |
x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl); |
| 402 |
} |
| 403 |
|
| 404 |
if (mCustomView != null) { |
| 405 |
x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl); |
| 406 |
} |
| 407 |
|
| 408 |
x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight(); |
| 409 |
|
| 410 |
if (mMenuView != null) { |
| 411 |
x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public boolean shouldDelayChildPressedState() { |
| 417 |
return false; |
| 418 |
} |
| 419 |
|
| 420 |
@Override |
| 421 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 422 |
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 423 |
|
| 424 |
event.setSource(this); |
| 425 |
event.setClassName(getClass().getName()); |
| 426 |
event.setPackageName(getContext().getPackageName()); |
| 427 |
event.setContentDescription(mTitle); |
| 428 |
} else { |
| 429 |
super.onInitializeAccessibilityEventInternal(event); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
public void setTitleOptional(boolean titleOptional) { |
| 434 |
if (titleOptional != mTitleOptional) { |
| 435 |
requestLayout(); |
| 436 |
} |
| 437 |
mTitleOptional = titleOptional; |
| 438 |
} |
| 439 |
|
| 440 |
public boolean isTitleOptional() { |
| 441 |
return mTitleOptional; |
| 442 |
} |
| 443 |
} |