| 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; |
| 11 import android.content.DialogInterface.OnCancelListener; | 11 import android.content.DialogInterface.OnCancelListener; |
| 12 import android.content.DialogInterface.OnClickListener; | 12 import android.content.DialogInterface.OnClickListener; |
| 13 import android.content.Intent; | 13 import android.content.Intent; |
| 14 import android.content.IntentFilter; | 14 import android.content.IntentFilter; |
| 15 import android.content.pm.ActivityInfo; | |
| 16 import android.content.pm.PackageManager; | 15 import android.content.pm.PackageManager; |
| 17 import android.content.pm.ResolveInfo; | 16 import android.content.pm.ResolveInfo; |
| 18 import android.net.Uri; | 17 import android.net.Uri; |
| 19 import android.os.Build; | 18 import android.os.Build; |
| 20 import android.os.TransactionTooLargeException; | 19 import android.os.TransactionTooLargeException; |
| 21 import android.provider.Browser; | 20 import android.provider.Browser; |
| 22 import android.provider.Telephony; | 21 import android.provider.Telephony; |
| 23 import android.support.v7.app.AlertDialog; | 22 import android.support.v7.app.AlertDialog; |
| 24 import android.text.TextUtils; | 23 import android.text.TextUtils; |
| 25 import android.util.Log; | 24 import android.util.Log; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 import org.chromium.chrome.browser.document.ChromeLauncherActivity; | 37 import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| 39 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
eUrlLoadingResult; | 38 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
eUrlLoadingResult; |
| 40 import org.chromium.chrome.browser.tab.Tab; | 39 import org.chromium.chrome.browser.tab.Tab; |
| 41 import org.chromium.chrome.browser.util.FeatureUtilities; | 40 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 42 import org.chromium.chrome.browser.util.UrlUtilities; | 41 import org.chromium.chrome.browser.util.UrlUtilities; |
| 43 import org.chromium.content_public.browser.LoadUrlParams; | 42 import org.chromium.content_public.browser.LoadUrlParams; |
| 44 import org.chromium.content_public.common.Referrer; | 43 import org.chromium.content_public.common.Referrer; |
| 45 import org.chromium.ui.base.PageTransition; | 44 import org.chromium.ui.base.PageTransition; |
| 46 import org.chromium.ui.base.WindowAndroid; | 45 import org.chromium.ui.base.WindowAndroid; |
| 47 import org.chromium.ui.base.WindowAndroid.PermissionCallback; | 46 import org.chromium.ui.base.WindowAndroid.PermissionCallback; |
| 47 import org.chromium.webapk.lib.client.WebApkValidator; |
| 48 | 48 |
| 49 import java.util.List; | 49 import java.util.List; |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * The main implementation of the {@link ExternalNavigationDelegate}. | 52 * The main implementation of the {@link ExternalNavigationDelegate}. |
| 53 */ | 53 */ |
| 54 public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
e { | 54 public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
e { |
| 55 private static final String TAG = "ExternalNavigationDelegateImpl"; | 55 private static final String TAG = "ExternalNavigationDelegateImpl"; |
| 56 private static final String PDF_VIEWER = "com.google.android.apps.docs"; | 56 private static final String PDF_VIEWER = "com.google.android.apps.docs"; |
| 57 private static final String PDF_MIME = "application/pdf"; | 57 private static final String PDF_MIME = "application/pdf"; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 PackageManager.GET_RESOLVED_FILTER); | 220 PackageManager.GET_RESOLVED_FILTER); |
| 221 } | 221 } |
| 222 | 222 |
| 223 @Override | 223 @Override |
| 224 public boolean willChromeHandleIntent(Intent intent) { | 224 public boolean willChromeHandleIntent(Intent intent) { |
| 225 return willChromeHandleIntent(mApplicationContext, intent, false); | 225 return willChromeHandleIntent(mApplicationContext, intent, false); |
| 226 } | 226 } |
| 227 | 227 |
| 228 @Override | 228 @Override |
| 229 public boolean isSpecializedHandlerAvailable(List<ResolveInfo> infos) { | 229 public boolean isSpecializedHandlerAvailable(List<ResolveInfo> infos) { |
| 230 return isPackageSpecializedHandler(infos, null); | 230 return countSpecializedHandlers(infos) > 0; |
| 231 } | 231 } |
| 232 | 232 |
| 233 static boolean isPackageSpecializedHandler(List<ResolveInfo> handlers, | 233 @Override |
| 234 String packageName) { | 234 public int countSpecializedHandlers(List<ResolveInfo> infos) { |
| 235 if (handlers == null || handlers.size() == 0) return false; | 235 return countSpecializedHandlersWithFilter(infos, null); |
| 236 for (ResolveInfo resolveInfo : handlers) { | 236 } |
| 237 IntentFilter filter = resolveInfo.filter; | 237 |
| 238 static int countSpecializedHandlersWithFilter( |
| 239 List<ResolveInfo> infos, String filterPackageName) { |
| 240 if (infos == null) { |
| 241 return 0; |
| 242 } |
| 243 |
| 244 int count = 0; |
| 245 for (ResolveInfo info : infos) { |
| 246 IntentFilter filter = info.filter; |
| 238 if (filter == null) { | 247 if (filter == null) { |
| 239 // No intent filter matches this intent? | 248 // Error on the side of classifying ResolveInfo as generic. |
| 240 // Error on the side of staying in the browser, ignore | |
| 241 continue; | 249 continue; |
| 242 } | 250 } |
| 243 if (filter.countDataAuthorities() == 0 && filter.countDataPaths() ==
0) { | 251 if (filter.countDataAuthorities() == 0 && filter.countDataPaths() ==
0) { |
| 244 // Generic handler, skip | 252 // Don't count generic handlers. |
| 245 continue; | 253 continue; |
| 246 } | 254 } |
| 247 if (TextUtils.isEmpty(packageName)) return true; | 255 |
| 248 ActivityInfo activityInfo = resolveInfo.activityInfo; | 256 if (!TextUtils.isEmpty(filterPackageName) |
| 249 if (activityInfo == null) continue; | 257 && (info.activityInfo == null |
| 250 if (!activityInfo.packageName.equals(packageName)) continue; | 258 || !info.activityInfo.packageName.equals(filterPa
ckageName))) { |
| 251 return true; | 259 continue; |
| 260 } |
| 261 |
| 262 ++count; |
| 252 } | 263 } |
| 253 | 264 return count; |
| 254 return false; | |
| 255 } | 265 } |
| 256 | 266 |
| 257 /** | 267 /** |
| 258 * Check whether the given package is a specialized handler for the given in
tent | 268 * Check whether the given package is a specialized handler for the given in
tent |
| 259 * | 269 * |
| 260 * @param context {@link Context} to use for getting the {@link PackageManag
er}. | 270 * @param context {@link Context} to use for getting the {@link PackageManag
er}. |
| 261 * @param packageName Package name to check against. Can be null or empty. | 271 * @param packageName Package name to check against. Can be null or empty. |
| 262 * @param intent The intent to resolve for. | 272 * @param intent The intent to resolve for. |
| 263 * @return Whether the given package is a specialized handler for the given
intent. If there is | 273 * @return Whether the given package is a specialized handler for the given
intent. If there is |
| 264 * no package name given checks whether there is any specialized han
dler. | 274 * no package name given checks whether there is any specialized han
dler. |
| 265 */ | 275 */ |
| 266 public static boolean isPackageSpecializedHandler( | 276 public static boolean isPackageSpecializedHandler( |
| 267 Context context, String packageName, Intent intent) { | 277 Context context, String packageName, Intent intent) { |
| 268 try { | 278 try { |
| 269 List<ResolveInfo> handlers = context.getPackageManager().queryIntent
Activities( | 279 List<ResolveInfo> handlers = context.getPackageManager().queryIntent
Activities( |
| 270 intent, PackageManager.GET_RESOLVED_FILTER); | 280 intent, PackageManager.GET_RESOLVED_FILTER); |
| 271 return isPackageSpecializedHandler(handlers, packageName); | 281 return countSpecializedHandlersWithFilter(handlers, packageName) > 0
; |
| 272 } catch (RuntimeException e) { | 282 } catch (RuntimeException e) { |
| 273 logTransactionTooLargeOrRethrow(e, intent); | 283 logTransactionTooLargeOrRethrow(e, intent); |
| 274 } | 284 } |
| 275 return false; | 285 return false; |
| 276 } | 286 } |
| 277 | 287 |
| 278 @Override | 288 @Override |
| 289 public String findValidWebApkPackageName(List<ResolveInfo> infos) { |
| 290 String webApkPackageName = WebApkValidator.findWebApkPackage(infos); |
| 291 return WebApkValidator.isValidWebApk(mApplicationContext, webApkPackageN
ame) |
| 292 ? webApkPackageName |
| 293 : null; |
| 294 } |
| 295 |
| 296 @Override |
| 279 public String getPackageName() { | 297 public String getPackageName() { |
| 280 return mApplicationContext.getPackageName(); | 298 return mApplicationContext.getPackageName(); |
| 281 } | 299 } |
| 282 | 300 |
| 283 @Override | 301 @Override |
| 284 public void startActivity(Intent intent) { | 302 public void startActivity(Intent intent) { |
| 285 try { | 303 try { |
| 286 forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); | 304 forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); |
| 287 Context context = getAvailableContext(); | 305 Context context = getAvailableContext(); |
| 288 if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTI
VITY_NEW_TASK); | 306 if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTI
VITY_NEW_TASK); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 @Override | 504 @Override |
| 487 public boolean isPdfDownload(String url) { | 505 public boolean isPdfDownload(String url) { |
| 488 if (ChromeFeatureList.isEnabled(ChromeFeatureList.SYSTEM_DOWNLOAD_MANAGE
R)) return false; | 506 if (ChromeFeatureList.isEnabled(ChromeFeatureList.SYSTEM_DOWNLOAD_MANAGE
R)) return false; |
| 489 | 507 |
| 490 String fileExtension = MimeTypeMap.getFileExtensionFromUrl(url); | 508 String fileExtension = MimeTypeMap.getFileExtensionFromUrl(url); |
| 491 if (TextUtils.isEmpty(fileExtension)) return false; | 509 if (TextUtils.isEmpty(fileExtension)) return false; |
| 492 | 510 |
| 493 return PDF_EXTENSION.equals(fileExtension); | 511 return PDF_EXTENSION.equals(fileExtension); |
| 494 } | 512 } |
| 495 } | 513 } |
| OLD | NEW |