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; |
| 37 | private TextView mUrl; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 38 | private Drawable mCloseDrawable; |
| 39 | private ImageView mRtButton; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 40 | private ProgressBar mCircularProgress; |
| 41 | private ProgressBar mHorizontalProgress; |
| 42 | private ImageView mFavicon; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 43 | private ImageView mLockIcon; // FIXME: Needs to be below the favicon |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 44 | private boolean mInLoad; |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 45 | private boolean mTitleSet; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 46 | private WebView mWebView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 47 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 48 | public TitleBar(Context context, WebView webview) { |
| 49 | super(context, null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 50 | LayoutInflater factory = LayoutInflater.from(context); |
| 51 | factory.inflate(R.layout.title_bar, this); |
| 52 | |
| 53 | mTitle = (TextView) findViewById(R.id.title); |
| 54 | mUrl = (TextView) findViewById(R.id.url); |
| 55 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 56 | mRtButton = (ImageView) findViewById(R.id.rt_button); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 57 | |
| 58 | mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular); |
| 59 | mHorizontalProgress = (ProgressBar) findViewById( |
| 60 | R.id.progress_horizontal); |
| 61 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 62 | mLockIcon = (ImageView) findViewById(R.id.lock_icon); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 63 | mWebView = webview; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 66 | /** |
| 67 | * Return the WebView associated with this TitleBar. |
| 68 | */ |
| 69 | /* package */ WebView getWebView() { |
| 70 | return mWebView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 73 | /** |
| 74 | * Determine whether a point (from a touch) hits the right button. |
| 75 | */ |
| 76 | /* package */ boolean hitRightButton(int x, int y) { |
| 77 | Rect hitRect = new Rect(); |
| 78 | mRtButton.getHitRect(hitRect); |
| 79 | return hitRect.contains(x - getLeft(), y - getTop()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return whether the associated WebView is currently loading. Needed to |
| 84 | * determine whether a click should stop the load or close the tab. |
| 85 | */ |
| 86 | /* package */ boolean isInLoad() { |
| 87 | return mInLoad; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set a new Drawable for the Favicon. |
| 92 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 93 | /* package */ void setFavicon(Drawable d) { |
| 94 | mFavicon.setImageDrawable(d); |
| 95 | } |
| 96 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 97 | /** |
| 98 | * Set the Drawable for the lock icon, or null to hide it. |
| 99 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 100 | /* package */ void setLock(Drawable d) { |
| 101 | if (d == null) { |
| 102 | mLockIcon.setVisibility(View.GONE); |
| 103 | } else { |
| 104 | mLockIcon.setImageDrawable(d); |
| 105 | mLockIcon.setVisibility(View.VISIBLE); |
| 106 | } |
| 107 | } |
| 108 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 109 | /** |
| 110 | * Update the progress, from 0 to 100. |
| 111 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 112 | /* package */ void setProgress(int newProgress) { |
| 113 | if (newProgress == mCircularProgress.getMax()) { |
| 114 | mCircularProgress.setVisibility(View.GONE); |
| 115 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 116 | mRtButton.setVisibility(View.VISIBLE); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 117 | mUrl.setVisibility(View.VISIBLE); |
| 118 | if (mCloseDrawable != null) { |
| 119 | mRtButton.setImageDrawable(mCloseDrawable); |
| 120 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 121 | mInLoad = false; |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 122 | if (!mTitleSet) { |
| 123 | mTitle.setText(mUrl.getText()); |
| 124 | mUrl.setText(null); |
| 125 | mTitleSet = true; |
| 126 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 127 | } else { |
| 128 | mCircularProgress.setProgress(newProgress); |
| 129 | mHorizontalProgress.setProgress(newProgress); |
| 130 | mCircularProgress.setVisibility(View.VISIBLE); |
| 131 | mHorizontalProgress.setVisibility(View.VISIBLE); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 132 | mUrl.setVisibility(View.VISIBLE); |
| 133 | if (mCloseDrawable == null) { |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 134 | // The drawable was assigned in the xml file, so it already |
| 135 | // exists. Keep a pointer to it when we switch to the resource |
| 136 | // so we can easily switch back. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 137 | mCloseDrawable = mRtButton.getDrawable(); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 138 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 139 | mRtButton.setImageResource( |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 140 | com.android.internal.R.drawable.ic_menu_stop); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 141 | mInLoad = true; |
| 142 | } |
| 143 | } |
| 144 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Update the title and url. |
| 147 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 148 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) { |
Leon Scroggins | 176215d | 2009-06-15 12:38:58 -0400 | [diff] [blame] | 149 | if (url != null) { |
| 150 | url = BrowserActivity.buildTitleUrl(url.toString()); |
| 151 | } |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 152 | if (null == title) { |
| 153 | if (mInLoad) { |
| 154 | mTitleSet = false; |
| 155 | mTitle.setText(R.string.title_bar_loading); |
| 156 | } else { |
| 157 | // If the page has no title, put the url in the title space |
| 158 | // and leave the url blank. |
| 159 | mTitle.setText(url); |
| 160 | mUrl.setText(null); |
| 161 | mTitleSet = true; |
| 162 | return; |
| 163 | } |
| 164 | } else { |
| 165 | mTitle.setText(title); |
| 166 | mTitleSet = true; |
| 167 | } |
Leon Scroggins | 176215d | 2009-06-15 12:38:58 -0400 | [diff] [blame] | 168 | mUrl.setText(url); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* package */ void setToTabPicker() { |
| 172 | mTitle.setText(R.string.tab_picker_title); |
| 173 | setFavicon(null); |
| 174 | setLock(null); |
| 175 | mCircularProgress.setVisibility(View.GONE); |
| 176 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 177 | mUrl.setVisibility(View.INVISIBLE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 178 | } |
| 179 | } |