Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 22 | import android.text.TextUtils; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 24 | import android.util.TypedValue; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 25 | import android.view.Gravity; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
| 28 | import android.widget.ImageButton; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.LinearLayout; |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 31 | import android.widget.RelativeLayout; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 37 | import com.android.browser.R; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 38 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Simple bread crumb view |
| 41 | * Use setController to receive callbacks from user interactions |
| 42 | * Use pushView, popView, clear, and getTopData to change/access the view stack |
| 43 | */ |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 44 | public class BreadCrumbView extends RelativeLayout implements OnClickListener { |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 45 | private static final int DIVIDER_PADDING = 12; // dips |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 46 | private static final int CRUMB_PADDING = 8; // dips |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 47 | |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 48 | public interface Controller { |
| 49 | public void onTop(BreadCrumbView view, int level, Object data); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private ImageButton mBackButton; |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 53 | private LinearLayout mCrumbLayout; |
| 54 | private LinearLayout mBackLayout; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 55 | private Controller mController; |
| 56 | private List<Crumb> mCrumbs; |
| 57 | private boolean mUseBackButton; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 58 | private Drawable mSeparatorDrawable; |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 59 | private float mDividerPadding; |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 60 | private int mMaxVisible = -1; |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 61 | private Context mContext; |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 62 | private int mCrumbPadding; |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 63 | private TextView mOverflowView; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @param context |
| 67 | * @param attrs |
| 68 | * @param defStyle |
| 69 | */ |
| 70 | public BreadCrumbView(Context context, AttributeSet attrs, int defStyle) { |
| 71 | super(context, attrs, defStyle); |
| 72 | init(context); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param context |
| 77 | * @param attrs |
| 78 | */ |
| 79 | public BreadCrumbView(Context context, AttributeSet attrs) { |
| 80 | super(context, attrs); |
| 81 | init(context); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param context |
| 86 | */ |
| 87 | public BreadCrumbView(Context context) { |
| 88 | super(context); |
| 89 | init(context); |
| 90 | } |
| 91 | |
| 92 | private void init(Context ctx) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 93 | mContext = ctx; |
| 94 | setFocusable(true); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 95 | setGravity(Gravity.CENTER_VERTICAL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 96 | mUseBackButton = false; |
| 97 | mCrumbs = new ArrayList<Crumb>(); |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 98 | mSeparatorDrawable = ctx.getResources().getDrawable( |
| 99 | android.R.drawable.divider_horizontal_dark); |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 100 | float density = mContext.getResources().getDisplayMetrics().density; |
| 101 | mDividerPadding = DIVIDER_PADDING * density; |
| 102 | mCrumbPadding = (int) (CRUMB_PADDING * density); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 103 | addCrumbLayout(); |
| 104 | addBackLayout(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public void setUseBackButton(boolean useflag) { |
| 108 | mUseBackButton = useflag; |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 109 | updateVisible(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public void setController(Controller ctl) { |
| 113 | mController = ctl; |
| 114 | } |
| 115 | |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 116 | public int getMaxVisible() { |
| 117 | return mMaxVisible; |
| 118 | } |
| 119 | |
| 120 | public void setMaxVisible(int max) { |
| 121 | mMaxVisible = max; |
| 122 | updateVisible(); |
| 123 | } |
| 124 | |
| 125 | public int getTopLevel() { |
| 126 | return mCrumbs.size(); |
| 127 | } |
| 128 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 129 | public Object getTopData() { |
| 130 | Crumb c = getTopCrumb(); |
| 131 | if (c != null) { |
| 132 | return c.data; |
| 133 | } |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | public int size() { |
| 138 | return mCrumbs.size(); |
| 139 | } |
| 140 | |
| 141 | public void clear() { |
| 142 | while (mCrumbs.size() > 1) { |
| 143 | pop(false); |
| 144 | } |
| 145 | pop(true); |
| 146 | } |
| 147 | |
| 148 | public void notifyController() { |
| 149 | if (mController != null) { |
| 150 | if (mCrumbs.size() > 0) { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 151 | mController.onTop(this, mCrumbs.size(), getTopCrumb().data); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 152 | } else { |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 153 | mController.onTop(this, 0, null); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 158 | public View pushView(String name, Object data) { |
| 159 | return pushView(name, true, data); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 162 | public View pushView(String name, boolean canGoBack, Object data) { |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 163 | Crumb crumb = new Crumb(name, canGoBack, data); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 164 | pushCrumb(crumb); |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 165 | return crumb.crumbView; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 168 | public void addOverflowLabel(TextView view) { |
| 169 | mOverflowView = view; |
| 170 | if (view != null) { |
| 171 | view.setTextAppearance(mContext, R.style.BookmarkPathText); |
| 172 | view.setPadding(mCrumbPadding, 0, mCrumbPadding, 0); |
| 173 | view.setGravity(Gravity.CENTER_VERTICAL); |
| 174 | view.setText("... >"); |
| 175 | } |
| 176 | } |
| 177 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 178 | public void pushView(View view, Object data) { |
| 179 | Crumb crumb = new Crumb(view, true, data); |
| 180 | pushCrumb(crumb); |
| 181 | } |
| 182 | |
| 183 | public void popView() { |
| 184 | pop(true); |
| 185 | } |
| 186 | |
| 187 | private void addBackButton() { |
| 188 | mBackButton = new ImageButton(mContext); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 189 | mBackButton.setImageResource(R.drawable.icon_up); |
Michael Kolb | 5a72f18 | 2011-01-13 20:35:06 -0800 | [diff] [blame] | 190 | TypedValue outValue = new TypedValue(); |
| 191 | getContext().getTheme().resolveAttribute( |
| 192 | android.R.attr.selectableItemBackground, outValue, true); |
| 193 | int resid = outValue.resourceId; |
| 194 | mBackButton.setBackgroundResource(resid); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 195 | mBackButton.setPadding(mCrumbPadding, 0, mCrumbPadding, 0); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 196 | mBackButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 197 | LayoutParams.MATCH_PARENT)); |
| 198 | mBackButton.setOnClickListener(this); |
John Reck | aac2ce0 | 2012-09-21 14:17:47 -0700 | [diff] [blame] | 199 | mBackButton.setContentDescription(mContext.getText( |
| 200 | R.string.accessibility_button_bookmarks_folder_up)); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 201 | mBackLayout.addView(mBackButton); |
| 202 | } |
| 203 | |
| 204 | private void addParentLabel() { |
| 205 | TextView tv = new TextView(mContext); |
| 206 | tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium); |
| 207 | tv.setPadding(mCrumbPadding, 0, 0, 0); |
| 208 | tv.setGravity(Gravity.CENTER_VERTICAL); |
| 209 | tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 210 | LayoutParams.WRAP_CONTENT)); |
| 211 | tv.setText("/ .../"); |
| 212 | tv.setSingleLine(); |
| 213 | tv.setVisibility(View.GONE); |
| 214 | mCrumbLayout.addView(tv); |
| 215 | } |
| 216 | |
| 217 | private void addCrumbLayout() { |
| 218 | mCrumbLayout = new LinearLayout(mContext); |
| 219 | LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 220 | LayoutParams.WRAP_CONTENT); |
| 221 | params.addRule(ALIGN_PARENT_LEFT, TRUE); |
| 222 | params.setMargins(0, 0, 4 * mCrumbPadding, 0); |
| 223 | mCrumbLayout.setLayoutParams(params); |
| 224 | mCrumbLayout.setVisibility(View.VISIBLE); |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 225 | //addParentLabel(); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 226 | addView(mCrumbLayout); |
| 227 | } |
| 228 | |
| 229 | private void addBackLayout() { |
| 230 | mBackLayout= new LinearLayout(mContext); |
| 231 | LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 232 | LayoutParams.WRAP_CONTENT); |
| 233 | params.addRule(ALIGN_PARENT_RIGHT, TRUE); |
| 234 | mBackLayout.setLayoutParams(params); |
| 235 | mBackLayout.setVisibility(View.GONE); |
| 236 | addSeparator(); |
| 237 | addBackButton(); |
| 238 | addView(mBackLayout); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private void pushCrumb(Crumb crumb) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 242 | mCrumbs.add(crumb); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 243 | mCrumbLayout.addView(crumb.crumbView); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 244 | updateVisible(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 245 | crumb.crumbView.setOnClickListener(this); |
| 246 | } |
| 247 | |
| 248 | private void addSeparator() { |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 249 | View sep = makeDividerView(); |
| 250 | sep.setLayoutParams(makeDividerLayoutParams()); |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 251 | mBackLayout.addView(sep); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 252 | } |
| 253 | |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 254 | private ImageView makeDividerView() { |
| 255 | ImageView result = new ImageView(mContext); |
| 256 | result.setImageDrawable(mSeparatorDrawable); |
| 257 | result.setScaleType(ImageView.ScaleType.FIT_XY); |
| 258 | return result; |
| 259 | } |
| 260 | |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 261 | private LinearLayout.LayoutParams makeDividerLayoutParams() { |
| 262 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( |
| 263 | LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); |
John Reck | b3417f0 | 2011-01-14 11:01:05 -0800 | [diff] [blame] | 264 | return params; |
| 265 | } |
| 266 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 267 | private void pop(boolean notify) { |
| 268 | int n = mCrumbs.size(); |
| 269 | if (n > 0) { |
| 270 | removeLastView(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 271 | mCrumbs.remove(n - 1); |
| 272 | if (mUseBackButton) { |
| 273 | Crumb top = getTopCrumb(); |
Leon Scroggins | 8a2a0e7 | 2010-10-15 15:49:40 -0400 | [diff] [blame] | 274 | if (top != null && top.canGoBack) { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 275 | mBackLayout.setVisibility(View.VISIBLE); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 276 | } else { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 277 | mBackLayout.setVisibility(View.GONE); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 280 | updateVisible(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 281 | if (notify) { |
| 282 | notifyController(); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 287 | private void updateVisible() { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 288 | // start at index 1 (0 == parent label) |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 289 | int childIndex = 0; |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 290 | if (mMaxVisible >= 0) { |
| 291 | int invisibleCrumbs = size() - mMaxVisible; |
| 292 | if (invisibleCrumbs > 0) { |
| 293 | int crumbIndex = 0; |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 294 | if (mOverflowView != null) { |
| 295 | mOverflowView.setVisibility(VISIBLE); |
| 296 | mOverflowView.setOnClickListener(this); |
| 297 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 298 | while (crumbIndex < invisibleCrumbs) { |
| 299 | // Set the crumb to GONE. |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 300 | mCrumbLayout.getChildAt(childIndex).setVisibility(View.GONE); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 301 | childIndex++; |
| 302 | // Move to the next crumb. |
| 303 | crumbIndex++; |
| 304 | } |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 305 | } else { |
| 306 | if (mOverflowView != null) { |
| 307 | mOverflowView.setVisibility(GONE); |
| 308 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 309 | } |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 310 | // Make sure the last is visible. |
| 311 | int childCount = mCrumbLayout.getChildCount(); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 312 | while (childIndex < childCount) { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 313 | mCrumbLayout.getChildAt(childIndex).setVisibility(View.VISIBLE); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 314 | childIndex++; |
| 315 | } |
| 316 | } else { |
| 317 | int count = getChildCount(); |
| 318 | for (int i = childIndex; i < count ; i++) { |
| 319 | getChildAt(i).setVisibility(View.VISIBLE); |
| 320 | } |
| 321 | } |
| 322 | if (mUseBackButton) { |
| 323 | boolean canGoBack = getTopCrumb() != null ? getTopCrumb().canGoBack : false; |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 324 | mBackLayout.setVisibility(canGoBack ? View.VISIBLE : View.GONE); |
| 325 | if (canGoBack) { |
| 326 | mCrumbLayout.getChildAt(0).setVisibility(VISIBLE); |
| 327 | } else { |
| 328 | mCrumbLayout.getChildAt(0).setVisibility(GONE); |
| 329 | } |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 330 | } else { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 331 | mBackLayout.setVisibility(View.GONE); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 335 | private void removeLastView() { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 336 | int ix = mCrumbLayout.getChildCount(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 337 | if (ix > 0) { |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 338 | mCrumbLayout.removeViewAt(ix-1); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
John Reck | d4893b0 | 2010-12-07 17:38:34 -0800 | [diff] [blame] | 342 | Crumb getTopCrumb() { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 343 | Crumb crumb = null; |
| 344 | if (mCrumbs.size() > 0) { |
| 345 | crumb = mCrumbs.get(mCrumbs.size() - 1); |
| 346 | } |
| 347 | return crumb; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void onClick(View v) { |
| 352 | if (mBackButton == v) { |
| 353 | popView(); |
| 354 | notifyController(); |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 355 | } else if (mOverflowView == v) { |
| 356 | int maxVisible = getMaxVisible(); |
| 357 | while (maxVisible > 0) { |
| 358 | pop(false); |
| 359 | maxVisible--; |
| 360 | } |
| 361 | notifyController(); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 362 | } else { |
| 363 | // pop until view matches crumb view |
| 364 | while (v != getTopCrumb().crumbView) { |
| 365 | pop(false); |
| 366 | } |
| 367 | notifyController(); |
| 368 | } |
| 369 | } |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 370 | @Override |
| 371 | public int getBaseline() { |
| 372 | int ix = getChildCount(); |
| 373 | if (ix > 0) { |
| 374 | // If there is at least one crumb, the baseline will be its |
| 375 | // baseline. |
| 376 | return getChildAt(ix-1).getBaseline(); |
| 377 | } |
| 378 | return super.getBaseline(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 383 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 384 | int height = mSeparatorDrawable.getIntrinsicHeight(); |
Dianne Hackborn | 7831da9 | 2010-12-03 00:19:01 -0800 | [diff] [blame] | 385 | if (getMeasuredHeight() < height) { |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 386 | // This should only be an issue if there are currently no separators |
| 387 | // showing; i.e. if there is one crumb and no back button. |
| 388 | int mode = View.MeasureSpec.getMode(heightMeasureSpec); |
| 389 | switch(mode) { |
| 390 | case View.MeasureSpec.AT_MOST: |
| 391 | if (View.MeasureSpec.getSize(heightMeasureSpec) < height) { |
| 392 | return; |
| 393 | } |
| 394 | break; |
| 395 | case View.MeasureSpec.EXACTLY: |
| 396 | return; |
| 397 | default: |
| 398 | break; |
| 399 | } |
Dianne Hackborn | 7831da9 | 2010-12-03 00:19:01 -0800 | [diff] [blame] | 400 | setMeasuredDimension(getMeasuredWidth(), height); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 403 | |
| 404 | class Crumb { |
| 405 | |
| 406 | public View crumbView; |
| 407 | public boolean canGoBack; |
| 408 | public Object data; |
| 409 | |
| 410 | public Crumb(String title, boolean backEnabled, Object tag) { |
| 411 | init(makeCrumbView(title), backEnabled, tag); |
| 412 | } |
| 413 | |
| 414 | public Crumb(View view, boolean backEnabled, Object tag) { |
| 415 | init(view, backEnabled, tag); |
| 416 | } |
| 417 | |
| 418 | private void init(View view, boolean back, Object tag) { |
| 419 | canGoBack = back; |
| 420 | crumbView = view; |
| 421 | data = tag; |
| 422 | } |
| 423 | |
| 424 | private TextView makeCrumbView(String name) { |
| 425 | TextView tv = new TextView(mContext); |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 426 | tv.setTextAppearance(mContext, R.style.BookmarkPathText); |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 427 | tv.setPadding(mCrumbPadding, 0, mCrumbPadding, 0); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 428 | tv.setGravity(Gravity.CENTER_VERTICAL); |
Pankaj Garg | 634bf43 | 2015-07-13 09:54:21 -0700 | [diff] [blame] | 429 | tv.setText(name + " >"); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 430 | tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 431 | LayoutParams.MATCH_PARENT)); |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 432 | tv.setSingleLine(); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 433 | tv.setEllipsize(TextUtils.TruncateAt.END); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 434 | return tv; |
| 435 | } |
| 436 | |
| 437 | } |
| 438 | |
| 439 | } |