blob: 3bdc18bdd868cdbb002496f5f0fc250c8ed56b9d [file] [log] [blame]
Leon Scroggins81db3662009-06-04 17:45:11 -04001/*
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
17package com.android.browser;
18
19import android.content.Context;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040020import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.Color;
Leon Scroggins1f005d32009-08-10 17:36:42 -040023import android.graphics.Rect;
Leon Scroggins62e8f942009-09-03 15:08:54 -040024import android.graphics.drawable.Animatable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040025import android.graphics.drawable.BitmapDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040026import android.graphics.drawable.Drawable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040027import android.graphics.drawable.LayerDrawable;
28import android.graphics.drawable.PaintDrawable;
29import android.util.TypedValue;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040030import android.view.LayoutInflater;
Leon Scroggins68579392009-09-15 15:31:54 -040031import android.view.MotionEvent;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040032import android.view.View;
Leon Scroggins68579392009-09-15 15:31:54 -040033import android.view.ViewConfiguration;
Leon Scroggins81db3662009-06-04 17:45:11 -040034import android.widget.ImageView;
35import android.widget.LinearLayout;
36import android.widget.ProgressBar;
37import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040038
Leon Scroggins1f005d32009-08-10 17:36:42 -040039/**
40 * This class represents a title bar for a particular "tab" or "window" in the
41 * browser.
42 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040043public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040044 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040045 private Drawable mCloseDrawable;
46 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040047 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040048 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040049 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040050 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040051 private Drawable mStopDrawable;
52 private Drawable mBookmarkDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040053 private boolean mInLoad;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040054 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040055 private Drawable mGenericFavicon;
56 private int mIconDimension;
Leon Scroggins68579392009-09-15 15:31:54 -040057 private View mTitleBg;
Leon Scroggins81db3662009-06-04 17:45:11 -040058
Leon Scroggins68579392009-09-15 15:31:54 -040059 public TitleBar(BrowserActivity context) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040060 super(context, null);
Leon Scroggins81db3662009-06-04 17:45:11 -040061 LayoutInflater factory = LayoutInflater.from(context);
62 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040063 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040064
65 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040066 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040067
Leon Scroggins68579392009-09-15 15:31:54 -040068 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040069 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040070 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040071
Leon Scrogginsa81a7642009-08-31 17:05:41 -040072 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040073 Resources resources = context.getResources();
74 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040075 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040076 mIconDimension = (int) TypedValue.applyDimension(
77 TypedValue.COMPLEX_UNIT_DIP, 20f,
78 resources.getDisplayMetrics());
79 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040080 mHorizontalProgress = (ProgressBar) findViewById(
81 R.id.progress_horizontal);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040082 mGenericFavicon = context.getResources().getDrawable(
83 R.drawable.app_web_browser_sm);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040084 }
85
Leon Scroggins68579392009-09-15 15:31:54 -040086 @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 Scroggins81db3662009-06-04 17:45:11 -0400139 }
140
Leon Scroggins1f005d32009-08-10 17:36:42 -0400141 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400142 * 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 Scroggins3bbb6ca2009-09-09 12:51:10 -0400150 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400151 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400152 /* 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 Scrogginsa81a7642009-08-31 17:05:41 -0400161 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400162 LayerDrawable d = new LayerDrawable(array);
163 d.setLayerInset(1, 1, 1, 1, 1);
164 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400165 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400166 }
167
Leon Scroggins1f005d32009-08-10 17:36:42 -0400168 /**
169 * Set the Drawable for the lock icon, or null to hide it.
170 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400171 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400172 if (null == d) {
173 mLockIcon.setVisibility(View.GONE);
174 } else {
175 mLockIcon.setImageDrawable(d);
176 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400177 }
178 }
179
Leon Scroggins1f005d32009-08-10 17:36:42 -0400180 /**
181 * Update the progress, from 0 to 100.
182 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400183 /* package */ void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100184 if (newProgress >= mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400185 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400186 ((Animatable) mCircularProgress).stop();
187 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400188 if (mBookmarkDrawable != null) {
189 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400190 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400191 mInLoad = false;
192 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400193 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100194 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 Scrogginsf4bb18a2009-09-11 18:37:53 -0400199 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400200 null);
201 ((Animatable) mCircularProgress).start();
202 mHorizontalProgress.setVisibility(View.VISIBLE);
203 if (mBookmarkDrawable == null) {
204 mBookmarkDrawable = mRtButton.getDrawable();
205 }
206 if (mStopDrawable == null) {
Leon Scroggins8f308a52009-09-16 17:31:59 -0400207 mRtButton.setImageResource(R.drawable.ic_btn_close_panel);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400208 mStopDrawable = mRtButton.getDrawable();
209 } else {
210 mRtButton.setImageDrawable(mStopDrawable);
211 }
212 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400213 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400214 }
215 }
216
Leon Scroggins1f005d32009-08-10 17:36:42 -0400217 /**
218 * Update the title and url.
219 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400220 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400221 if (url == null) {
222 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400223 } else {
Leon Scrogginsc62e9082009-09-03 10:20:44 -0400224 mTitle.setText(url.toString());
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400225 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400226 }
227
228 /* package */ void setToTabPicker() {
229 mTitle.setText(R.string.tab_picker_title);
230 setFavicon(null);
231 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400232 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400233 }
234}