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.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.net.Uri; 24 import android.support.v7.preference.Preference; 25 import android.text.TextUtils; 26 27 import java.util.List; 28 29 public class DefaultBrowserPreferenceController extends DefaultAppPreferenceController { 30 31 static final Intent BROWSE_PROBE = new Intent() 32 .setAction(Intent.ACTION_VIEW) 33 .addCategory(Intent.CATEGORY_BROWSABLE) 34 .setData(Uri.parse("http:")); 35 36 public DefaultBrowserPreferenceController(Context context) { 37 super(context); 38 } 39 40 @Override 41 public boolean isAvailable() { 42 final List<ResolveInfo> candidates = getCandidates(); 43 return candidates != null && !candidates.isEmpty(); 44 } 45 46 @Override 47 public String getPreferenceKey() { 48 return "default_browser"; 49 } 50 51 @Override 52 public void updateState(Preference preference) { 53 super.updateState(preference); 54 final CharSequence defaultAppLabel = getDefaultAppLabel(); 55 if (!TextUtils.isEmpty(defaultAppLabel)) { 56 preference.setSummary(defaultAppLabel); 57 } 58 } 59 60 @Override 61 protected DefaultAppInfo getDefaultAppInfo() { 62 try { 63 return new DefaultAppInfo(mPackageManager, 64 mPackageManager.getPackageManager().getApplicationInfo( 65 mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId), 0)); 66 } catch (PackageManager.NameNotFoundException e) { 67 return null; 68 } 69 } 70 71 @Override 72 public CharSequence getDefaultAppLabel() { 73 if (!isAvailable()) { 74 return null; 75 } 76 final DefaultAppInfo defaultApp = getDefaultAppInfo(); 77 final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null; 78 if (!TextUtils.isEmpty(defaultAppLabel)) { 79 return defaultAppLabel; 80 } 81 return getOnlyAppLabel(); 82 } 83 84 private List<ResolveInfo> getCandidates() { 85 return mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, PackageManager.MATCH_ALL, 86 mUserId); 87 } 88 89 private String getOnlyAppLabel() { 90 // Resolve that intent and check that the handleAllWebDataURI boolean is set 91 final List<ResolveInfo> list = getCandidates(); 92 if (list != null && list.size() == 1) { 93 return list.get(0).loadLabel(mPackageManager.getPackageManager()).toString(); 94 } 95 return null; 96 } 97 98 /** 99 * Whether or not the pkg contains browser capability 100 */ 101 public static boolean hasBrowserPreference(String pkg, Context context) { 102 final Intent intent = new Intent(BROWSE_PROBE); 103 intent.setPackage(pkg); 104 final List<ResolveInfo> resolveInfos = 105 context.getPackageManager().queryIntentActivities(intent, 0); 106 return resolveInfos != null && resolveInfos.size() != 0; 107 } 108 109 /** 110 * Whether or not the pkg is the default browser 111 */ 112 public boolean isBrowserDefault(String pkg, int userId) { 113 String defaultPackage = mPackageManager.getDefaultBrowserPackageNameAsUser(userId); 114 if (defaultPackage != null) { 115 return defaultPackage.equals(pkg); 116 } 117 118 final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, 119 PackageManager.MATCH_ALL, userId); 120 // There is only 1 app, it must be the default browser. 121 return list != null && list.size() == 1; 122 } 123 } 124