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 | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 20 | import android.content.res.Resources; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.Color; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 23 | import android.graphics.Rect; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 24 | import android.graphics.drawable.Animatable; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 25 | import android.graphics.drawable.BitmapDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 27 | import android.graphics.drawable.LayerDrawable; |
| 28 | import android.graphics.drawable.PaintDrawable; |
| 29 | import android.util.TypedValue; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 32 | import android.webkit.WebView; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.LinearLayout; |
| 35 | import android.widget.ProgressBar; |
| 36 | import android.widget.TextView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 37 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 38 | /** |
| 39 | * This class represents a title bar for a particular "tab" or "window" in the |
| 40 | * browser. |
| 41 | */ |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 42 | public class TitleBar extends LinearLayout { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 43 | private TextView mTitle; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 44 | private Drawable mCloseDrawable; |
| 45 | private ImageView mRtButton; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 46 | private Drawable mCircularProgress; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 47 | private ProgressBar mHorizontalProgress; |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 48 | private ImageView mFavicon; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 49 | private ImageView mLockIcon; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 50 | private Drawable mStopDrawable; |
| 51 | private Drawable mBookmarkDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 52 | private boolean mInLoad; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 53 | private WebView mWebView; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 54 | private BrowserActivity mBrowserActivity; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 55 | private Drawable mGenericFavicon; |
| 56 | private int mIconDimension; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 57 | |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 58 | public TitleBar(BrowserActivity context, WebView webview) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 59 | super(context, null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 60 | LayoutInflater factory = LayoutInflater.from(context); |
| 61 | factory.inflate(R.layout.title_bar, this); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 62 | mBrowserActivity = context; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 63 | |
| 64 | mTitle = (TextView) findViewById(R.id.title); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 65 | mTitle.setCompoundDrawablePadding(5); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 66 | |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 67 | mLockIcon = (ImageView) findViewById(R.id.lock); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 68 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 69 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 70 | mRtButton = (ImageView) findViewById(R.id.rt_btn); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 71 | Resources resources = context.getResources(); |
| 72 | mCircularProgress = (Drawable) resources.getDrawable( |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 73 | com.android.internal.R.drawable.search_spinner); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 74 | mIconDimension = (int) TypedValue.applyDimension( |
| 75 | TypedValue.COMPLEX_UNIT_DIP, 20f, |
| 76 | resources.getDisplayMetrics()); |
| 77 | mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 78 | mHorizontalProgress = (ProgressBar) findViewById( |
| 79 | R.id.progress_horizontal); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 80 | mWebView = webview; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 81 | mGenericFavicon = context.getResources().getDrawable( |
| 82 | R.drawable.app_web_browser_sm); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 85 | /** |
| 86 | * Return the WebView associated with this TitleBar. |
| 87 | */ |
| 88 | /* package */ WebView getWebView() { |
| 89 | return mWebView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 92 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 93 | * Return whether the associated WebView is currently loading. Needed to |
| 94 | * determine whether a click should stop the load or close the tab. |
| 95 | */ |
| 96 | /* package */ boolean isInLoad() { |
| 97 | return mInLoad; |
| 98 | } |
| 99 | |
| 100 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 101 | * Set a new Bitmap for the Favicon. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 102 | */ |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 103 | /* package */ void setFavicon(Bitmap icon) { |
| 104 | Drawable[] array = new Drawable[3]; |
| 105 | array[0] = new PaintDrawable(Color.BLACK); |
| 106 | PaintDrawable p = new PaintDrawable(Color.WHITE); |
| 107 | array[1] = p; |
| 108 | if (icon == null) { |
| 109 | array[2] = mGenericFavicon; |
| 110 | } else { |
| 111 | array[2] = new BitmapDrawable(icon); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 112 | } |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 113 | LayerDrawable d = new LayerDrawable(array); |
| 114 | d.setLayerInset(1, 1, 1, 1, 1); |
| 115 | d.setLayerInset(2, 2, 2, 2, 2); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 116 | mFavicon.setImageDrawable(d); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 119 | /** |
| 120 | * Set the Drawable for the lock icon, or null to hide it. |
| 121 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 122 | /* package */ void setLock(Drawable d) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 123 | if (null == d) { |
| 124 | mLockIcon.setVisibility(View.GONE); |
| 125 | } else { |
| 126 | mLockIcon.setImageDrawable(d); |
| 127 | mLockIcon.setVisibility(View.VISIBLE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 131 | /** |
| 132 | * Update the progress, from 0 to 100. |
| 133 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 134 | /* package */ void setProgress(int newProgress) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 135 | if (newProgress == mHorizontalProgress.getMax()) { |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 136 | mTitle.setCompoundDrawables(null, null, null, null); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 137 | ((Animatable) mCircularProgress).stop(); |
| 138 | mHorizontalProgress.setVisibility(View.INVISIBLE); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 139 | if (mBookmarkDrawable != null) { |
| 140 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 141 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 142 | mInLoad = false; |
| 143 | } else { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 144 | mHorizontalProgress.setProgress(newProgress); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 145 | if (!mInLoad) { |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 146 | mTitle.setCompoundDrawables(null, null, mCircularProgress, |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 147 | null); |
| 148 | ((Animatable) mCircularProgress).start(); |
| 149 | mHorizontalProgress.setVisibility(View.VISIBLE); |
| 150 | if (mBookmarkDrawable == null) { |
| 151 | mBookmarkDrawable = mRtButton.getDrawable(); |
| 152 | } |
| 153 | if (mStopDrawable == null) { |
| 154 | mRtButton.setImageResource( |
| 155 | com.android.internal.R.drawable.ic_menu_stop); |
| 156 | mStopDrawable = mRtButton.getDrawable(); |
| 157 | } else { |
| 158 | mRtButton.setImageDrawable(mStopDrawable); |
| 159 | } |
| 160 | mInLoad = true; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 161 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 165 | /** |
| 166 | * Update the title and url. |
| 167 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 168 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 169 | if (url == null) { |
| 170 | mTitle.setText(R.string.title_bar_loading); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 171 | } else { |
Leon Scroggins | c62e908 | 2009-09-03 10:20:44 -0400 | [diff] [blame] | 172 | mTitle.setText(url.toString()); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 173 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* package */ void setToTabPicker() { |
| 177 | mTitle.setText(R.string.tab_picker_title); |
| 178 | setFavicon(null); |
| 179 | setLock(null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 180 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 181 | } |
| 182 | } |