blob: 7e8ea1ac1b83f7ee7b9bd98c67166f0ceff6f2d7 [file] [log] [blame]
Leon Scroggins571b3762010-05-26 10:25:01 -04001/*
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
Michael Kolb11d19782011-03-20 10:17:40 -070019import com.android.browser.UI.DropdownChangeListener;
John Reck92026732011-02-15 10:12:30 -080020import com.android.browser.UrlInputView.UrlInputListener;
Michael Kolb11d19782011-03-20 10:17:40 -070021import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
John Reck92026732011-02-15 10:12:30 -080022
23import android.app.SearchManager;
Leon Scroggins571b3762010-05-26 10:25:01 -040024import android.content.Context;
John Reck92026732011-02-15 10:12:30 -080025import android.content.Intent;
Leon Scroggins571b3762010-05-26 10:25:01 -040026import android.graphics.Bitmap;
27import android.graphics.Color;
28import android.graphics.drawable.BitmapDrawable;
29import android.graphics.drawable.Drawable;
30import android.graphics.drawable.LayerDrawable;
31import android.graphics.drawable.PaintDrawable;
John Reck92026732011-02-15 10:12:30 -080032import android.os.Bundle;
33import android.speech.RecognizerResultsIntent;
Michael Kolb11d19782011-03-20 10:17:40 -070034import android.text.TextUtils;
35import android.view.ContextThemeWrapper;
Michael Kolb7cdc4902011-02-03 17:54:40 -080036import android.view.Gravity;
Michael Kolb11d19782011-03-20 10:17:40 -070037import android.view.KeyEvent;
38import android.view.LayoutInflater;
Leon Scroggins571b3762010-05-26 10:25:01 -040039import android.view.View;
Michael Kolb11d19782011-03-20 10:17:40 -070040import android.view.View.OnClickListener;
41import android.view.View.OnFocusChangeListener;
42import android.view.animation.Animation;
43import android.view.animation.Animation.AnimationListener;
44import android.view.animation.AnimationUtils;
45import android.webkit.WebView;
Michael Kolb7cdc4902011-02-03 17:54:40 -080046import android.widget.AbsoluteLayout;
Michael Kolb11d19782011-03-20 10:17:40 -070047import android.widget.ArrayAdapter;
48import android.widget.Button;
49import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040050import android.widget.ImageView;
Michael Kolb11d19782011-03-20 10:17:40 -070051import android.widget.ProgressBar;
52import android.widget.RelativeLayout;
53import android.widget.Spinner;
54import android.widget.TextView;
55
56import java.util.List;
Leon Scroggins571b3762010-05-26 10:25:01 -040057
58/**
59 * Base class for a title bar used by the browser.
60 */
Michael Kolb11d19782011-03-20 10:17:40 -070061public class TitleBarBase extends RelativeLayout
62 implements OnClickListener, OnFocusChangeListener, UrlInputListener,
63 TextChangeWatcher, DeviceAccountLogin.AutoLoginCallback {
John Reck94b7e042011-02-15 15:02:33 -080064
65 protected static final int PROGRESS_MAX = 100;
66
Leon Scroggins571b3762010-05-26 10:25:01 -040067 // These need to be set by the subclass.
68 protected ImageView mFavicon;
69 protected ImageView mLockIcon;
70
Michael Kolbfe251992010-07-08 15:41:55 -070071 protected Drawable mGenericFavicon;
John Reck92026732011-02-15 10:12:30 -080072 protected UiController mUiController;
73 protected BaseUi mBaseUi;
Michael Kolb11d19782011-03-20 10:17:40 -070074
John Reck92026732011-02-15 10:12:30 -080075 protected UrlInputView mUrlInput;
76 protected boolean mInVoiceMode;
Leon Scroggins571b3762010-05-26 10:25:01 -040077
Michael Kolb11d19782011-03-20 10:17:40 -070078 // Auto-login UI
79 protected View mAutoLogin;
80 protected Spinner mAutoLoginAccount;
81 protected Button mAutoLoginLogin;
82 protected ProgressBar mAutoLoginProgress;
83 protected TextView mAutoLoginError;
84 protected ImageButton mAutoLoginCancel;
85 protected DeviceAccountLogin mAutoLoginHandler;
86 protected ArrayAdapter<String> mAccountsAdapter;
87
88
John Reck92026732011-02-15 10:12:30 -080089 public TitleBarBase(Context context, UiController controller, BaseUi ui) {
Leon Scroggins571b3762010-05-26 10:25:01 -040090 super(context, null);
John Reck92026732011-02-15 10:12:30 -080091 mUiController = controller;
92 mBaseUi = ui;
Leon Scroggins571b3762010-05-26 10:25:01 -040093 mGenericFavicon = context.getResources().getDrawable(
94 R.drawable.app_web_browser_sm);
95 }
96
Michael Kolb11d19782011-03-20 10:17:40 -070097 protected void initLayout(Context context, int layoutId) {
98 LayoutInflater factory = LayoutInflater.from(context);
99 factory.inflate(layoutId, this);
100
101 mUrlInput = (UrlInputView) findViewById(R.id.url);
102 mLockIcon = (ImageView) findViewById(R.id.lock);
103 mUrlInput.setUrlInputListener(this);
104 mUrlInput.setController(mUiController);
105 mUrlInput.setOnFocusChangeListener(this);
106 mUrlInput.setSelectAllOnFocus(true);
107 mUrlInput.addQueryTextWatcher(this);
108 mAutoLogin = findViewById(R.id.autologin);
109 mAutoLoginAccount = (Spinner) findViewById(R.id.autologin_account);
110 mAutoLoginLogin = (Button) findViewById(R.id.autologin_login);
111 mAutoLoginLogin.setOnClickListener(this);
112 mAutoLoginProgress = (ProgressBar) findViewById(R.id.autologin_progress);
113 mAutoLoginError = (TextView) findViewById(R.id.autologin_error);
114 mAutoLoginCancel = (ImageButton) mAutoLogin.findViewById(R.id.autologin_close);
115 mAutoLoginCancel.setOnClickListener(this);
116 }
117
118 protected void setupUrlInput() {
119 }
120
Leon Scroggins571b3762010-05-26 10:25:01 -0400121 /* package */ void setProgress(int newProgress) {}
Leon Scroggins571b3762010-05-26 10:25:01 -0400122
123 /* package */ void setLock(Drawable d) {
124 assert mLockIcon != null;
125 if (null == d) {
126 mLockIcon.setVisibility(View.GONE);
127 } else {
128 mLockIcon.setImageDrawable(d);
129 mLockIcon.setVisibility(View.VISIBLE);
130 }
131 }
132
133 /* package */ void setFavicon(Bitmap icon) {
134 assert mFavicon != null;
135 Drawable[] array = new Drawable[3];
136 array[0] = new PaintDrawable(Color.BLACK);
137 PaintDrawable p = new PaintDrawable(Color.WHITE);
138 array[1] = p;
139 if (icon == null) {
140 array[2] = mGenericFavicon;
141 } else {
142 array[2] = new BitmapDrawable(icon);
143 }
144 LayerDrawable d = new LayerDrawable(array);
145 d.setLayerInset(1, 1, 1, 1, 1);
146 d.setLayerInset(2, 2, 2, 2, 2);
147 mFavicon.setImageDrawable(d);
148 }
149
Michael Kolb7cdc4902011-02-03 17:54:40 -0800150 void setTitleGravity(int gravity) {
151 int newTop = 0;
152 if (gravity != Gravity.NO_GRAVITY) {
153 View parent = (View) getParent();
154 if (parent != null) {
155 if (gravity == Gravity.TOP) {
156 newTop = parent.getScrollY();
157 } else if (gravity == Gravity.BOTTOM) {
158 newTop = parent.getScrollY() + parent.getHeight() - getHeight();
159 }
160 }
161 }
162 AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) getLayoutParams();
163 if (lp != null) {
164 lp.y = newTop;
165 setLayoutParams(lp);
166 }
167 }
168
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800169 public int getEmbeddedHeight() {
170 return getHeight();
171 }
172
Michael Kolb11d19782011-03-20 10:17:40 -0700173 protected void updateAutoLogin(Tab tab, boolean animate) {
174 DeviceAccountLogin login = tab.getDeviceAccountLogin();
175 if (login != null) {
176 mAutoLoginHandler = login;
177 ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext,
178 android.R.style.Theme_Holo_Light);
179 mAccountsAdapter = new ArrayAdapter<String>(wrapper,
180 android.R.layout.simple_spinner_item, login.getAccountNames());
181 mAccountsAdapter.setDropDownViewResource(
182 android.R.layout.simple_spinner_dropdown_item);
183 mAutoLoginAccount.setAdapter(mAccountsAdapter);
184 mAutoLoginAccount.setSelection(0);
185 mAutoLoginAccount.setEnabled(true);
186 mAutoLoginLogin.setEnabled(true);
187 mAutoLoginProgress.setVisibility(View.GONE);
188 mAutoLoginError.setVisibility(View.GONE);
189 switch (login.getState()) {
190 case DeviceAccountLogin.PROCESSING:
191 mAutoLoginAccount.setEnabled(false);
192 mAutoLoginLogin.setEnabled(false);
193 mAutoLoginProgress.setVisibility(View.VISIBLE);
194 break;
195 case DeviceAccountLogin.FAILED:
196 mAutoLoginProgress.setVisibility(View.GONE);
197 mAutoLoginError.setVisibility(View.VISIBLE);
198 break;
199 case DeviceAccountLogin.INITIAL:
200 break;
201 default:
202 throw new IllegalStateException();
203 }
204 showAutoLogin(animate);
Michael Kolb43909f22011-03-23 13:18:36 -0700205 } else {
206 hideAutoLogin(animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700207 }
208 }
209
210 protected void showAutoLogin(boolean animate) {
211 mAutoLogin.setVisibility(View.VISIBLE);
212 if (animate) {
213 mAutoLogin.startAnimation(AnimationUtils.loadAnimation(
214 getContext(), R.anim.autologin_enter));
215 }
216 }
217
218 protected void hideAutoLogin(boolean animate) {
219 mAutoLoginHandler = null;
220 if (animate) {
221 Animation anim = AnimationUtils.loadAnimation(
222 getContext(), R.anim.autologin_exit);
223 anim.setAnimationListener(new AnimationListener() {
224 @Override public void onAnimationEnd(Animation a) {
225 mAutoLogin.setVisibility(View.GONE);
226 mBaseUi.refreshWebView();
227 }
228 @Override public void onAnimationStart(Animation a) {}
229 @Override public void onAnimationRepeat(Animation a) {}
230 });
231 mAutoLogin.startAnimation(anim);
232 } else if (mAutoLogin.getAnimation() == null) {
233 mAutoLogin.setVisibility(View.GONE);
234 mBaseUi.refreshWebView();
235 }
236 }
237
238 @Override
239 public void loginFailed() {
240 mAutoLoginAccount.setEnabled(true);
241 mAutoLoginLogin.setEnabled(true);
242 mAutoLoginProgress.setVisibility(View.GONE);
243 mAutoLoginError.setVisibility(View.VISIBLE);
244 }
245
246
247 protected boolean inAutoLogin() {
248 return mAutoLoginHandler != null;
249 }
250
251 @Override
252 public void onClick(View v) {
253 if (mAutoLoginCancel == v) {
254 if (mAutoLoginHandler != null) {
255 mAutoLoginHandler.cancel();
256 mAutoLoginHandler = null;
257 }
258 hideAutoLogin(true);
259 } else if (mAutoLoginLogin == v) {
260 if (mAutoLoginHandler != null) {
261 mAutoLoginAccount.setEnabled(false);
262 mAutoLoginLogin.setEnabled(false);
263 mAutoLoginProgress.setVisibility(View.VISIBLE);
264 mAutoLoginError.setVisibility(View.GONE);
265 mAutoLoginHandler.login(
266 mAutoLoginAccount.getSelectedItemPosition(), this);
267 }
268 }
269 }
270
271 @Override
272 public void onFocusChange(View view, boolean hasFocus) {
273 // if losing focus and not in touch mode, leave as is
274 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
275 setFocusState(hasFocus);
276 }
277 if (hasFocus) {
278 mUrlInput.forceIme();
279 if (mInVoiceMode) {
280 mUrlInput.forceFilter();
281 }
282 } else if (!mUrlInput.needsUpdate()) {
283 mUrlInput.dismissDropDown();
284 mUrlInput.hideIME();
285 if (mUrlInput.getText().length() == 0) {
286 Tab currentTab = mUiController.getTabControl().getCurrentTab();
287 if (currentTab != null) {
288 mUrlInput.setText(currentTab.getUrl(), false);
289 }
290 }
291 }
292 mUrlInput.clearNeedsUpdate();
293 }
294
295 protected void setFocusState(boolean focus) {
296 if (focus) {
297 updateSearchMode(false);
298 }
299 }
300
301 protected void updateSearchMode(boolean userEdited) {
302 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
303 }
304
305 protected void setSearchMode(boolean voiceSearchEnabled) {}
306
307 boolean isEditingUrl() {
308 return mUrlInput.hasFocus();
309 }
310
311 void stopEditingUrl() {
312 mUrlInput.clearFocus();
313 }
314
315 void setDisplayTitle(String title) {
316 if (!isEditingUrl()) {
317 mUrlInput.setText(title, false);
318 }
319 }
320
321 // UrlInput text watcher
322
323 @Override
324 public void onTextChanged(String newText) {
325 if (mUrlInput.hasFocus()) {
326 // check if input field is empty and adjust voice search state
327 updateSearchMode(true);
328 // clear voice mode when user types
329 setInVoiceMode(false, null);
330 }
331 }
332
333 // voicesearch
334
335 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
336 mInVoiceMode = voicemode;
337 mUrlInput.setVoiceResults(voiceResults);
338 }
339
340 void setIncognitoMode(boolean incognito) {
341 mUrlInput.setIncognitoMode(incognito);
342 }
343
344 void clearCompletions() {
345 mUrlInput.setSuggestedText(null);
346 }
347
John Reck92026732011-02-15 10:12:30 -0800348 // UrlInputListener implementation
349
350 /**
351 * callback from suggestion dropdown
352 * user selected a suggestion
353 */
354 @Override
355 public void onAction(String text, String extra, String source) {
356 mUiController.getCurrentTopWebView().requestFocus();
357 mBaseUi.hideTitleBar();
358 Intent i = new Intent();
359 String action = null;
360 if (UrlInputView.VOICE.equals(source)) {
361 action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
362 source = null;
363 } else {
364 action = Intent.ACTION_SEARCH;
365 }
366 i.setAction(action);
367 i.putExtra(SearchManager.QUERY, text);
368 if (extra != null) {
369 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
370 }
371 if (source != null) {
372 Bundle appData = new Bundle();
373 appData.putString(com.android.common.Search.SOURCE, source);
374 i.putExtra(SearchManager.APP_DATA, appData);
375 }
376 mUiController.handleNewIntent(i);
377 setDisplayTitle(text);
378 }
379
380 @Override
381 public void onDismiss() {
382 final Tab currentTab = mBaseUi.getActiveTab();
383 mBaseUi.hideTitleBar();
384 post(new Runnable() {
385 public void run() {
386 clearFocus();
387 if ((currentTab != null) && !mInVoiceMode) {
388 setDisplayTitle(currentTab.getUrl());
389 }
390 }
391 });
392 }
393
394 /**
395 * callback from the suggestion dropdown
396 * copy text to input field and stay in edit mode
397 */
398 @Override
399 public void onCopySuggestion(String text) {
400 mUrlInput.setText(text, true);
401 if (text != null) {
402 mUrlInput.setSelection(text.length());
403 }
404 }
405
John Reck94b7e042011-02-15 15:02:33 -0800406 public void setCurrentUrlIsBookmark(boolean isBookmark) {
407 }
408
Michael Kolb11d19782011-03-20 10:17:40 -0700409 @Override
410 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
411 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
412 // catch back key in order to do slightly more cleanup than usual
413 mUrlInput.clearFocus();
414 return true;
415 }
416 return super.dispatchKeyEventPreIme(evt);
417 }
418
419 protected WebView getCurrentWebView() {
420 Tab t = mBaseUi.getActiveTab();
421 if (t != null) {
422 return t.getWebView();
423 } else {
424 return null;
425 }
426 }
427
428 void registerDropdownChangeListener(DropdownChangeListener d) {
429 mUrlInput.registerDropdownChangeListener(d);
430 }
431
Leon Scroggins571b3762010-05-26 10:25:01 -0400432}