blob: 4e9312422016f5cea376dc2c644ba141c64b5e0c [file] [log] [blame]
John Reck0f602f32011-07-07 15:38:43 -07001/*
2 * Copyright (C) 2010 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.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.ObjectAnimator;
22import android.content.Context;
23import android.content.res.Resources;
24import android.view.Gravity;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.animation.Animation;
29import android.view.animation.Animation.AnimationListener;
30import android.view.animation.AnimationUtils;
31import android.view.animation.DecelerateInterpolator;
32import android.webkit.WebView;
33import android.widget.AbsoluteLayout;
34import android.widget.FrameLayout;
35import android.widget.RelativeLayout;
36
37
38/**
39 * Base class for a title bar used by the browser.
40 */
41public class TitleBar extends RelativeLayout {
42
43 private static final int PROGRESS_MAX = 100;
44 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
45
46 private UiController mUiController;
47 private BaseUi mBaseUi;
48 private FrameLayout mParent;
49 private PageProgressView mProgress;
50
51 private AutologinBar mAutoLogin;
52 private NavigationBarBase mNavBar;
53 private boolean mUseQuickControls;
54
55 //state
56 private boolean mShowing;
57 private boolean mInLoad;
58 private boolean mSkipTitleBarAnimations;
59 private Animator mTitleBarAnimator;
60
61 public TitleBar(Context context, UiController controller, BaseUi ui,
62 FrameLayout parent) {
63 super(context, null);
64 mUiController = controller;
65 mBaseUi = ui;
66 mParent = parent;
67 initLayout(context);
68 }
69
70 private void initLayout(Context context) {
71 LayoutInflater factory = LayoutInflater.from(context);
72 factory.inflate(R.layout.title_bar, this);
73 mProgress = (PageProgressView) findViewById(R.id.progress);
74 mAutoLogin = (AutologinBar) findViewById(R.id.autologin);
75 mAutoLogin.setTitleBar(this);
76 mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar);
77 mNavBar.setTitleBar(this);
78 }
79
80 public BaseUi getUi() {
81 return mBaseUi;
82 }
83
84 public UiController getUiController() {
85 return mUiController;
86 }
87
88 public void setUseQuickControls(boolean use) {
89 mUseQuickControls = use;
90 setLayoutParams(makeLayoutParams());
91 }
92
93 void setShowProgressOnly(boolean progress) {
94 if (progress && !inAutoLogin()) {
95 mNavBar.setVisibility(View.GONE);
96 } else {
97 mNavBar.setVisibility(View.VISIBLE);
98 }
99 }
100
101 void setSkipTitleBarAnimations(boolean skip) {
102 mSkipTitleBarAnimations = skip;
103 }
104
105 void setupTitleBarAnimator(Animator animator) {
106 Resources res = mContext.getResources();
107 int duration = res.getInteger(R.integer.titlebar_animation_duration);
108 animator.setInterpolator(new DecelerateInterpolator(
109 ANIM_TITLEBAR_DECELERATE));
110 animator.setDuration(duration);
111 }
112
113 void show() {
114 if (mUseQuickControls) {
115 mParent.addView(this);
116 } else {
117 if (!mSkipTitleBarAnimations) {
118 cancelTitleBarAnimation(false);
119 int visibleHeight = getVisibleTitleHeight();
120 float startPos = (-getEmbeddedHeight() + visibleHeight);
121 if (getTranslationY() != 0) {
122 startPos = Math.max(startPos, getTranslationY());
123 }
124 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
125 "translationY",
126 startPos, 0);
127 setupTitleBarAnimator(mTitleBarAnimator);
128 mTitleBarAnimator.start();
129 }
130 mBaseUi.setTitleGravity(Gravity.TOP);
131 }
132 mShowing = true;
133 }
134
135 void hide() {
136 if (mUseQuickControls) {
137 mParent.removeView(this);
138 } else {
139 if (!mSkipTitleBarAnimations) {
140 cancelTitleBarAnimation(false);
141 int visibleHeight = getVisibleTitleHeight();
142 mTitleBarAnimator = ObjectAnimator.ofFloat(this,
143 "translationY", getTranslationY(),
144 (-getEmbeddedHeight() + visibleHeight));
145 mTitleBarAnimator.addListener(mHideTileBarAnimatorListener);
146 setupTitleBarAnimator(mTitleBarAnimator);
147 mTitleBarAnimator.start();
148 } else {
149 mBaseUi.setTitleGravity(Gravity.NO_GRAVITY);
150 }
151 }
152 mShowing = false;
153 }
154
155 boolean isShowing() {
156 return mShowing;
157 }
158
159 void cancelTitleBarAnimation(boolean reset) {
160 if (mTitleBarAnimator != null) {
161 mTitleBarAnimator.cancel();
162 mTitleBarAnimator = null;
163 }
164 if (reset) {
165 setTranslationY(0);
166 }
167 }
168
169 private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
170
171 boolean mWasCanceled;
172 @Override
173 public void onAnimationStart(Animator animation) {
174 mWasCanceled = false;
175 }
176
177 @Override
178 public void onAnimationRepeat(Animator animation) {
179 }
180
181 @Override
182 public void onAnimationEnd(Animator animation) {
183 if (!mWasCanceled) {
184 setTranslationY(0);
185 }
186 mBaseUi.setTitleGravity(Gravity.NO_GRAVITY);
187 }
188
189 @Override
190 public void onAnimationCancel(Animator animation) {
191 mWasCanceled = true;
192 }
193 };
194
195 private int getVisibleTitleHeight() {
196 Tab tab = mBaseUi.getActiveTab();
197 WebView webview = tab != null ? tab.getWebView() : null;
198 return webview != null ? webview.getVisibleTitleHeight() : 0;
199 }
200
201 /**
202 * Update the progress, from 0 to 100.
203 */
204 public void setProgress(int newProgress) {
205 if (newProgress >= PROGRESS_MAX) {
206 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
207 mProgress.setVisibility(View.GONE);
208 mInLoad = false;
209 mNavBar.onProgressStopped();
210 // check if needs to be hidden
211 if (!isEditingUrl() && !inAutoLogin()) {
212 hide();
213 if (mUseQuickControls) {
214 setShowProgressOnly(false);
215 }
216 }
217 } else {
218 if (!mInLoad) {
219 mProgress.setVisibility(View.VISIBLE);
220 mInLoad = true;
221 mNavBar.onProgressStarted();
222 }
223 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
224 / PROGRESS_MAX);
225 if (!mShowing) {
226 if (mUseQuickControls && !isEditingUrl()) {
227 setShowProgressOnly(true);
228 }
229 show();
230 }
231 }
232 }
233
234 public int getEmbeddedHeight() {
235 int height = mNavBar.getHeight();
236 if (mAutoLogin.getVisibility() == View.VISIBLE) {
237 height += mAutoLogin.getHeight();
238 }
239 return height;
240 }
241
242 public void updateAutoLogin(Tab tab, boolean animate) {
243 mAutoLogin.updateAutoLogin(tab, animate);
244 }
245
246 public void showAutoLogin(boolean animate) {
247 if (mUseQuickControls) {
248 mBaseUi.showTitleBar();
249 }
250 mAutoLogin.setVisibility(View.VISIBLE);
251 if (animate) {
252 mAutoLogin.startAnimation(AnimationUtils.loadAnimation(
253 getContext(), R.anim.autologin_enter));
254 }
255 }
256
257 public void hideAutoLogin(boolean animate) {
258 if (mUseQuickControls) {
259 mBaseUi.hideTitleBar();
260 mAutoLogin.setVisibility(View.GONE);
261 mBaseUi.refreshWebView();
262 } else {
263 if (animate) {
264 Animation anim = AnimationUtils.loadAnimation(getContext(),
265 R.anim.autologin_exit);
266 anim.setAnimationListener(new AnimationListener() {
267 @Override
268 public void onAnimationEnd(Animation a) {
269 mAutoLogin.setVisibility(View.GONE);
270 mBaseUi.refreshWebView();
271 }
272
273 @Override
274 public void onAnimationStart(Animation a) {
275 }
276
277 @Override
278 public void onAnimationRepeat(Animation a) {
279 }
280 });
281 mAutoLogin.startAnimation(anim);
282 } else if (mAutoLogin.getAnimation() == null) {
283 mAutoLogin.setVisibility(View.GONE);
284 mBaseUi.refreshWebView();
285 }
286 }
287 }
288
289 public boolean inAutoLogin() {
290 return mAutoLogin.getVisibility() == View.VISIBLE;
291 }
292
293 public boolean isEditingUrl() {
294 return mNavBar.isEditingUrl();
295 }
296
297 public WebView getCurrentWebView() {
298 Tab t = mBaseUi.getActiveTab();
299 if (t != null) {
300 return t.getWebView();
301 } else {
302 return null;
303 }
304 }
305
306 public PageProgressView getProgressView() {
307 return mProgress;
308 }
309
310 public NavigationBarBase getNavigationBar() {
311 return mNavBar;
312 }
313
314 public boolean useQuickControls() {
315 return mUseQuickControls;
316 }
317
318 public boolean isInLoad() {
319 return mInLoad;
320 }
321
322 private ViewGroup.LayoutParams makeLayoutParams() {
323 if (mUseQuickControls) {
324 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
325 LayoutParams.WRAP_CONTENT);
326 } else {
327 return new AbsoluteLayout.LayoutParams(
328 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
329 0, 0);
330 }
331 }
332
333 @Override
334 public View focusSearch(View focused, int dir) {
335 if (FOCUS_DOWN == dir && hasFocus()) {
336 return getCurrentWebView();
337 }
338 return super.focusSearch(focused, dir);
339 }
340
341}