Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.Intent; | |
| 9 import android.content.pm.PackageManager; | |
| 10 import android.content.pm.ResolveInfo; | |
| 11 import android.net.Uri; | |
| 12 import android.os.AsyncTask; | |
| 13 | |
| 14 import org.chromium.base.BuildInfo; | |
| 15 import org.chromium.base.ContextUtils; | |
| 16 import org.chromium.chrome.R; | |
| 17 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; | |
| 18 | |
| 19 import java.util.ArrayList; | |
| 20 import java.util.concurrent.ExecutionException; | |
| 21 | |
| 22 /** | |
| 23 * A utility class for querying information about the default browser setting. | |
| 24 */ | |
| 25 public class DefaultBrowserInfo { | |
| 26 private static final String SAMPLE_URL = "http://www.madeupdomainforcheck123 .com/"; | |
| 27 | |
| 28 /** A lock to synchronize background tasks to retrieve browser information. */ | |
| 29 private static final Object sDirCreationLock = new Object(); | |
| 30 | |
| 31 private static AsyncTask<Void, Void, ArrayList<String>> sDefaultBrowserFetch er; | |
| 32 | |
| 33 /** | |
| 34 * Initialize an AsyncTask for getting menu title of opening a link in defau lt browser. | |
| 35 */ | |
| 36 public static void initBrowserFetcher() { | |
| 37 synchronized (sDirCreationLock) { | |
| 38 if (sDefaultBrowserFetcher == null) { | |
| 39 sDefaultBrowserFetcher = new AsyncTask<Void, Void, ArrayList<Str ing>>() { | |
| 40 @Override | |
| 41 protected ArrayList<String> doInBackground(Void... params) { | |
| 42 Context context = ContextUtils.getApplicationContext(); | |
| 43 ArrayList<String> menuTitles = new ArrayList<String>(2); | |
| 44 // Store the package label of current application. | |
| 45 menuTitles.add(BuildInfo.getPackageLabel(context)); | |
| 46 | |
| 47 // Check if there is a default handler for the Intent. If so, store its | |
| 48 // label. | |
| 49 String packageLabel = null; | |
| 50 PackageManager pm = context.getPackageManager(); | |
| 51 ResolveInfo info = getResolveInfoForViewIntent(pm); | |
| 52 if (info != null && info.match != 0 && info.loadLabel(pm ) != null) { | |
| 53 packageLabel = info.loadLabel(pm).toString(); | |
| 54 } | |
| 55 if (packageLabel == null) { | |
| 56 menuTitles.add( | |
| 57 context.getString(R.string.menu_open_in_prod uct_default)); | |
| 58 } else { | |
| 59 menuTitles.add( | |
| 60 context.getString(R.string.menu_open_in_prod uct, packageLabel)); | |
| 61 } | |
| 62 return menuTitles; | |
| 63 } | |
| 64 }; | |
| 65 sDefaultBrowserFetcher.executeOnExecutor(AsyncTask.THREAD_POOL_E XECUTOR); | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Caches whether Chrome is set as a default browser on the device. | |
| 72 */ | |
| 73 public static void cacheIsChromeDefaultBrowser() { | |
| 74 // Retrieve whether Chrome is default in background to avoid strict mode checks. | |
| 75 Context context = ContextUtils.getApplicationContext(); | |
| 76 PackageManager pm = context.getPackageManager(); | |
| 77 ResolveInfo info = getResolveInfoForViewIntent(pm); | |
| 78 boolean isDefault = (info != null && info.match != 0 | |
| 79 && context.getPackageName().equals(info.activityInfo.packageName )); | |
| 80 ChromePreferenceManager.getInstance().setCachedChromeDefaultBrowser(isDe fault); | |
|
Maria
2017/02/17 23:01:23
Just do this in initBrowserFetcher.
E.g. on line
ltian
2017/02/21 19:15:37
Done.
| |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * @return Default ResolveInfo to handle a VIEW intent for a url. | |
| 85 * @param pm The PackageManager of current context. | |
| 86 */ | |
| 87 public static ResolveInfo getResolveInfoForViewIntent(PackageManager pm) { | |
| 88 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SAMPLE_URL)); | |
| 89 return pm.resolveActivity(intent, 0); | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * @return Title of the menu item for opening a link in the default browser. | |
| 94 * @param forceChromeAsDefault Whether the Custom Tab is created by Chrome. | |
| 95 */ | |
| 96 public static String getTitleOpenInDefaultBrowser(final boolean forceChromeA sDefault) { | |
| 97 if (sDefaultBrowserFetcher == null) { | |
| 98 initBrowserFetcher(); | |
| 99 } | |
| 100 try { | |
| 101 // If the Custom Tab was created by Chrome, Chrome should handle the action for the | |
| 102 // overflow menu. | |
| 103 return forceChromeAsDefault ? sDefaultBrowserFetcher.get().get(0) | |
| 104 : sDefaultBrowserFetcher.get().get(1); | |
| 105 } catch (InterruptedException | ExecutionException e) { | |
| 106 return ContextUtils.getApplicationContext().getString( | |
| 107 R.string.menu_open_in_product_default); | |
| 108 } | |
| 109 } | |
| 110 } | |
| OLD | NEW |