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