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.pm.ComponentInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 24 import android.util.ArraySet; 25 import com.android.internal.logging.nano.MetricsProto; 26 import com.android.settings.R; 27 28 import com.android.settingslib.applications.DefaultAppInfo; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Set; 33 34 /** 35 * Fragment for choosing default browser. 36 */ 37 public class DefaultBrowserPicker extends DefaultAppPickerFragment { 38 39 @Override getPreferenceScreenResId()40 protected int getPreferenceScreenResId() { 41 return R.xml.default_browser_settings; 42 } 43 44 @Override getMetricsCategory()45 public int getMetricsCategory() { 46 return MetricsProto.MetricsEvent.DEFAULT_BROWSER_PICKER; 47 } 48 49 @Override getDefaultKey()50 protected String getDefaultKey() { 51 return mPm.getDefaultBrowserPackageNameAsUser(mUserId); 52 } 53 54 @Override setDefaultKey(String packageName)55 protected boolean setDefaultKey(String packageName) { 56 return mPm.setDefaultBrowserPackageNameAsUser(packageName, mUserId); 57 } 58 59 @Override getCandidates()60 protected List<DefaultAppInfo> getCandidates() { 61 final List<DefaultAppInfo> candidates = new ArrayList<>(); 62 final Context context = getContext(); 63 // Resolve that intent and check that the handleAllWebDataURI boolean is set 64 final List<ResolveInfo> list = mPm.queryIntentActivitiesAsUser( 65 DefaultBrowserPreferenceController.BROWSE_PROBE, PackageManager.MATCH_ALL, mUserId); 66 67 final int count = list.size(); 68 final Set<String> addedPackages = new ArraySet<>(); 69 for (int i = 0; i < count; i++) { 70 ResolveInfo info = list.get(i); 71 if (info.activityInfo == null || !info.handleAllWebDataURI) { 72 continue; 73 } 74 final String packageName = info.activityInfo.packageName; 75 if (addedPackages.contains(packageName)) { 76 continue; 77 } 78 try { 79 candidates.add(new DefaultAppInfo(context, mPm, 80 mPm.getApplicationInfoAsUser(packageName, 0, mUserId))); 81 addedPackages.add(packageName); 82 } catch (PackageManager.NameNotFoundException e) { 83 // Skip unknown packages. 84 } 85 } 86 87 return candidates; 88 } 89 } 90