summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BreadCrumbView.java
blob: d331b53649c5a18c1f2ae4f186e09b100a32c57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Simple bread crumb view
 * Use setController to receive callbacks from user interactions
 * Use pushView, popView, clear, and getTopData to change/access the view stack
 */
public class BreadCrumbView extends LinearLayout implements OnClickListener {
    private static final int DIVIDER_PADDING = 12; // dips
    private static final int CRUMB_PADDING = 8; // dips

    public interface Controller {
        public void onTop(BreadCrumbView view, int level, Object data);
    }

    private ImageButton mBackButton;
    private Controller mController;
    private List<Crumb> mCrumbs;
    private boolean mUseBackButton;
    private Drawable mSeparatorDrawable;
    private float mDividerPadding;
    private int mMaxVisible = -1;
    private Context mContext;
    private int mCrumbPadding;

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public BreadCrumbView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public BreadCrumbView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * @param context
     */
    public BreadCrumbView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context ctx) {
        mContext = ctx;
        setFocusable(true);
        mUseBackButton = false;
        mCrumbs = new ArrayList<Crumb>();
        TypedArray a = mContext.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
        mSeparatorDrawable = a.getDrawable(com.android.internal.R.styleable.Theme_dividerVertical);
        a.recycle();
        float density = mContext.getResources().getDisplayMetrics().density;
        mDividerPadding = DIVIDER_PADDING * density;
        mCrumbPadding = (int) (CRUMB_PADDING * density);
        addBackButton();
    }

    public void setUseBackButton(boolean useflag) {
        mUseBackButton = useflag;
        updateVisible();
    }

    public void setController(Controller ctl) {
        mController = ctl;
    }

    public int getMaxVisible() {
        return mMaxVisible;
    }

    public void setMaxVisible(int max) {
        mMaxVisible = max;
        updateVisible();
    }

    public int getTopLevel() {
        return mCrumbs.size();
    }

    public Object getTopData() {
        Crumb c = getTopCrumb();
        if (c != null) {
            return c.data;
        }
        return null;
    }

    public int size() {
        return mCrumbs.size();
    }

    public void clear() {
        while (mCrumbs.size() > 1) {
            pop(false);
        }
        pop(true);
    }

    public void notifyController() {
        if (mController != null) {
            if (mCrumbs.size() > 0) {
                mController.onTop(this, mCrumbs.size(), getTopCrumb().data);
            } else {
                mController.onTop(this, 0, null);
            }
        }
    }

    public View pushView(String name, Object data) {
        return pushView(name, true, data);
    }

    public View pushView(String name, boolean canGoBack, Object data) {
        Crumb crumb = new Crumb(name, canGoBack, data);
        pushCrumb(crumb);
        return crumb.crumbView;
    }

    public void pushView(View view, Object data) {
        Crumb crumb = new Crumb(view, true, data);
        pushCrumb(crumb);
    }

    public void popView() {
        pop(true);
    }

    private void addBackButton() {
        mBackButton = new ImageButton(mContext);
        mBackButton.setImageResource(R.drawable.ic_back_hierarchy_holo_dark);
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(
                android.R.attr.selectableItemBackground, outValue, true);
        int resid = outValue.resourceId;
        mBackButton.setBackgroundResource(resid);
        mBackButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT));
        mBackButton.setOnClickListener(this);
        mBackButton.setVisibility(View.GONE);
        mBackButton.setContentDescription(mContext.getText(
                R.string.accessibility_button_bookmarks_folder_up));
        addView(mBackButton, 0);
    }

    private void pushCrumb(Crumb crumb) {
        if (mCrumbs.size() > 0) {
            addSeparator();
        }
        mCrumbs.add(crumb);
        addView(crumb.crumbView);
        updateVisible();
        crumb.crumbView.setOnClickListener(this);
    }

    private void addSeparator() {
        View sep = makeDividerView();
        sep.setLayoutParams(makeDividerLayoutParams());
        addView(sep);
    }

    private ImageView makeDividerView() {
        ImageView result = new ImageView(mContext);
        result.setImageDrawable(mSeparatorDrawable);
        result.setScaleType(ImageView.ScaleType.FIT_XY);
        return result;
    }

    private LayoutParams makeDividerLayoutParams() {
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        params.topMargin = (int) mDividerPadding;
        params.bottomMargin = (int) mDividerPadding;
        return params;
    }

    private void pop(boolean notify) {
        int n = mCrumbs.size();
        if (n > 0) {
            removeLastView();
            if (!mUseBackButton || (n > 1)) {
                // remove separator
                removeLastView();
            }
            mCrumbs.remove(n - 1);
            if (mUseBackButton) {
                Crumb top = getTopCrumb();
                if (top != null && top.canGoBack) {
                    mBackButton.setVisibility(View.VISIBLE);
                } else {
                    mBackButton.setVisibility(View.GONE);
                }
            }
            updateVisible();
            if (notify) {
                notifyController();
            }
        }
    }

    private void updateVisible() {
        // start at index 1 (0 == back button)
        int childIndex = 1;
        if (mMaxVisible >= 0) {
            int invisibleCrumbs = size() - mMaxVisible;
            if (invisibleCrumbs > 0) {
                int crumbIndex = 0;
                while (crumbIndex < invisibleCrumbs) {
                    // Set the crumb to GONE.
                    getChildAt(childIndex).setVisibility(View.GONE);
                    childIndex++;
                    // Each crumb is followed by a separator (except the last
                    // one).  Also make it GONE
                    if (getChildAt(childIndex) != null) {
                        getChildAt(childIndex).setVisibility(View.GONE);
                    }
                    childIndex++;
                    // Move to the next crumb.
                    crumbIndex++;
                }
            }
            // Make sure the last two are visible.
            int childCount = getChildCount();
            while (childIndex < childCount) {
                getChildAt(childIndex).setVisibility(View.VISIBLE);
                childIndex++;
            }
        } else {
            int count = getChildCount();
            for (int i = childIndex; i < count ; i++) {
                getChildAt(i).setVisibility(View.VISIBLE);
            }
        }
        if (mUseBackButton) {
            boolean canGoBack = getTopCrumb() != null ? getTopCrumb().canGoBack : false;
            mBackButton.setVisibility(canGoBack ? View.VISIBLE : View.GONE);
        } else {
            mBackButton.setVisibility(View.GONE);
        }
    }

    private void removeLastView() {
        int ix = getChildCount();
        if (ix > 0) {
            removeViewAt(ix-1);
        }
    }

    Crumb getTopCrumb() {
        Crumb crumb = null;
        if (mCrumbs.size() > 0) {
            crumb = mCrumbs.get(mCrumbs.size() - 1);
        }
        return crumb;
    }

    @Override
    public void onClick(View v) {
        if (mBackButton == v) {
            popView();
            notifyController();
        } else {
            // pop until view matches crumb view
            while (v != getTopCrumb().crumbView) {
                pop(false);
            }
            notifyController();
        }
    }
    @Override
    public int getBaseline() {
        int ix = getChildCount();
        if (ix > 0) {
            // If there is at least one crumb, the baseline will be its
            // baseline.
            return getChildAt(ix-1).getBaseline();
        }
        return super.getBaseline();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = mSeparatorDrawable.getIntrinsicHeight();
        if (getMeasuredHeight() < height) {
            // This should only be an issue if there are currently no separators
            // showing; i.e. if there is one crumb and no back button.
            int mode = View.MeasureSpec.getMode(heightMeasureSpec);
            switch(mode) {
                case View.MeasureSpec.AT_MOST:
                    if (View.MeasureSpec.getSize(heightMeasureSpec) < height) {
                        return;
                    }
                    break;
                case View.MeasureSpec.EXACTLY:
                    return;
                default:
                    break;
            }
            setMeasuredDimension(getMeasuredWidth(), height);
        }
    }

    class Crumb {

        public View crumbView;
        public boolean canGoBack;
        public Object data;

        public Crumb(String title, boolean backEnabled, Object tag) {
            init(makeCrumbView(title), backEnabled, tag);
        }

        public Crumb(View view, boolean backEnabled, Object tag) {
            init(view, backEnabled, tag);
        }

        private void init(View view, boolean back, Object tag) {
            canGoBack = back;
            crumbView = view;
            data = tag;
        }

        private TextView makeCrumbView(String name) {
            TextView tv = new TextView(mContext);
            tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
            tv.setPadding(mCrumbPadding, 0, mCrumbPadding, 0);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setText(name);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.MATCH_PARENT));
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);
            return tv;
        }

    }

}