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.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.ActivityInfo; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.support.v7.preference.Preference; 27 import android.text.TextUtils; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 public class DefaultHomePreferenceController extends DefaultAppPreferenceController { 33 34 static final IntentFilter HOME_FILTER; 35 36 private final String mPackageName; 37 38 static { 39 HOME_FILTER = new IntentFilter(Intent.ACTION_MAIN); 40 HOME_FILTER.addCategory(Intent.CATEGORY_HOME); 41 HOME_FILTER.addCategory(Intent.CATEGORY_DEFAULT); 42 } 43 DefaultHomePreferenceController(Context context)44 public DefaultHomePreferenceController(Context context) { 45 super(context); 46 mPackageName = mContext.getPackageName(); 47 } 48 49 @Override getPreferenceKey()50 public String getPreferenceKey() { 51 return "default_home"; 52 } 53 54 @Override isAvailable()55 public boolean isAvailable() { 56 return true; 57 } 58 59 @Override updateState(Preference preference)60 public void updateState(Preference preference) { 61 super.updateState(preference); 62 final DefaultAppInfo defaultApp = getDefaultAppInfo(); 63 final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null; 64 if (TextUtils.isEmpty(defaultAppLabel)) { 65 final String onlyAppLabel = getOnlyAppLabel(); 66 if (!TextUtils.isEmpty(onlyAppLabel)) { 67 preference.setSummary(onlyAppLabel); 68 } 69 } 70 } 71 72 @Override getDefaultAppInfo()73 protected DefaultAppInfo getDefaultAppInfo() { 74 final ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 75 final ComponentName currentDefaultHome = mPackageManager.getHomeActivities(homeActivities); 76 77 return new DefaultAppInfo(mPackageManager, mUserId, currentDefaultHome); 78 } 79 getOnlyAppLabel()80 private String getOnlyAppLabel() { 81 final List<ResolveInfo> homeActivities = new ArrayList<>(); 82 final List<ActivityInfo> appLabels = new ArrayList<>(); 83 84 mPackageManager.getHomeActivities(homeActivities); 85 for (ResolveInfo candidate : homeActivities) { 86 final ActivityInfo info = candidate.activityInfo; 87 if (info.packageName.equals(mPackageName)) { 88 continue; 89 } 90 appLabels.add(info); 91 } 92 return appLabels.size() == 1 93 ? appLabels.get(0).loadLabel(mPackageManager.getPackageManager()).toString() 94 : null; 95 } 96 hasHomePreference(String pkg, Context context)97 public static boolean hasHomePreference(String pkg, Context context) { 98 ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 99 PackageManager pm = context.getPackageManager(); 100 pm.getHomeActivities(homeActivities); 101 for (int i = 0; i < homeActivities.size(); i++) { 102 if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) { 103 return true; 104 } 105 } 106 return false; 107 } 108 isHomeDefault(String pkg, Context context)109 public static boolean isHomeDefault(String pkg, Context context) { 110 ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 111 PackageManager pm = context.getPackageManager(); 112 ComponentName def = pm.getHomeActivities(homeActivities); 113 114 return def != null && def.getPackageName().equals(pkg); 115 } 116 } 117