summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/TabBar.java
blob: b2c687d24c34ac96d13e86263a99ccca08ac0f02 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * 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 com.android.browser.ScrollWebView.ScrollListener;
import com.android.browser.TabControl.TabChangeListener;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * tabbed title bar for xlarge screen browser
 */
public class TabBar extends LinearLayout
        implements TabChangeListener, ScrollListener, OnClickListener {

    private static final int PROGRESS_MAX = 100;

    private BrowserActivity mBrowserActivity;

    private final int mTabWidthSelected;
    private final int mTabWidthUnselected;

    private TitleBarXLarge mTitleBar;

    private TabScrollView mTabs;
    private TabControl mControl;
    private ImageButton mNewTab;
    private int mButtonWidth;

    private Map<Tab, TabViewData> mTabMap;

    private boolean mUserRequestedUrlbar;
    private boolean mTitleVisible;
    private boolean mShowUrlMode;

    public TabBar(BrowserActivity context, TabControl tabcontrol, TitleBarXLarge titlebar) {
        super(context);
        Resources res = context.getResources();
        mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
        mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);

        mTitleBar = titlebar;
        mTitleBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        mTabMap = new HashMap<Tab, TabViewData>();
        mBrowserActivity = context;
        mControl = tabcontrol;
        Resources resources = context.getResources();
        LayoutInflater factory = LayoutInflater.from(context);
        factory.inflate(R.layout.tab_bar, this);
        mTabs = (TabScrollView) findViewById(R.id.tabs);
        mNewTab = (ImageButton) findViewById(R.id.newtab);
        mNewTab.setOnClickListener(this);

        // TODO: Change enabled states based on whether you can go
        // back/forward.  Probably should be done inside onPageStarted.

        // build tabs
        int tabcount = mControl.getTabCount();
        for (int i = 0; i < tabcount; i++) {
            Tab tab = mControl.getTab(i);
            TabViewData data = buildTab(tab);
            TabView tv = buildView(data);
        }
        mTabs.setSelectedTab(mControl.getCurrentIndex());

        // register the tab change listener
        mControl.setOnTabChangeListener(this);
        mUserRequestedUrlbar = false;
        mTitleVisible = true;
        mButtonWidth = -1;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (mButtonWidth == -1) {
            mButtonWidth = mNewTab.getMeasuredWidth();
        }
        int sw = mTabs.getMeasuredWidth();
        int w = right-left;
        if (w-sw < mButtonWidth) {
            sw = w - mButtonWidth;
        }
        mTabs.layout(0, 0, sw, bottom-top );
        mNewTab.layout(sw, 0, sw+mButtonWidth, bottom-top);
    }

    public void onClick(View view) {
        mBrowserActivity.removeComboView();
        if (mNewTab == view) {
            mBrowserActivity.openTabToHomePage();
        } else if (mTabs.getSelectedTab() == view) {
            if (mBrowserActivity.isFakeTitleBarShowing() && !isLoading()) {
                mBrowserActivity.hideFakeTitleBar();
            } else {
                showUrlBar();
            }
        } else {
            int ix = mTabs.getChildIndex(view);
            if (ix >= 0) {
                mTabs.setSelectedTab(ix);
                mBrowserActivity.switchToTab(ix);
            }
        }
    }

    private void showUrlBar() {
        mBrowserActivity.stopScrolling();
        mBrowserActivity.showFakeTitleBar();
        mUserRequestedUrlbar = true;
    }

    private void setShowUrlMode(boolean showUrl) {
        mShowUrlMode = showUrl;
    }

    // callback after fake titlebar is shown
    void onShowTitleBar() {
        setShowUrlMode(false);
    }

    // callback after fake titlebar is hidden
    void onHideTitleBar() {
        setShowUrlMode(!mTitleVisible);
        Tab tab = mControl.getCurrentTab();
        tab.getWebView().requestFocus();
        mUserRequestedUrlbar = false;
    }

    // webview scroll listener

    @Override
    public void onScroll(boolean titleVisible) {
        mTitleVisible = titleVisible;
        if (!mShowUrlMode && !mTitleVisible && !isLoading()) {
            if (mUserRequestedUrlbar) {
                mBrowserActivity.hideFakeTitleBar();
            } else {
                setShowUrlMode(true);
            }
        } else if (mTitleVisible && !isLoading()) {
            if (mShowUrlMode) {
                setShowUrlMode(false);
            }
        }
    }

    @Override
    public void createContextMenu(ContextMenu menu) {
        MenuInflater inflater = mBrowserActivity.getMenuInflater();
        inflater.inflate(R.menu.title_context, menu);
        mBrowserActivity.onCreateContextMenu(menu, this, null);
    }

    private TabViewData buildTab(Tab tab) {
        TabViewData data = new TabViewData(tab);
        mTabMap.put(tab, data);
        return data;
    }

    private TabView buildView(final TabViewData data) {
        TabView tv = new TabView(mBrowserActivity, data);
        tv.setTag(data);
        tv.setOnClickListener(this);
        mTabs.addTab(tv);
        return tv;
    }

    /**
     * View used in the tab bar
     */
    class TabView extends LinearLayout implements OnClickListener {

        TabViewData mTabData;
        View mTabContent;
        TextView mTitle;
        View mIncognito;
        ImageView mIconView;
        ImageView mLock;
        ImageView mClose;
        boolean mSelected;
        boolean mInLoad;

        /**
         * @param context
         */
        public TabView(Context context, TabViewData tab) {
            super(context);
            mTabData = tab;
            setGravity(Gravity.CENTER_VERTICAL);
            setOrientation(LinearLayout.HORIZONTAL);
            setBackgroundResource(R.drawable.tab_background);
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mTabContent = inflater.inflate(R.layout.tab_title, this, true);
            mTitle = (TextView) mTabContent.findViewById(R.id.title);
            mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
            mLock = (ImageView) mTabContent.findViewById(R.id.lock);
            mClose = (ImageView) mTabContent.findViewById(R.id.close);
            mClose.setOnClickListener(this);
            mIncognito = mTabContent.findViewById(R.id.incognito);
            mSelected = false;
            mInLoad = false;
            // update the status
            updateFromData();
        }

        @Override
        public void onClick(View v) {
            if (v == mClose) {
                closeTab();
            }
        }

        private void updateFromData() {
            mTabData.mTabView = this;
            if (mTabData.mUrl != null) {
                setDisplayTitle(mTabData.mUrl);
            }
            if (mTabData.mTitle != null) {
                setDisplayTitle(mTabData.mTitle);
            }
            setProgress(mTabData.mProgress);
            if (mTabData.mIcon != null) {
                setFavicon(mTabData.mIcon);
            }
            if (mTabData.mLock != null) {
                setLock(mTabData.mLock);
            }
            if (mTabData.mTab != null) {
                mIncognito.setVisibility(
                        mTabData.mTab.getWebView().isPrivateBrowsingEnabled() ?
                        View.VISIBLE : View.GONE);
            }
        }

        @Override
        public void setActivated(boolean selected) {
            mSelected = selected;
            mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
            mTitle.setTextAppearance(mBrowserActivity, mSelected ?
                    R.style.TabTitleSelected : R.style.TabTitleUnselected);
            setHorizontalFadingEdgeEnabled(!mSelected);
            setFadingEdgeLength(50);
            super.setActivated(selected);
            setLayoutParams(new LayoutParams(selected ?
                    mTabWidthSelected : mTabWidthUnselected,
                    LayoutParams.MATCH_PARENT));
        }

        void setDisplayTitle(String title) {
            mTitle.setText(title);
        }

        void setFavicon(Drawable d) {
            mIconView.setImageDrawable(d);
        }

        void setLock(Drawable d) {
            if (null == d) {
                mLock.setVisibility(View.GONE);
            } else {
                mLock.setImageDrawable(d);
                mLock.setVisibility(View.VISIBLE);
            }
        }

        void setTitleCompoundDrawables(Drawable left, Drawable top,
                Drawable right, Drawable bottom) {
            mTitle.setCompoundDrawables(left, top, right, bottom);
        }

        void setProgress(int newProgress) {
            if (newProgress >= PROGRESS_MAX) {
                mInLoad = false;
            } else {
                if (!mInLoad && getWindowToken() != null) {
                    mInLoad = true;
                }
            }
        }

        private void closeTab() {
            if (mTabData.mTab == mControl.getCurrentTab()) {
                mBrowserActivity.closeCurrentWindow();
            } else {
                mBrowserActivity.closeTab(mTabData.mTab);
            }
        }

    }

    /**
     * Store tab state within the title bar
     */
    class TabViewData {

        Tab mTab;
        TabView mTabView;
        int mProgress;
        Drawable mIcon;
        Drawable mLock;
        String mTitle;
        String mUrl;

        TabViewData(Tab tab) {
            mTab = tab;
        }

        void setUrlAndTitle(String url, String title) {
            mUrl = url;
            mTitle = title;
            if (mTabView != null) {
                if (title != null) {
                    mTabView.setDisplayTitle(title);
                } else if (url != null) {
                    mTabView.setDisplayTitle(url);
                }
            }
        }

        void setProgress(int newProgress) {
            mProgress = newProgress;
            if (mTabView != null) {
                mTabView.setProgress(mProgress);
            }
        }

        void setFavicon(Bitmap icon) {
            Drawable[] array = new Drawable[3];
            array[0] = new PaintDrawable(Color.BLACK);
            array[1] = new PaintDrawable(Color.WHITE);
            if (icon == null) {
//                array[2] = mGenericFavicon;
            } else {
                array[2] = new BitmapDrawable(icon);
            }
            LayerDrawable d = new LayerDrawable(array);
            d.setLayerInset(1, 1, 1, 1, 1);
            d.setLayerInset(2, 2, 2, 2, 2);
            mIcon = d;
            if (mTabView != null) {
                mTabView.setFavicon(mIcon);
            }
        }

    }

    // TabChangeListener implementation

    @Override
    public void onCurrentTab(Tab tab) {
        mTabs.setSelectedTab(mControl.getCurrentIndex());
        TabViewData tvd = mTabMap.get(tab);
        if (tvd != null) {
            tvd.setProgress(tvd.mProgress);
            // update the scroll state
            WebView webview = tab.getWebView();
            onScroll(webview.getVisibleTitleHeight() > 0);
        }
    }

    @Override
    public void onFavicon(Tab tab, Bitmap favicon) {
        TabViewData tvd = mTabMap.get(tab);
        if (tvd != null) {
            tvd.setFavicon(favicon);
        }
    }

    @Override
    public void onNewTab(Tab tab) {
        TabViewData tvd = buildTab(tab);
        buildView(tvd);
    }

    @Override
    public void onProgress(Tab tab, int progress) {
        TabViewData tvd = mTabMap.get(tab);
        if (tvd != null) {
            tvd.setProgress(progress);
        }
    }

    @Override
    public void onRemoveTab(Tab tab) {
        TabViewData tvd = mTabMap.get(tab);
        if (tvd != null) {
            TabView tv = tvd.mTabView;
            if (tv != null) {
                mTabs.removeTab(tv);
            }
        }
        mTabMap.remove(tab);
    }

    @Override
    public void onUrlAndTitle(Tab tab, String url, String title) {
        TabViewData tvd = mTabMap.get(tab);
        if (tvd != null) {
            tvd.setUrlAndTitle(url, title);
        }
    }

    @Override
    public void onPageFinished(Tab tab) {
    }

    @Override
    public void onPageStarted(Tab tab) {
    }


    private boolean isLoading() {
        return mTabMap.get(mControl.getCurrentTab()).mTabView.mInLoad;
    }

}