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 PackageManager pm = context.getPackageManager(); |
| 48 ResolveInfo info = getResolveInfoForViewIntent(pm); |
| 49 |
| 50 // Caches whether Chrome is set as a default browser on
the device. |
| 51 boolean isDefault = (info != null && info.match != 0 |
| 52 && context.getPackageName().equals(info.activity
Info.packageName)); |
| 53 ChromePreferenceManager.getInstance().setCachedChromeDef
aultBrowser( |
| 54 isDefault); |
| 55 |
| 56 // Check if there is a default handler for the Intent.
If so, store its |
| 57 // label. |
| 58 String packageLabel = null; |
| 59 if (info != null && info.match != 0 && info.loadLabel(pm
) != null) { |
| 60 packageLabel = info.loadLabel(pm).toString(); |
| 61 } |
| 62 if (packageLabel == null) { |
| 63 menuTitles.add( |
| 64 context.getString(R.string.menu_open_in_prod
uct_default)); |
| 65 } else { |
| 66 menuTitles.add( |
| 67 context.getString(R.string.menu_open_in_prod
uct, packageLabel)); |
| 68 } |
| 69 return menuTitles; |
| 70 } |
| 71 }; |
| 72 sDefaultBrowserFetcher.executeOnExecutor(AsyncTask.THREAD_POOL_E
XECUTOR); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 /** |
| 78 * @return Default ResolveInfo to handle a VIEW intent for a url. |
| 79 * @param pm The PackageManager of current context. |
| 80 */ |
| 81 public static ResolveInfo getResolveInfoForViewIntent(PackageManager pm) { |
| 82 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SAMPLE_URL)); |
| 83 return pm.resolveActivity(intent, 0); |
| 84 } |
| 85 |
| 86 /** |
| 87 * @return Title of the menu item for opening a link in the default browser. |
| 88 * @param forceChromeAsDefault Whether the Custom Tab is created by Chrome. |
| 89 */ |
| 90 public static String getTitleOpenInDefaultBrowser(final boolean forceChromeA
sDefault) { |
| 91 if (sDefaultBrowserFetcher == null) { |
| 92 initBrowserFetcher(); |
| 93 } |
| 94 try { |
| 95 // If the Custom Tab was created by Chrome, Chrome should handle the
action for the |
| 96 // overflow menu. |
| 97 return forceChromeAsDefault ? sDefaultBrowserFetcher.get().get(0) |
| 98 : sDefaultBrowserFetcher.get().get(1); |
| 99 } catch (InterruptedException | ExecutionException e) { |
| 100 return ContextUtils.getApplicationContext().getString( |
| 101 R.string.menu_open_in_product_default); |
| 102 } |
| 103 } |
| 104 } |
OLD | NEW |