DefaultBrowserPreferenceController.java revision 6786d8cd5335d51479ec09b4aa0dd67a68bd9729
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 17package com.android.settings.applications.defaultapps; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.pm.PackageManager; 22import android.content.pm.ResolveInfo; 23import android.net.Uri; 24import android.support.v7.preference.Preference; 25import android.text.TextUtils; 26 27import java.util.List; 28 29public 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 return true; 43 } 44 45 @Override 46 public String getPreferenceKey() { 47 return "default_browser"; 48 } 49 50 @Override 51 public void updateState(Preference preference) { 52 super.updateState(preference); 53 final DefaultAppInfo defaultApp = getDefaultAppInfo(); 54 final CharSequence defaultAppLabel = defaultApp != null 55 ? defaultApp.loadLabel(mPackageManager.getPackageManager()) : null; 56 if (TextUtils.isEmpty(defaultAppLabel)) { 57 final String onlyAppLabel = getOnlyAppLabel(); 58 if (!TextUtils.isEmpty(onlyAppLabel)) { 59 preference.setSummary(onlyAppLabel); 60 } 61 } 62 } 63 64 private String getOnlyAppLabel() { 65 // Resolve that intent and check that the handleAllWebDataURI boolean is set 66 final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, 67 PackageManager.MATCH_ALL, mUserId); 68 if (list != null && list.size() == 1) { 69 return list.get(0).loadLabel(mPackageManager.getPackageManager()).toString(); 70 } 71 return null; 72 } 73 74 @Override 75 protected DefaultAppInfo getDefaultAppInfo() { 76 try { 77 return new DefaultAppInfo(mPackageManager.getPackageManager().getApplicationInfo( 78 mPackageManager.getDefaultBrowserPackageNameAsUser(mUserId), 0)); 79 } catch (PackageManager.NameNotFoundException e) { 80 return null; 81 } 82 } 83 84 /** 85 * Whether or not the pkg contains browser capability 86 */ 87 public static boolean hasBrowserPreference(String pkg, Context context) { 88 final Intent intent = new Intent(BROWSE_PROBE); 89 intent.setPackage(pkg); 90 final List<ResolveInfo> resolveInfos = 91 context.getPackageManager().queryIntentActivities(intent, 0); 92 return resolveInfos != null && resolveInfos.size() != 0; 93 } 94 95 /** 96 * Whether or not the pkg is the default browser 97 */ 98 public boolean isBrowserDefault(String pkg, int userId) { 99 String defaultPackage = mPackageManager.getDefaultBrowserPackageNameAsUser(userId); 100 if (defaultPackage != null) { 101 return defaultPackage.equals(pkg); 102 } 103 104 final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, 105 PackageManager.MATCH_ALL, userId); 106 // There is only 1 app, it must be the default browser. 107 return list != null && list.size() == 1; 108 } 109} 110