TitleBar.java revision c62e9082481850fd4c38866b9e6e955784668170
1/*
2 * Copyright (C) 2009 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.graphics.Rect;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.webkit.WebView;
26import android.widget.ImageView;
27import android.widget.LinearLayout;
28import android.widget.ProgressBar;
29import android.widget.TextView;
30
31/**
32 * This class represents a title bar for a particular "tab" or "window" in the
33 * browser.
34 */
35public class TitleBar extends LinearLayout {
36    private TextView        mTitle;
37    private Drawable        mCloseDrawable;
38    private ImageView       mRtButton;
39    private ProgressBar     mCircularProgress;
40    private ProgressBar     mHorizontalProgress;
41    private Drawable        mFavicon;
42    private Drawable        mLockIcon;
43    private Drawable        mStopDrawable;
44    private Drawable        mBookmarkDrawable;
45    private boolean         mInLoad;
46    private WebView         mWebView;
47    private BrowserActivity mBrowserActivity;
48
49    public TitleBar(Context context, WebView webview, BrowserActivity ba) {
50        super(context, null);
51        LayoutInflater factory = LayoutInflater.from(context);
52        factory.inflate(R.layout.title_bar, this);
53        mBrowserActivity = ba;
54
55        mTitle = (TextView) findViewById(R.id.title);
56        mTitle.setCompoundDrawablePadding(5);
57
58        mRtButton = (ImageView) findViewById(R.id.rt_btn);
59        mRtButton.setOnClickListener(new View.OnClickListener() {
60            public void onClick(View v) {
61                if (mInLoad) {
62                    if (mWebView != null) {
63                        mWebView.stopLoading();
64                    }
65                } else {
66                    mBrowserActivity.bookmarksOrHistoryPicker(false, false);
67                }
68            }
69        });
70        mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular);
71        mHorizontalProgress = (ProgressBar) findViewById(
72                R.id.progress_horizontal);
73        mWebView = webview;
74    }
75
76    /**
77     * Return the WebView associated with this TitleBar.
78     */
79    /* package */ WebView getWebView() {
80        return mWebView;
81    }
82
83    /**
84     * Return whether the associated WebView is currently loading.  Needed to
85     * determine whether a click should stop the load or close the tab.
86     */
87    /* package */ boolean isInLoad() {
88        return mInLoad;
89    }
90
91    /**
92     * Set a new Drawable for the Favicon.
93     */
94    /* package */ void setFavicon(Drawable d) {
95        if (d != null) {
96            d.setBounds(0, 0, 20, 20);
97        }
98        mTitle.setCompoundDrawables(d, null, mLockIcon, null);
99        mFavicon = d;
100    }
101
102    /**
103     * Set the Drawable for the lock icon, or null to hide it.
104     */
105    /* package */ void setLock(Drawable d) {
106        if (d != null) {
107            d.setBounds(0, 0, 20, 20);
108        }
109        mTitle.setCompoundDrawables(mFavicon, null, d, null);
110        mLockIcon = d;
111    }
112
113    /**
114     * Update the progress, from 0 to 100.
115     */
116    /* package */ void setProgress(int newProgress) {
117        if (newProgress == mCircularProgress.getMax()) {
118            mCircularProgress.setVisibility(View.GONE);
119            mHorizontalProgress.setVisibility(View.GONE);
120            if (mBookmarkDrawable != null) {
121                mRtButton.setImageDrawable(mBookmarkDrawable);
122            }
123            mInLoad = false;
124        } else {
125            mCircularProgress.setProgress(newProgress);
126            mHorizontalProgress.setProgress(newProgress);
127            mCircularProgress.setVisibility(View.VISIBLE);
128            mHorizontalProgress.setVisibility(View.VISIBLE);
129            if (mBookmarkDrawable == null) {
130                mBookmarkDrawable = mRtButton.getDrawable();
131            }
132            if (mStopDrawable == null) {
133                mRtButton.setImageResource(
134                        com.android.internal.R.drawable.ic_menu_stop);
135                mStopDrawable = mRtButton.getDrawable();
136            } else {
137                mRtButton.setImageDrawable(mStopDrawable);
138            }
139            mInLoad = true;
140        }
141    }
142
143    /**
144     * Update the title and url.
145     */
146    /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
147        if (url == null) {
148            mTitle.setText(R.string.title_bar_loading);
149        } else {
150            mTitle.setText(url.toString());
151        }
152    }
153
154    /* package */ void setToTabPicker() {
155        mTitle.setText(R.string.tab_picker_title);
156        setFavicon(null);
157        setLock(null);
158        mCircularProgress.setVisibility(View.GONE);
159        mHorizontalProgress.setVisibility(View.GONE);
160    }
161}
162