blob: 100e8d7eed5e34bbd2c89e89f52fe412c9f14e51 [file] [log] [blame]
John Reck0f602f32011-07-07 15:38:43 -07001/*
2 * Copyright (C) 2011 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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reck0f602f32011-07-07 15:38:43 -070017
18import android.app.SearchManager;
kaiyizc4ada322013-07-30 09:58:07 +080019import android.content.ActivityNotFoundException;
John Reck0f602f32011-07-07 15:38:43 -070020import android.content.Context;
21import android.content.Intent;
22import android.graphics.Bitmap;
23import android.graphics.drawable.Drawable;
kaiyizc4ada322013-07-30 09:58:07 +080024import android.net.Uri;
John Reck0f602f32011-07-07 15:38:43 -070025import android.os.Bundle;
Narayan Kamathf3174a52011-11-17 14:43:32 +000026import android.text.Editable;
27import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070028import android.util.AttributeSet;
kaiyizc4ada322013-07-30 09:58:07 +080029import android.util.Log;
John Reck0f602f32011-07-07 15:38:43 -070030import android.view.KeyEvent;
John Reck0f602f32011-07-07 15:38:43 -070031import android.view.View;
32import android.view.View.OnClickListener;
33import android.view.View.OnFocusChangeListener;
John Reck0f602f32011-07-07 15:38:43 -070034import android.widget.ImageView;
35import android.widget.LinearLayout;
kaiyizc4ada322013-07-30 09:58:07 +080036import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070037
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.R;
39import com.android.browser.UrlInputView.UrlInputListener;
John Reck0f602f32011-07-07 15:38:43 -070040
kaiyizc4ada322013-07-30 09:58:07 +080041import java.io.UnsupportedEncodingException;
kaiyiz950eca22013-08-08 11:01:28 +080042import java.net.URISyntaxException;
kaiyizc4ada322013-07-30 09:58:07 +080043
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080044import org.codeaurora.swe.WebView;
45
John Reck42229bc2011-08-19 13:26:43 -070046public class NavigationBarBase extends LinearLayout implements
47 OnClickListener, UrlInputListener, OnFocusChangeListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000048 TextWatcher {
John Reck0f602f32011-07-07 15:38:43 -070049
kaiyizc4ada322013-07-30 09:58:07 +080050 private final static String TAG = "NavigationBarBase";
51
John Reck0f602f32011-07-07 15:38:43 -070052 protected BaseUi mBaseUi;
53 protected TitleBar mTitleBar;
54 protected UiController mUiController;
55 protected UrlInputView mUrlInput;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070056 protected LocationButton mLocationButton;
John Reck0f602f32011-07-07 15:38:43 -070057
58 private ImageView mFavicon;
59 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070060
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070061
John Reck0f602f32011-07-07 15:38:43 -070062 public NavigationBarBase(Context context) {
63 super(context);
64 }
65
66 public NavigationBarBase(Context context, AttributeSet attrs) {
67 super(context, attrs);
68 }
69
70 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
71 super(context, attrs, defStyle);
72 }
73
74 @Override
75 protected void onFinishInflate() {
76 super.onFinishInflate();
77 mLockIcon = (ImageView) findViewById(R.id.lock);
Tarun Nainani8eb00912014-07-17 12:28:32 -070078 mLocationButton = (LocationButton) findViewById(R.id.location_button);
John Reck0f602f32011-07-07 15:38:43 -070079 mFavicon = (ImageView) findViewById(R.id.favicon);
80 mUrlInput = (UrlInputView) findViewById(R.id.url);
81 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -070082 mUrlInput.setOnFocusChangeListener(this);
83 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +000084 mUrlInput.addTextChangedListener(this);
John Reck0f602f32011-07-07 15:38:43 -070085 }
86
87 public void setTitleBar(TitleBar titleBar) {
88 mTitleBar = titleBar;
89 mBaseUi = mTitleBar.getUi();
90 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -070091 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -070092 }
93
94 public void setLock(Drawable d) {
95 if (mLockIcon == null) return;
96 if (d == null) {
97 mLockIcon.setVisibility(View.GONE);
98 } else {
99 mLockIcon.setImageDrawable(d);
100 mLockIcon.setVisibility(View.VISIBLE);
101 }
102 }
103
104 public void setFavicon(Bitmap icon) {
105 if (mFavicon == null) return;
106 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
107 }
108
John Reck0f602f32011-07-07 15:38:43 -0700109 @Override
110 public void onClick(View v) {
John Reck0f602f32011-07-07 15:38:43 -0700111 }
112
113 @Override
114 public void onFocusChange(View view, boolean hasFocus) {
115 // if losing focus and not in touch mode, leave as is
116 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
117 setFocusState(hasFocus);
118 }
119 if (hasFocus) {
120 mBaseUi.showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700121 } else if (!mUrlInput.needsUpdate()) {
122 mUrlInput.dismissDropDown();
123 mUrlInput.hideIME();
124 if (mUrlInput.getText().length() == 0) {
125 Tab currentTab = mUiController.getTabControl().getCurrentTab();
126 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700127 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700128 }
129 }
130 mBaseUi.suggestHideTitleBar();
131 }
132 mUrlInput.clearNeedsUpdate();
133 }
134
135 protected void setFocusState(boolean focus) {
136 }
137
John Reck0f602f32011-07-07 15:38:43 -0700138 public boolean isEditingUrl() {
139 return mUrlInput.hasFocus();
140 }
141
142 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700143 WebView currentTopWebView = mUiController.getCurrentTopWebView();
144 if (currentTopWebView != null) {
145 currentTopWebView.requestFocus();
146 }
John Reck0f602f32011-07-07 15:38:43 -0700147 }
148
149 void setDisplayTitle(String title) {
150 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700151 if (!title.equals(mUrlInput.getText().toString())) {
152 mUrlInput.setText(title, false);
153 }
John Reck0f602f32011-07-07 15:38:43 -0700154 }
155 }
156
John Reck0f602f32011-07-07 15:38:43 -0700157 void setIncognitoMode(boolean incognito) {
158 mUrlInput.setIncognitoMode(incognito);
159 }
160
161 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000162 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700163 }
164
165 // UrlInputListener implementation
166
167 /**
168 * callback from suggestion dropdown
169 * user selected a suggestion
170 */
171 @Override
172 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700173 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700174 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800175 String url = null;
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700176 boolean wap2estore = getContext().getResources().getBoolean(R.bool.wap2estore);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700177 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
178 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800179 url = text;
180 } else {
181 url = UrlUtils.smartUrlFilter(text, false);
182 }
183
John Reck0f602f32011-07-07 15:38:43 -0700184 Tab t = mBaseUi.getActiveTab();
185 // Only shortcut javascript URIs for now, as there is special
186 // logic in UrlHandler for other schemas
187 if (url != null && t != null && url.startsWith("javascript:")) {
188 mUiController.loadUrl(t, url);
189 setDisplayTitle(text);
190 return;
191 }
kaiyizc4ada322013-07-30 09:58:07 +0800192
193 // add for carrier wap2estore feature
194 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700195 if (handleEstoreTypeUrl(url)) {
196 setDisplayTitle(text);
197 return;
198 }
kaiyizc4ada322013-07-30 09:58:07 +0800199 }
kaiyiz950eca22013-08-08 11:01:28 +0800200 // add for rtsp scheme feature
201 if (url != null && t != null && isRtspTypeUrl(url)) {
202 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700203 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800204 return;
205 }
206 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700207 // add for "wtai://wp/mc;" scheme feature
208 if (url != null && t != null && isMakeCallTypeUrl(url)) {
209 if (handleMakeCallTypeUrl(url)) {
210 return;
211 }
212 }
John Reck0f602f32011-07-07 15:38:43 -0700213 }
214 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700215 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700216 i.setAction(action);
217 i.putExtra(SearchManager.QUERY, text);
218 if (extra != null) {
219 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
220 }
221 if (source != null) {
222 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800223 appData.putString("source", source);
224 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700225 }
226 mUiController.handleNewIntent(i);
227 setDisplayTitle(text);
228 }
229
Vivek Sekhar5e631472014-03-31 23:59:10 -0700230 private boolean isMakeCallTypeUrl(String url) {
231 String utf8Url = null;
232 try {
233 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
234 } catch (UnsupportedEncodingException e) {
235 Log.e(TAG, "err " + e);
236 }
237 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
238 return true;
239 }
240 return false;
241 }
242
243 private boolean handleMakeCallTypeUrl(String url) {
244 // wtai://wp/mc;number
245 // number=string(phone-number)
246 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
247 Intent intent = new Intent(Intent.ACTION_VIEW,
248 Uri.parse(WebView.SCHEME_TEL +
249 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
250 getContext().startActivity(intent);
251 // before leaving BrowserActivity, close the empty child tab.
252 // If a new tab is created through JavaScript open to load this
253 // url, we would like to close it as we will load this url in a
254 // different Activity.
255 Tab current = mUiController.getCurrentTab();
256 if (current != null
257 && current.getWebView().copyBackForwardList().getSize() == 0) {
258 mUiController.closeCurrentTab();
259 }
260 return true;
261 }
262 return false;
263 }
264
kaiyizc4ada322013-07-30 09:58:07 +0800265 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700266 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800267 return true;
268 }
269 return false;
270 }
271
Vivek Sekharb54614f2014-05-01 19:03:37 -0700272 private boolean handleEstoreTypeUrl(String url) {
273 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800274 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700275 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800276 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700277
278 Intent intent;
279 // perform generic parsing of the URI to turn it into an Intent.
280 try {
281 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
282 } catch (URISyntaxException ex) {
283 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
284 return false;
285 }
286
kaiyizc4ada322013-07-30 09:58:07 +0800287 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800288 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800289 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800290 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800291 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800292 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800293 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700294
295 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800296 }
297
kaiyiz950eca22013-08-08 11:01:28 +0800298 private boolean isRtspTypeUrl(String url) {
299 String utf8Url = null;
300 try {
301 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
302 } catch (UnsupportedEncodingException e) {
303 Log.e(TAG, "err " + e);
304 }
305 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
306 return true;
307 }
308 return false;
309 }
310
311 private boolean handleRtspTypeUrl(String url) {
312 Intent intent;
313 // perform generic parsing of the URI to turn it into an Intent.
314 try {
315 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
316 } catch (URISyntaxException ex) {
317 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
318 return false;
319 }
320
321 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800322 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800323 } catch (ActivityNotFoundException ex) {
324 Log.w("Browser", "No resolveActivity " + url);
325 return false;
326 }
327 return true;
328 }
329
John Reck0f602f32011-07-07 15:38:43 -0700330 @Override
331 public void onDismiss() {
332 final Tab currentTab = mBaseUi.getActiveTab();
333 mBaseUi.hideTitleBar();
334 post(new Runnable() {
335 public void run() {
336 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700337 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700338 setDisplayTitle(currentTab.getUrl());
339 }
340 }
341 });
342 }
343
344 /**
345 * callback from the suggestion dropdown
346 * copy text to input field and stay in edit mode
347 */
348 @Override
349 public void onCopySuggestion(String text) {
350 mUrlInput.setText(text, true);
351 if (text != null) {
352 mUrlInput.setSelection(text.length());
353 }
354 }
355
356 public void setCurrentUrlIsBookmark(boolean isBookmark) {
357 }
358
359 @Override
360 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
361 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
362 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700363 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700364 }
365 return super.dispatchKeyEventPreIme(evt);
366 }
367
John Reck0f602f32011-07-07 15:38:43 -0700368 /**
369 * called from the Ui when the user wants to edit
370 * @param clearInput clear the input field
371 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700372 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700373 // editing takes preference of progress
374 setVisibility(View.VISIBLE);
375 if (mTitleBar.useQuickControls()) {
376 mTitleBar.getProgressView().setVisibility(View.GONE);
377 }
378 if (!mUrlInput.hasFocus()) {
379 mUrlInput.requestFocus();
380 }
381 if (clearInput) {
382 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700383 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700384 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700385 mUrlInput.showIME();
386 }
John Reck0f602f32011-07-07 15:38:43 -0700387 }
388
389 public void onProgressStarted() {
390 }
391
392 public void onProgressStopped() {
393 }
394
John Reck58891902011-08-11 17:48:53 -0700395 public boolean isMenuShowing() {
John Reck42229bc2011-08-19 13:26:43 -0700396 return false;
John Reck58891902011-08-11 17:48:53 -0700397 }
398
John Reck419f6b42011-08-16 16:10:51 -0700399 public void onTabDataChanged(Tab tab) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700400 mLocationButton.onTabDataChanged(tab);
John Reck419f6b42011-08-16 16:10:51 -0700401 }
402
Michael Kolb0b129122012-06-04 16:31:58 -0700403 public void onVoiceResult(String s) {
404 startEditingUrl(true, true);
405 onCopySuggestion(s);
406 }
407
Narayan Kamathf3174a52011-11-17 14:43:32 +0000408 @Override
409 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
410
411 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700412 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000413
414 @Override
415 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700416
John Reck0f602f32011-07-07 15:38:43 -0700417}