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