blob: d2a50c94aa98fa7acf147b67cd0a27af23e033bc [file] [log] [blame]
Michael Kolb370a4f32010-10-06 10:45:32 -07001/*
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
17package com.android.browser;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.Gravity;
22import android.view.View;
23import android.view.View.OnClickListener;
24import android.widget.ImageButton;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Simple bread crumb view
34 * Use setController to receive callbacks from user interactions
35 * Use pushView, popView, clear, and getTopData to change/access the view stack
36 */
37public class BreadCrumbView extends LinearLayout implements OnClickListener {
38
39 interface Controller {
40 public void onTop(int level, Object data);
41 }
42
43 private ImageButton mBackButton;
44 private Controller mController;
45 private List<Crumb> mCrumbs;
46 private boolean mUseBackButton;
47
48 /**
49 * @param context
50 * @param attrs
51 * @param defStyle
52 */
53 public BreadCrumbView(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
55 init(context);
56 }
57
58 /**
59 * @param context
60 * @param attrs
61 */
62 public BreadCrumbView(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 init(context);
65 }
66
67 /**
68 * @param context
69 */
70 public BreadCrumbView(Context context) {
71 super(context);
72 init(context);
73 }
74
75 private void init(Context ctx) {
76 mUseBackButton = false;
77 mCrumbs = new ArrayList<Crumb>();
78 }
79
80 public void setUseBackButton(boolean useflag) {
81 mUseBackButton = useflag;
82 if (mUseBackButton && (mBackButton == null)) {
83 addBackButton();
84 } else if (!mUseBackButton && (mBackButton != null)) {
85 removeView(mBackButton);
86 mBackButton = null;
87 }
88 }
89
90 public void setController(Controller ctl) {
91 mController = ctl;
92 }
93
94 public Object getTopData() {
95 Crumb c = getTopCrumb();
96 if (c != null) {
97 return c.data;
98 }
99 return null;
100 }
101
102 public int size() {
103 return mCrumbs.size();
104 }
105
106 public void clear() {
107 while (mCrumbs.size() > 1) {
108 pop(false);
109 }
110 pop(true);
111 }
112
113 public void notifyController() {
114 if (mController != null) {
115 if (mCrumbs.size() > 0) {
116 mController.onTop(mCrumbs.size(), getTopCrumb().data);
117 } else {
118 mController.onTop(0, null);
119 }
120 }
121 }
122
123 public void pushView(String name, Object data) {
124 Crumb crumb = new Crumb(name, true, data);
125 pushCrumb(crumb);
126 }
127
128 public void pushView(View view, Object data) {
129 Crumb crumb = new Crumb(view, true, data);
130 pushCrumb(crumb);
131 }
132
133 public void popView() {
134 pop(true);
135 }
136
137 private void addBackButton() {
138 mBackButton = new ImageButton(mContext);
139 mBackButton.setImageResource(R.drawable.ic_back_normal);
140 mBackButton.setBackgroundResource(R.drawable.browserbarbutton);
141 mBackButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
142 LayoutParams.MATCH_PARENT));
143 mBackButton.setOnClickListener(this);
144 mBackButton.setVisibility(View.GONE);
145 addView(mBackButton, 0);
146 }
147
148 private void pushCrumb(Crumb crumb) {
149 if (!mUseBackButton || (mCrumbs.size() > 0)) {
150 addSeparator();
151 }
152 mCrumbs.add(crumb);
153 addView(crumb.crumbView);
154 if (mUseBackButton) {
155 mBackButton.setVisibility(crumb.canGoBack ? View.VISIBLE : View.GONE);
156 }
157 crumb.crumbView.setOnClickListener(this);
158 }
159
160 private void addSeparator() {
161 ImageView sep = new ImageView(mContext);
162 sep.setImageResource(R.drawable.crumb_divider);
163 sep.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
164 LayoutParams.MATCH_PARENT));
165 addView(sep);
166 }
167
168 private void pop(boolean notify) {
169 int n = mCrumbs.size();
170 if (n > 0) {
171 removeLastView();
172 if (!mUseBackButton || (n > 1)) {
173 // remove separator
174 removeLastView();
175 }
176 mCrumbs.remove(n - 1);
177 if (mUseBackButton) {
178 Crumb top = getTopCrumb();
179 if (top != null) {
180 mBackButton.setVisibility(top.canGoBack ? View.VISIBLE : View.GONE);
181 } else {
182 mBackButton.setVisibility(View.GONE);
183 }
184 }
185 if (notify) {
186 notifyController();
187 }
188 }
189 }
190
191 private void removeLastView() {
192 int ix = getChildCount();
193 if (ix > 0) {
194 removeViewAt(ix-1);
195 }
196 }
197
198 private Crumb getTopCrumb() {
199 Crumb crumb = null;
200 if (mCrumbs.size() > 0) {
201 crumb = mCrumbs.get(mCrumbs.size() - 1);
202 }
203 return crumb;
204 }
205
206 @Override
207 public void onClick(View v) {
208 if (mBackButton == v) {
209 popView();
210 notifyController();
211 } else {
212 // pop until view matches crumb view
213 while (v != getTopCrumb().crumbView) {
214 pop(false);
215 }
216 notifyController();
217 }
218 }
219
220 class Crumb {
221
222 public View crumbView;
223 public boolean canGoBack;
224 public Object data;
225
226 public Crumb(String title, boolean backEnabled, Object tag) {
227 init(makeCrumbView(title), backEnabled, tag);
228 }
229
230 public Crumb(View view, boolean backEnabled, Object tag) {
231 init(view, backEnabled, tag);
232 }
233
234 private void init(View view, boolean back, Object tag) {
235 canGoBack = back;
236 crumbView = view;
237 data = tag;
238 }
239
240 private TextView makeCrumbView(String name) {
241 TextView tv = new TextView(mContext);
242 tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
243 tv.setPadding(16, 0, 16, 0);
244 tv.setGravity(Gravity.CENTER_VERTICAL);
245 tv.setText(name);
246 tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
247 LayoutParams.MATCH_PARENT));
248 return tv;
249 }
250
251 }
252
253}