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; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 31 | import android.view.MotionEvent; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 32 | import android.view.View; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 33 | import android.view.ViewConfiguration; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 34 | import android.widget.ImageView; |
| 35 | import android.widget.LinearLayout; |
| 36 | import android.widget.ProgressBar; |
| 37 | import android.widget.TextView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 38 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 39 | /** |
| 40 | * This class represents a title bar for a particular "tab" or "window" in the |
| 41 | * browser. |
| 42 | */ |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 43 | public class TitleBar extends LinearLayout { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 44 | private TextView mTitle; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 45 | private Drawable mCloseDrawable; |
| 46 | private ImageView mRtButton; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 47 | private Drawable mCircularProgress; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 48 | private ProgressBar mHorizontalProgress; |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 49 | private ImageView mFavicon; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 50 | private ImageView mLockIcon; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 51 | private Drawable mStopDrawable; |
| 52 | private Drawable mBookmarkDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 53 | private boolean mInLoad; |
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 | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 57 | private View mTitleBg; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 58 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 59 | public TitleBar(BrowserActivity context) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 60 | super(context, null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 61 | LayoutInflater factory = LayoutInflater.from(context); |
| 62 | factory.inflate(R.layout.title_bar, this); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 63 | mBrowserActivity = context; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 64 | |
| 65 | mTitle = (TextView) findViewById(R.id.title); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 66 | mTitle.setCompoundDrawablePadding(5); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 67 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 68 | mTitleBg = findViewById(R.id.title_bg); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 69 | mLockIcon = (ImageView) findViewById(R.id.lock); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 70 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 71 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 72 | mRtButton = (ImageView) findViewById(R.id.rt_btn); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 73 | Resources resources = context.getResources(); |
| 74 | mCircularProgress = (Drawable) resources.getDrawable( |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 75 | com.android.internal.R.drawable.search_spinner); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 76 | mIconDimension = (int) TypedValue.applyDimension( |
| 77 | TypedValue.COMPLEX_UNIT_DIP, 20f, |
| 78 | resources.getDisplayMetrics()); |
| 79 | mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 80 | mHorizontalProgress = (ProgressBar) findViewById( |
| 81 | R.id.progress_horizontal); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 82 | mGenericFavicon = context.getResources().getDrawable( |
| 83 | R.drawable.app_web_browser_sm); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 86 | @Override |
| 87 | public boolean onTouchEvent(MotionEvent event) { |
| 88 | switch (event.getAction()) { |
| 89 | case MotionEvent.ACTION_DOWN: |
| 90 | // Make all touches hit either the textfield or the button, |
| 91 | // depending on which side of the right edge of the textfield |
| 92 | // they hit. |
| 93 | if ((int) event.getX() > mTitleBg.getRight()) { |
| 94 | mRtButton.setPressed(true); |
| 95 | } else { |
| 96 | mTitleBg.setPressed(true); |
| 97 | } |
| 98 | break; |
| 99 | case MotionEvent.ACTION_MOVE: |
| 100 | int slop = ViewConfiguration.get(mBrowserActivity) |
| 101 | .getScaledTouchSlop(); |
| 102 | if ((int) event.getY() > getHeight() + slop) { |
| 103 | // We only trigger the actions in ACTION_UP if one or the |
| 104 | // other is pressed. Since the user moved off the title |
| 105 | // bar, mark both as not pressed. |
| 106 | mTitleBg.setPressed(false); |
| 107 | mRtButton.setPressed(false); |
| 108 | break; |
| 109 | } |
| 110 | int x = (int) event.getX(); |
| 111 | int titleRight = mTitleBg.getRight(); |
| 112 | if (mTitleBg.isPressed() && x > titleRight + slop) { |
| 113 | mTitleBg.setPressed(false); |
| 114 | } else if (mRtButton.isPressed() && x < titleRight - slop) { |
| 115 | mRtButton.setPressed(false); |
| 116 | } |
| 117 | break; |
| 118 | case MotionEvent.ACTION_CANCEL: |
| 119 | mRtButton.setPressed(false); |
| 120 | mTitleBg.setPressed(false); |
| 121 | break; |
| 122 | case MotionEvent.ACTION_UP: |
| 123 | if (mRtButton.isPressed()) { |
| 124 | if (mInLoad) { |
| 125 | mBrowserActivity.stopLoading(); |
| 126 | } else { |
| 127 | mBrowserActivity.bookmarksOrHistoryPicker(false); |
| 128 | } |
| 129 | mRtButton.setPressed(false); |
| 130 | } else if (mTitleBg.isPressed()) { |
| 131 | mBrowserActivity.onSearchRequested(); |
| 132 | mTitleBg.setPressed(false); |
| 133 | } |
| 134 | break; |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | return true; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 141 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 142 | * Return whether the associated WebView is currently loading. Needed to |
| 143 | * determine whether a click should stop the load or close the tab. |
| 144 | */ |
| 145 | /* package */ boolean isInLoad() { |
| 146 | return mInLoad; |
| 147 | } |
| 148 | |
| 149 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 150 | * Set a new Bitmap for the Favicon. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 151 | */ |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 152 | /* package */ void setFavicon(Bitmap icon) { |
| 153 | Drawable[] array = new Drawable[3]; |
| 154 | array[0] = new PaintDrawable(Color.BLACK); |
| 155 | PaintDrawable p = new PaintDrawable(Color.WHITE); |
| 156 | array[1] = p; |
| 157 | if (icon == null) { |
| 158 | array[2] = mGenericFavicon; |
| 159 | } else { |
| 160 | array[2] = new BitmapDrawable(icon); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 161 | } |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 162 | LayerDrawable d = new LayerDrawable(array); |
| 163 | d.setLayerInset(1, 1, 1, 1, 1); |
| 164 | d.setLayerInset(2, 2, 2, 2, 2); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 165 | mFavicon.setImageDrawable(d); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 168 | /** |
| 169 | * Set the Drawable for the lock icon, or null to hide it. |
| 170 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 171 | /* package */ void setLock(Drawable d) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 172 | if (null == d) { |
| 173 | mLockIcon.setVisibility(View.GONE); |
| 174 | } else { |
| 175 | mLockIcon.setImageDrawable(d); |
| 176 | mLockIcon.setVisibility(View.VISIBLE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 180 | /** |
| 181 | * Update the progress, from 0 to 100. |
| 182 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 183 | /* package */ void setProgress(int newProgress) { |
Nicolas Roard | 1382b49 | 2009-09-16 21:50:06 +0100 | [diff] [blame^] | 184 | if (newProgress >= mHorizontalProgress.getMax()) { |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 185 | mTitle.setCompoundDrawables(null, null, null, null); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 186 | ((Animatable) mCircularProgress).stop(); |
| 187 | mHorizontalProgress.setVisibility(View.INVISIBLE); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 188 | if (mBookmarkDrawable != null) { |
| 189 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 190 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 191 | mInLoad = false; |
| 192 | } else { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 193 | mHorizontalProgress.setProgress(newProgress); |
Nicolas Roard | 1382b49 | 2009-09-16 21:50:06 +0100 | [diff] [blame^] | 194 | if (!mInLoad && getWindowToken() != null) { |
| 195 | // checking the window token lets us be sure that we |
| 196 | // are attached to a window before starting the animation, |
| 197 | // preventing a potential race condition |
| 198 | // (fix for bug http://b/2115736) |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 199 | mTitle.setCompoundDrawables(null, null, mCircularProgress, |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 200 | null); |
| 201 | ((Animatable) mCircularProgress).start(); |
| 202 | mHorizontalProgress.setVisibility(View.VISIBLE); |
| 203 | if (mBookmarkDrawable == null) { |
| 204 | mBookmarkDrawable = mRtButton.getDrawable(); |
| 205 | } |
| 206 | if (mStopDrawable == null) { |
Leon Scroggins | 8f308a5 | 2009-09-16 17:31:59 -0400 | [diff] [blame] | 207 | mRtButton.setImageResource(R.drawable.ic_btn_close_panel); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 208 | mStopDrawable = mRtButton.getDrawable(); |
| 209 | } else { |
| 210 | mRtButton.setImageDrawable(mStopDrawable); |
| 211 | } |
| 212 | mInLoad = true; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 213 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 217 | /** |
| 218 | * Update the title and url. |
| 219 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 220 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 221 | if (url == null) { |
| 222 | mTitle.setText(R.string.title_bar_loading); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 223 | } else { |
Leon Scroggins | c62e908 | 2009-09-03 10:20:44 -0400 | [diff] [blame] | 224 | mTitle.setText(url.toString()); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 225 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* package */ void setToTabPicker() { |
| 229 | mTitle.setText(R.string.tab_picker_title); |
| 230 | setFavicon(null); |
| 231 | setLock(null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 232 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 233 | } |
| 234 | } |