Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/DefaultBrowserInfo.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/DefaultBrowserInfo.java b/chrome/android/java/src/org/chromium/chrome/browser/DefaultBrowserInfo.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..42a6524ace4e454a409fadbfaa880f4450c74410 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/DefaultBrowserInfo.java |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.content.pm.PackageManager; |
| +import android.content.pm.ResolveInfo; |
| +import android.net.Uri; |
| +import android.os.AsyncTask; |
| + |
| +import org.chromium.base.BuildInfo; |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.chrome.R; |
| + |
| +import java.util.ArrayList; |
| +import java.util.concurrent.ExecutionException; |
| + |
| +/** |
| + * A utility class for querying information about the default browser setting. |
| + */ |
| +public class DefaultBrowserInfo { |
| + private static final String SAMPLE_URL = "http://www.madeupdomainforcheck123.com/"; |
| + |
| + /** Prevents multi-threads from getting created simultaneously. */ |
|
Maria
2017/02/16 05:41:11
A more useful comment would be to say something li
ltian
2017/02/17 05:52:54
Done.
|
| + private static final Object DIR_CREATION_LOCK = new Object(); |
|
Maria
2017/02/16 05:41:12
this should be named sDirCreationLock, it's final,
ltian
2017/02/17 05:52:54
Done.
|
| + |
| + private static AsyncTask<Void, Void, ArrayList<String>> sDefaultBrowserFetcher; |
| + |
| + /** |
| + * Initialize an AsyncTask for getting menu title of opening a link in default browser. |
| + */ |
| + public static void initBrowserFetcher() { |
| + synchronized (DIR_CREATION_LOCK) { |
| + if (sDefaultBrowserFetcher == null) { |
| + sDefaultBrowserFetcher = new AsyncTask<Void, Void, ArrayList<String>>() { |
| + @Override |
| + protected ArrayList<String> doInBackground(Void... params) { |
| + Context context = ContextUtils.getApplicationContext(); |
| + ArrayList<String> menuTitles = new ArrayList<String>(2); |
| + // Store the package label of current application. |
| + menuTitles.add(BuildInfo.getPackageLabel(context)); |
| + |
| + // Check if there is a default handler for the Intent. If so, store its |
| + // label. |
| + String packageLabel = null; |
| + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SAMPLE_URL)); |
| + PackageManager pm = context.getPackageManager(); |
| + ResolveInfo info = pm.resolveActivity(intent, 0); |
| + if (info != null && info.match != 0) { |
| + packageLabel = info.loadLabel(pm).toString(); |
|
Maria
2017/02/16 05:41:12
is info.loadLabel() guaranteed to be non-null? Oth
ltian
2017/02/17 05:52:54
Done.
|
| + } |
| + if (packageLabel == null) { |
| + menuTitles.add( |
| + context.getString(R.string.menu_open_in_product_default)); |
| + } else { |
| + menuTitles.add( |
| + context.getString(R.string.menu_open_in_product, packageLabel)); |
| + } |
| + return menuTitles; |
| + } |
| + }; |
| + sDefaultBrowserFetcher.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * @return Title of the menu item for opening a link in the default browser. |
| + * @param forceChromeAsDefault Whether the Custom Tab is created by Chrome. |
| + */ |
| + public static String getTitleOpenInDefaultBrowser(final boolean forceChromeAsDefault) { |
| + if (sDefaultBrowserFetcher == null) { |
| + initBrowserFetcher(); |
| + } |
| + try { |
| + // If the Custom Tab was created by Chrome, Chrome should handle the action for the |
| + // overflow menu. |
| + return forceChromeAsDefault ? sDefaultBrowserFetcher.get().get(0) |
| + : sDefaultBrowserFetcher.get().get(1); |
| + } catch (InterruptedException | ExecutionException e) { |
| + return ContextUtils.getApplicationContext().getString( |
| + R.string.menu_open_in_product_default); |
| + } |
| + } |
| +} |