Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 20 | import android.graphics.Rect; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 22 | import android.util.AttributeSet; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 25 | import android.webkit.WebView; |
| 26 | import android.widget.ImageView; |
| 27 | import android.widget.LinearLayout; |
| 28 | import android.widget.ProgressBar; |
| 29 | import android.widget.TextView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 30 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 31 | /** |
| 32 | * This class represents a title bar for a particular "tab" or "window" in the |
| 33 | * browser. |
| 34 | */ |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 35 | public class TitleBar extends LinearLayout { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 36 | private TextView mTitle; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 37 | private Drawable mCloseDrawable; |
| 38 | private ImageView mRtButton; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 39 | private ProgressBar mCircularProgress; |
| 40 | private ProgressBar mHorizontalProgress; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 41 | private Drawable mFavicon; |
| 42 | private Drawable mLockIcon; |
| 43 | private Drawable mStopDrawable; |
| 44 | private Drawable mBookmarkDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 45 | private boolean mInLoad; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 46 | private WebView mWebView; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 47 | private BrowserActivity mBrowserActivity; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 48 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 49 | public TitleBar(Context context, WebView webview, BrowserActivity ba) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 50 | super(context, null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 51 | LayoutInflater factory = LayoutInflater.from(context); |
| 52 | factory.inflate(R.layout.title_bar, this); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 53 | mBrowserActivity = ba; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 54 | |
| 55 | mTitle = (TextView) findViewById(R.id.title); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 56 | mTitle.setCompoundDrawablePadding(5); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 57 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 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 | }); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 70 | mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular); |
| 71 | mHorizontalProgress = (ProgressBar) findViewById( |
| 72 | R.id.progress_horizontal); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 73 | mWebView = webview; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 76 | /** |
| 77 | * Return the WebView associated with this TitleBar. |
| 78 | */ |
| 79 | /* package */ WebView getWebView() { |
| 80 | return mWebView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 83 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 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 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 94 | /* package */ void setFavicon(Drawable d) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 95 | if (d != null) { |
| 96 | d.setBounds(0, 0, 20, 20); |
| 97 | } |
| 98 | mTitle.setCompoundDrawables(d, null, mLockIcon, null); |
| 99 | mFavicon = d; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Set the Drawable for the lock icon, or null to hide it. |
| 104 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 105 | /* package */ void setLock(Drawable d) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 106 | if (d != null) { |
| 107 | d.setBounds(0, 0, 20, 20); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 108 | } |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 109 | mTitle.setCompoundDrawables(mFavicon, null, d, null); |
| 110 | mLockIcon = d; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 113 | /** |
| 114 | * Update the progress, from 0 to 100. |
| 115 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 116 | /* package */ void setProgress(int newProgress) { |
| 117 | if (newProgress == mCircularProgress.getMax()) { |
| 118 | mCircularProgress.setVisibility(View.GONE); |
| 119 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 120 | if (mBookmarkDrawable != null) { |
| 121 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 122 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 123 | mInLoad = false; |
| 124 | } else { |
| 125 | mCircularProgress.setProgress(newProgress); |
| 126 | mHorizontalProgress.setProgress(newProgress); |
| 127 | mCircularProgress.setVisibility(View.VISIBLE); |
| 128 | mHorizontalProgress.setVisibility(View.VISIBLE); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 129 | if (mBookmarkDrawable == null) { |
| 130 | mBookmarkDrawable = mRtButton.getDrawable(); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 131 | } |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 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 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 139 | mInLoad = true; |
| 140 | } |
| 141 | } |
| 142 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 143 | /** |
| 144 | * Update the title and url. |
| 145 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 146 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 147 | if (url == null) { |
| 148 | mTitle.setText(R.string.title_bar_loading); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 149 | } else { |
Leon Scroggins | c62e908 | 2009-09-03 10:20:44 -0400 | [diff] [blame] | 150 | mTitle.setText(url.toString()); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 151 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 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); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 160 | } |
| 161 | } |