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