blob: ee4a2a68f23c2e82935ff7a1602c48d174be77b7 [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);
205 }
206 }
207
208 protected void showAutoLogin(boolean animate) {
209 mAutoLogin.setVisibility(View.VISIBLE);
210 if (animate) {
211 mAutoLogin.startAnimation(AnimationUtils.loadAnimation(
212 getContext(), R.anim.autologin_enter));
213 }
214 }
215
216 protected void hideAutoLogin(boolean animate) {
217 mAutoLoginHandler = null;
218 if (animate) {
219 Animation anim = AnimationUtils.loadAnimation(
220 getContext(), R.anim.autologin_exit);
221 anim.setAnimationListener(new AnimationListener() {
222 @Override public void onAnimationEnd(Animation a) {
223 mAutoLogin.setVisibility(View.GONE);
224 mBaseUi.refreshWebView();
225 }
226 @Override public void onAnimationStart(Animation a) {}
227 @Override public void onAnimationRepeat(Animation a) {}
228 });
229 mAutoLogin.startAnimation(anim);
230 } else if (mAutoLogin.getAnimation() == null) {
231 mAutoLogin.setVisibility(View.GONE);
232 mBaseUi.refreshWebView();
233 }
234 }
235
236 @Override
237 public void loginFailed() {
238 mAutoLoginAccount.setEnabled(true);
239 mAutoLoginLogin.setEnabled(true);
240 mAutoLoginProgress.setVisibility(View.GONE);
241 mAutoLoginError.setVisibility(View.VISIBLE);
242 }
243
244
245 protected boolean inAutoLogin() {
246 return mAutoLoginHandler != null;
247 }
248
249 @Override
250 public void onClick(View v) {
251 if (mAutoLoginCancel == v) {
252 if (mAutoLoginHandler != null) {
253 mAutoLoginHandler.cancel();
254 mAutoLoginHandler = null;
255 }
256 hideAutoLogin(true);
257 } else if (mAutoLoginLogin == v) {
258 if (mAutoLoginHandler != null) {
259 mAutoLoginAccount.setEnabled(false);
260 mAutoLoginLogin.setEnabled(false);
261 mAutoLoginProgress.setVisibility(View.VISIBLE);
262 mAutoLoginError.setVisibility(View.GONE);
263 mAutoLoginHandler.login(
264 mAutoLoginAccount.getSelectedItemPosition(), this);
265 }
266 }
267 }
268
269 @Override
270 public void onFocusChange(View view, boolean hasFocus) {
271 // if losing focus and not in touch mode, leave as is
272 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
273 setFocusState(hasFocus);
274 }
275 if (hasFocus) {
276 mUrlInput.forceIme();
277 if (mInVoiceMode) {
278 mUrlInput.forceFilter();
279 }
280 } else if (!mUrlInput.needsUpdate()) {
281 mUrlInput.dismissDropDown();
282 mUrlInput.hideIME();
283 if (mUrlInput.getText().length() == 0) {
284 Tab currentTab = mUiController.getTabControl().getCurrentTab();
285 if (currentTab != null) {
286 mUrlInput.setText(currentTab.getUrl(), false);
287 }
288 }
289 }
290 mUrlInput.clearNeedsUpdate();
291 }
292
293 protected void setFocusState(boolean focus) {
294 if (focus) {
295 updateSearchMode(false);
296 }
297 }
298
299 protected void updateSearchMode(boolean userEdited) {
300 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
301 }
302
303 protected void setSearchMode(boolean voiceSearchEnabled) {}
304
305 boolean isEditingUrl() {
306 return mUrlInput.hasFocus();
307 }
308
309 void stopEditingUrl() {
310 mUrlInput.clearFocus();
311 }
312
313 void setDisplayTitle(String title) {
314 if (!isEditingUrl()) {
315 mUrlInput.setText(title, false);
316 }
317 }
318
319 // UrlInput text watcher
320
321 @Override
322 public void onTextChanged(String newText) {
323 if (mUrlInput.hasFocus()) {
324 // check if input field is empty and adjust voice search state
325 updateSearchMode(true);
326 // clear voice mode when user types
327 setInVoiceMode(false, null);
328 }
329 }
330
331 // voicesearch
332
333 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
334 mInVoiceMode = voicemode;
335 mUrlInput.setVoiceResults(voiceResults);
336 }
337
338 void setIncognitoMode(boolean incognito) {
339 mUrlInput.setIncognitoMode(incognito);
340 }
341
342 void clearCompletions() {
343 mUrlInput.setSuggestedText(null);
344 }
345
John Reck92026732011-02-15 10:12:30 -0800346 // UrlInputListener implementation
347
348 /**
349 * callback from suggestion dropdown
350 * user selected a suggestion
351 */
352 @Override
353 public void onAction(String text, String extra, String source) {
354 mUiController.getCurrentTopWebView().requestFocus();
355 mBaseUi.hideTitleBar();
356 Intent i = new Intent();
357 String action = null;
358 if (UrlInputView.VOICE.equals(source)) {
359 action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
360 source = null;
361 } else {
362 action = Intent.ACTION_SEARCH;
363 }
364 i.setAction(action);
365 i.putExtra(SearchManager.QUERY, text);
366 if (extra != null) {
367 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
368 }
369 if (source != null) {
370 Bundle appData = new Bundle();
371 appData.putString(com.android.common.Search.SOURCE, source);
372 i.putExtra(SearchManager.APP_DATA, appData);
373 }
374 mUiController.handleNewIntent(i);
375 setDisplayTitle(text);
376 }
377
378 @Override
379 public void onDismiss() {
380 final Tab currentTab = mBaseUi.getActiveTab();
381 mBaseUi.hideTitleBar();
382 post(new Runnable() {
383 public void run() {
384 clearFocus();
385 if ((currentTab != null) && !mInVoiceMode) {
386 setDisplayTitle(currentTab.getUrl());
387 }
388 }
389 });
390 }
391
392 /**
393 * callback from the suggestion dropdown
394 * copy text to input field and stay in edit mode
395 */
396 @Override
397 public void onCopySuggestion(String text) {
398 mUrlInput.setText(text, true);
399 if (text != null) {
400 mUrlInput.setSelection(text.length());
401 }
402 }
403
John Reck94b7e042011-02-15 15:02:33 -0800404 public void setCurrentUrlIsBookmark(boolean isBookmark) {
405 }
406
Michael Kolb11d19782011-03-20 10:17:40 -0700407 @Override
408 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
409 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
410 // catch back key in order to do slightly more cleanup than usual
411 mUrlInput.clearFocus();
412 return true;
413 }
414 return super.dispatchKeyEventPreIme(evt);
415 }
416
417 protected WebView getCurrentWebView() {
418 Tab t = mBaseUi.getActiveTab();
419 if (t != null) {
420 return t.getWebView();
421 } else {
422 return null;
423 }
424 }
425
426 void registerDropdownChangeListener(DropdownChangeListener d) {
427 mUrlInput.registerDropdownChangeListener(d);
428 }
429
Leon Scroggins571b3762010-05-26 10:25:01 -0400430}