OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.externalnav; | 5 package org.chromium.chrome.browser.externalnav; |
6 | 6 |
7 import android.Manifest.permission; | 7 import android.Manifest.permission; |
8 import android.app.Activity; | 8 import android.app.Activity; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.DialogInterface; | 10 import android.content.DialogInterface; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 * The main implementation of the {@link ExternalNavigationDelegate}. | 50 * The main implementation of the {@link ExternalNavigationDelegate}. |
51 */ | 51 */ |
52 public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
e { | 52 public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
e { |
53 private static final String TAG = "ExternalNavigationDelegateImpl"; | 53 private static final String TAG = "ExternalNavigationDelegateImpl"; |
54 private static final String PDF_VIEWER = "com.google.android.apps.docs"; | 54 private static final String PDF_VIEWER = "com.google.android.apps.docs"; |
55 private static final String PDF_MIME = "application/pdf"; | 55 private static final String PDF_MIME = "application/pdf"; |
56 private static final String PDF_SUFFIX = ".pdf"; | 56 private static final String PDF_SUFFIX = ".pdf"; |
57 | 57 |
58 protected final Context mApplicationContext; | 58 protected final Context mApplicationContext; |
59 private final Tab mTab; | 59 private final Tab mTab; |
| 60 private final String mWebApkPackageName; |
60 | 61 |
61 public ExternalNavigationDelegateImpl(Tab tab) { | 62 /** |
| 63 * Creates instance of a {@link ExternalNavigationDelegateImpl}. |
| 64 * @param tab The associated tab. |
| 65 * @param webApkPackage The package of the WebAPK hosting {@link tab}. Null
if |
| 66 * {@link tab} is not hosted by a WebAPK. |
| 67 */ |
| 68 public ExternalNavigationDelegateImpl(Tab tab, String webApkPackageName) { |
62 mTab = tab; | 69 mTab = tab; |
| 70 mWebApkPackageName = webApkPackageName; |
63 mApplicationContext = tab.getWindowAndroid().getApplicationContext(); | 71 mApplicationContext = tab.getWindowAndroid().getApplicationContext(); |
64 } | 72 } |
65 | 73 |
66 /** | 74 /** |
67 * Get a {@link Context} linked to this delegate with preference to {@link A
ctivity}. | 75 * Get a {@link Context} linked to this delegate with preference to {@link A
ctivity}. |
68 * The tab this delegate associates with can swap the {@link Activity} it is
hosted in and | 76 * The tab this delegate associates with can swap the {@link Activity} it is
hosted in and |
69 * during the swap, there might not be an available {@link Activity}. | 77 * during the swap, there might not be an available {@link Activity}. |
70 * @return The activity {@link Context} if it can be reached. | 78 * @return The activity {@link Context} if it can be reached. |
71 * Application {@link Context} if not. | 79 * Application {@link Context} if not. |
72 */ | 80 */ |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 List<ResolveInfo> handlers = context.getPackageManager().queryIntent
Activities( | 274 List<ResolveInfo> handlers = context.getPackageManager().queryIntent
Activities( |
267 intent, PackageManager.GET_RESOLVED_FILTER); | 275 intent, PackageManager.GET_RESOLVED_FILTER); |
268 return isPackageSpecializedHandler(handlers, packageName); | 276 return isPackageSpecializedHandler(handlers, packageName); |
269 } catch (RuntimeException e) { | 277 } catch (RuntimeException e) { |
270 logTransactionTooLargeOrRethrow(e, intent); | 278 logTransactionTooLargeOrRethrow(e, intent); |
271 } | 279 } |
272 return false; | 280 return false; |
273 } | 281 } |
274 | 282 |
275 @Override | 283 @Override |
276 public String getPackageName() { | 284 public String getBrowserPackageName() { |
277 return mApplicationContext.getPackageName(); | 285 return mApplicationContext.getPackageName(); |
278 } | 286 } |
279 | 287 |
280 @Override | 288 @Override |
| 289 public String getWebApkPackageName() { |
| 290 return mWebApkPackageName; |
| 291 } |
| 292 |
| 293 @Override |
281 public void startActivity(Intent intent) { | 294 public void startActivity(Intent intent) { |
282 try { | 295 try { |
283 forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); | 296 forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); |
284 Context context = getAvailableContext(); | 297 Context context = getAvailableContext(); |
285 if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTI
VITY_NEW_TASK); | 298 if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTI
VITY_NEW_TASK); |
286 context.startActivity(intent); | 299 context.startActivity(intent); |
287 } catch (RuntimeException e) { | 300 } catch (RuntimeException e) { |
288 logTransactionTooLargeOrRethrow(e, intent); | 301 logTransactionTooLargeOrRethrow(e, intent); |
289 } | 302 } |
290 } | 303 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 } | 402 } |
390 | 403 |
391 String url = fallbackUrl != null ? fallbackUrl : intent.getDataString(); | 404 String url = fallbackUrl != null ? fallbackUrl : intent.getDataString(); |
392 if (!UrlUtilities.isAcceptedScheme(url)) { | 405 if (!UrlUtilities.isAcceptedScheme(url)) { |
393 if (needsToCloseTab) closeTab(tab); | 406 if (needsToCloseTab) closeTab(tab); |
394 return; | 407 return; |
395 } | 408 } |
396 | 409 |
397 if (needsToStartIntent) { | 410 if (needsToStartIntent) { |
398 intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | 411 intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
399 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName()); | 412 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getBrowserPackageName(
)); |
400 if (launchIncogntio) intent.putExtra(IntentHandler.EXTRA_OPEN_NEW_IN
COGNITO_TAB, true); | 413 if (launchIncogntio) intent.putExtra(IntentHandler.EXTRA_OPEN_NEW_IN
COGNITO_TAB, true); |
401 intent.addCategory(Intent.CATEGORY_BROWSABLE); | 414 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
402 intent.setClassName(getPackageName(), ChromeLauncherActivity.class.g
etName()); | 415 intent.setClassName(getBrowserPackageName(), ChromeLauncherActivity.
class.getName()); |
403 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 416 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
404 IntentHandler.addTrustedIntentExtras(intent, mApplicationContext); | 417 IntentHandler.addTrustedIntentExtras(intent, mApplicationContext); |
405 startActivity(intent); | 418 startActivity(intent); |
406 | 419 |
407 if (needsToCloseTab) closeTab(tab); | 420 if (needsToCloseTab) closeTab(tab); |
408 return; | 421 return; |
409 } | 422 } |
410 | 423 |
411 LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.AUTO
_TOPLEVEL); | 424 LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.AUTO
_TOPLEVEL); |
412 if (!TextUtils.isEmpty(referrerUrl)) { | 425 if (!TextUtils.isEmpty(referrerUrl)) { |
(...skipping 12 matching lines...) Expand all Loading... |
425 Referrer referrer = new Referrer(referrerUrl, Referrer.REFERRER_POLI
CY_ALWAYS); | 438 Referrer referrer = new Referrer(referrerUrl, Referrer.REFERRER_POLI
CY_ALWAYS); |
426 loadUrlParams.setReferrer(referrer); | 439 loadUrlParams.setReferrer(referrer); |
427 } | 440 } |
428 if (tab != null) { | 441 if (tab != null) { |
429 tab.loadUrl(loadUrlParams); | 442 tab.loadUrl(loadUrlParams); |
430 return OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB; | 443 return OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB; |
431 } else { | 444 } else { |
432 assert false : "clobberCurrentTab was called with an empty tab."; | 445 assert false : "clobberCurrentTab was called with an empty tab."; |
433 Uri uri = Uri.parse(url); | 446 Uri uri = Uri.parse(url); |
434 Intent intent = new Intent(Intent.ACTION_VIEW, uri); | 447 Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
435 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName()); | 448 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getBrowserPackageName(
)); |
436 intent.addCategory(Intent.CATEGORY_BROWSABLE); | 449 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
437 intent.setPackage(getPackageName()); | 450 intent.setPackage(getBrowserPackageName()); |
438 startActivity(intent); | 451 startActivity(intent); |
439 return OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT; | 452 return OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT; |
440 } | 453 } |
441 } | 454 } |
442 | 455 |
443 @Override | 456 @Override |
444 public boolean isChromeAppInForeground() { | 457 public boolean isChromeAppInForeground() { |
445 return ApplicationStatus.getStateForApplication() | 458 return ApplicationStatus.getStateForApplication() |
446 == ApplicationState.HAS_RUNNING_ACTIVITIES; | 459 == ApplicationState.HAS_RUNNING_ACTIVITIES; |
447 } | 460 } |
448 | 461 |
449 @Override | 462 @Override |
450 public void maybeSetWindowId(Intent intent) { | 463 public void maybeSetWindowId(Intent intent) { |
451 Context context = getAvailableContext(); | 464 Context context = getAvailableContext(); |
452 if (!(context instanceof ChromeTabbedActivity2)) return; | 465 if (!(context instanceof ChromeTabbedActivity2)) return; |
453 intent.putExtra(IntentHandler.EXTRA_WINDOW_ID, 2); | 466 intent.putExtra(IntentHandler.EXTRA_WINDOW_ID, 2); |
454 } | 467 } |
455 | 468 |
456 @Override | 469 @Override |
457 public boolean isDocumentMode() { | 470 public boolean isDocumentMode() { |
458 return FeatureUtilities.isDocumentMode(mApplicationContext); | 471 return FeatureUtilities.isDocumentMode(mApplicationContext); |
459 } | 472 } |
460 | 473 |
461 @Override | 474 @Override |
| 475 public void startChromeActivity(String url) { |
| 476 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 477 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getBrowserPackageName()); |
| 478 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 479 intent.setClassName(getBrowserPackageName(), ChromeLauncherActivity.clas
s.getName()); |
| 480 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 481 startActivity(intent); |
| 482 } |
| 483 |
| 484 @Override |
462 public String getDefaultSmsPackageName() { | 485 public String getDefaultSmsPackageName() { |
463 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null; | 486 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null; |
464 return Telephony.Sms.getDefaultSmsPackage(mApplicationContext); | 487 return Telephony.Sms.getDefaultSmsPackage(mApplicationContext); |
465 } | 488 } |
466 | 489 |
467 private static void logTransactionTooLargeOrRethrow(RuntimeException e, Inte
nt intent) { | 490 private static void logTransactionTooLargeOrRethrow(RuntimeException e, Inte
nt intent) { |
468 // See http://crbug.com/369574. | 491 // See http://crbug.com/369574. |
469 if (e.getCause() instanceof TransactionTooLargeException) { | 492 if (e.getCause() instanceof TransactionTooLargeException) { |
470 Log.e(TAG, "Could not resolve Activity for intent " + intent.toStrin
g(), e); | 493 Log.e(TAG, "Could not resolve Activity for intent " + intent.toStrin
g(), e); |
471 } else { | 494 } else { |
472 throw e; | 495 throw e; |
473 } | 496 } |
474 } | 497 } |
475 | 498 |
476 private void closeTab(Tab tab) { | 499 private void closeTab(Tab tab) { |
477 Context context = tab.getWindowAndroid().getContext().get(); | 500 Context context = tab.getWindowAndroid().getContext().get(); |
478 if (context instanceof ChromeActivity) { | 501 if (context instanceof ChromeActivity) { |
479 ((ChromeActivity) context).getTabModelSelector().closeTab(tab); | 502 ((ChromeActivity) context).getTabModelSelector().closeTab(tab); |
480 } | 503 } |
481 } | 504 } |
482 } | 505 } |
OLD | NEW |