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 | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 48 | private Drawable 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); |
| 68 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 69 | mRtButton = (ImageView) findViewById(R.id.rt_btn); |
| 70 | mRtButton.setOnClickListener(new View.OnClickListener() { |
| 71 | public void onClick(View v) { |
| 72 | if (mInLoad) { |
| 73 | if (mWebView != null) { |
| 74 | mWebView.stopLoading(); |
| 75 | } |
| 76 | } else { |
Leon Scroggins | 3044423 | 2009-09-04 18:36:20 -0400 | [diff] [blame] | 77 | mBrowserActivity.bookmarksOrHistoryPicker(false); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | }); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 81 | Resources resources = context.getResources(); |
| 82 | mCircularProgress = (Drawable) resources.getDrawable( |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 83 | com.android.internal.R.drawable.search_spinner); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 84 | mIconDimension = (int) TypedValue.applyDimension( |
| 85 | TypedValue.COMPLEX_UNIT_DIP, 20f, |
| 86 | resources.getDisplayMetrics()); |
| 87 | mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 88 | mHorizontalProgress = (ProgressBar) findViewById( |
| 89 | R.id.progress_horizontal); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 90 | mWebView = webview; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 91 | mGenericFavicon = context.getResources().getDrawable( |
| 92 | R.drawable.app_web_browser_sm); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Return the WebView associated with this TitleBar. |
| 97 | */ |
| 98 | /* package */ WebView getWebView() { |
| 99 | return mWebView; |
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 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 103 | * Return whether the associated WebView is currently loading. Needed to |
| 104 | * determine whether a click should stop the load or close the tab. |
| 105 | */ |
| 106 | /* package */ boolean isInLoad() { |
| 107 | return mInLoad; |
| 108 | } |
| 109 | |
| 110 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 111 | * Set a new Bitmap for the Favicon. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 112 | */ |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 113 | /* package */ void setFavicon(Bitmap icon) { |
| 114 | Drawable[] array = new Drawable[3]; |
| 115 | array[0] = new PaintDrawable(Color.BLACK); |
| 116 | PaintDrawable p = new PaintDrawable(Color.WHITE); |
| 117 | array[1] = p; |
| 118 | if (icon == null) { |
| 119 | array[2] = mGenericFavicon; |
| 120 | } else { |
| 121 | array[2] = new BitmapDrawable(icon); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 122 | } |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame^] | 123 | LayerDrawable d = new LayerDrawable(array); |
| 124 | d.setLayerInset(1, 1, 1, 1, 1); |
| 125 | d.setLayerInset(2, 2, 2, 2, 2); |
| 126 | d.setBounds(0, 0, mIconDimension, mIconDimension); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 127 | Drawable progress = mInLoad ? mCircularProgress : null; |
| 128 | mTitle.setCompoundDrawables(d, null, progress, null); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 129 | mFavicon = d; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 132 | /** |
| 133 | * Set the Drawable for the lock icon, or null to hide it. |
| 134 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 135 | /* package */ void setLock(Drawable d) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 136 | if (null == d) { |
| 137 | mLockIcon.setVisibility(View.GONE); |
| 138 | } else { |
| 139 | mLockIcon.setImageDrawable(d); |
| 140 | mLockIcon.setVisibility(View.VISIBLE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 144 | /** |
| 145 | * Update the progress, from 0 to 100. |
| 146 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 147 | /* package */ void setProgress(int newProgress) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 148 | if (newProgress == mHorizontalProgress.getMax()) { |
| 149 | mTitle.setCompoundDrawables(mFavicon, null, null, null); |
| 150 | ((Animatable) mCircularProgress).stop(); |
| 151 | mHorizontalProgress.setVisibility(View.INVISIBLE); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 152 | if (mBookmarkDrawable != null) { |
| 153 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 154 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 155 | mInLoad = false; |
| 156 | } else { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 157 | mHorizontalProgress.setProgress(newProgress); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 158 | if (!mInLoad) { |
| 159 | mTitle.setCompoundDrawables(mFavicon, null, mCircularProgress, |
| 160 | null); |
| 161 | ((Animatable) mCircularProgress).start(); |
| 162 | mHorizontalProgress.setVisibility(View.VISIBLE); |
| 163 | if (mBookmarkDrawable == null) { |
| 164 | mBookmarkDrawable = mRtButton.getDrawable(); |
| 165 | } |
| 166 | if (mStopDrawable == null) { |
| 167 | mRtButton.setImageResource( |
| 168 | com.android.internal.R.drawable.ic_menu_stop); |
| 169 | mStopDrawable = mRtButton.getDrawable(); |
| 170 | } else { |
| 171 | mRtButton.setImageDrawable(mStopDrawable); |
| 172 | } |
| 173 | mInLoad = true; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 174 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 178 | /** |
| 179 | * Update the title and url. |
| 180 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 181 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 182 | if (url == null) { |
| 183 | mTitle.setText(R.string.title_bar_loading); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 184 | } else { |
Leon Scroggins | c62e908 | 2009-09-03 10:20:44 -0400 | [diff] [blame] | 185 | mTitle.setText(url.toString()); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 186 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* package */ void setToTabPicker() { |
| 190 | mTitle.setText(R.string.tab_picker_title); |
| 191 | setFavicon(null); |
| 192 | setLock(null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 193 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 194 | } |
| 195 | } |