blob: 344e7d996d32bc5084a4793e7e00afc488c230bd [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
Pankaj Garg32e1b942015-06-03 18:13:24 -070018import android.animation.ArgbEvaluator;
19import android.animation.ValueAnimator;
Enrico Ros1f5a0952014-11-18 20:15:48 -080020import android.app.Activity;
John Reck0f602f32011-07-07 15:38:43 -070021import android.app.SearchManager;
kaiyizc4ada322013-07-30 09:58:07 +080022import android.content.ActivityNotFoundException;
John Reck0f602f32011-07-07 15:38:43 -070023import android.content.Context;
24import android.content.Intent;
Pankaj Garg32e1b942015-06-03 18:13:24 -070025import android.content.SharedPreferences;
John Reck0f602f32011-07-07 15:38:43 -070026import android.graphics.Bitmap;
Pankaj Garg32e1b942015-06-03 18:13:24 -070027import android.graphics.Color;
John Reck0f602f32011-07-07 15:38:43 -070028import android.graphics.drawable.Drawable;
kaiyizc4ada322013-07-30 09:58:07 +080029import android.net.Uri;
Pankaj Garg32e1b942015-06-03 18:13:24 -070030import android.net.http.SslCertificate;
31import android.net.http.SslError;
32import android.os.Build;
John Reck0f602f32011-07-07 15:38:43 -070033import android.os.Bundle;
Narayan Kamathf3174a52011-11-17 14:43:32 +000034import android.text.Editable;
35import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070036import android.util.AttributeSet;
kaiyizc4ada322013-07-30 09:58:07 +080037import android.util.Log;
John Reck0f602f32011-07-07 15:38:43 -070038import android.view.KeyEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080039import android.view.Menu;
40import android.view.MenuItem;
John Reck0f602f32011-07-07 15:38:43 -070041import android.view.View;
42import android.view.View.OnClickListener;
43import android.view.View.OnFocusChangeListener;
Enrico Ros1f5a0952014-11-18 20:15:48 -080044import android.view.ViewConfiguration;
John Reck0f602f32011-07-07 15:38:43 -070045import android.widget.ImageView;
46import android.widget.LinearLayout;
Enrico Ros1f5a0952014-11-18 20:15:48 -080047import android.widget.PopupMenu;
kaiyizc4ada322013-07-30 09:58:07 +080048import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070049
Bijan Amirzada41242f22014-03-21 12:12:18 -070050import com.android.browser.UrlInputView.UrlInputListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070051import com.android.browser.preferences.SiteSpecificPreferencesFragment;
John Reck0f602f32011-07-07 15:38:43 -070052
Pankaj Garg32e1b942015-06-03 18:13:24 -070053import java.io.ByteArrayOutputStream;
kaiyizc4ada322013-07-30 09:58:07 +080054import java.io.UnsupportedEncodingException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070055import java.net.MalformedURLException;
kaiyiz950eca22013-08-08 11:01:28 +080056import java.net.URISyntaxException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070057import java.net.URL;
kaiyizc4ada322013-07-30 09:58:07 +080058
Dany Rybnikovbcd37da2015-04-20 11:43:15 +030059import org.codeaurora.net.NetworkServices;
Kevin Hart055e6d82014-12-19 15:53:19 -080060import org.codeaurora.swe.Engine;
Pankaj Garg32e1b942015-06-03 18:13:24 -070061import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070063import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064
John Reck42229bc2011-08-19 13:26:43 -070065public class NavigationBarBase extends LinearLayout implements
66 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080067 TextWatcher, UrlInputView.StateListener,
68 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070069
kaiyizc4ada322013-07-30 09:58:07 +080070 private final static String TAG = "NavigationBarBase";
71
John Reck0f602f32011-07-07 15:38:43 -070072 protected BaseUi mBaseUi;
73 protected TitleBar mTitleBar;
74 protected UiController mUiController;
75 protected UrlInputView mUrlInput;
John Reck0f602f32011-07-07 15:38:43 -070076
Pankaj Garg32e1b942015-06-03 18:13:24 -070077 protected ImageView mFaviconBadge;
78
John Reck0f602f32011-07-07 15:38:43 -070079 private ImageView mFavicon;
80 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070081
Enrico Ros1f5a0952014-11-18 20:15:48 -080082 private View mMore;
83 private PopupMenu mPopupMenu;
84 private boolean mOverflowMenuShowing;
85 private boolean mNeedsMenu;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070086
Pankaj Garg32e1b942015-06-03 18:13:24 -070087 private int mStatusBarColor;
88 private static int mDefaultStatusBarColor = -1;
89
90 private Tab.SecurityState mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
91
John Reck0f602f32011-07-07 15:38:43 -070092 public NavigationBarBase(Context context) {
93 super(context);
94 }
95
96 public NavigationBarBase(Context context, AttributeSet attrs) {
97 super(context, attrs);
98 }
99
100 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
101 super(context, attrs, defStyle);
102 }
103
104 @Override
105 protected void onFinishInflate() {
106 super.onFinishInflate();
107 mLockIcon = (ImageView) findViewById(R.id.lock);
108 mFavicon = (ImageView) findViewById(R.id.favicon);
109 mUrlInput = (UrlInputView) findViewById(R.id.url);
110 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700111 mUrlInput.setOnFocusChangeListener(this);
112 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000113 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800114 mMore = findViewById(R.id.more_browser_settings);
115 mMore.setOnClickListener(this);
116 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700117 mFaviconBadge = (ImageView) findViewById(R.id.favicon_badge);
John Reck0f602f32011-07-07 15:38:43 -0700118 }
119
120 public void setTitleBar(TitleBar titleBar) {
121 mTitleBar = titleBar;
122 mBaseUi = mTitleBar.getUi();
123 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700124 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700125 }
126
Pankaj Garg32e1b942015-06-03 18:13:24 -0700127 public void setLock(Drawable d, Tab.SecurityState securityState) {
128 mSecurityState = securityState;
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700129 if (mFaviconBadge != null) {
130 switch (mSecurityState) {
131 case SECURITY_STATE_SECURE:
132 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_good);
133 break;
134 case SECURITY_STATE_MIXED:
135 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_warning);
136 break;
137 case SECURITY_STATE_BAD_CERTIFICATE:
138 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_severe);
139 break;
140 case SECURITY_STATE_NOT_SECURE:
141 default:
142 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
143 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700144 }
John Reck0f602f32011-07-07 15:38:43 -0700145 if (mLockIcon == null) return;
146 if (d == null) {
147 mLockIcon.setVisibility(View.GONE);
148 } else {
149 mLockIcon.setImageDrawable(d);
150 mLockIcon.setVisibility(View.VISIBLE);
151 }
152 }
153
Pankaj Garg32e1b942015-06-03 18:13:24 -0700154 public static int adjustColor(int color, float hueMultiplier,
155 float saturationMultiplier, float valueMultiplier) {
156 float[] hsv = new float[3];
157 Color.colorToHSV(color, hsv);
158 hsv[0] *= hueMultiplier;
159 hsv[1] *= saturationMultiplier;
160 hsv[2] *= valueMultiplier;
161 return Color.HSVToColor(Color.alpha(color), hsv);
162 }
163
164 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
165 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
166 int currentColor = activity.getWindow().getStatusBarColor();
167 Integer from = currentColor;
168 Integer to = color;
169 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
170
171 if (mDefaultStatusBarColor == -1) {
172 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
173 }
174
175 animator.addUpdateListener(
176 new ValueAnimator.AnimatorUpdateListener() {
177 @Override
178 public void onAnimationUpdate(ValueAnimator animation) {
179 Integer value = (Integer) animation.getAnimatedValue();
180 activity.getWindow().setStatusBarColor(value.intValue());
181 //activity.getWindow().setNavigationBarColor(value.intValue());
182 }
183 }
184 );
185 animator.start();
186 }
187 }
188
189 private void updateSiteIconColor(String urlString, int color) {
190 try {
191 URL url = new URL(urlString);
192 String host = url.getHost();
193 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
194 int currentColor = prefs.getInt(host + ":color", 0);
195 if (currentColor != color) {
196 SharedPreferences.Editor editor = prefs.edit();
197 editor.remove(host + ":color");
198 editor.putInt(host + ":color", color);
199 editor.commit();
200 }
201 } catch (MalformedURLException e) {
202 }
203 }
204
205 public static int getSiteIconColor(String urlString) {
206 try {
207 URL url = new URL(urlString);
208 String host = url.getHost();
209 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
210 return prefs.getInt(host + ":color", 0);
211 } catch (MalformedURLException e) {
212 return 0;
213 }
214 }
215
216 public static int getDefaultStatusBarColor() {
217 return mDefaultStatusBarColor;
218 }
219
John Reck0f602f32011-07-07 15:38:43 -0700220 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700221 int color = ColorUtils.getDominantColorForBitmap(icon);
222 Tab tab = mUiController.getCurrentTab();
223
224 if (tab != null) {
225 if (tab.hasFavicon()) {
226 updateSiteIconColor(tab.getUrl(), color);
227 setStatusAndNavigationBarColor(mUiController.getActivity(),
228 adjustColor(color, 1, 1, 0.7f));
229 } else {
230 color = getSiteIconColor(tab.getUrl());
231 if (color != 0) {
232 setStatusAndNavigationBarColor(mUiController.getActivity(),
233 adjustColor(color, 1, 1, 0.7f));
234 } else {
235 setStatusAndNavigationBarColor(mUiController.getActivity(),
236 mDefaultStatusBarColor);
237 }
238 }
239 } else {
240 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
241 }
242
John Reck0f602f32011-07-07 15:38:43 -0700243 if (mFavicon == null) return;
244 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
245 }
246
Pankaj Garg32e1b942015-06-03 18:13:24 -0700247 protected void showSiteSpecificSettings() {
248 WebView wv = mUiController.getCurrentTopWebView();
249 int count = 0;
250
251 if (wv != null && WebRefiner.getInstance() != null) {
252 count = WebRefiner.getInstance().getBlockedURLCount(wv);
253 }
254
255 Bundle bundle = new Bundle();
256 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
257 mUiController.getCurrentTab().getUrl());
258 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count);
259
260 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
261 SslCertificate.saveState(wv.getCertificate()));
262
263 SslError error = mUiController.getCurrentTab().getSslCertificateError();
264 if (error != null) {
265 int certError = 0;
266 if (error.hasError(SslError.SSL_DATE_INVALID)) {
267 certError |= (1 << SslError.SSL_DATE_INVALID);
268 }
269
270 if (error.hasError(SslError.SSL_EXPIRED)) {
271 certError |= (1 << SslError.SSL_EXPIRED);
272 }
273
274 if (error.hasError(SslError.SSL_IDMISMATCH)) {
275 certError |= (1 << SslError.SSL_IDMISMATCH);
276 }
277
278 if (error.hasError(SslError.SSL_INVALID)) {
279 certError |= (1 << SslError.SSL_INVALID);
280 }
281
282 if (error.hasError(SslError.SSL_NOTYETVALID)) {
283 certError |= (1 << SslError.SSL_NOTYETVALID);
284 }
285
286 if (error.hasError(SslError.SSL_UNTRUSTED)) {
287 certError |= (1 << SslError.SSL_UNTRUSTED);
288 }
289
290 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError);
291 }
292
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700293 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700294 if (favicon != null) {
295 ByteArrayOutputStream baos = new ByteArrayOutputStream();
296 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
297 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
298 baos.toByteArray());
299 }
300 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
301 mUiController.getActivity(),
302 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
303 }
304
John Reck0f602f32011-07-07 15:38:43 -0700305 @Override
306 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800307 if (mMore == v) {
308 showMenu(mMore);
309 }
310 }
311
312 void showMenu(View anchor) {
313 Activity activity = mUiController.getActivity();
314 if (mPopupMenu == null) {
315 mPopupMenu = new PopupMenu(getContext(), anchor);
316 mPopupMenu.setOnMenuItemClickListener(this);
317 mPopupMenu.setOnDismissListener(this);
318 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
319 mPopupMenu = null;
320 return;
321 }
322 }
323 Menu menu = mPopupMenu.getMenu();
324
325 if (mUiController instanceof Controller) {
326 Controller controller = (Controller) mUiController;
327 if (controller.onPrepareOptionsMenu(menu)) {
328 mOverflowMenuShowing = true;
329 }
330 }
John Reck0f602f32011-07-07 15:38:43 -0700331 }
332
333 @Override
334 public void onFocusChange(View view, boolean hasFocus) {
335 // if losing focus and not in touch mode, leave as is
336 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
337 setFocusState(hasFocus);
338 }
339 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800340 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700341 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300342 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
343 //Notify about anticipated network activity
344 NetworkServices.hintUpcomingUserActivity();
345 }
John Reck0f602f32011-07-07 15:38:43 -0700346 } else if (!mUrlInput.needsUpdate()) {
347 mUrlInput.dismissDropDown();
348 mUrlInput.hideIME();
349 if (mUrlInput.getText().length() == 0) {
350 Tab currentTab = mUiController.getTabControl().getCurrentTab();
351 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700352 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700353 }
354 }
355 mBaseUi.suggestHideTitleBar();
356 }
357 mUrlInput.clearNeedsUpdate();
358 }
359
360 protected void setFocusState(boolean focus) {
361 }
362
John Reck0f602f32011-07-07 15:38:43 -0700363 public boolean isEditingUrl() {
364 return mUrlInput.hasFocus();
365 }
366
367 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700368 WebView currentTopWebView = mUiController.getCurrentTopWebView();
369 if (currentTopWebView != null) {
370 currentTopWebView.requestFocus();
371 }
John Reck0f602f32011-07-07 15:38:43 -0700372 }
373
374 void setDisplayTitle(String title) {
375 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700376 if (!title.equals(mUrlInput.getText().toString())) {
377 mUrlInput.setText(title, false);
378 }
John Reck0f602f32011-07-07 15:38:43 -0700379 }
380 }
381
John Reck0f602f32011-07-07 15:38:43 -0700382 void setIncognitoMode(boolean incognito) {
383 mUrlInput.setIncognitoMode(incognito);
384 }
385
386 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000387 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700388 }
389
390 // UrlInputListener implementation
391
392 /**
393 * callback from suggestion dropdown
394 * user selected a suggestion
395 */
396 @Override
397 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700398 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700399 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800400 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800401 boolean wap2estore = BrowserConfig.getInstance(getContext())
402 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700403 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
404 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800405 url = text;
406 } else {
407 url = UrlUtils.smartUrlFilter(text, false);
408 }
409
John Reck0f602f32011-07-07 15:38:43 -0700410 Tab t = mBaseUi.getActiveTab();
411 // Only shortcut javascript URIs for now, as there is special
412 // logic in UrlHandler for other schemas
413 if (url != null && t != null && url.startsWith("javascript:")) {
414 mUiController.loadUrl(t, url);
415 setDisplayTitle(text);
416 return;
417 }
kaiyizc4ada322013-07-30 09:58:07 +0800418
419 // add for carrier wap2estore feature
420 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700421 if (handleEstoreTypeUrl(url)) {
422 setDisplayTitle(text);
423 return;
424 }
kaiyizc4ada322013-07-30 09:58:07 +0800425 }
kaiyiz950eca22013-08-08 11:01:28 +0800426 // add for rtsp scheme feature
427 if (url != null && t != null && isRtspTypeUrl(url)) {
428 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700429 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800430 return;
431 }
432 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700433 // add for "wtai://wp/mc;" scheme feature
434 if (url != null && t != null && isMakeCallTypeUrl(url)) {
435 if (handleMakeCallTypeUrl(url)) {
436 return;
437 }
438 }
John Reck0f602f32011-07-07 15:38:43 -0700439 }
440 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700441 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700442 i.setAction(action);
443 i.putExtra(SearchManager.QUERY, text);
444 if (extra != null) {
445 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
446 }
447 if (source != null) {
448 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800449 appData.putString("source", source);
450 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700451 }
452 mUiController.handleNewIntent(i);
453 setDisplayTitle(text);
454 }
455
Vivek Sekhar5e631472014-03-31 23:59:10 -0700456 private boolean isMakeCallTypeUrl(String url) {
457 String utf8Url = null;
458 try {
459 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
460 } catch (UnsupportedEncodingException e) {
461 Log.e(TAG, "err " + e);
462 }
463 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
464 return true;
465 }
466 return false;
467 }
468
469 private boolean handleMakeCallTypeUrl(String url) {
470 // wtai://wp/mc;number
471 // number=string(phone-number)
472 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
473 Intent intent = new Intent(Intent.ACTION_VIEW,
474 Uri.parse(WebView.SCHEME_TEL +
475 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
476 getContext().startActivity(intent);
477 // before leaving BrowserActivity, close the empty child tab.
478 // If a new tab is created through JavaScript open to load this
479 // url, we would like to close it as we will load this url in a
480 // different Activity.
481 Tab current = mUiController.getCurrentTab();
482 if (current != null
483 && current.getWebView().copyBackForwardList().getSize() == 0) {
484 mUiController.closeCurrentTab();
485 }
486 return true;
487 }
488 return false;
489 }
490
kaiyizc4ada322013-07-30 09:58:07 +0800491 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700492 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800493 return true;
494 }
495 return false;
496 }
497
Vivek Sekharb54614f2014-05-01 19:03:37 -0700498 private boolean handleEstoreTypeUrl(String url) {
499 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800500 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700501 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800502 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700503
504 Intent intent;
505 // perform generic parsing of the URI to turn it into an Intent.
506 try {
507 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
508 } catch (URISyntaxException ex) {
509 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
510 return false;
511 }
512
kaiyizc4ada322013-07-30 09:58:07 +0800513 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800514 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800515 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800516 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800517 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800518 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800519 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700520
521 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800522 }
523
kaiyiz950eca22013-08-08 11:01:28 +0800524 private boolean isRtspTypeUrl(String url) {
525 String utf8Url = null;
526 try {
527 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
528 } catch (UnsupportedEncodingException e) {
529 Log.e(TAG, "err " + e);
530 }
531 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
532 return true;
533 }
534 return false;
535 }
536
537 private boolean handleRtspTypeUrl(String url) {
538 Intent intent;
539 // perform generic parsing of the URI to turn it into an Intent.
540 try {
541 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
542 } catch (URISyntaxException ex) {
543 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
544 return false;
545 }
546
547 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800548 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800549 } catch (ActivityNotFoundException ex) {
550 Log.w("Browser", "No resolveActivity " + url);
551 return false;
552 }
553 return true;
554 }
555
John Reck0f602f32011-07-07 15:38:43 -0700556 @Override
557 public void onDismiss() {
558 final Tab currentTab = mBaseUi.getActiveTab();
559 mBaseUi.hideTitleBar();
560 post(new Runnable() {
561 public void run() {
562 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700563 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700564 setDisplayTitle(currentTab.getUrl());
565 }
566 }
567 });
568 }
569
570 /**
571 * callback from the suggestion dropdown
572 * copy text to input field and stay in edit mode
573 */
574 @Override
575 public void onCopySuggestion(String text) {
576 mUrlInput.setText(text, true);
577 if (text != null) {
578 mUrlInput.setSelection(text.length());
579 }
580 }
581
582 public void setCurrentUrlIsBookmark(boolean isBookmark) {
583 }
584
585 @Override
586 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
587 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
588 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700589 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700590 }
591 return super.dispatchKeyEventPreIme(evt);
592 }
593
John Reck0f602f32011-07-07 15:38:43 -0700594 /**
595 * called from the Ui when the user wants to edit
596 * @param clearInput clear the input field
597 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700598 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700599 // editing takes preference of progress
600 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700601 if (!mUrlInput.hasFocus()) {
602 mUrlInput.requestFocus();
603 }
604 if (clearInput) {
605 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700606 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700607 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700608 mUrlInput.showIME();
609 }
John Reck0f602f32011-07-07 15:38:43 -0700610 }
611
612 public void onProgressStarted() {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700613 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
614 mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck0f602f32011-07-07 15:38:43 -0700615 }
616
617 public void onProgressStopped() {
618 }
619
John Reck419f6b42011-08-16 16:10:51 -0700620 public void onTabDataChanged(Tab tab) {
621 }
622
Michael Kolb0b129122012-06-04 16:31:58 -0700623 public void onVoiceResult(String s) {
624 startEditingUrl(true, true);
625 onCopySuggestion(s);
626 }
627
Narayan Kamathf3174a52011-11-17 14:43:32 +0000628 @Override
629 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
630
631 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700632 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000633
634 @Override
635 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700636
Enrico Ros1f5a0952014-11-18 20:15:48 -0800637 @Override
638 public void onStateChanged(int state) {
639 switch(state) {
640 case STATE_NORMAL:
641 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
642 break;
643 case STATE_HIGHLIGHTED:
644 mMore.setVisibility(View.GONE);
645 break;
646 case STATE_EDITED:
647 mMore.setVisibility(View.GONE);
648 break;
649 }
650 }
651
652 public boolean isMenuShowing() {
653 return mOverflowMenuShowing;
654 }
655
656
657 @Override
658 public void onDismiss(PopupMenu popupMenu) {
659 if (popupMenu == mPopupMenu) {
660 onMenuHidden();
661 }
662 }
663
664 private void onMenuHidden() {
665 mOverflowMenuShowing = false;
666 mBaseUi.showTitleBarForDuration();
667 }
668
669
670 @Override
671 public boolean onMenuItemClick(MenuItem item) {
672 return mUiController.onOptionsItemSelected(item);
673 }
John Reck0f602f32011-07-07 15:38:43 -0700674}