Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.internal.widget; |
| 17 | |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 18 | import com.android.internal.R; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 19 | import android.widget.ActionMenuPresenter; |
| 20 | import android.widget.ActionMenuView; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 21 | import com.android.internal.view.menu.MenuBuilder; |
| 22 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 23 | import android.animation.Animator; |
| 24 | import android.animation.Animator.AnimatorListener; |
| 25 | import android.animation.AnimatorSet; |
| 26 | import android.animation.ObjectAnimator; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.res.TypedArray; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 31 | import android.util.AttributeSet; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 32 | import android.view.ActionMode; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 34 | import android.view.View; |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 35 | import android.view.ViewGroup; |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 37 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
| 40 | |
| 41 | /** |
| 42 | * @hide |
| 43 | */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 44 | public class ActionBarContextView extends AbsActionBarView implements AnimatorListener { |
Adam Powell | 77769c7 | 2011-01-17 12:05:08 -0800 | [diff] [blame] | 45 | private static final String TAG = "ActionBarContextView"; |
| 46 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 47 | private CharSequence mTitle; |
| 48 | private CharSequence mSubtitle; |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 49 | |
| 50 | private View mClose; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 51 | private View mCustomView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 52 | private LinearLayout mTitleLayout; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 53 | private TextView mTitleView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 54 | private TextView mSubtitleView; |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 55 | private int mTitleStyleRes; |
| 56 | private int mSubtitleStyleRes; |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 57 | private Drawable mSplitBackground; |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 58 | private boolean mTitleOptional; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 59 | |
| 60 | private Animator mCurrentAnimation; |
| 61 | private boolean mAnimateInOnLayout; |
| 62 | private int mAnimationMode; |
| 63 | |
| 64 | private static final int ANIMATE_IDLE = 0; |
| 65 | private static final int ANIMATE_IN = 1; |
| 66 | private static final int ANIMATE_OUT = 2; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 67 | |
| 68 | public ActionBarContextView(Context context) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 69 | this(context, null); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public ActionBarContextView(Context context, AttributeSet attrs) { |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 73 | this(context, attrs, com.android.internal.R.attr.actionModeStyle); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 76 | public ActionBarContextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 77 | this(context, attrs, defStyleAttr, 0); |
| 78 | } |
| 79 | |
| 80 | public ActionBarContextView( |
| 81 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 82 | super(context, attrs, defStyleAttr, defStyleRes); |
| 83 | |
| 84 | final TypedArray a = context.obtainStyledAttributes( |
| 85 | attrs, R.styleable.ActionMode, defStyleAttr, defStyleRes); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 86 | setBackgroundDrawable(a.getDrawable( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 87 | com.android.internal.R.styleable.ActionMode_background)); |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 88 | mTitleStyleRes = a.getResourceId( |
| 89 | com.android.internal.R.styleable.ActionMode_titleTextStyle, 0); |
| 90 | mSubtitleStyleRes = a.getResourceId( |
| 91 | com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0); |
Adam Powell | dcdefbb | 2010-07-19 15:45:22 -0700 | [diff] [blame] | 92 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 93 | mContentHeight = a.getLayoutDimension( |
Adam Powell | bc234a1 | 2010-09-09 17:03:50 -0700 | [diff] [blame] | 94 | com.android.internal.R.styleable.ActionMode_height, 0); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 95 | |
| 96 | mSplitBackground = a.getDrawable( |
| 97 | com.android.internal.R.styleable.ActionMode_backgroundSplit); |
| 98 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 99 | a.recycle(); |
| 100 | } |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 101 | |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 102 | @Override |
Adam Powell | 97e1836 | 2011-10-20 15:37:18 -0700 | [diff] [blame] | 103 | public void onDetachedFromWindow() { |
| 104 | super.onDetachedFromWindow(); |
| 105 | if (mActionMenuPresenter != null) { |
| 106 | mActionMenuPresenter.hideOverflowMenu(); |
| 107 | mActionMenuPresenter.hideSubMenus(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 112 | public void setSplitActionBar(boolean split) { |
| 113 | if (mSplitActionBar != split) { |
| 114 | if (mActionMenuPresenter != null) { |
| 115 | // Mode is already active; move everything over and adjust the menu itself. |
| 116 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 117 | LayoutParams.MATCH_PARENT); |
| 118 | if (!split) { |
| 119 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 120 | mMenuView.setBackgroundDrawable(null); |
| 121 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 122 | if (oldParent != null) oldParent.removeView(mMenuView); |
| 123 | addView(mMenuView, layoutParams); |
| 124 | } else { |
| 125 | // Allow full screen width in split mode. |
| 126 | mActionMenuPresenter.setWidthLimit( |
| 127 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 128 | // No limit to the item count; use whatever will fit. |
| 129 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 130 | // Span the whole width |
| 131 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 132 | layoutParams.height = mContentHeight; |
| 133 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
| 134 | mMenuView.setBackgroundDrawable(mSplitBackground); |
| 135 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 136 | if (oldParent != null) oldParent.removeView(mMenuView); |
| 137 | mSplitView.addView(mMenuView, layoutParams); |
| 138 | } |
| 139 | } |
| 140 | super.setSplitActionBar(split); |
| 141 | } |
| 142 | } |
| 143 | |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 144 | public void setContentHeight(int height) { |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 145 | mContentHeight = height; |
| 146 | } |
| 147 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 148 | public void setCustomView(View view) { |
| 149 | if (mCustomView != null) { |
| 150 | removeView(mCustomView); |
| 151 | } |
| 152 | mCustomView = view; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 153 | if (mTitleLayout != null) { |
| 154 | removeView(mTitleLayout); |
| 155 | mTitleLayout = null; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 156 | } |
| 157 | if (view != null) { |
| 158 | addView(view); |
| 159 | } |
| 160 | requestLayout(); |
| 161 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 162 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 163 | public void setTitle(CharSequence title) { |
| 164 | mTitle = title; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 165 | initTitle(); |
| 166 | } |
| 167 | |
| 168 | public void setSubtitle(CharSequence subtitle) { |
| 169 | mSubtitle = subtitle; |
| 170 | initTitle(); |
| 171 | } |
| 172 | |
Adam Powell | 29ed757 | 2010-07-14 16:24:56 -0700 | [diff] [blame] | 173 | public CharSequence getTitle() { |
| 174 | return mTitle; |
| 175 | } |
| 176 | |
| 177 | public CharSequence getSubtitle() { |
| 178 | return mSubtitle; |
| 179 | } |
| 180 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 181 | private void initTitle() { |
| 182 | if (mTitleLayout == null) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 183 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 184 | inflater.inflate(R.layout.action_bar_title_item, this); |
| 185 | mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 186 | mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 187 | mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 188 | if (mTitleStyleRes != 0) { |
| 189 | mTitleView.setTextAppearance(mContext, mTitleStyleRes); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 190 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 191 | if (mSubtitleStyleRes != 0) { |
| 192 | mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 193 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | mTitleView.setText(mTitle); |
| 197 | mSubtitleView.setText(mSubtitle); |
| 198 | |
| 199 | final boolean hasTitle = !TextUtils.isEmpty(mTitle); |
| 200 | final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle); |
| 201 | mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE); |
| 202 | mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE); |
| 203 | if (mTitleLayout.getParent() == null) { |
| 204 | addView(mTitleLayout); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 207 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 208 | public void initForMode(final ActionMode mode) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 209 | if (mClose == null) { |
| 210 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 211 | mClose = inflater.inflate(R.layout.action_mode_close_item, this, false); |
| 212 | addView(mClose); |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 213 | } else if (mClose.getParent() == null) { |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 214 | addView(mClose); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 215 | } |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 216 | |
| 217 | View closeButton = mClose.findViewById(R.id.action_mode_close_button); |
| 218 | closeButton.setOnClickListener(new OnClickListener() { |
Adam Powell | 269b8bb | 2010-07-20 16:46:42 -0700 | [diff] [blame] | 219 | public void onClick(View v) { |
| 220 | mode.finish(); |
| 221 | } |
| 222 | }); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 223 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 224 | final MenuBuilder menu = (MenuBuilder) mode.getMenu(); |
Adam Powell | 678ed0c | 2011-10-27 17:46:07 -0700 | [diff] [blame] | 225 | if (mActionMenuPresenter != null) { |
| 226 | mActionMenuPresenter.dismissPopupMenus(); |
| 227 | } |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 228 | mActionMenuPresenter = new ActionMenuPresenter(mContext); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 229 | mActionMenuPresenter.setReserveOverflow(true); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 230 | |
| 231 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 232 | LayoutParams.MATCH_PARENT); |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 233 | if (!mSplitActionBar) { |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 234 | menu.addMenuPresenter(mActionMenuPresenter); |
| 235 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 236 | mMenuView.setBackgroundDrawable(null); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 237 | addView(mMenuView, layoutParams); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 238 | } else { |
| 239 | // Allow full screen width in split mode. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 240 | mActionMenuPresenter.setWidthLimit( |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 241 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 242 | // No limit to the item count; use whatever will fit. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 243 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 244 | // Span the whole width |
| 245 | layoutParams.width = LayoutParams.MATCH_PARENT; |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 246 | layoutParams.height = mContentHeight; |
| 247 | menu.addMenuPresenter(mActionMenuPresenter); |
| 248 | mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 249 | mMenuView.setBackgroundDrawable(mSplitBackground); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 250 | mSplitView.addView(mMenuView, layoutParams); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 251 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 252 | |
| 253 | mAnimateInOnLayout = true; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 254 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 255 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 256 | public void closeMode() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 257 | if (mAnimationMode == ANIMATE_OUT) { |
| 258 | // Called again during close; just finish what we were doing. |
| 259 | return; |
| 260 | } |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 261 | if (mClose == null) { |
| 262 | killMode(); |
| 263 | return; |
| 264 | } |
| 265 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 266 | finishAnimation(); |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 267 | mAnimationMode = ANIMATE_OUT; |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 268 | mCurrentAnimation = makeOutAnimation(); |
| 269 | mCurrentAnimation.start(); |
| 270 | } |
| 271 | |
| 272 | private void finishAnimation() { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 273 | final Animator a = mCurrentAnimation; |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 274 | if (a != null) { |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 275 | mCurrentAnimation = null; |
| 276 | a.end(); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | public void killMode() { |
| 281 | finishAnimation(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 282 | removeAllViews(); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 283 | if (mSplitView != null) { |
| 284 | mSplitView.removeView(mMenuView); |
| 285 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 286 | mCustomView = null; |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 287 | mMenuView = null; |
Adam Powell | 00bba68 | 2011-01-08 15:53:38 -0800 | [diff] [blame] | 288 | mAnimateInOnLayout = false; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 289 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 290 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 291 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 292 | public boolean showOverflowMenu() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 293 | if (mActionMenuPresenter != null) { |
| 294 | return mActionMenuPresenter.showOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 299 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 300 | public boolean hideOverflowMenu() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 301 | if (mActionMenuPresenter != null) { |
| 302 | return mActionMenuPresenter.hideOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 307 | @Override |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 308 | public boolean isOverflowMenuShowing() { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 309 | if (mActionMenuPresenter != null) { |
| 310 | return mActionMenuPresenter.isOverflowMenuShowing(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 311 | } |
| 312 | return false; |
| 313 | } |
| 314 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 315 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 316 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 317 | // Used by custom views if they don't supply layout params. Everything else |
| 318 | // added to an ActionBarContextView should have them already. |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 319 | return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
| 320 | } |
| 321 | |
| 322 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 323 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 324 | return new MarginLayoutParams(getContext(), attrs); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 328 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 329 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 330 | if (widthMode != MeasureSpec.EXACTLY) { |
| 331 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 332 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 333 | } |
| 334 | |
| 335 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 336 | if (heightMode == MeasureSpec.UNSPECIFIED) { |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 337 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 338 | "with android:layout_height=\"wrap_content\""); |
| 339 | } |
| 340 | |
| 341 | final int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 342 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 343 | int maxHeight = mContentHeight > 0 ? |
| 344 | mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
| 345 | |
| 346 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 347 | int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 348 | final int height = maxHeight - verticalPadding; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 349 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
| 350 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 351 | if (mClose != null) { |
| 352 | availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0); |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 353 | MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
| 354 | availableWidth -= lp.leftMargin + lp.rightMargin; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 357 | if (mMenuView != null && mMenuView.getParent() == this) { |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 358 | availableWidth = measureChildView(mMenuView, availableWidth, |
| 359 | childSpecHeight, 0); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adam Powell | b0ff6f9 | 2011-01-23 18:07:45 -0800 | [diff] [blame] | 362 | if (mTitleLayout != null && mCustomView == null) { |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 363 | if (mTitleOptional) { |
| 364 | final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 365 | mTitleLayout.measure(titleWidthSpec, childSpecHeight); |
| 366 | final int titleWidth = mTitleLayout.getMeasuredWidth(); |
| 367 | final boolean titleFits = titleWidth <= availableWidth; |
| 368 | if (titleFits) { |
| 369 | availableWidth -= titleWidth; |
| 370 | } |
| 371 | mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE); |
| 372 | } else { |
| 373 | availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); |
| 374 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | if (mCustomView != null) { |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 378 | ViewGroup.LayoutParams lp = mCustomView.getLayoutParams(); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 379 | final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 380 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 381 | final int customWidth = lp.width >= 0 ? |
| 382 | Math.min(lp.width, availableWidth) : availableWidth; |
| 383 | final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 384 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 385 | final int customHeight = lp.height >= 0 ? |
| 386 | Math.min(lp.height, height) : height; |
| 387 | mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode), |
| 388 | MeasureSpec.makeMeasureSpec(customHeight, customHeightMode)); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 391 | if (mContentHeight <= 0) { |
| 392 | int measuredHeight = 0; |
| 393 | final int count = getChildCount(); |
| 394 | for (int i = 0; i < count; i++) { |
| 395 | View v = getChildAt(i); |
| 396 | int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 397 | if (paddedViewHeight > measuredHeight) { |
| 398 | measuredHeight = paddedViewHeight; |
| 399 | } |
| 400 | } |
| 401 | setMeasuredDimension(contentWidth, measuredHeight); |
| 402 | } else { |
| 403 | setMeasuredDimension(contentWidth, maxHeight); |
| 404 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 407 | private Animator makeInAnimation() { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 408 | mClose.setTranslationX(-mClose.getWidth() - |
| 409 | ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 410 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0); |
| 411 | buttonAnimator.setDuration(200); |
| 412 | buttonAnimator.addListener(this); |
| 413 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 414 | |
| 415 | AnimatorSet set = new AnimatorSet(); |
| 416 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 417 | |
| 418 | if (mMenuView != null) { |
| 419 | final int count = mMenuView.getChildCount(); |
| 420 | if (count > 0) { |
| 421 | for (int i = count - 1, j = 0; i >= 0; i--, j++) { |
| 422 | View child = mMenuView.getChildAt(i); |
| 423 | child.setScaleY(0); |
| 424 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1); |
Adam Powell | 482ae5f | 2012-02-17 15:18:01 -0800 | [diff] [blame] | 425 | a.setDuration(300); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 426 | b.with(a); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return set; |
| 432 | } |
| 433 | |
| 434 | private Animator makeOutAnimation() { |
| 435 | ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 436 | -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 437 | buttonAnimator.setDuration(200); |
| 438 | buttonAnimator.addListener(this); |
| 439 | buttonAnimator.setInterpolator(new DecelerateInterpolator()); |
| 440 | |
| 441 | AnimatorSet set = new AnimatorSet(); |
| 442 | AnimatorSet.Builder b = set.play(buttonAnimator); |
| 443 | |
| 444 | if (mMenuView != null) { |
| 445 | final int count = mMenuView.getChildCount(); |
| 446 | if (count > 0) { |
| 447 | for (int i = 0; i < 0; i++) { |
| 448 | View child = mMenuView.getChildAt(i); |
| 449 | child.setScaleY(0); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 450 | ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0); |
Adam Powell | 482ae5f | 2012-02-17 15:18:01 -0800 | [diff] [blame] | 451 | a.setDuration(300); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 452 | b.with(a); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return set; |
| 458 | } |
| 459 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 460 | @Override |
| 461 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 462 | final boolean isLayoutRtl = isLayoutRtl(); |
| 463 | int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 464 | final int y = getPaddingTop(); |
| 465 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 466 | |
Adam Powell | f16888f | 2010-10-11 17:05:29 -0700 | [diff] [blame] | 467 | if (mClose != null && mClose.getVisibility() != GONE) { |
Adam Powell | 9ca705e | 2011-08-11 16:57:59 -0700 | [diff] [blame] | 468 | MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams(); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 469 | final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin); |
| 470 | final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin); |
| 471 | x = next(x, startMargin, isLayoutRtl); |
| 472 | x += positionChild(mClose, x, y, contentHeight, isLayoutRtl); |
| 473 | x = next(x, endMargin, isLayoutRtl); |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 474 | |
| 475 | if (mAnimateInOnLayout) { |
| 476 | mAnimationMode = ANIMATE_IN; |
| 477 | mCurrentAnimation = makeInAnimation(); |
| 478 | mCurrentAnimation.start(); |
| 479 | mAnimateInOnLayout = false; |
| 480 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 481 | } |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 482 | |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 483 | if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 484 | x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (mCustomView != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 488 | x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 489 | } |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 490 | |
| 491 | x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight(); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 492 | |
Adam Powell | 2c9c9fe | 2010-07-16 10:17:57 -0700 | [diff] [blame] | 493 | if (mMenuView != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 494 | x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl); |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 498 | @Override |
| 499 | public void onAnimationStart(Animator animation) { |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void onAnimationEnd(Animator animation) { |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 504 | if (mAnimationMode == ANIMATE_OUT) { |
Adam Powell | d8b3f2e | 2010-12-02 13:37:03 -0800 | [diff] [blame] | 505 | killMode(); |
| 506 | } |
| 507 | mAnimationMode = ANIMATE_IDLE; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void onAnimationCancel(Animator animation) { |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public void onAnimationRepeat(Animator animation) { |
| 516 | } |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 517 | |
| 518 | @Override |
| 519 | public boolean shouldDelayChildPressedState() { |
| 520 | return false; |
| 521 | } |
Adam Powell | 86ed436 | 2011-09-14 16:18:53 -0700 | [diff] [blame] | 522 | |
| 523 | @Override |
| 524 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 525 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 526 | // Action mode started |
| 527 | event.setSource(this); |
| 528 | event.setClassName(getClass().getName()); |
| 529 | event.setPackageName(getContext().getPackageName()); |
| 530 | event.setContentDescription(mTitle); |
| 531 | } else { |
| 532 | super.onInitializeAccessibilityEvent(event); |
| 533 | } |
| 534 | } |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 535 | |
| 536 | public void setTitleOptional(boolean titleOptional) { |
| 537 | if (titleOptional != mTitleOptional) { |
| 538 | requestLayout(); |
| 539 | } |
| 540 | mTitleOptional = titleOptional; |
| 541 | } |
| 542 | |
| 543 | public boolean isTitleOptional() { |
| 544 | return mTitleOptional; |
| 545 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 546 | } |