blob: f4d63d9281ec92394055f41873f17a2eda96bf56 [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;
40import com.android.browser.reflect.ReflectHelper;
John Reck0f602f32011-07-07 15:38:43 -070041
kaiyizc4ada322013-07-30 09:58:07 +080042import java.io.UnsupportedEncodingException;
kaiyiz950eca22013-08-08 11:01:28 +080043import java.net.URISyntaxException;
kaiyizc4ada322013-07-30 09:58:07 +080044
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080045import org.codeaurora.swe.WebView;
46
John Reck42229bc2011-08-19 13:26:43 -070047public class NavigationBarBase extends LinearLayout implements
48 OnClickListener, UrlInputListener, OnFocusChangeListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000049 TextWatcher {
John Reck0f602f32011-07-07 15:38:43 -070050
kaiyizc4ada322013-07-30 09:58:07 +080051 private final static String TAG = "NavigationBarBase";
52
John Reck0f602f32011-07-07 15:38:43 -070053 protected BaseUi mBaseUi;
54 protected TitleBar mTitleBar;
55 protected UiController mUiController;
56 protected UrlInputView mUrlInput;
John Reck0f602f32011-07-07 15:38:43 -070057
58 private ImageView mFavicon;
59 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070060
61 public NavigationBarBase(Context context) {
62 super(context);
63 }
64
65 public NavigationBarBase(Context context, AttributeSet attrs) {
66 super(context, attrs);
67 }
68
69 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
70 super(context, attrs, defStyle);
71 }
72
73 @Override
74 protected void onFinishInflate() {
75 super.onFinishInflate();
76 mLockIcon = (ImageView) findViewById(R.id.lock);
77 mFavicon = (ImageView) findViewById(R.id.favicon);
78 mUrlInput = (UrlInputView) findViewById(R.id.url);
79 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -070080 mUrlInput.setOnFocusChangeListener(this);
81 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +000082 mUrlInput.addTextChangedListener(this);
John Reck0f602f32011-07-07 15:38:43 -070083 }
84
85 public void setTitleBar(TitleBar titleBar) {
86 mTitleBar = titleBar;
87 mBaseUi = mTitleBar.getUi();
88 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -070089 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -070090 }
91
92 public void setLock(Drawable d) {
93 if (mLockIcon == null) return;
94 if (d == null) {
95 mLockIcon.setVisibility(View.GONE);
96 } else {
97 mLockIcon.setImageDrawable(d);
98 mLockIcon.setVisibility(View.VISIBLE);
99 }
100 }
101
102 public void setFavicon(Bitmap icon) {
103 if (mFavicon == null) return;
104 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
105 }
106
John Reck0f602f32011-07-07 15:38:43 -0700107 @Override
108 public void onClick(View v) {
John Reck0f602f32011-07-07 15:38:43 -0700109 }
110
111 @Override
112 public void onFocusChange(View view, boolean hasFocus) {
113 // if losing focus and not in touch mode, leave as is
114 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
115 setFocusState(hasFocus);
116 }
117 if (hasFocus) {
118 mBaseUi.showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700119 } else if (!mUrlInput.needsUpdate()) {
120 mUrlInput.dismissDropDown();
121 mUrlInput.hideIME();
122 if (mUrlInput.getText().length() == 0) {
123 Tab currentTab = mUiController.getTabControl().getCurrentTab();
124 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700125 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700126 }
127 }
128 mBaseUi.suggestHideTitleBar();
129 }
130 mUrlInput.clearNeedsUpdate();
131 }
132
133 protected void setFocusState(boolean focus) {
134 }
135
John Reck0f602f32011-07-07 15:38:43 -0700136 public boolean isEditingUrl() {
137 return mUrlInput.hasFocus();
138 }
139
140 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700141 WebView currentTopWebView = mUiController.getCurrentTopWebView();
142 if (currentTopWebView != null) {
143 currentTopWebView.requestFocus();
144 }
John Reck0f602f32011-07-07 15:38:43 -0700145 }
146
147 void setDisplayTitle(String title) {
148 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700149 if (!title.equals(mUrlInput.getText().toString())) {
150 mUrlInput.setText(title, false);
151 }
John Reck0f602f32011-07-07 15:38:43 -0700152 }
153 }
154
John Reck0f602f32011-07-07 15:38:43 -0700155 void setIncognitoMode(boolean incognito) {
156 mUrlInput.setIncognitoMode(incognito);
157 }
158
159 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000160 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700161 }
162
163 // UrlInputListener implementation
164
165 /**
166 * callback from suggestion dropdown
167 * user selected a suggestion
168 */
169 @Override
170 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700171 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700172 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800173 String url = null;
Vivek Sekharb54614f2014-05-01 19:03:37 -0700174 String browserRes =
175 getContext().getResources().getString(R.string.config_carrier_resource);
176 boolean wap2estore = "ct".equals(browserRes);
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) {
400 }
401
Michael Kolb0b129122012-06-04 16:31:58 -0700402 public void onVoiceResult(String s) {
403 startEditingUrl(true, true);
404 onCopySuggestion(s);
405 }
406
Narayan Kamathf3174a52011-11-17 14:43:32 +0000407 @Override
408 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
409
410 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700411 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000412
413 @Override
414 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700415
John Reck0f602f32011-07-07 15:38:43 -0700416}