• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.applications.defaultapps;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.ComponentInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.graphics.drawable.Drawable;
26 import android.net.Uri;
27 import android.support.v7.preference.Preference;
28 import android.text.TextUtils;
29 import android.util.IconDrawableFactory;
30 import android.util.Log;
31 
32 import com.android.settingslib.applications.DefaultAppInfo;
33 
34 import java.util.List;
35 
36 public class DefaultBrowserPreferenceController extends DefaultAppPreferenceController {
37 
38     private static final String TAG = "BrowserPrefCtrl";
39 
40     static final Intent BROWSE_PROBE = new Intent()
41             .setAction(Intent.ACTION_VIEW)
42             .addCategory(Intent.CATEGORY_BROWSABLE)
43             .setData(Uri.parse("http:"));
44 
DefaultBrowserPreferenceController(Context context)45     public DefaultBrowserPreferenceController(Context context) {
46         super(context);
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         final List<ResolveInfo> candidates = getCandidates();
52         return candidates != null && !candidates.isEmpty();
53     }
54 
55     @Override
getPreferenceKey()56     public String getPreferenceKey() {
57         return "default_browser";
58     }
59 
60     @Override
updateState(Preference preference)61     public void updateState(Preference preference) {
62         super.updateState(preference);
63         final CharSequence defaultAppLabel = getDefaultAppLabel();
64         if (!TextUtils.isEmpty(defaultAppLabel)) {
65             preference.setSummary(defaultAppLabel);
66         }
67     }
68 
69     @Override
getDefaultAppInfo()70     protected DefaultAppInfo getDefaultAppInfo() {
71         try {
72             final String packageName = mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId);
73             Log.d(TAG, "Get default browser package: " + packageName);
74             return new DefaultAppInfo(mContext, mPackageManager,
75                     mPackageManager.getPackageManager().getApplicationInfo(packageName, 0));
76         } catch (PackageManager.NameNotFoundException e) {
77             return null;
78         }
79     }
80 
81     @Override
getDefaultAppLabel()82     public CharSequence getDefaultAppLabel() {
83         if (!isAvailable()) {
84             return null;
85         }
86         final DefaultAppInfo defaultApp = getDefaultAppInfo();
87         final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null;
88         if (!TextUtils.isEmpty(defaultAppLabel)) {
89             return defaultAppLabel;
90         }
91         return getOnlyAppLabel();
92     }
93 
94     @Override
getDefaultAppIcon()95     public Drawable getDefaultAppIcon() {
96         if (!isAvailable()) {
97             return null;
98         }
99         final DefaultAppInfo defaultApp = getDefaultAppInfo();
100         if (defaultApp != null) {
101             return defaultApp.loadIcon();
102         }
103         return getOnlyAppIcon();
104     }
105 
getCandidates()106     private List<ResolveInfo> getCandidates() {
107         return mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, PackageManager.MATCH_ALL,
108                 mUserId);
109     }
110 
getOnlyAppLabel()111     private String getOnlyAppLabel() {
112         // Resolve that intent and check that the handleAllWebDataURI boolean is set
113         final List<ResolveInfo> list = getCandidates();
114         if (list != null && list.size() == 1) {
115             final ResolveInfo info = list.get(0);
116             final String label = info.loadLabel(mPackageManager.getPackageManager()).toString();
117             final ComponentInfo cn = info.getComponentInfo();
118             final String packageName = cn == null ? null : cn.packageName;
119             Log.d(TAG, "Getting label for the only browser app: " + packageName + label);
120             return label;
121         }
122         return null;
123     }
124 
getOnlyAppIcon()125     private Drawable getOnlyAppIcon() {
126         final List<ResolveInfo> list = getCandidates();
127         if (list != null && list.size() == 1) {
128             final ResolveInfo info = list.get(0);
129             final ComponentInfo cn = info.getComponentInfo();
130             final String packageName = cn == null ? null : cn.packageName;
131             if (TextUtils.isEmpty(packageName)) {
132                 return null;
133             }
134             final ApplicationInfo appInfo;
135             try {
136                 appInfo = mPackageManager.getPackageManager().getApplicationInfo(packageName, 0);
137             } catch (PackageManager.NameNotFoundException e) {
138                 Log.w(TAG, "Error getting app info for " + packageName);
139                 return null;
140             }
141             Log.d(TAG, "Getting icon for the only browser app: " + packageName);
142             final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(mContext);
143             return iconFactory.getBadgedIcon(cn, appInfo, mUserId);
144         }
145         return null;
146     }
147 
148     /**
149      * Whether or not the pkg contains browser capability
150      */
hasBrowserPreference(String pkg, Context context)151     public static boolean hasBrowserPreference(String pkg, Context context) {
152         final Intent intent = new Intent(BROWSE_PROBE);
153         intent.setPackage(pkg);
154         final List<ResolveInfo> resolveInfos =
155                 context.getPackageManager().queryIntentActivities(intent, 0);
156         return resolveInfos != null && resolveInfos.size() != 0;
157     }
158 
159     /**
160      * Whether or not the pkg is the default browser
161      */
isBrowserDefault(String pkg, int userId)162     public boolean isBrowserDefault(String pkg, int userId) {
163         String defaultPackage = mPackageManager.getDefaultBrowserPackageNameAsUser(userId);
164         if (defaultPackage != null) {
165             return defaultPackage.equals(pkg);
166         }
167 
168         final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE,
169                 PackageManager.MATCH_ALL, userId);
170         // There is only 1 app, it must be the default browser.
171         return list != null && list.size() == 1;
172     }
173 }
174