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 | |
| 17 | package android.view; |
| 18 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | import com.android.internal.view.menu.ActionMenu; |
| 21 | import com.android.internal.view.menu.ActionMenuItem; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame^] | 22 | import com.android.internal.view.menu.ActionMenuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 23 | import com.android.internal.view.menu.MenuBuilder; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 24 | |
| 25 | import android.app.ActionBar; |
| 26 | import android.app.Activity; |
| 27 | import android.app.ActionBar.Callback; |
| 28 | import android.content.Context; |
| 29 | import android.content.pm.ApplicationInfo; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.res.TypedArray; |
| 32 | import android.graphics.PorterDuff; |
| 33 | import android.graphics.PorterDuffColorFilter; |
| 34 | import android.graphics.drawable.Drawable; |
| 35 | import android.util.AttributeSet; |
| 36 | import android.util.DisplayMetrics; |
| 37 | import android.util.SparseArray; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 38 | import android.widget.AdapterView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 39 | import android.widget.ImageView; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 40 | import android.widget.Spinner; |
| 41 | import android.widget.SpinnerAdapter; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 42 | import android.widget.TextView; |
| 43 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 44 | /** |
| 45 | * @hide |
| 46 | */ |
| 47 | public class ActionBarView extends ViewGroup { |
| 48 | private static final String TAG = "ActionBarView"; |
| 49 | |
| 50 | // TODO: This must be defined in the default theme |
| 51 | private static final int CONTENT_HEIGHT_DIP = 50; |
| 52 | private static final int CONTENT_PADDING_DIP = 3; |
| 53 | private static final int CONTENT_SPACING_DIP = 6; |
| 54 | private static final int CONTENT_ACTION_SPACING_DIP = 12; |
| 55 | |
| 56 | /** |
| 57 | * Display options applied by default |
| 58 | */ |
| 59 | public static final int DISPLAY_DEFAULT = 0; |
| 60 | |
| 61 | /** |
| 62 | * Display options that require re-layout as opposed to a simple invalidate |
| 63 | */ |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 64 | private static final int DISPLAY_RELAYOUT_MASK = |
| 65 | ActionBar.DISPLAY_HIDE_HOME | |
| 66 | ActionBar.DISPLAY_USE_LOGO; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 67 | |
| 68 | private final int mContentHeight; |
| 69 | |
| 70 | private int mNavigationMode; |
| 71 | private int mDisplayOptions; |
| 72 | private int mSpacing; |
| 73 | private int mActionSpacing; |
| 74 | private CharSequence mTitle; |
| 75 | private CharSequence mSubtitle; |
| 76 | private Drawable mIcon; |
| 77 | private Drawable mLogo; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 78 | |
| 79 | private ImageView mIconView; |
| 80 | private ImageView mLogoView; |
| 81 | private TextView mTitleView; |
| 82 | private TextView mSubtitleView; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 83 | private Spinner mSpinner; |
| 84 | private View mCustomNavView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 85 | |
| 86 | private boolean mShowMenu; |
| 87 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 88 | private MenuBuilder mOptionsMenu; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame^] | 89 | private ActionMenuView mMenuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 90 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 91 | private ActionMenuItem mLogoNavItem; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 92 | |
| 93 | private SparseArray<ActionMenu> mContextMenus; |
| 94 | |
| 95 | private Callback mCallback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 96 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 97 | private final AdapterView.OnItemSelectedListener mNavItemSelectedListener = |
| 98 | new AdapterView.OnItemSelectedListener() { |
| 99 | public void onItemSelected(AdapterView parent, View view, int position, long id) { |
| 100 | if (mCallback != null) { |
| 101 | mCallback.onNavigationItemSelected(position, id); |
| 102 | } |
| 103 | } |
| 104 | public void onNothingSelected(AdapterView parent) { |
| 105 | // Do nothing |
| 106 | } |
| 107 | }; |
| 108 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 109 | private OnClickListener mHomeClickListener = null; |
| 110 | |
| 111 | public ActionBarView(Context context, AttributeSet attrs) { |
| 112 | super(context, attrs); |
| 113 | |
| 114 | final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 115 | mContentHeight = (int) (CONTENT_HEIGHT_DIP * metrics.density + 0.5f); |
| 116 | |
| 117 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar); |
| 118 | |
| 119 | final int colorFilter = a.getColor(R.styleable.ActionBar_colorFilter, 0); |
| 120 | |
| 121 | if (colorFilter != 0) { |
| 122 | final Drawable d = getBackground(); |
| 123 | d.setDither(true); |
| 124 | d.setColorFilter(new PorterDuffColorFilter(colorFilter, PorterDuff.Mode.OVERLAY)); |
| 125 | } |
| 126 | |
| 127 | ApplicationInfo info = context.getApplicationInfo(); |
| 128 | PackageManager pm = context.getPackageManager(); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 129 | mNavigationMode = a.getInt(R.styleable.ActionBar_navigationMode, ActionBar.NAVIGATION_MODE_STANDARD); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 130 | mTitle = a.getText(R.styleable.ActionBar_title); |
| 131 | mSubtitle = a.getText(R.styleable.ActionBar_subtitle); |
| 132 | mDisplayOptions = a.getInt(R.styleable.ActionBar_displayOptions, DISPLAY_DEFAULT); |
| 133 | |
| 134 | mLogo = a.getDrawable(R.styleable.ActionBar_logo); |
| 135 | if (mLogo == null) { |
| 136 | mLogo = info.loadLogo(pm); |
| 137 | } |
| 138 | mIcon = a.getDrawable(R.styleable.ActionBar_icon); |
| 139 | if (mIcon == null) { |
| 140 | mIcon = info.loadIcon(pm); |
| 141 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 142 | |
| 143 | Drawable background = a.getDrawable(R.styleable.ActionBar_background); |
| 144 | if (background != null) { |
| 145 | setBackgroundDrawable(background); |
| 146 | } |
| 147 | |
| 148 | final int customNavId = a.getResourceId(R.styleable.ActionBar_customNavigationLayout, 0); |
| 149 | if (customNavId != 0) { |
| 150 | LayoutInflater inflater = LayoutInflater.from(context); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 151 | mCustomNavView = (View) inflater.inflate(customNavId, null); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 152 | mNavigationMode = ActionBar.NAVIGATION_MODE_CUSTOM; |
| 153 | } |
| 154 | |
| 155 | a.recycle(); |
| 156 | |
| 157 | // TODO: Set this in the theme |
| 158 | int padding = (int) (CONTENT_PADDING_DIP * metrics.density + 0.5f); |
| 159 | setPadding(padding, padding, padding, padding); |
| 160 | |
| 161 | mSpacing = (int) (CONTENT_SPACING_DIP * metrics.density + 0.5f); |
| 162 | mActionSpacing = (int) (CONTENT_ACTION_SPACING_DIP * metrics.density + 0.5f); |
| 163 | |
| 164 | if (mLogo != null || mIcon != null || mTitle != null) { |
| 165 | mLogoNavItem = new ActionMenuItem(context, 0, android.R.id.home, 0, 0, mTitle); |
| 166 | mHomeClickListener = new OnClickListener() { |
| 167 | public void onClick(View v) { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 168 | Context context = getContext(); |
| 169 | if (context instanceof Activity) { |
| 170 | Activity activity = (Activity) context; |
| 171 | activity.onOptionsItemSelected(mLogoNavItem); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | mContextMenus = new SparseArray<ActionMenu>(); |
| 178 | } |
| 179 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 180 | public void setCallback(Callback callback) { |
| 181 | mCallback = callback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 184 | public void setMenu(Menu menu) { |
| 185 | MenuBuilder builder = (MenuBuilder) menu; |
| 186 | mOptionsMenu = builder; |
| 187 | if (mMenuView != null) { |
| 188 | removeView(mMenuView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 189 | } |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame^] | 190 | final ActionMenuView menuView = (ActionMenuView) builder.getMenuView( |
| 191 | MenuBuilder.TYPE_ACTION_BUTTON, null); |
| 192 | mActionSpacing = menuView.getItemMargin(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 193 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 194 | LayoutParams.MATCH_PARENT); |
| 195 | menuView.setLayoutParams(layoutParams); |
| 196 | addView(menuView); |
| 197 | mMenuView = menuView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | public void setCustomNavigationView(View view) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 201 | mCustomNavView = view; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 202 | if (view != null) { |
| 203 | setNavigationMode(ActionBar.NAVIGATION_MODE_CUSTOM); |
| 204 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 207 | public CharSequence getTitle() { |
| 208 | return mTitle; |
| 209 | } |
| 210 | |
| 211 | public void setTitle(CharSequence title) { |
| 212 | mTitle = title; |
| 213 | if (mTitleView != null) { |
| 214 | mTitleView.setText(title); |
| 215 | } |
| 216 | if (mLogoNavItem != null) { |
| 217 | mLogoNavItem.setTitle(title); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public CharSequence getSubtitle() { |
| 222 | return mSubtitle; |
| 223 | } |
| 224 | |
| 225 | public void setSubtitle(CharSequence subtitle) { |
| 226 | mSubtitle = subtitle; |
| 227 | if (mSubtitleView != null) { |
| 228 | mSubtitleView.setText(subtitle); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | public void setDisplayOptions(int options) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 233 | final int flagsChanged = options ^ mDisplayOptions; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 234 | mDisplayOptions = options; |
| 235 | if ((flagsChanged & DISPLAY_RELAYOUT_MASK) != 0) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 236 | final int vis = (options & ActionBar.DISPLAY_HIDE_HOME) != 0 ? GONE : VISIBLE; |
| 237 | if (mLogoView != null) { |
| 238 | mLogoView.setVisibility(vis); |
| 239 | } |
| 240 | if (mIconView != null) { |
| 241 | mIconView.setVisibility(vis); |
| 242 | } |
| 243 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 244 | requestLayout(); |
| 245 | } else { |
| 246 | invalidate(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public void setNavigationMode(int mode) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 251 | final int oldMode = mNavigationMode; |
| 252 | if (mode != oldMode) { |
| 253 | switch (oldMode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 254 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 255 | if (mTitleView != null) { |
| 256 | removeView(mTitleView); |
| 257 | mTitleView = null; |
| 258 | } |
| 259 | break; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 260 | case ActionBar.NAVIGATION_MODE_DROPDOWN_LIST: |
| 261 | if (mSpinner != null) { |
| 262 | removeView(mSpinner); |
| 263 | mSpinner = null; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 264 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 265 | break; |
| 266 | case ActionBar.NAVIGATION_MODE_CUSTOM: |
| 267 | if (mCustomNavView != null) { |
| 268 | removeView(mCustomNavView); |
| 269 | mCustomNavView = null; |
| 270 | } |
| 271 | break; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | switch (mode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 275 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 276 | initTitle(); |
| 277 | break; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 278 | case ActionBar.NAVIGATION_MODE_DROPDOWN_LIST: |
| 279 | mSpinner = new Spinner(mContext, null, |
| 280 | com.android.internal.R.attr.dropDownSpinnerStyle); |
| 281 | mSpinner.setOnItemSelectedListener(mNavItemSelectedListener); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 282 | mSpinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, |
| 283 | LayoutParams.WRAP_CONTENT)); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 284 | addView(mSpinner); |
| 285 | break; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 286 | case ActionBar.NAVIGATION_MODE_CUSTOM: |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 287 | mCustomNavView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, |
| 288 | LayoutParams.WRAP_CONTENT)); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 289 | addView(mCustomNavView); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 290 | break; |
| 291 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 292 | mNavigationMode = mode; |
| 293 | requestLayout(); |
| 294 | } |
| 295 | } |
| 296 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 297 | public void setDropdownAdapter(SpinnerAdapter adapter) { |
| 298 | mSpinner.setAdapter(adapter); |
| 299 | } |
| 300 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 301 | public View getCustomNavigationView() { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 302 | return mCustomNavView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | public int getNavigationMode() { |
| 306 | return mNavigationMode; |
| 307 | } |
| 308 | |
| 309 | public int getDisplayOptions() { |
| 310 | return mDisplayOptions; |
| 311 | } |
| 312 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 313 | public void setContextMode(int mode) { |
| 314 | Callback callback = mCallback; |
| 315 | if (callback == null) { |
| 316 | throw new IllegalStateException( |
| 317 | "Attempted to set ActionBar context mode with no callback"); |
| 318 | } |
| 319 | |
| 320 | ActionMenu menu = mContextMenus.get(mode); |
| 321 | if (menu == null) { |
| 322 | // Initialize the new mode |
| 323 | menu = new ActionMenu(getContext()); |
| 324 | |
| 325 | if (!callback.onCreateContextMode(mode, menu)) { |
| 326 | throw new IllegalArgumentException( |
| 327 | "ActionBar callback does not know how to create context mode " + mode); |
| 328 | } |
| 329 | mContextMenus.put(mode, menu); |
| 330 | } |
| 331 | |
| 332 | if (callback.onPrepareContextMode(mode, menu)) { |
| 333 | // TODO Set mode, animate, etc. |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | public void exitContextMode() { |
| 338 | // TODO Turn off context mode; go back to normal. |
| 339 | } |
| 340 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 341 | @Override |
| 342 | protected void onFinishInflate() { |
| 343 | super.onFinishInflate(); |
| 344 | |
| 345 | if ((mDisplayOptions & ActionBar.DISPLAY_HIDE_HOME) == 0) { |
| 346 | if (mLogo != null && (mDisplayOptions & ActionBar.DISPLAY_USE_LOGO) != 0) { |
| 347 | mLogoView = new ImageView(getContext()); |
| 348 | mLogoView.setAdjustViewBounds(true); |
| 349 | mLogoView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 350 | LayoutParams.MATCH_PARENT)); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 351 | mLogoView.setImageDrawable(mLogo); |
| 352 | mLogoView.setClickable(true); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 353 | mLogoView.setFocusable(true); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 354 | mLogoView.setOnClickListener(mHomeClickListener); |
| 355 | addView(mLogoView); |
| 356 | } else if (mIcon != null) { |
| 357 | mIconView = new ImageView(getContext()); |
| 358 | mIconView.setAdjustViewBounds(true); |
| 359 | mIconView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 360 | LayoutParams.MATCH_PARENT)); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 361 | mIconView.setImageDrawable(mIcon); |
| 362 | mIconView.setClickable(true); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 363 | mIconView.setFocusable(true); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 364 | mIconView.setOnClickListener(mHomeClickListener); |
| 365 | addView(mIconView); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | switch (mNavigationMode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 370 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 371 | if (mLogoView == null) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 372 | initTitle(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 373 | } |
| 374 | break; |
| 375 | |
| 376 | case ActionBar.NAVIGATION_MODE_DROPDOWN_LIST: |
| 377 | throw new UnsupportedOperationException( |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 378 | "Inflating dropdown list navigation isn't supported yet!"); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 379 | |
| 380 | case ActionBar.NAVIGATION_MODE_TABS: |
| 381 | throw new UnsupportedOperationException( |
| 382 | "Tab navigation isn't supported yet!"); |
| 383 | |
| 384 | case ActionBar.NAVIGATION_MODE_CUSTOM: |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 385 | if (mCustomNavView != null) { |
| 386 | addView(mCustomNavView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 387 | } |
| 388 | break; |
| 389 | } |
| 390 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 391 | |
| 392 | private void initTitle() { |
| 393 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 394 | mTitleView = (TextView) inflater.inflate(R.layout.action_bar_title_item, null); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 395 | mTitleView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, |
| 396 | LayoutParams.WRAP_CONTENT)); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 397 | if (mTitle != null) { |
| 398 | mTitleView.setText(mTitle); |
| 399 | } |
| 400 | addView(mTitleView); |
| 401 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 402 | |
| 403 | @Override |
| 404 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 405 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 406 | if (widthMode != MeasureSpec.EXACTLY) { |
| 407 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 408 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 409 | } |
| 410 | |
| 411 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 412 | if (heightMode != MeasureSpec.AT_MOST) { |
| 413 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 414 | "with android:layout_height=\"wrap_content\""); |
| 415 | } |
| 416 | |
| 417 | int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 418 | |
| 419 | int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight(); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 420 | final int height = mContentHeight - getPaddingTop() - getPaddingBottom(); |
| 421 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 422 | |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 423 | if (mLogoView != null && mLogoView.getVisibility() != GONE) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 424 | availableWidth = measureChildView(mLogoView, availableWidth, childSpecHeight, mSpacing); |
| 425 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 426 | if (mIconView != null && mIconView.getVisibility() != GONE) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 427 | availableWidth = measureChildView(mIconView, availableWidth, childSpecHeight, mSpacing); |
| 428 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 429 | |
| 430 | if (mMenuView != null) { |
| 431 | availableWidth = measureChildView(mMenuView, availableWidth, |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame^] | 432 | childSpecHeight, 0); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | switch (mNavigationMode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 436 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 437 | if (mTitleView != null) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 438 | measureChildView(mTitleView, availableWidth, childSpecHeight, mSpacing); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 439 | } |
| 440 | break; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 441 | case ActionBar.NAVIGATION_MODE_DROPDOWN_LIST: |
| 442 | if (mSpinner != null) { |
| 443 | mSpinner.measure( |
| 444 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.EXACTLY), |
| 445 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 446 | } |
| 447 | break; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 448 | case ActionBar.NAVIGATION_MODE_CUSTOM: |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 449 | if (mCustomNavView != null) { |
| 450 | mCustomNavView.measure( |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 451 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.EXACTLY), |
| 452 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 453 | } |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | setMeasuredDimension(contentWidth, mContentHeight); |
| 458 | } |
| 459 | |
| 460 | private int measureChildView(View child, int availableWidth, int childSpecHeight, int spacing) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 461 | child.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 462 | childSpecHeight); |
| 463 | |
| 464 | availableWidth -= child.getMeasuredWidth(); |
| 465 | availableWidth -= spacing; |
| 466 | |
| 467 | return availableWidth; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 472 | int x = getPaddingLeft(); |
| 473 | final int y = getPaddingTop(); |
| 474 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
| 475 | |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 476 | if (mLogoView != null && mLogoView.getVisibility() != GONE) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 477 | x += positionChild(mLogoView, x, y, contentHeight) + mSpacing; |
| 478 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 479 | if (mIconView != null && mIconView.getVisibility() != GONE) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 480 | x += positionChild(mIconView, x, y, contentHeight) + mSpacing; |
| 481 | } |
| 482 | |
| 483 | switch (mNavigationMode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 484 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 485 | if (mTitleView != null) { |
| 486 | x += positionChild(mTitleView, x, y, contentHeight) + mSpacing; |
| 487 | } |
| 488 | break; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 489 | case ActionBar.NAVIGATION_MODE_DROPDOWN_LIST: |
| 490 | if (mSpinner != null) { |
| 491 | x += positionChild(mSpinner, x, y, contentHeight) + mSpacing; |
| 492 | } |
| 493 | break; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 494 | case ActionBar.NAVIGATION_MODE_CUSTOM: |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 495 | if (mCustomNavView != null) { |
| 496 | x += positionChild(mCustomNavView, x, y, contentHeight) + mSpacing; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 497 | } |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | x = r - l - getPaddingRight(); |
| 502 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 503 | if (mMenuView != null) { |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame^] | 504 | x -= positionChildInverse(mMenuView, x + mActionSpacing, y, contentHeight) |
| 505 | - mActionSpacing; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | private int positionChild(View child, int x, int y, int contentHeight) { |
| 510 | int childWidth = child.getMeasuredWidth(); |
| 511 | int childHeight = child.getMeasuredHeight(); |
| 512 | int childTop = y + (contentHeight - childHeight) / 2; |
| 513 | |
| 514 | child.layout(x, childTop, x + childWidth, childTop + childHeight); |
| 515 | |
| 516 | return childWidth; |
| 517 | } |
| 518 | |
| 519 | private int positionChildInverse(View child, int x, int y, int contentHeight) { |
| 520 | int childWidth = child.getMeasuredWidth(); |
| 521 | int childHeight = child.getMeasuredHeight(); |
| 522 | int childTop = y + (contentHeight - childHeight) / 2; |
| 523 | |
| 524 | child.layout(x - childWidth, childTop, x, childTop + childHeight); |
| 525 | |
| 526 | return childWidth; |
| 527 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 528 | } |