Adam Powell | 33b9743 | 2010-04-20 10:01:14 -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 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 17 | package com.android.internal.widget; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 18 | |
Adam Powell | fd75eef | 2012-08-02 10:27:31 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 20 | import android.app.ActionBar; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 21 | import android.app.ActionBar.OnNavigationListener; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 30 | import android.text.Layout; |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 31 | import android.text.TextUtils; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Adam Powell | 038f1c8 | 2011-07-21 14:28:10 -0700 | [diff] [blame] | 33 | import android.view.CollapsibleActionView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 34 | import android.view.Gravity; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
| 36 | import android.view.Menu; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 37 | import android.view.MenuItem; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 38 | import android.view.MotionEvent; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 41 | import android.view.ViewParent; |
Adam Powell | 8d12e20 | 2010-11-15 16:44:17 -0800 | [diff] [blame] | 42 | import android.view.Window; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 44 | import android.widget.ActionMenuPresenter; |
| 45 | import android.widget.ActionMenuView; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 46 | import android.widget.AdapterView; |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 47 | import android.widget.FrameLayout; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 48 | import android.widget.ImageView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 50 | import android.widget.ProgressBar; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 51 | import android.widget.Spinner; |
| 52 | import android.widget.SpinnerAdapter; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 53 | import android.widget.TextView; |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 54 | import com.android.internal.R; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 55 | import com.android.internal.transition.ActionBarTransition; |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 56 | import com.android.internal.view.menu.ActionMenuItem; |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 57 | import com.android.internal.view.menu.MenuBuilder; |
| 58 | import com.android.internal.view.menu.MenuItemImpl; |
| 59 | import com.android.internal.view.menu.MenuPresenter; |
| 60 | import com.android.internal.view.menu.MenuView; |
| 61 | import com.android.internal.view.menu.SubMenuBuilder; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 62 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @hide |
| 65 | */ |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 66 | public class ActionBarView extends AbsActionBarView { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 67 | private static final String TAG = "ActionBarView"; |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 68 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Display options applied by default |
| 71 | */ |
| 72 | public static final int DISPLAY_DEFAULT = 0; |
| 73 | |
| 74 | /** |
| 75 | * Display options that require re-layout as opposed to a simple invalidate |
| 76 | */ |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 77 | private static final int DISPLAY_RELAYOUT_MASK = |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 78 | ActionBar.DISPLAY_SHOW_HOME | |
| 79 | ActionBar.DISPLAY_USE_LOGO | |
| 80 | ActionBar.DISPLAY_HOME_AS_UP | |
| 81 | ActionBar.DISPLAY_SHOW_CUSTOM | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 82 | ActionBar.DISPLAY_SHOW_TITLE | |
| 83 | ActionBar.DISPLAY_TITLE_MULTIPLE_LINES; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 84 | |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 85 | private static final int DEFAULT_CUSTOM_GRAVITY = Gravity.START | Gravity.CENTER_VERTICAL; |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 86 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 87 | private int mNavigationMode; |
Adam Powell | 82f24e8 | 2011-08-18 20:15:20 -0700 | [diff] [blame] | 88 | private int mDisplayOptions = -1; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 89 | private CharSequence mTitle; |
| 90 | private CharSequence mSubtitle; |
| 91 | private Drawable mIcon; |
| 92 | private Drawable mLogo; |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 93 | private CharSequence mHomeDescription; |
| 94 | private int mHomeDescriptionRes; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 95 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 96 | private HomeView mHomeLayout; |
| 97 | private HomeView mExpandedHomeLayout; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 98 | private LinearLayout mTitleLayout; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 99 | private TextView mTitleView; |
| 100 | private TextView mSubtitleView; |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 101 | private ViewGroup mUpGoerFive; |
Adam Powell | 54c7d7e | 2011-06-17 20:30:13 -0700 | [diff] [blame] | 102 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 103 | private Spinner mSpinner; |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 104 | private LinearLayout mListNavLayout; |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 105 | private ScrollingTabContainerView mTabScrollView; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 106 | private View mCustomNavView; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 107 | private ProgressBar mProgressView; |
| 108 | private ProgressBar mIndeterminateProgressView; |
| 109 | |
| 110 | private int mProgressBarPadding; |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 111 | private int mItemPadding; |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 112 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 113 | private int mTitleStyleRes; |
| 114 | private int mSubtitleStyleRes; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 115 | private int mProgressStyle; |
| 116 | private int mIndeterminateProgressStyle; |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 117 | |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 118 | private boolean mUserTitle; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 119 | private boolean mIncludeTabs; |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 120 | private boolean mIsCollapsable; |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 121 | private boolean mIsCollapsed; |
Adam Powell | 62f3303 | 2013-02-04 12:44:34 -0800 | [diff] [blame] | 122 | private boolean mWasHomeEnabled; // Was it enabled before action view expansion? |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 123 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 124 | private MenuBuilder mOptionsMenu; |
Adam Powell | 8c16aa9 | 2013-04-04 10:47:52 -0700 | [diff] [blame] | 125 | private boolean mMenuPrepared; |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 126 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 127 | private ActionBarContextView mContextView; |
| 128 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 129 | private ActionMenuItem mLogoNavItem; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 130 | |
| 131 | private SpinnerAdapter mSpinnerAdapter; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 132 | private OnNavigationListener mCallback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 133 | |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 134 | private Runnable mTabSelector; |
| 135 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 136 | private ExpandedActionViewMenuPresenter mExpandedMenuPresenter; |
| 137 | View mExpandedActionView; |
| 138 | |
Adam Powell | 915ce0d | 2011-09-28 15:52:57 -0700 | [diff] [blame] | 139 | Window.Callback mWindowCallback; |
| 140 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 141 | private final AdapterView.OnItemSelectedListener mNavItemSelectedListener = |
| 142 | new AdapterView.OnItemSelectedListener() { |
| 143 | public void onItemSelected(AdapterView parent, View view, int position, long id) { |
| 144 | if (mCallback != null) { |
| 145 | mCallback.onNavigationItemSelected(position, id); |
| 146 | } |
| 147 | } |
| 148 | public void onNothingSelected(AdapterView parent) { |
| 149 | // Do nothing |
| 150 | } |
| 151 | }; |
| 152 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 153 | private final OnClickListener mExpandedActionViewUpListener = new OnClickListener() { |
| 154 | @Override |
| 155 | public void onClick(View v) { |
| 156 | final MenuItemImpl item = mExpandedMenuPresenter.mCurrentExpandedItem; |
| 157 | if (item != null) { |
| 158 | item.collapseActionView(); |
| 159 | } |
| 160 | } |
| 161 | }; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 162 | |
Adam Powell | 54c7d7e | 2011-06-17 20:30:13 -0700 | [diff] [blame] | 163 | private final OnClickListener mUpClickListener = new OnClickListener() { |
| 164 | public void onClick(View v) { |
Adam Powell | 8c16aa9 | 2013-04-04 10:47:52 -0700 | [diff] [blame] | 165 | if (mMenuPrepared) { |
| 166 | // Only invoke the window callback if the options menu has been initialized. |
| 167 | mWindowCallback.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, mLogoNavItem); |
| 168 | } |
Adam Powell | 54c7d7e | 2011-06-17 20:30:13 -0700 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 172 | public ActionBarView(Context context, AttributeSet attrs) { |
| 173 | super(context, attrs); |
| 174 | |
Adam Powell | 654e4e4 | 2011-01-03 16:33:50 -0800 | [diff] [blame] | 175 | // Background is always provided by the container. |
| 176 | setBackgroundResource(0); |
| 177 | |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 178 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, |
| 179 | com.android.internal.R.attr.actionBarStyle, 0); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 180 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 181 | ApplicationInfo appInfo = context.getApplicationInfo(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 182 | PackageManager pm = context.getPackageManager(); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 183 | mNavigationMode = a.getInt(R.styleable.ActionBar_navigationMode, |
| 184 | ActionBar.NAVIGATION_MODE_STANDARD); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 185 | mTitle = a.getText(R.styleable.ActionBar_title); |
| 186 | mSubtitle = a.getText(R.styleable.ActionBar_subtitle); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 187 | mLogo = a.getDrawable(R.styleable.ActionBar_logo); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 188 | mIcon = a.getDrawable(R.styleable.ActionBar_icon); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 189 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 190 | final LayoutInflater inflater = LayoutInflater.from(context); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 191 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 192 | final int homeResId = a.getResourceId( |
Adam Powell | b33be1c | 2010-11-18 12:11:40 -0800 | [diff] [blame] | 193 | com.android.internal.R.styleable.ActionBar_homeLayout, |
| 194 | com.android.internal.R.layout.action_bar_home); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 195 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 196 | mUpGoerFive = (ViewGroup) inflater.inflate( |
| 197 | com.android.internal.R.layout.action_bar_up_container, this, false); |
| 198 | mHomeLayout = (HomeView) inflater.inflate(homeResId, mUpGoerFive, false); |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 199 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 200 | mExpandedHomeLayout = (HomeView) inflater.inflate(homeResId, mUpGoerFive, false); |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 201 | mExpandedHomeLayout.setShowUp(true); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 202 | mExpandedHomeLayout.setOnClickListener(mExpandedActionViewUpListener); |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 203 | mExpandedHomeLayout.setContentDescription(getResources().getText( |
| 204 | R.string.action_bar_up_description)); |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 205 | |
Adam Powell | 62f3303 | 2013-02-04 12:44:34 -0800 | [diff] [blame] | 206 | // This needs to highlight/be focusable on its own. |
| 207 | // TODO: Clean up the handoff between expanded/normal. |
| 208 | final Drawable upBackground = mUpGoerFive.getBackground(); |
| 209 | if (upBackground != null) { |
| 210 | mExpandedHomeLayout.setBackground(upBackground.getConstantState().newDrawable()); |
| 211 | } |
| 212 | mExpandedHomeLayout.setEnabled(true); |
| 213 | mExpandedHomeLayout.setFocusable(true); |
| 214 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 215 | mTitleStyleRes = a.getResourceId(R.styleable.ActionBar_titleTextStyle, 0); |
| 216 | mSubtitleStyleRes = a.getResourceId(R.styleable.ActionBar_subtitleTextStyle, 0); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 217 | mProgressStyle = a.getResourceId(R.styleable.ActionBar_progressBarStyle, 0); |
| 218 | mIndeterminateProgressStyle = a.getResourceId( |
| 219 | R.styleable.ActionBar_indeterminateProgressStyle, 0); |
| 220 | |
| 221 | mProgressBarPadding = a.getDimensionPixelOffset(R.styleable.ActionBar_progressBarPadding, 0); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 222 | mItemPadding = a.getDimensionPixelOffset(R.styleable.ActionBar_itemPadding, 0); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 223 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 224 | setDisplayOptions(a.getInt(R.styleable.ActionBar_displayOptions, DISPLAY_DEFAULT)); |
| 225 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 226 | final int customNavId = a.getResourceId(R.styleable.ActionBar_customNavigationLayout, 0); |
| 227 | if (customNavId != 0) { |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 228 | mCustomNavView = (View) inflater.inflate(customNavId, this, false); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 229 | mNavigationMode = ActionBar.NAVIGATION_MODE_STANDARD; |
| 230 | setDisplayOptions(mDisplayOptions | ActionBar.DISPLAY_SHOW_CUSTOM); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 233 | mContentHeight = a.getLayoutDimension(R.styleable.ActionBar_height, 0); |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 234 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 235 | a.recycle(); |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 236 | |
Ben Komalo | ad199ec | 2010-11-01 12:24:16 -0700 | [diff] [blame] | 237 | mLogoNavItem = new ActionMenuItem(context, 0, android.R.id.home, 0, 0, mTitle); |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 238 | |
| 239 | mUpGoerFive.setOnClickListener(mUpClickListener); |
| 240 | mUpGoerFive.setClickable(true); |
| 241 | mUpGoerFive.setFocusable(true); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 242 | |
| 243 | if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 244 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 245 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 246 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 247 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 248 | @Override |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 249 | protected void onConfigurationChanged(Configuration newConfig) { |
| 250 | super.onConfigurationChanged(newConfig); |
| 251 | |
Adam Powell | 07a047f | 2011-08-31 10:33:20 -0700 | [diff] [blame] | 252 | mTitleView = null; |
| 253 | mSubtitleView = null; |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 254 | if (mTitleLayout != null && mTitleLayout.getParent() == mUpGoerFive) { |
| 255 | mUpGoerFive.removeView(mTitleLayout); |
Adam Powell | 07a047f | 2011-08-31 10:33:20 -0700 | [diff] [blame] | 256 | } |
| 257 | mTitleLayout = null; |
| 258 | if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
| 259 | initTitle(); |
| 260 | } |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 261 | |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 262 | if (mHomeDescriptionRes != 0) { |
| 263 | setHomeActionContentDescription(mHomeDescriptionRes); |
| 264 | } |
| 265 | |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 266 | if (mTabScrollView != null && mIncludeTabs) { |
| 267 | ViewGroup.LayoutParams lp = mTabScrollView.getLayoutParams(); |
Adam Powell | b844011 | 2011-09-12 19:14:11 -0700 | [diff] [blame] | 268 | if (lp != null) { |
| 269 | lp.width = LayoutParams.WRAP_CONTENT; |
| 270 | lp.height = LayoutParams.MATCH_PARENT; |
| 271 | } |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 272 | mTabScrollView.setAllowCollapse(true); |
| 273 | } |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Adam Powell | 915ce0d | 2011-09-28 15:52:57 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Set the window callback used to invoke menu items; used for dispatching home button presses. |
| 278 | * @param cb Window callback to dispatch to |
| 279 | */ |
| 280 | public void setWindowCallback(Window.Callback cb) { |
| 281 | mWindowCallback = cb; |
| 282 | } |
| 283 | |
Adam Powell | 45c0b19 | 2011-07-28 15:11:57 -0700 | [diff] [blame] | 284 | @Override |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 285 | public void onDetachedFromWindow() { |
| 286 | super.onDetachedFromWindow(); |
| 287 | removeCallbacks(mTabSelector); |
Adam Powell | 97e1836 | 2011-10-20 15:37:18 -0700 | [diff] [blame] | 288 | if (mActionMenuPresenter != null) { |
| 289 | mActionMenuPresenter.hideOverflowMenu(); |
| 290 | mActionMenuPresenter.hideSubMenus(); |
| 291 | } |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 295 | public boolean shouldDelayChildPressedState() { |
| 296 | return false; |
| 297 | } |
| 298 | |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 299 | public void initProgress() { |
| 300 | mProgressView = new ProgressBar(mContext, null, 0, mProgressStyle); |
| 301 | mProgressView.setId(R.id.progress_horizontal); |
| 302 | mProgressView.setMax(10000); |
Adam Powell | 45b075d | 2012-01-09 15:41:40 -0800 | [diff] [blame] | 303 | mProgressView.setVisibility(GONE); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 304 | addView(mProgressView); |
| 305 | } |
| 306 | |
| 307 | public void initIndeterminateProgress() { |
| 308 | mIndeterminateProgressView = new ProgressBar(mContext, null, 0, |
| 309 | mIndeterminateProgressStyle); |
| 310 | mIndeterminateProgressView.setId(R.id.progress_circular); |
Adam Powell | 45b075d | 2012-01-09 15:41:40 -0800 | [diff] [blame] | 311 | mIndeterminateProgressView.setVisibility(GONE); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 312 | addView(mIndeterminateProgressView); |
| 313 | } |
| 314 | |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 315 | @Override |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 316 | public void setSplitActionBar(boolean splitActionBar) { |
| 317 | if (mSplitActionBar != splitActionBar) { |
| 318 | if (mMenuView != null) { |
Adam Powell | 894bf54 | 2011-07-22 13:39:08 -0700 | [diff] [blame] | 319 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 320 | if (oldParent != null) { |
| 321 | oldParent.removeView(mMenuView); |
| 322 | } |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 323 | if (splitActionBar) { |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 324 | if (mSplitView != null) { |
| 325 | mSplitView.addView(mMenuView); |
| 326 | } |
Adam Powell | f203e0a | 2012-03-06 17:40:12 -0800 | [diff] [blame] | 327 | mMenuView.getLayoutParams().width = LayoutParams.MATCH_PARENT; |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 328 | } else { |
| 329 | addView(mMenuView); |
Adam Powell | f203e0a | 2012-03-06 17:40:12 -0800 | [diff] [blame] | 330 | mMenuView.getLayoutParams().width = LayoutParams.WRAP_CONTENT; |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 331 | } |
Adam Powell | f203e0a | 2012-03-06 17:40:12 -0800 | [diff] [blame] | 332 | mMenuView.requestLayout(); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 333 | } |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 334 | if (mSplitView != null) { |
| 335 | mSplitView.setVisibility(splitActionBar ? VISIBLE : GONE); |
| 336 | } |
Adam Powell | f203e0a | 2012-03-06 17:40:12 -0800 | [diff] [blame] | 337 | |
| 338 | if (mActionMenuPresenter != null) { |
| 339 | if (!splitActionBar) { |
| 340 | mActionMenuPresenter.setExpandedActionViewsExclusive( |
| 341 | getResources().getBoolean( |
| 342 | com.android.internal.R.bool.action_bar_expanded_action_views_exclusive)); |
| 343 | } else { |
| 344 | mActionMenuPresenter.setExpandedActionViewsExclusive(false); |
| 345 | // Allow full screen width in split mode. |
| 346 | mActionMenuPresenter.setWidthLimit( |
| 347 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
| 348 | // No limit to the item count; use whatever will fit. |
| 349 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
| 350 | } |
| 351 | } |
Adam Powell | a05aba9 | 2011-09-23 14:22:49 -0700 | [diff] [blame] | 352 | super.setSplitActionBar(splitActionBar); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | public boolean isSplitActionBar() { |
| 357 | return mSplitActionBar; |
| 358 | } |
| 359 | |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 360 | public boolean hasEmbeddedTabs() { |
| 361 | return mIncludeTabs; |
| 362 | } |
| 363 | |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 364 | public void setEmbeddedTabView(ScrollingTabContainerView tabs) { |
Adam Powell | e0adac0 | 2011-06-14 13:46:30 -0700 | [diff] [blame] | 365 | if (mTabScrollView != null) { |
| 366 | removeView(mTabScrollView); |
| 367 | } |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 368 | mTabScrollView = tabs; |
| 369 | mIncludeTabs = tabs != null; |
Adam Powell | c307642 | 2011-06-01 18:19:04 -0700 | [diff] [blame] | 370 | if (mIncludeTabs && mNavigationMode == ActionBar.NAVIGATION_MODE_TABS) { |
Adam Powell | 39a8dc4 | 2011-05-27 18:44:00 -0700 | [diff] [blame] | 371 | addView(mTabScrollView); |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 372 | ViewGroup.LayoutParams lp = mTabScrollView.getLayoutParams(); |
| 373 | lp.width = LayoutParams.WRAP_CONTENT; |
| 374 | lp.height = LayoutParams.MATCH_PARENT; |
| 375 | tabs.setAllowCollapse(true); |
Adam Powell | 39a8dc4 | 2011-05-27 18:44:00 -0700 | [diff] [blame] | 376 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 379 | public void setCallback(OnNavigationListener callback) { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 380 | mCallback = callback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 381 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 382 | |
Adam Powell | 8c16aa9 | 2013-04-04 10:47:52 -0700 | [diff] [blame] | 383 | public void setMenuPrepared() { |
| 384 | mMenuPrepared = true; |
| 385 | } |
| 386 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 387 | public void setMenu(Menu menu, MenuPresenter.Callback cb) { |
Adam Powell | f2d7a5d | 2011-02-07 18:05:24 -0800 | [diff] [blame] | 388 | if (menu == mOptionsMenu) return; |
| 389 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 390 | if (mOptionsMenu != null) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 391 | mOptionsMenu.removeMenuPresenter(mActionMenuPresenter); |
| 392 | mOptionsMenu.removeMenuPresenter(mExpandedMenuPresenter); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 395 | MenuBuilder builder = (MenuBuilder) menu; |
| 396 | mOptionsMenu = builder; |
| 397 | if (mMenuView != null) { |
Adam Powell | 894bf54 | 2011-07-22 13:39:08 -0700 | [diff] [blame] | 398 | final ViewGroup oldParent = (ViewGroup) mMenuView.getParent(); |
| 399 | if (oldParent != null) { |
| 400 | oldParent.removeView(mMenuView); |
| 401 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 402 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 403 | if (mActionMenuPresenter == null) { |
Adam Powell | 538e565 | 2011-10-11 13:47:08 -0700 | [diff] [blame] | 404 | mActionMenuPresenter = new ActionMenuPresenter(mContext); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 405 | mActionMenuPresenter.setCallback(cb); |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 406 | mActionMenuPresenter.setId(com.android.internal.R.id.action_menu_presenter); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 407 | mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 408 | } |
Adam Powell | 3526556 | 2011-05-27 15:29:41 -0700 | [diff] [blame] | 409 | |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 410 | ActionMenuView menuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 411 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 412 | LayoutParams.MATCH_PARENT); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 413 | if (!mSplitActionBar) { |
Adam Powell | b187cd9 | 2011-07-20 14:17:56 -0700 | [diff] [blame] | 414 | mActionMenuPresenter.setExpandedActionViewsExclusive( |
| 415 | getResources().getBoolean( |
| 416 | com.android.internal.R.bool.action_bar_expanded_action_views_exclusive)); |
Adam Powell | f35d049 | 2011-08-17 13:56:47 -0700 | [diff] [blame] | 417 | configPresenters(builder); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 418 | menuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | 894bf54 | 2011-07-22 13:39:08 -0700 | [diff] [blame] | 419 | final ViewGroup oldParent = (ViewGroup) menuView.getParent(); |
| 420 | if (oldParent != null && oldParent != this) { |
| 421 | oldParent.removeView(menuView); |
| 422 | } |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 423 | addView(menuView, layoutParams); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 424 | } else { |
Adam Powell | b187cd9 | 2011-07-20 14:17:56 -0700 | [diff] [blame] | 425 | mActionMenuPresenter.setExpandedActionViewsExclusive(false); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 426 | // Allow full screen width in split mode. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 427 | mActionMenuPresenter.setWidthLimit( |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 428 | getContext().getResources().getDisplayMetrics().widthPixels, true); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 429 | // No limit to the item count; use whatever will fit. |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 430 | mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE); |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 431 | // Span the whole width |
| 432 | layoutParams.width = LayoutParams.MATCH_PARENT; |
Adam Powell | fa18d18 | 2014-01-07 15:56:59 -0800 | [diff] [blame] | 433 | layoutParams.height = mContentHeight; |
Adam Powell | f35d049 | 2011-08-17 13:56:47 -0700 | [diff] [blame] | 434 | configPresenters(builder); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 435 | menuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this); |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 436 | if (mSplitView != null) { |
Adam Powell | 894bf54 | 2011-07-22 13:39:08 -0700 | [diff] [blame] | 437 | final ViewGroup oldParent = (ViewGroup) menuView.getParent(); |
| 438 | if (oldParent != null && oldParent != mSplitView) { |
| 439 | oldParent.removeView(menuView); |
| 440 | } |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 441 | menuView.setVisibility(getAnimatedVisibility()); |
Adam Powell | 1ab418a | 2011-06-09 20:49:49 -0700 | [diff] [blame] | 442 | mSplitView.addView(menuView, layoutParams); |
| 443 | } else { |
| 444 | // We'll add this later if we missed it this time. |
| 445 | menuView.setLayoutParams(layoutParams); |
| 446 | } |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 447 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 448 | mMenuView = menuView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 449 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 450 | |
Adam Powell | f35d049 | 2011-08-17 13:56:47 -0700 | [diff] [blame] | 451 | private void configPresenters(MenuBuilder builder) { |
| 452 | if (builder != null) { |
| 453 | builder.addMenuPresenter(mActionMenuPresenter); |
| 454 | builder.addMenuPresenter(mExpandedMenuPresenter); |
| 455 | } else { |
| 456 | mActionMenuPresenter.initForMenu(mContext, null); |
| 457 | mExpandedMenuPresenter.initForMenu(mContext, null); |
| 458 | mActionMenuPresenter.updateMenuView(true); |
| 459 | mExpandedMenuPresenter.updateMenuView(true); |
| 460 | } |
| 461 | } |
| 462 | |
Adam Powell | b80d332 | 2011-07-15 21:33:11 -0700 | [diff] [blame] | 463 | public boolean hasExpandedActionView() { |
| 464 | return mExpandedMenuPresenter != null && |
| 465 | mExpandedMenuPresenter.mCurrentExpandedItem != null; |
| 466 | } |
| 467 | |
| 468 | public void collapseActionView() { |
| 469 | final MenuItemImpl item = mExpandedMenuPresenter == null ? null : |
| 470 | mExpandedMenuPresenter.mCurrentExpandedItem; |
| 471 | if (item != null) { |
| 472 | item.collapseActionView(); |
| 473 | } |
| 474 | } |
| 475 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 476 | public void setCustomNavigationView(View view) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 477 | final boolean showCustom = (mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 478 | if (showCustom) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 479 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 480 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 481 | if (mCustomNavView != null && showCustom) { |
| 482 | removeView(mCustomNavView); |
| 483 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 484 | mCustomNavView = view; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 485 | if (mCustomNavView != null && showCustom) { |
| 486 | addView(mCustomNavView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 487 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 488 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 489 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 490 | public CharSequence getTitle() { |
| 491 | return mTitle; |
| 492 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 493 | |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 494 | /** |
| 495 | * Set the action bar title. This will always replace or override window titles. |
| 496 | * @param title Title to set |
| 497 | * |
| 498 | * @see #setWindowTitle(CharSequence) |
| 499 | */ |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 500 | public void setTitle(CharSequence title) { |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 501 | mUserTitle = true; |
| 502 | setTitleImpl(title); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Set the window title. A window title will always be replaced or overridden by a user title. |
| 507 | * @param title Title to set |
| 508 | * |
| 509 | * @see #setTitle(CharSequence) |
| 510 | */ |
| 511 | public void setWindowTitle(CharSequence title) { |
| 512 | if (!mUserTitle) { |
| 513 | setTitleImpl(title); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | private void setTitleImpl(CharSequence title) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 518 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 519 | mTitle = title; |
| 520 | if (mTitleView != null) { |
| 521 | mTitleView.setText(title); |
Adam Powell | b0e217e | 2011-08-12 11:36:51 -0700 | [diff] [blame] | 522 | final boolean visible = mExpandedActionView == null && |
Adam Powell | 71f3417 | 2011-08-11 20:07:11 -0700 | [diff] [blame] | 523 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0 && |
Adam Powell | b0e217e | 2011-08-12 11:36:51 -0700 | [diff] [blame] | 524 | (!TextUtils.isEmpty(mTitle) || !TextUtils.isEmpty(mSubtitle)); |
| 525 | mTitleLayout.setVisibility(visible ? VISIBLE : GONE); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 526 | } |
| 527 | if (mLogoNavItem != null) { |
| 528 | mLogoNavItem.setTitle(title); |
| 529 | } |
Adam Powell | 2229ca0 | 2013-11-12 16:20:46 -0800 | [diff] [blame] | 530 | updateHomeAccessibility(mUpGoerFive.isEnabled()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 531 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 532 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 533 | public CharSequence getSubtitle() { |
| 534 | return mSubtitle; |
| 535 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 536 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 537 | public void setSubtitle(CharSequence subtitle) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 538 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 539 | mSubtitle = subtitle; |
| 540 | if (mSubtitleView != null) { |
| 541 | mSubtitleView.setText(subtitle); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 542 | mSubtitleView.setVisibility(subtitle != null ? VISIBLE : GONE); |
Adam Powell | b0e217e | 2011-08-12 11:36:51 -0700 | [diff] [blame] | 543 | final boolean visible = mExpandedActionView == null && |
Adam Powell | 71f3417 | 2011-08-11 20:07:11 -0700 | [diff] [blame] | 544 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0 && |
Adam Powell | b0e217e | 2011-08-12 11:36:51 -0700 | [diff] [blame] | 545 | (!TextUtils.isEmpty(mTitle) || !TextUtils.isEmpty(mSubtitle)); |
| 546 | mTitleLayout.setVisibility(visible ? VISIBLE : GONE); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 547 | } |
Adam Powell | 2229ca0 | 2013-11-12 16:20:46 -0800 | [diff] [blame] | 548 | updateHomeAccessibility(mUpGoerFive.isEnabled()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 549 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 550 | |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 551 | public void setHomeButtonEnabled(boolean enable) { |
Adam Powell | 07cf971 | 2013-02-05 16:13:57 -0800 | [diff] [blame] | 552 | setHomeButtonEnabled(enable, true); |
| 553 | } |
| 554 | |
| 555 | private void setHomeButtonEnabled(boolean enable, boolean recordState) { |
| 556 | if (recordState) { |
| 557 | mWasHomeEnabled = enable; |
| 558 | } |
| 559 | |
| 560 | if (mExpandedActionView != null) { |
| 561 | // There's an action view currently showing and we want to keep the state |
| 562 | // configured for the action view at the moment. If we needed to record the |
| 563 | // new state for later we will have done so above. |
| 564 | return; |
| 565 | } |
| 566 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 567 | mUpGoerFive.setEnabled(enable); |
| 568 | mUpGoerFive.setFocusable(enable); |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 569 | // Make sure the home button has an accurate content description for accessibility. |
Adam Powell | f37a513 | 2013-11-01 16:32:37 -0700 | [diff] [blame] | 570 | updateHomeAccessibility(enable); |
| 571 | } |
| 572 | |
| 573 | private void updateHomeAccessibility(boolean homeEnabled) { |
| 574 | if (!homeEnabled) { |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 575 | mUpGoerFive.setContentDescription(null); |
| 576 | mUpGoerFive.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 577 | } else { |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 578 | mUpGoerFive.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_AUTO); |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 579 | mUpGoerFive.setContentDescription(buildHomeContentDescription()); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Compose a content description for the Home/Up affordance. |
| 585 | * |
| 586 | * <p>As this encompasses the icon/logo, title and subtitle all in one, we need |
| 587 | * a description for the whole wad of stuff that can be localized properly.</p> |
| 588 | */ |
| 589 | private CharSequence buildHomeContentDescription() { |
| 590 | final CharSequence homeDesc; |
| 591 | if (mHomeDescription != null) { |
| 592 | homeDesc = mHomeDescription; |
| 593 | } else { |
Casey Burkhardt | 4aeb961 | 2012-10-01 14:13:41 -0700 | [diff] [blame] | 594 | if ((mDisplayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 595 | homeDesc = mContext.getResources().getText(R.string.action_bar_up_description); |
Casey Burkhardt | 4aeb961 | 2012-10-01 14:13:41 -0700 | [diff] [blame] | 596 | } else { |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 597 | homeDesc = mContext.getResources().getText(R.string.action_bar_home_description); |
Casey Burkhardt | 4aeb961 | 2012-10-01 14:13:41 -0700 | [diff] [blame] | 598 | } |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 599 | } |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 600 | |
| 601 | final CharSequence title = getTitle(); |
| 602 | final CharSequence subtitle = getSubtitle(); |
| 603 | if (!TextUtils.isEmpty(title)) { |
| 604 | final String result; |
| 605 | if (!TextUtils.isEmpty(subtitle)) { |
| 606 | result = getResources().getString( |
| 607 | R.string.action_bar_home_subtitle_description_format, |
| 608 | title, subtitle, homeDesc); |
| 609 | } else { |
| 610 | result = getResources().getString(R.string.action_bar_home_description_format, |
| 611 | title, homeDesc); |
| 612 | } |
| 613 | return result; |
| 614 | } |
| 615 | return homeDesc; |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 618 | public void setDisplayOptions(int options) { |
Adam Powell | 82f24e8 | 2011-08-18 20:15:20 -0700 | [diff] [blame] | 619 | final int flagsChanged = mDisplayOptions == -1 ? -1 : options ^ mDisplayOptions; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 620 | mDisplayOptions = options; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 621 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 622 | if ((flagsChanged & DISPLAY_RELAYOUT_MASK) != 0) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 623 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 624 | |
| 625 | if ((flagsChanged & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 626 | final boolean setUp = (options & ActionBar.DISPLAY_HOME_AS_UP) != 0; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 627 | mHomeLayout.setShowUp(setUp); |
Adam Powell | c29f4e5 | 2011-07-13 20:40:52 -0700 | [diff] [blame] | 628 | |
| 629 | // Showing home as up implicitly enables interaction with it. |
| 630 | // In honeycomb it was always enabled, so make this transition |
| 631 | // a bit easier for developers in the common case. |
| 632 | // (It would be silly to show it as up without responding to it.) |
| 633 | if (setUp) { |
| 634 | setHomeButtonEnabled(true); |
| 635 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 636 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 637 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 638 | if ((flagsChanged & ActionBar.DISPLAY_USE_LOGO) != 0) { |
| 639 | final boolean logoVis = mLogo != null && (options & ActionBar.DISPLAY_USE_LOGO) != 0; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 640 | mHomeLayout.setIcon(logoVis ? mLogo : mIcon); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | if ((flagsChanged & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
| 644 | if ((options & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
| 645 | initTitle(); |
| 646 | } else { |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 647 | mUpGoerFive.removeView(mTitleLayout); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 651 | final boolean showHome = (options & ActionBar.DISPLAY_SHOW_HOME) != 0; |
| 652 | final boolean homeAsUp = (mDisplayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0; |
| 653 | final boolean titleUp = !showHome && homeAsUp; |
| 654 | mHomeLayout.setShowIcon(showHome); |
| 655 | |
| 656 | final int homeVis = (showHome || titleUp) && mExpandedActionView == null ? |
| 657 | VISIBLE : GONE; |
| 658 | mHomeLayout.setVisibility(homeVis); |
Adam Powell | 54c7d7e | 2011-06-17 20:30:13 -0700 | [diff] [blame] | 659 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 660 | if ((flagsChanged & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) { |
| 661 | if ((options & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) { |
| 662 | addView(mCustomNavView); |
| 663 | } else { |
| 664 | removeView(mCustomNavView); |
| 665 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 666 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 667 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 668 | if (mTitleLayout != null && |
| 669 | (flagsChanged & ActionBar.DISPLAY_TITLE_MULTIPLE_LINES) != 0) { |
| 670 | if ((options & ActionBar.DISPLAY_TITLE_MULTIPLE_LINES) != 0) { |
| 671 | mTitleView.setSingleLine(false); |
| 672 | mTitleView.setMaxLines(2); |
| 673 | } else { |
| 674 | mTitleView.setMaxLines(1); |
| 675 | mTitleView.setSingleLine(true); |
| 676 | } |
| 677 | } |
| 678 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 679 | requestLayout(); |
| 680 | } else { |
| 681 | invalidate(); |
| 682 | } |
Adam Powell | f3cd1d9 | 2011-04-26 17:31:52 -0700 | [diff] [blame] | 683 | |
| 684 | // Make sure the home button has an accurate content description for accessibility. |
Adam Powell | 2229ca0 | 2013-11-12 16:20:46 -0800 | [diff] [blame] | 685 | updateHomeAccessibility(mUpGoerFive.isEnabled()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 688 | public void setIcon(Drawable icon) { |
| 689 | mIcon = icon; |
| 690 | if (icon != null && |
| 691 | ((mDisplayOptions & ActionBar.DISPLAY_USE_LOGO) == 0 || mLogo == null)) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 692 | mHomeLayout.setIcon(icon); |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 693 | } |
Adam Powell | f821819 | 2012-02-21 17:10:10 -0800 | [diff] [blame] | 694 | if (mExpandedActionView != null) { |
| 695 | mExpandedHomeLayout.setIcon(mIcon.getConstantState().newDrawable(getResources())); |
| 696 | } |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 699 | public void setIcon(int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 700 | setIcon(resId != 0 ? mContext.getDrawable(resId) : null); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | public boolean hasIcon() { |
| 704 | return mIcon != null; |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 707 | public void setLogo(Drawable logo) { |
| 708 | mLogo = logo; |
| 709 | if (logo != null && (mDisplayOptions & ActionBar.DISPLAY_USE_LOGO) != 0) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 710 | mHomeLayout.setIcon(logo); |
Adam Powell | 1969b87 | 2011-03-22 11:52:48 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 714 | public void setLogo(int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 715 | setLogo(resId != 0 ? mContext.getDrawable(resId) : null); |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | public boolean hasLogo() { |
| 719 | return mLogo != null; |
Adam Powell | 45c515b | 2011-04-21 18:50:20 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 722 | public void setNavigationMode(int mode) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 723 | final int oldMode = mNavigationMode; |
| 724 | if (mode != oldMode) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 725 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 726 | switch (oldMode) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 727 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | c307642 | 2011-06-01 18:19:04 -0700 | [diff] [blame] | 728 | if (mListNavLayout != null) { |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 729 | removeView(mListNavLayout); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 730 | } |
| 731 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 732 | case ActionBar.NAVIGATION_MODE_TABS: |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 733 | if (mTabScrollView != null && mIncludeTabs) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 734 | removeView(mTabScrollView); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 735 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 736 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 737 | |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 738 | switch (mode) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 739 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 740 | if (mSpinner == null) { |
| 741 | mSpinner = new Spinner(mContext, null, |
| 742 | com.android.internal.R.attr.actionDropDownStyle); |
Adam Powell | 29c04ef | 2012-12-10 15:42:22 -0800 | [diff] [blame] | 743 | mSpinner.setId(com.android.internal.R.id.action_bar_spinner); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 744 | mListNavLayout = new LinearLayout(mContext, null, |
| 745 | com.android.internal.R.attr.actionBarTabBarStyle); |
| 746 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 747 | LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 748 | params.gravity = Gravity.CENTER; |
| 749 | mListNavLayout.addView(mSpinner, params); |
| 750 | } |
| 751 | if (mSpinner.getAdapter() != mSpinnerAdapter) { |
| 752 | mSpinner.setAdapter(mSpinnerAdapter); |
| 753 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 754 | mSpinner.setOnItemSelectedListener(mNavItemSelectedListener); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 755 | addView(mListNavLayout); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 756 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 757 | case ActionBar.NAVIGATION_MODE_TABS: |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 758 | if (mTabScrollView != null && mIncludeTabs) { |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 759 | addView(mTabScrollView); |
| 760 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 761 | break; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 762 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 763 | mNavigationMode = mode; |
| 764 | requestLayout(); |
| 765 | } |
| 766 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 767 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 768 | public void setDropdownAdapter(SpinnerAdapter adapter) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 769 | mSpinnerAdapter = adapter; |
| 770 | if (mSpinner != null) { |
| 771 | mSpinner.setAdapter(adapter); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | public SpinnerAdapter getDropdownAdapter() { |
| 776 | return mSpinnerAdapter; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 777 | } |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 778 | |
| 779 | public void setDropdownSelectedPosition(int position) { |
| 780 | mSpinner.setSelection(position); |
| 781 | } |
| 782 | |
| 783 | public int getDropdownSelectedPosition() { |
| 784 | return mSpinner.getSelectedItemPosition(); |
| 785 | } |
| 786 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 787 | public View getCustomNavigationView() { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 788 | return mCustomNavView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 789 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 790 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 791 | public int getNavigationMode() { |
| 792 | return mNavigationMode; |
| 793 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 794 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 795 | public int getDisplayOptions() { |
| 796 | return mDisplayOptions; |
| 797 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 798 | |
| 799 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 800 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 801 | // Used by custom nav views if they don't supply layout params. Everything else |
| 802 | // added to an ActionBarView should have them already. |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 803 | return new ActionBar.LayoutParams(DEFAULT_CUSTOM_GRAVITY); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 806 | @Override |
| 807 | protected void onFinishInflate() { |
| 808 | super.onFinishInflate(); |
| 809 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 810 | mUpGoerFive.addView(mHomeLayout, 0); |
| 811 | addView(mUpGoerFive); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 812 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 813 | if (mCustomNavView != null && (mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) { |
| 814 | final ViewParent parent = mCustomNavView.getParent(); |
| 815 | if (parent != this) { |
| 816 | if (parent instanceof ViewGroup) { |
| 817 | ((ViewGroup) parent).removeView(mCustomNavView); |
| 818 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 819 | addView(mCustomNavView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 820 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
Adam Powell | c8b0b00 | 2011-06-23 14:39:05 -0700 | [diff] [blame] | 823 | |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 824 | private void initTitle() { |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 825 | if (mTitleLayout == null) { |
| 826 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Adam Powell | 9e58df5 | 2011-08-15 17:06:49 -0700 | [diff] [blame] | 827 | mTitleLayout = (LinearLayout) inflater.inflate(R.layout.action_bar_title_item, |
| 828 | this, false); |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 829 | mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 830 | mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
Adam Powell | 54c7d7e | 2011-06-17 20:30:13 -0700 | [diff] [blame] | 831 | |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 832 | if (mTitleStyleRes != 0) { |
| 833 | mTitleView.setTextAppearance(mContext, mTitleStyleRes); |
| 834 | } |
| 835 | if (mTitle != null) { |
| 836 | mTitleView.setText(mTitle); |
| 837 | } |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 838 | |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 839 | if (mSubtitleStyleRes != 0) { |
| 840 | mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); |
| 841 | } |
| 842 | if (mSubtitle != null) { |
| 843 | mSubtitleView.setText(mSubtitle); |
| 844 | mSubtitleView.setVisibility(VISIBLE); |
| 845 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 846 | } |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 847 | |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 848 | ActionBarTransition.beginDelayedTransition(this); |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 849 | mUpGoerFive.addView(mTitleLayout); |
Adam Powell | 82f24e8 | 2011-08-18 20:15:20 -0700 | [diff] [blame] | 850 | if (mExpandedActionView != null || |
| 851 | (TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mSubtitle))) { |
| 852 | // Don't show while in expanded mode or with empty text |
Adam Powell | 71f3417 | 2011-08-11 20:07:11 -0700 | [diff] [blame] | 853 | mTitleLayout.setVisibility(GONE); |
Adam Powell | 6684e92 | 2013-03-26 15:38:26 -0700 | [diff] [blame] | 854 | } else { |
| 855 | mTitleLayout.setVisibility(VISIBLE); |
Adam Powell | 71f3417 | 2011-08-11 20:07:11 -0700 | [diff] [blame] | 856 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 857 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 858 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 859 | public void setContextView(ActionBarContextView view) { |
| 860 | mContextView = view; |
| 861 | } |
| 862 | |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 863 | public void setCollapsable(boolean collapsable) { |
| 864 | mIsCollapsable = collapsable; |
| 865 | } |
| 866 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 867 | public boolean isCollapsed() { |
| 868 | return mIsCollapsed; |
| 869 | } |
| 870 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 871 | /** |
| 872 | * @return True if any characters in the title were truncated |
| 873 | */ |
| 874 | public boolean isTitleTruncated() { |
| 875 | if (mTitleView == null) { |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | final Layout titleLayout = mTitleView.getLayout(); |
| 880 | if (titleLayout == null) { |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | final int lineCount = titleLayout.getLineCount(); |
| 885 | for (int i = 0; i < lineCount; i++) { |
| 886 | if (titleLayout.getEllipsisCount(i) > 0) { |
| 887 | return true; |
| 888 | } |
| 889 | } |
| 890 | return false; |
| 891 | } |
| 892 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 893 | @Override |
| 894 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 895 | final int childCount = getChildCount(); |
| 896 | if (mIsCollapsable) { |
| 897 | int visibleChildren = 0; |
| 898 | for (int i = 0; i < childCount; i++) { |
| 899 | final View child = getChildAt(i); |
| 900 | if (child.getVisibility() != GONE && |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 901 | !(child == mMenuView && mMenuView.getChildCount() == 0) && |
| 902 | child != mUpGoerFive) { |
| 903 | visibleChildren++; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | final int upChildCount = mUpGoerFive.getChildCount(); |
| 908 | for (int i = 0; i < upChildCount; i++) { |
| 909 | final View child = mUpGoerFive.getChildAt(i); |
| 910 | if (child.getVisibility() != GONE) { |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 911 | visibleChildren++; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (visibleChildren == 0) { |
| 916 | // No size for an empty action bar when collapsable. |
| 917 | setMeasuredDimension(0, 0); |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 918 | mIsCollapsed = true; |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | } |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 922 | mIsCollapsed = false; |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 923 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 924 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 925 | if (widthMode != MeasureSpec.EXACTLY) { |
| 926 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 927 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 928 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 929 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 930 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 931 | if (heightMode != MeasureSpec.AT_MOST) { |
| 932 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 933 | "with android:layout_height=\"wrap_content\""); |
| 934 | } |
| 935 | |
| 936 | int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 937 | |
Adam Powell | 570470a | 2012-08-06 17:00:17 -0700 | [diff] [blame] | 938 | int maxHeight = mContentHeight >= 0 ? |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 939 | mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 940 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 941 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 942 | final int paddingLeft = getPaddingLeft(); |
| 943 | final int paddingRight = getPaddingRight(); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 944 | final int height = maxHeight - verticalPadding; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 945 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
Adam Powell | 6322af5 | 2012-08-08 15:59:12 -0700 | [diff] [blame] | 946 | final int exactHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 947 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 948 | int availableWidth = contentWidth - paddingLeft - paddingRight; |
| 949 | int leftOfCenter = availableWidth / 2; |
| 950 | int rightOfCenter = leftOfCenter; |
| 951 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 952 | final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE && |
| 953 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0; |
| 954 | |
Adam Powell | b1e2f25 | 2011-08-22 17:42:12 -0700 | [diff] [blame] | 955 | HomeView homeLayout = mExpandedActionView != null ? mExpandedHomeLayout : mHomeLayout; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 956 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 957 | final ViewGroup.LayoutParams homeLp = homeLayout.getLayoutParams(); |
| 958 | int homeWidthSpec; |
| 959 | if (homeLp.width < 0) { |
| 960 | homeWidthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
| 961 | } else { |
| 962 | homeWidthSpec = MeasureSpec.makeMeasureSpec(homeLp.width, MeasureSpec.EXACTLY); |
| 963 | } |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 964 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 965 | /* |
| 966 | * This is a little weird. |
| 967 | * We're only measuring the *home* affordance within the Up container here |
| 968 | * on purpose, because we want to give the available space to all other views before |
| 969 | * the title text. We'll remeasure the whole up container again later. |
| 970 | * We need to measure this container so we know the right offset for the up affordance |
| 971 | * no matter what. |
| 972 | */ |
| 973 | homeLayout.measure(homeWidthSpec, exactHeightSpec); |
| 974 | |
| 975 | int homeWidth = 0; |
| 976 | if ((homeLayout.getVisibility() != GONE && homeLayout.getParent() == mUpGoerFive) |
| 977 | || showTitle) { |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 978 | homeWidth = homeLayout.getMeasuredWidth(); |
| 979 | final int homeOffsetWidth = homeWidth + homeLayout.getStartOffset(); |
| 980 | availableWidth = Math.max(0, availableWidth - homeOffsetWidth); |
| 981 | leftOfCenter = Math.max(0, availableWidth - homeOffsetWidth); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 982 | } |
Casey Burkhardt | 8452a7f | 2013-04-30 15:56:41 -0700 | [diff] [blame] | 983 | |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 984 | if (mMenuView != null && mMenuView.getParent() == this) { |
Adam Powell | 6322af5 | 2012-08-08 15:59:12 -0700 | [diff] [blame] | 985 | availableWidth = measureChildView(mMenuView, availableWidth, exactHeightSpec, 0); |
Adam Powell | 1b4a162 | 2011-05-17 12:13:13 -0700 | [diff] [blame] | 986 | rightOfCenter = Math.max(0, rightOfCenter - mMenuView.getMeasuredWidth()); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 987 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 988 | |
Adam Powell | aa8e509b | 2011-08-25 09:29:21 -0700 | [diff] [blame] | 989 | if (mIndeterminateProgressView != null && |
| 990 | mIndeterminateProgressView.getVisibility() != GONE) { |
| 991 | availableWidth = measureChildView(mIndeterminateProgressView, availableWidth, |
| 992 | childSpecHeight, 0); |
| 993 | rightOfCenter = Math.max(0, |
| 994 | rightOfCenter - mIndeterminateProgressView.getMeasuredWidth()); |
| 995 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 996 | |
Adam Powell | aa8e509b | 2011-08-25 09:29:21 -0700 | [diff] [blame] | 997 | if (mExpandedActionView == null) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 998 | switch (mNavigationMode) { |
| 999 | case ActionBar.NAVIGATION_MODE_LIST: |
| 1000 | if (mListNavLayout != null) { |
| 1001 | final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding; |
| 1002 | availableWidth = Math.max(0, availableWidth - itemPaddingSize); |
| 1003 | leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize); |
| 1004 | mListNavLayout.measure( |
| 1005 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
| 1006 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 1007 | final int listNavWidth = mListNavLayout.getMeasuredWidth(); |
| 1008 | availableWidth = Math.max(0, availableWidth - listNavWidth); |
| 1009 | leftOfCenter = Math.max(0, leftOfCenter - listNavWidth); |
| 1010 | } |
| 1011 | break; |
| 1012 | case ActionBar.NAVIGATION_MODE_TABS: |
| 1013 | if (mTabScrollView != null) { |
| 1014 | final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding; |
| 1015 | availableWidth = Math.max(0, availableWidth - itemPaddingSize); |
| 1016 | leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize); |
| 1017 | mTabScrollView.measure( |
| 1018 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
| 1019 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 1020 | final int tabWidth = mTabScrollView.getMeasuredWidth(); |
| 1021 | availableWidth = Math.max(0, availableWidth - tabWidth); |
| 1022 | leftOfCenter = Math.max(0, leftOfCenter - tabWidth); |
| 1023 | } |
| 1024 | break; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1025 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1028 | View customView = null; |
| 1029 | if (mExpandedActionView != null) { |
| 1030 | customView = mExpandedActionView; |
| 1031 | } else if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && |
| 1032 | mCustomNavView != null) { |
| 1033 | customView = mCustomNavView; |
| 1034 | } |
| 1035 | |
| 1036 | if (customView != null) { |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 1037 | final ViewGroup.LayoutParams lp = generateLayoutParams(customView.getLayoutParams()); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1038 | final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? |
| 1039 | (ActionBar.LayoutParams) lp : null; |
| 1040 | |
| 1041 | int horizontalMargin = 0; |
| 1042 | int verticalMargin = 0; |
| 1043 | if (ablp != null) { |
| 1044 | horizontalMargin = ablp.leftMargin + ablp.rightMargin; |
| 1045 | verticalMargin = ablp.topMargin + ablp.bottomMargin; |
| 1046 | } |
| 1047 | |
| 1048 | // If the action bar is wrapping to its content height, don't allow a custom |
| 1049 | // view to MATCH_PARENT. |
| 1050 | int customNavHeightMode; |
| 1051 | if (mContentHeight <= 0) { |
| 1052 | customNavHeightMode = MeasureSpec.AT_MOST; |
| 1053 | } else { |
| 1054 | customNavHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 1055 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 1056 | } |
| 1057 | final int customNavHeight = Math.max(0, |
| 1058 | (lp.height >= 0 ? Math.min(lp.height, height) : height) - verticalMargin); |
| 1059 | |
| 1060 | final int customNavWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 1061 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 1062 | int customNavWidth = Math.max(0, |
| 1063 | (lp.width >= 0 ? Math.min(lp.width, availableWidth) : availableWidth) |
| 1064 | - horizontalMargin); |
| 1065 | final int hgrav = (ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY) & |
| 1066 | Gravity.HORIZONTAL_GRAVITY_MASK; |
| 1067 | |
| 1068 | // Centering a custom view is treated specially; we try to center within the whole |
| 1069 | // action bar rather than in the available space. |
| 1070 | if (hgrav == Gravity.CENTER_HORIZONTAL && lp.width == LayoutParams.MATCH_PARENT) { |
| 1071 | customNavWidth = Math.min(leftOfCenter, rightOfCenter) * 2; |
| 1072 | } |
| 1073 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1074 | customView.measure( |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1075 | MeasureSpec.makeMeasureSpec(customNavWidth, customNavWidthMode), |
| 1076 | MeasureSpec.makeMeasureSpec(customNavHeight, customNavHeightMode)); |
Adam Powell | aa8e509b | 2011-08-25 09:29:21 -0700 | [diff] [blame] | 1077 | availableWidth -= horizontalMargin + customView.getMeasuredWidth(); |
| 1078 | } |
| 1079 | |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 1080 | /* |
| 1081 | * Measure the whole up container now, allowing for the full home+title sections. |
| 1082 | * (This will re-measure the home view.) |
| 1083 | */ |
| 1084 | availableWidth = measureChildView(mUpGoerFive, availableWidth + homeWidth, |
| 1085 | MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.EXACTLY), 0); |
| 1086 | if (mTitleLayout != null) { |
Adam Powell | aa8e509b | 2011-08-25 09:29:21 -0700 | [diff] [blame] | 1087 | leftOfCenter = Math.max(0, leftOfCenter - mTitleLayout.getMeasuredWidth()); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 1090 | if (mContentHeight <= 0) { |
| 1091 | int measuredHeight = 0; |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 1092 | for (int i = 0; i < childCount; i++) { |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 1093 | View v = getChildAt(i); |
| 1094 | int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 1095 | if (paddedViewHeight > measuredHeight) { |
| 1096 | measuredHeight = paddedViewHeight; |
| 1097 | } |
| 1098 | } |
| 1099 | setMeasuredDimension(contentWidth, measuredHeight); |
| 1100 | } else { |
| 1101 | setMeasuredDimension(contentWidth, maxHeight); |
| 1102 | } |
| 1103 | |
| 1104 | if (mContextView != null) { |
Adam Powell | 425689e | 2011-09-08 18:09:33 -0700 | [diff] [blame] | 1105 | mContextView.setContentHeight(getMeasuredHeight()); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 1106 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 1107 | |
| 1108 | if (mProgressView != null && mProgressView.getVisibility() != GONE) { |
| 1109 | mProgressView.measure(MeasureSpec.makeMeasureSpec( |
| 1110 | contentWidth - mProgressBarPadding * 2, MeasureSpec.EXACTLY), |
| 1111 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST)); |
| 1112 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1115 | @Override |
| 1116 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1117 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
| 1118 | |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 1119 | if (contentHeight <= 0) { |
| 1120 | // Nothing to do if we can't see anything. |
| 1121 | return; |
| 1122 | } |
| 1123 | |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1124 | final boolean isLayoutRtl = isLayoutRtl(); |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1125 | final int direction = isLayoutRtl ? 1 : -1; |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1126 | int menuStart = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight(); |
| 1127 | // In LTR mode, we start from left padding and go to the right; in RTL mode, we start |
| 1128 | // from the padding right and go to the left (in reverse way) |
| 1129 | int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft(); |
| 1130 | final int y = getPaddingTop(); |
| 1131 | |
Adam Powell | b1e2f25 | 2011-08-22 17:42:12 -0700 | [diff] [blame] | 1132 | HomeView homeLayout = mExpandedActionView != null ? mExpandedHomeLayout : mHomeLayout; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1133 | final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE && |
| 1134 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0; |
| 1135 | int startOffset = 0; |
| 1136 | if (homeLayout.getParent() == mUpGoerFive) { |
| 1137 | if (homeLayout.getVisibility() != GONE) { |
| 1138 | startOffset = homeLayout.getStartOffset(); |
| 1139 | } else if (showTitle) { |
| 1140 | startOffset = homeLayout.getUpWidth(); |
| 1141 | } |
| 1142 | } |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 1143 | |
| 1144 | // Position the up container based on where the edge of the home layout should go. |
| 1145 | x += positionChild(mUpGoerFive, |
| 1146 | next(x, startOffset, isLayoutRtl), y, contentHeight, isLayoutRtl); |
| 1147 | x = next(x, startOffset, isLayoutRtl); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1148 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1149 | if (mExpandedActionView == null) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1150 | switch (mNavigationMode) { |
| 1151 | case ActionBar.NAVIGATION_MODE_STANDARD: |
| 1152 | break; |
| 1153 | case ActionBar.NAVIGATION_MODE_LIST: |
| 1154 | if (mListNavLayout != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1155 | if (showTitle) { |
| 1156 | x = next(x, mItemPadding, isLayoutRtl); |
| 1157 | } |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1158 | x += positionChild(mListNavLayout, x, y, contentHeight, isLayoutRtl); |
| 1159 | x = next(x, mItemPadding, isLayoutRtl); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1160 | } |
| 1161 | break; |
| 1162 | case ActionBar.NAVIGATION_MODE_TABS: |
| 1163 | if (mTabScrollView != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1164 | if (showTitle) x = next(x, mItemPadding, isLayoutRtl); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1165 | x += positionChild(mTabScrollView, x, y, contentHeight, isLayoutRtl); |
| 1166 | x = next(x, mItemPadding, isLayoutRtl); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1167 | } |
| 1168 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 1169 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Adam Powell | 9b4bee0 | 2011-04-27 19:24:47 -0700 | [diff] [blame] | 1172 | if (mMenuView != null && mMenuView.getParent() == this) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1173 | positionChild(mMenuView, menuStart, y, contentHeight, !isLayoutRtl); |
| 1174 | menuStart += direction * mMenuView.getMeasuredWidth(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 1177 | if (mIndeterminateProgressView != null && |
| 1178 | mIndeterminateProgressView.getVisibility() != GONE) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1179 | positionChild(mIndeterminateProgressView, menuStart, y, contentHeight, !isLayoutRtl); |
| 1180 | menuStart += direction * mIndeterminateProgressView.getMeasuredWidth(); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1183 | View customView = null; |
| 1184 | if (mExpandedActionView != null) { |
| 1185 | customView = mExpandedActionView; |
| 1186 | } else if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && |
| 1187 | mCustomNavView != null) { |
| 1188 | customView = mCustomNavView; |
| 1189 | } |
| 1190 | if (customView != null) { |
Fabrice Di Meglio | 6bf6eb7 | 2012-10-10 15:30:28 -0700 | [diff] [blame] | 1191 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | bb4b601 | 2012-10-26 16:27:55 -0700 | [diff] [blame] | 1192 | ViewGroup.LayoutParams lp = customView.getLayoutParams(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1193 | final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? |
| 1194 | (ActionBar.LayoutParams) lp : null; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1195 | final int gravity = ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1196 | final int navWidth = customView.getMeasuredWidth(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1197 | |
| 1198 | int topMargin = 0; |
| 1199 | int bottomMargin = 0; |
| 1200 | if (ablp != null) { |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1201 | x = next(x, ablp.getMarginStart(), isLayoutRtl); |
| 1202 | menuStart += direction * ablp.getMarginEnd(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1203 | topMargin = ablp.topMargin; |
| 1204 | bottomMargin = ablp.bottomMargin; |
| 1205 | } |
| 1206 | |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1207 | int hgravity = gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1208 | // See if we actually have room to truly center; if not push against left or right. |
| 1209 | if (hgravity == Gravity.CENTER_HORIZONTAL) { |
| 1210 | final int centeredLeft = ((mRight - mLeft) - navWidth) / 2; |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1211 | if (isLayoutRtl) { |
| 1212 | final int centeredStart = centeredLeft + navWidth; |
| 1213 | final int centeredEnd = centeredLeft; |
| 1214 | if (centeredStart > x) { |
| 1215 | hgravity = Gravity.RIGHT; |
| 1216 | } else if (centeredEnd < menuStart) { |
| 1217 | hgravity = Gravity.LEFT; |
| 1218 | } |
| 1219 | } else { |
| 1220 | final int centeredStart = centeredLeft; |
| 1221 | final int centeredEnd = centeredLeft + navWidth; |
| 1222 | if (centeredStart < x) { |
| 1223 | hgravity = Gravity.LEFT; |
| 1224 | } else if (centeredEnd > menuStart) { |
| 1225 | hgravity = Gravity.RIGHT; |
| 1226 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1227 | } |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1228 | } else if (gravity == Gravity.NO_GRAVITY) { |
| 1229 | hgravity = Gravity.START; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | int xpos = 0; |
Fabrice Di Meglio | 6bf6eb7 | 2012-10-10 15:30:28 -0700 | [diff] [blame] | 1233 | switch (Gravity.getAbsoluteGravity(hgravity, layoutDirection)) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1234 | case Gravity.CENTER_HORIZONTAL: |
| 1235 | xpos = ((mRight - mLeft) - navWidth) / 2; |
| 1236 | break; |
| 1237 | case Gravity.LEFT: |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1238 | xpos = isLayoutRtl ? menuStart : x; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1239 | break; |
| 1240 | case Gravity.RIGHT: |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1241 | xpos = isLayoutRtl ? x - navWidth : menuStart - navWidth; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | |
Adam Powell | 7a2424d | 2011-08-18 11:59:11 -0700 | [diff] [blame] | 1245 | int vgravity = gravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1246 | |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1247 | if (gravity == Gravity.NO_GRAVITY) { |
Adam Powell | 7a2424d | 2011-08-18 11:59:11 -0700 | [diff] [blame] | 1248 | vgravity = Gravity.CENTER_VERTICAL; |
| 1249 | } |
| 1250 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1251 | int ypos = 0; |
Adam Powell | 7a2424d | 2011-08-18 11:59:11 -0700 | [diff] [blame] | 1252 | switch (vgravity) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1253 | case Gravity.CENTER_VERTICAL: |
Adam Powell | 6556c07 | 2011-06-24 17:16:57 -0700 | [diff] [blame] | 1254 | final int paddedTop = getPaddingTop(); |
| 1255 | final int paddedBottom = mBottom - mTop - getPaddingBottom(); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1256 | ypos = ((paddedBottom - paddedTop) - customView.getMeasuredHeight()) / 2; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | case Gravity.TOP: |
| 1259 | ypos = getPaddingTop() + topMargin; |
| 1260 | break; |
| 1261 | case Gravity.BOTTOM: |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1262 | ypos = getHeight() - getPaddingBottom() - customView.getMeasuredHeight() |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 1263 | - bottomMargin; |
| 1264 | break; |
| 1265 | } |
Adam Powell | 6556c07 | 2011-06-24 17:16:57 -0700 | [diff] [blame] | 1266 | final int customWidth = customView.getMeasuredWidth(); |
| 1267 | customView.layout(xpos, ypos, xpos + customWidth, |
| 1268 | ypos + customView.getMeasuredHeight()); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1269 | x = next(x, customWidth, isLayoutRtl); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1270 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 1271 | |
| 1272 | if (mProgressView != null) { |
| 1273 | mProgressView.bringToFront(); |
| 1274 | final int halfProgressHeight = mProgressView.getMeasuredHeight() / 2; |
| 1275 | mProgressView.layout(mProgressBarPadding, -halfProgressHeight, |
| 1276 | mProgressBarPadding + mProgressView.getMeasuredWidth(), halfProgressHeight); |
| 1277 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1280 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 1281 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
Adam Powell | aad47fd | 2011-08-17 17:33:33 -0700 | [diff] [blame] | 1282 | return new ActionBar.LayoutParams(getContext(), attrs); |
| 1283 | } |
| 1284 | |
| 1285 | @Override |
Adam Powell | 9a5cc28 | 2011-08-28 16:18:16 -0700 | [diff] [blame] | 1286 | public ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1287 | if (lp == null) { |
| 1288 | lp = generateDefaultLayoutParams(); |
| 1289 | } |
| 1290 | return lp; |
| 1291 | } |
| 1292 | |
| 1293 | @Override |
| 1294 | public Parcelable onSaveInstanceState() { |
| 1295 | Parcelable superState = super.onSaveInstanceState(); |
| 1296 | SavedState state = new SavedState(superState); |
| 1297 | |
| 1298 | if (mExpandedMenuPresenter != null && mExpandedMenuPresenter.mCurrentExpandedItem != null) { |
| 1299 | state.expandedMenuItemId = mExpandedMenuPresenter.mCurrentExpandedItem.getItemId(); |
| 1300 | } |
| 1301 | |
| 1302 | state.isOverflowOpen = isOverflowMenuShowing(); |
| 1303 | |
| 1304 | return state; |
| 1305 | } |
| 1306 | |
| 1307 | @Override |
| 1308 | public void onRestoreInstanceState(Parcelable p) { |
| 1309 | SavedState state = (SavedState) p; |
| 1310 | |
| 1311 | super.onRestoreInstanceState(state.getSuperState()); |
| 1312 | |
| 1313 | if (state.expandedMenuItemId != 0 && |
| 1314 | mExpandedMenuPresenter != null && mOptionsMenu != null) { |
| 1315 | final MenuItem item = mOptionsMenu.findItem(state.expandedMenuItemId); |
| 1316 | if (item != null) { |
| 1317 | item.expandActionView(); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | if (state.isOverflowOpen) { |
| 1322 | postShowOverflowMenu(); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1326 | public void setHomeAsUpIndicator(Drawable indicator) { |
| 1327 | mHomeLayout.setUpIndicator(indicator); |
| 1328 | } |
| 1329 | |
| 1330 | public void setHomeAsUpIndicator(int resId) { |
| 1331 | mHomeLayout.setUpIndicator(resId); |
| 1332 | } |
| 1333 | |
| 1334 | public void setHomeActionContentDescription(CharSequence description) { |
| 1335 | mHomeDescription = description; |
Adam Powell | 2229ca0 | 2013-11-12 16:20:46 -0800 | [diff] [blame] | 1336 | updateHomeAccessibility(mUpGoerFive.isEnabled()); |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | public void setHomeActionContentDescription(int resId) { |
| 1340 | mHomeDescriptionRes = resId; |
Adam Powell | 2eeb419 | 2013-04-18 13:53:27 -0700 | [diff] [blame] | 1341 | mHomeDescription = resId != 0 ? getResources().getText(resId) : null; |
Adam Powell | 2229ca0 | 2013-11-12 16:20:46 -0800 | [diff] [blame] | 1342 | updateHomeAccessibility(mUpGoerFive.isEnabled()); |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1345 | static class SavedState extends BaseSavedState { |
| 1346 | int expandedMenuItemId; |
| 1347 | boolean isOverflowOpen; |
| 1348 | |
| 1349 | SavedState(Parcelable superState) { |
| 1350 | super(superState); |
| 1351 | } |
| 1352 | |
| 1353 | private SavedState(Parcel in) { |
| 1354 | super(in); |
| 1355 | expandedMenuItemId = in.readInt(); |
| 1356 | isOverflowOpen = in.readInt() != 0; |
| 1357 | } |
| 1358 | |
| 1359 | @Override |
| 1360 | public void writeToParcel(Parcel out, int flags) { |
| 1361 | super.writeToParcel(out, flags); |
| 1362 | out.writeInt(expandedMenuItemId); |
| 1363 | out.writeInt(isOverflowOpen ? 1 : 0); |
| 1364 | } |
| 1365 | |
| 1366 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1367 | new Parcelable.Creator<SavedState>() { |
| 1368 | public SavedState createFromParcel(Parcel in) { |
| 1369 | return new SavedState(in); |
| 1370 | } |
| 1371 | |
| 1372 | public SavedState[] newArray(int size) { |
| 1373 | return new SavedState[size]; |
| 1374 | } |
| 1375 | }; |
| 1376 | } |
| 1377 | |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1378 | private static class HomeView extends FrameLayout { |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1379 | private ImageView mUpView; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1380 | private ImageView mIconView; |
Adam Powell | b1e2f25 | 2011-08-22 17:42:12 -0700 | [diff] [blame] | 1381 | private int mUpWidth; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1382 | private int mStartOffset; |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1383 | private int mUpIndicatorRes; |
| 1384 | private Drawable mDefaultUpIndicator; |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1385 | |
Adam Powell | fd75eef | 2012-08-02 10:27:31 -0700 | [diff] [blame] | 1386 | private static final long DEFAULT_TRANSITION_DURATION = 150; |
| 1387 | |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1388 | public HomeView(Context context) { |
| 1389 | this(context, null); |
| 1390 | } |
| 1391 | |
| 1392 | public HomeView(Context context, AttributeSet attrs) { |
| 1393 | super(context, attrs); |
Adam Powell | fd75eef | 2012-08-02 10:27:31 -0700 | [diff] [blame] | 1394 | LayoutTransition t = getLayoutTransition(); |
| 1395 | if (t != null) { |
| 1396 | // Set a lower duration than the default |
| 1397 | t.setDuration(DEFAULT_TRANSITION_DURATION); |
| 1398 | } |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1401 | public void setShowUp(boolean isUp) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1402 | mUpView.setVisibility(isUp ? VISIBLE : GONE); |
| 1403 | } |
| 1404 | |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1405 | public void setShowIcon(boolean showIcon) { |
| 1406 | mIconView.setVisibility(showIcon ? VISIBLE : GONE); |
| 1407 | } |
| 1408 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1409 | public void setIcon(Drawable icon) { |
| 1410 | mIconView.setImageDrawable(icon); |
| 1411 | } |
| 1412 | |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1413 | public void setUpIndicator(Drawable d) { |
| 1414 | mUpView.setImageDrawable(d != null ? d : mDefaultUpIndicator); |
| 1415 | mUpIndicatorRes = 0; |
| 1416 | } |
| 1417 | |
| 1418 | public void setUpIndicator(int resId) { |
| 1419 | mUpIndicatorRes = resId; |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 1420 | mUpView.setImageDrawable(resId != 0 ? getContext().getDrawable(resId) : null); |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | @Override |
| 1424 | protected void onConfigurationChanged(Configuration newConfig) { |
| 1425 | super.onConfigurationChanged(newConfig); |
| 1426 | if (mUpIndicatorRes != 0) { |
| 1427 | // Reload for config change |
| 1428 | setUpIndicator(mUpIndicatorRes); |
| 1429 | } |
| 1430 | } |
| 1431 | |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1432 | @Override |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 1433 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1434 | onPopulateAccessibilityEvent(event); |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | @Override |
| 1439 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1440 | super.onPopulateAccessibilityEvent(event); |
| 1441 | final CharSequence cdesc = getContentDescription(); |
| 1442 | if (!TextUtils.isEmpty(cdesc)) { |
| 1443 | event.getText().add(cdesc); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | @Override |
| 1448 | public boolean dispatchHoverEvent(MotionEvent event) { |
| 1449 | // Don't allow children to hover; we want this to be treated as a single component. |
| 1450 | return onHoverEvent(event); |
| 1451 | } |
| 1452 | |
| 1453 | @Override |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1454 | protected void onFinishInflate() { |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1455 | mUpView = (ImageView) findViewById(com.android.internal.R.id.up); |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1456 | mIconView = (ImageView) findViewById(com.android.internal.R.id.home); |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 1457 | mDefaultUpIndicator = mUpView.getDrawable(); |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1460 | public int getStartOffset() { |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1461 | return mUpView.getVisibility() == GONE ? mStartOffset : 0; |
| 1462 | } |
| 1463 | |
| 1464 | public int getUpWidth() { |
| 1465 | return mUpWidth; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1468 | @Override |
| 1469 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1470 | measureChildWithMargins(mUpView, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 1471 | final LayoutParams upLp = (LayoutParams) mUpView.getLayoutParams(); |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1472 | final int upMargins = upLp.leftMargin + upLp.rightMargin; |
| 1473 | mUpWidth = mUpView.getMeasuredWidth(); |
| 1474 | mStartOffset = mUpWidth + upMargins; |
| 1475 | int width = mUpView.getVisibility() == GONE ? 0 : mStartOffset; |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1476 | int height = upLp.topMargin + mUpView.getMeasuredHeight() + upLp.bottomMargin; |
Adam Powell | ad79b90 | 2013-06-19 11:33:28 -0700 | [diff] [blame] | 1477 | |
| 1478 | if (mIconView.getVisibility() != GONE) { |
| 1479 | measureChildWithMargins(mIconView, widthMeasureSpec, width, heightMeasureSpec, 0); |
| 1480 | final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); |
| 1481 | width += iconLp.leftMargin + mIconView.getMeasuredWidth() + iconLp.rightMargin; |
| 1482 | height = Math.max(height, |
| 1483 | iconLp.topMargin + mIconView.getMeasuredHeight() + iconLp.bottomMargin); |
| 1484 | } else if (upMargins < 0) { |
| 1485 | // Remove the measurement effects of negative margins used for offsets |
| 1486 | width -= upMargins; |
| 1487 | } |
Adam Powell | 0d5d1b7e | 2011-06-22 17:40:40 -0700 | [diff] [blame] | 1488 | |
| 1489 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1490 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1491 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 1492 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 1493 | |
| 1494 | switch (widthMode) { |
| 1495 | case MeasureSpec.AT_MOST: |
| 1496 | width = Math.min(width, widthSize); |
| 1497 | break; |
| 1498 | case MeasureSpec.EXACTLY: |
| 1499 | width = widthSize; |
| 1500 | break; |
| 1501 | case MeasureSpec.UNSPECIFIED: |
| 1502 | default: |
| 1503 | break; |
| 1504 | } |
| 1505 | switch (heightMode) { |
| 1506 | case MeasureSpec.AT_MOST: |
| 1507 | height = Math.min(height, heightSize); |
| 1508 | break; |
| 1509 | case MeasureSpec.EXACTLY: |
| 1510 | height = heightSize; |
| 1511 | break; |
| 1512 | case MeasureSpec.UNSPECIFIED: |
| 1513 | default: |
| 1514 | break; |
| 1515 | } |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1516 | setMeasuredDimension(width, height); |
| 1517 | } |
| 1518 | |
| 1519 | @Override |
| 1520 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 1521 | final int vCenter = (b - t) / 2; |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1522 | final boolean isLayoutRtl = isLayoutRtl(); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1523 | final int width = getWidth(); |
Adam Powell | c8b0b00 | 2011-06-23 14:39:05 -0700 | [diff] [blame] | 1524 | int upOffset = 0; |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1525 | if (mUpView.getVisibility() != GONE) { |
| 1526 | final LayoutParams upLp = (LayoutParams) mUpView.getLayoutParams(); |
| 1527 | final int upHeight = mUpView.getMeasuredHeight(); |
| 1528 | final int upWidth = mUpView.getMeasuredWidth(); |
Adam Powell | c8b0b00 | 2011-06-23 14:39:05 -0700 | [diff] [blame] | 1529 | upOffset = upLp.leftMargin + upWidth + upLp.rightMargin; |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1530 | final int upTop = vCenter - upHeight / 2; |
| 1531 | final int upBottom = upTop + upHeight; |
| 1532 | final int upRight; |
| 1533 | final int upLeft; |
| 1534 | if (isLayoutRtl) { |
| 1535 | upRight = width; |
| 1536 | upLeft = upRight - upWidth; |
| 1537 | r -= upOffset; |
| 1538 | } else { |
| 1539 | upRight = upWidth; |
| 1540 | upLeft = 0; |
| 1541 | l += upOffset; |
| 1542 | } |
| 1543 | mUpView.layout(upLeft, upTop, upRight, upBottom); |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1544 | } |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1545 | |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1546 | final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); |
| 1547 | final int iconHeight = mIconView.getMeasuredHeight(); |
| 1548 | final int iconWidth = mIconView.getMeasuredWidth(); |
| 1549 | final int hCenter = (r - l) / 2; |
Adam Powell | c8b0b00 | 2011-06-23 14:39:05 -0700 | [diff] [blame] | 1550 | final int iconTop = Math.max(iconLp.topMargin, vCenter - iconHeight / 2); |
Fabrice Di Meglio | cf1ba02 | 2012-06-25 15:49:11 -0700 | [diff] [blame] | 1551 | final int iconBottom = iconTop + iconHeight; |
| 1552 | final int iconLeft; |
| 1553 | final int iconRight; |
| 1554 | int marginStart = iconLp.getMarginStart(); |
| 1555 | final int delta = Math.max(marginStart, hCenter - iconWidth / 2); |
| 1556 | if (isLayoutRtl) { |
| 1557 | iconRight = width - upOffset - delta; |
| 1558 | iconLeft = iconRight - iconWidth; |
| 1559 | } else { |
| 1560 | iconLeft = upOffset + delta; |
| 1561 | iconRight = iconLeft + iconWidth; |
| 1562 | } |
| 1563 | mIconView.layout(iconLeft, iconTop, iconRight, iconBottom); |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 1564 | } |
| 1565 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1566 | |
| 1567 | private class ExpandedActionViewMenuPresenter implements MenuPresenter { |
| 1568 | MenuBuilder mMenu; |
| 1569 | MenuItemImpl mCurrentExpandedItem; |
| 1570 | |
| 1571 | @Override |
| 1572 | public void initForMenu(Context context, MenuBuilder menu) { |
| 1573 | // Clear the expanded action view when menus change. |
Adam Powell | 6b0e97c | 2011-08-03 12:06:32 -0700 | [diff] [blame] | 1574 | if (mMenu != null && mCurrentExpandedItem != null) { |
| 1575 | mMenu.collapseItemActionView(mCurrentExpandedItem); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1576 | } |
| 1577 | mMenu = menu; |
| 1578 | } |
| 1579 | |
| 1580 | @Override |
| 1581 | public MenuView getMenuView(ViewGroup root) { |
| 1582 | return null; |
| 1583 | } |
| 1584 | |
| 1585 | @Override |
| 1586 | public void updateMenuView(boolean cleared) { |
| 1587 | // Make sure the expanded item we have is still there. |
| 1588 | if (mCurrentExpandedItem != null) { |
| 1589 | boolean found = false; |
Adam Powell | f35d049 | 2011-08-17 13:56:47 -0700 | [diff] [blame] | 1590 | |
| 1591 | if (mMenu != null) { |
| 1592 | final int count = mMenu.size(); |
| 1593 | for (int i = 0; i < count; i++) { |
| 1594 | final MenuItem item = mMenu.getItem(i); |
| 1595 | if (item == mCurrentExpandedItem) { |
| 1596 | found = true; |
| 1597 | break; |
| 1598 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if (!found) { |
| 1603 | // The item we had expanded disappeared. Collapse. |
| 1604 | collapseItemActionView(mMenu, mCurrentExpandedItem); |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | @Override |
| 1610 | public void setCallback(Callback cb) { |
| 1611 | } |
| 1612 | |
| 1613 | @Override |
| 1614 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
| 1618 | @Override |
| 1619 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 1620 | } |
| 1621 | |
| 1622 | @Override |
| 1623 | public boolean flagActionItems() { |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | @Override |
| 1628 | public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 1629 | ActionBarTransition.beginDelayedTransition(ActionBarView.this); |
Adam Powell | c46d333 | 2013-09-17 10:08:41 -0700 | [diff] [blame] | 1630 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1631 | mExpandedActionView = item.getActionView(); |
Adam Powell | 28fa07e | 2011-07-15 20:17:25 -0700 | [diff] [blame] | 1632 | mExpandedHomeLayout.setIcon(mIcon.getConstantState().newDrawable(getResources())); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1633 | mCurrentExpandedItem = item; |
| 1634 | if (mExpandedActionView.getParent() != ActionBarView.this) { |
| 1635 | addView(mExpandedActionView); |
| 1636 | } |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 1637 | if (mExpandedHomeLayout.getParent() != mUpGoerFive) { |
| 1638 | mUpGoerFive.addView(mExpandedHomeLayout); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1639 | } |
| 1640 | mHomeLayout.setVisibility(GONE); |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 1641 | if (mTitleLayout != null) mTitleLayout.setVisibility(GONE); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1642 | if (mTabScrollView != null) mTabScrollView.setVisibility(GONE); |
| 1643 | if (mSpinner != null) mSpinner.setVisibility(GONE); |
| 1644 | if (mCustomNavView != null) mCustomNavView.setVisibility(GONE); |
Adam Powell | 07cf971 | 2013-02-05 16:13:57 -0800 | [diff] [blame] | 1645 | setHomeButtonEnabled(false, false); |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1646 | requestLayout(); |
| 1647 | item.setActionViewExpanded(true); |
Adam Powell | 038f1c8 | 2011-07-21 14:28:10 -0700 | [diff] [blame] | 1648 | |
| 1649 | if (mExpandedActionView instanceof CollapsibleActionView) { |
| 1650 | ((CollapsibleActionView) mExpandedActionView).onActionViewExpanded(); |
| 1651 | } |
Amith Yamasani | 10da590 | 2011-07-26 16:14:26 -0700 | [diff] [blame] | 1652 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1653 | return true; |
| 1654 | } |
| 1655 | |
| 1656 | @Override |
| 1657 | public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) { |
Chet Haase | d8d7c38 | 2013-09-23 11:26:36 -0700 | [diff] [blame] | 1658 | ActionBarTransition.beginDelayedTransition(ActionBarView.this); |
Adam Powell | c46d333 | 2013-09-17 10:08:41 -0700 | [diff] [blame] | 1659 | |
Amith Yamasani | 10da590 | 2011-07-26 16:14:26 -0700 | [diff] [blame] | 1660 | // Do this before detaching the actionview from the hierarchy, in case |
| 1661 | // it needs to dismiss the soft keyboard, etc. |
| 1662 | if (mExpandedActionView instanceof CollapsibleActionView) { |
| 1663 | ((CollapsibleActionView) mExpandedActionView).onActionViewCollapsed(); |
| 1664 | } |
| 1665 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1666 | removeView(mExpandedActionView); |
Adam Powell | 27cba38 | 2013-01-22 18:55:20 -0800 | [diff] [blame] | 1667 | mUpGoerFive.removeView(mExpandedHomeLayout); |
Adam Powell | b0e217e | 2011-08-12 11:36:51 -0700 | [diff] [blame] | 1668 | mExpandedActionView = null; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1669 | if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0) { |
| 1670 | mHomeLayout.setVisibility(VISIBLE); |
| 1671 | } |
| 1672 | if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
Adam Powell | 15a16a8 | 2011-06-20 10:35:27 -0700 | [diff] [blame] | 1673 | if (mTitleLayout == null) { |
| 1674 | initTitle(); |
| 1675 | } else { |
| 1676 | mTitleLayout.setVisibility(VISIBLE); |
| 1677 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1678 | } |
Adam Powell | 6684e92 | 2013-03-26 15:38:26 -0700 | [diff] [blame] | 1679 | if (mTabScrollView != null) mTabScrollView.setVisibility(VISIBLE); |
| 1680 | if (mSpinner != null) mSpinner.setVisibility(VISIBLE); |
| 1681 | if (mCustomNavView != null) mCustomNavView.setVisibility(VISIBLE); |
| 1682 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1683 | mExpandedHomeLayout.setIcon(null); |
| 1684 | mCurrentExpandedItem = null; |
Adam Powell | 62f3303 | 2013-02-04 12:44:34 -0800 | [diff] [blame] | 1685 | setHomeButtonEnabled(mWasHomeEnabled); // Set by expandItemActionView above |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1686 | requestLayout(); |
| 1687 | item.setActionViewExpanded(false); |
Adam Powell | 038f1c8 | 2011-07-21 14:28:10 -0700 | [diff] [blame] | 1688 | |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1689 | return true; |
| 1690 | } |
Adam Powell | 11ed1d6 | 2011-07-11 21:19:59 -0700 | [diff] [blame] | 1691 | |
| 1692 | @Override |
| 1693 | public int getId() { |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | @Override |
| 1698 | public Parcelable onSaveInstanceState() { |
| 1699 | return null; |
| 1700 | } |
| 1701 | |
| 1702 | @Override |
| 1703 | public void onRestoreInstanceState(Parcelable state) { |
| 1704 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 1705 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1706 | } |