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 | |
18 import java.util.ArrayList; | |
19 import java.util.concurrent.ExecutionException; | |
20 | |
21 /** | |
22 * A utility class for querying information about the default browser setting. | |
23 */ | |
24 public class DefaultBrowserInfo { | |
25 private static final String SAMPLE_URL = "http://www.madeupdomainforcheck123 .com/"; | |
26 | |
27 /** 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.
| |
28 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.
| |
29 | |
30 private static AsyncTask<Void, Void, ArrayList<String>> sDefaultBrowserFetch er; | |
31 | |
32 /** | |
33 * Initialize an AsyncTask for getting menu title of opening a link in defau lt browser. | |
34 */ | |
35 public static void initBrowserFetcher() { | |
36 synchronized (DIR_CREATION_LOCK) { | |
37 if (sDefaultBrowserFetcher == null) { | |
38 sDefaultBrowserFetcher = new AsyncTask<Void, Void, ArrayList<Str ing>>() { | |
39 @Override | |
40 protected ArrayList<String> doInBackground(Void... params) { | |
41 Context context = ContextUtils.getApplicationContext(); | |
42 ArrayList<String> menuTitles = new ArrayList<String>(2); | |
43 // Store the package label of current application. | |
44 menuTitles.add(BuildInfo.getPackageLabel(context)); | |
45 | |
46 // Check if there is a default handler for the Intent. If so, store its | |
47 // label. | |
48 String packageLabel = null; | |
49 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse (SAMPLE_URL)); | |
50 PackageManager pm = context.getPackageManager(); | |
51 ResolveInfo info = pm.resolveActivity(intent, 0); | |
52 if (info != null && info.match != 0) { | |
53 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.
| |
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 * @return Title of the menu item for opening a link in the default browser. | |
72 * @param forceChromeAsDefault Whether the Custom Tab is created by Chrome. | |
73 */ | |
74 public static String getTitleOpenInDefaultBrowser(final boolean forceChromeA sDefault) { | |
75 if (sDefaultBrowserFetcher == null) { | |
76 initBrowserFetcher(); | |
77 } | |
78 try { | |
79 // If the Custom Tab was created by Chrome, Chrome should handle the action for the | |
80 // overflow menu. | |
81 return forceChromeAsDefault ? sDefaultBrowserFetcher.get().get(0) | |
82 : sDefaultBrowserFetcher.get().get(1); | |
83 } catch (InterruptedException | ExecutionException e) { | |
84 return ContextUtils.getApplicationContext().getString( | |
85 R.string.menu_open_in_product_default); | |
86 } | |
87 } | |
88 } | |
OLD | NEW |