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..598bb18fb4cfd2bee68b60bc3f404fef0874e217 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/DefaultBrowserInfo.java |
| @@ -0,0 +1,59 @@ |
| +// 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.app.Activity; |
| +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.chrome.R; |
| + |
| +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 = "https://www.google.com"; |
|
Ted C
2017/02/14 19:02:00
I would use the one from cacheIsChromeDefaultBrows
ltian
2017/02/16 05:05:10
Done.
|
| + |
| + /** |
| + * @return Title of menu item for opening tab in the default browser. |
| + * @param activity The activity to open the menu. |
| + * @param isOpenedByChrome Whether the menu is opened by Chrome. |
| + * @throws ExecutionException |
| + * @throws InterruptedException |
| + */ |
| + public static String getTitleOpenInDefaultBrowser(final Activity activity, |
| + final Boolean isOpenedByChrome) throws InterruptedException, ExecutionException { |
|
Ted C
2017/02/14 19:02:00
Why capital B boolean?
Also, do you need Activity
ltian
2017/02/16 05:05:10
Done.
|
| + AsyncTask<Void, Void, String> defaultBrowserFetcher = new AsyncTask<Void, Void, String>() { |
|
Ted C
2017/02/14 19:02:00
Creating an AsyncTask and then calling get on it i
|
| + @Override |
| + protected String doInBackground(Void... params) { |
| + String packageLabel = null; |
| + if (isOpenedByChrome) { |
| + // If the Custom Tab was created by Chrome, Chrome should open it. |
| + packageLabel = BuildInfo.getPackageLabel(activity); |
| + } else { |
| + // Check if there is a default handler for the Intent. If so, grab its label. |
| + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SAMPLE_URL)); |
| + PackageManager pm = activity.getPackageManager(); |
| + ResolveInfo info = pm.resolveActivity(intent, 0); |
| + if (info != null && info.match != 0) { |
| + packageLabel = info.loadLabel(pm).toString(); |
| + } |
| + } |
| + |
| + return packageLabel == null |
| + ? activity.getString(R.string.menu_open_in_product_default) |
| + : activity.getString(R.string.menu_open_in_product, packageLabel); |
| + } |
| + }; |
| + defaultBrowserFetcher.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| + return defaultBrowserFetcher.get(); |
| + } |
| +} |