blob: 8e8a5b7d2664f3464d1299724e90e0858b3d6204 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800174 Object[] params = {new String("persist.env.browser.wap2estore"),
175 Boolean.valueOf(false)};
176 Class[] type = new Class[] {String.class, boolean.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -0700177 Boolean wap2estore = (Boolean) ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800178 "android.os.SystemProperties", "getBoolean", type, params);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700179 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
180 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800181 url = text;
182 } else {
183 url = UrlUtils.smartUrlFilter(text, false);
184 }
185
John Reck0f602f32011-07-07 15:38:43 -0700186 Tab t = mBaseUi.getActiveTab();
187 // Only shortcut javascript URIs for now, as there is special
188 // logic in UrlHandler for other schemas
189 if (url != null && t != null && url.startsWith("javascript:")) {
190 mUiController.loadUrl(t, url);
191 setDisplayTitle(text);
192 return;
193 }
kaiyizc4ada322013-07-30 09:58:07 +0800194
195 // add for carrier wap2estore feature
196 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
197 handleEstoreTypeUrl(url);
198 setDisplayTitle(text);
199 return;
200 }
kaiyiz950eca22013-08-08 11:01:28 +0800201 // add for rtsp scheme feature
202 if (url != null && t != null && isRtspTypeUrl(url)) {
203 if (handleRtspTypeUrl(url)) {
204 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) {
266 String utf8Url = null;
267 try {
268 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
269 } catch (UnsupportedEncodingException e) {
270 Log.e(TAG, "err " + e);
271 }
272 if (utf8Url != null && utf8Url.startsWith("estore:")) {
273 return true;
274 }
275 return false;
276 }
277
278 private void handleEstoreTypeUrl(String url) {
279 String utf8Url = null, finalUrl = null;
280 try {
281 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
282 } catch (UnsupportedEncodingException e) {
283 Log.e(TAG, "err " + e);
284 }
285 if (utf8Url != null) {
286 finalUrl = utf8Url;
287 } else {
288 finalUrl = url;
289 }
290 if (finalUrl.replaceFirst("estore:", "").length() > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800291 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800292 return;
293 }
294 Intent intent = new Intent(Intent.ACTION_VIEW);
295 intent.setData(Uri.parse(finalUrl));
296 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800297 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800298 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800300 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800302 }
303 }
304
kaiyiz950eca22013-08-08 11:01:28 +0800305 private boolean isRtspTypeUrl(String url) {
306 String utf8Url = null;
307 try {
308 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
309 } catch (UnsupportedEncodingException e) {
310 Log.e(TAG, "err " + e);
311 }
312 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
313 return true;
314 }
315 return false;
316 }
317
318 private boolean handleRtspTypeUrl(String url) {
319 Intent intent;
320 // perform generic parsing of the URI to turn it into an Intent.
321 try {
322 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
323 } catch (URISyntaxException ex) {
324 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
325 return false;
326 }
327
328 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800329 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800330 } catch (ActivityNotFoundException ex) {
331 Log.w("Browser", "No resolveActivity " + url);
332 return false;
333 }
334 return true;
335 }
336
John Reck0f602f32011-07-07 15:38:43 -0700337 @Override
338 public void onDismiss() {
339 final Tab currentTab = mBaseUi.getActiveTab();
340 mBaseUi.hideTitleBar();
341 post(new Runnable() {
342 public void run() {
343 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700344 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700345 setDisplayTitle(currentTab.getUrl());
346 }
347 }
348 });
349 }
350
351 /**
352 * callback from the suggestion dropdown
353 * copy text to input field and stay in edit mode
354 */
355 @Override
356 public void onCopySuggestion(String text) {
357 mUrlInput.setText(text, true);
358 if (text != null) {
359 mUrlInput.setSelection(text.length());
360 }
361 }
362
363 public void setCurrentUrlIsBookmark(boolean isBookmark) {
364 }
365
366 @Override
367 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
368 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
369 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700370 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700371 }
372 return super.dispatchKeyEventPreIme(evt);
373 }
374
John Reck0f602f32011-07-07 15:38:43 -0700375 /**
376 * called from the Ui when the user wants to edit
377 * @param clearInput clear the input field
378 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700379 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700380 // editing takes preference of progress
381 setVisibility(View.VISIBLE);
382 if (mTitleBar.useQuickControls()) {
383 mTitleBar.getProgressView().setVisibility(View.GONE);
384 }
385 if (!mUrlInput.hasFocus()) {
386 mUrlInput.requestFocus();
387 }
388 if (clearInput) {
389 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700390 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700391 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700392 mUrlInput.showIME();
393 }
John Reck0f602f32011-07-07 15:38:43 -0700394 }
395
396 public void onProgressStarted() {
397 }
398
399 public void onProgressStopped() {
400 }
401
John Reck58891902011-08-11 17:48:53 -0700402 public boolean isMenuShowing() {
John Reck42229bc2011-08-19 13:26:43 -0700403 return false;
John Reck58891902011-08-11 17:48:53 -0700404 }
405
John Reck419f6b42011-08-16 16:10:51 -0700406 public void onTabDataChanged(Tab tab) {
407 }
408
Michael Kolb0b129122012-06-04 16:31:58 -0700409 public void onVoiceResult(String s) {
410 startEditingUrl(true, true);
411 onCopySuggestion(s);
412 }
413
Narayan Kamathf3174a52011-11-17 14:43:32 +0000414 @Override
415 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
416
417 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700418 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000419
420 @Override
421 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700422
John Reck0f602f32011-07-07 15:38:43 -0700423}