1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.applications; 16 17 import android.content.ComponentName; 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.IntentFilter; 21 import android.content.pm.ActivityInfo; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.pm.UserInfo; 26 import android.os.Build; 27 import android.os.UserManager; 28 import android.util.AttributeSet; 29 import com.android.settings.AppListPreference; 30 import com.android.settings.R; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 public class DefaultHomePreference extends AppListPreference { 36 37 private final ArrayList<ComponentName> mAllHomeComponents = new ArrayList<>(); 38 private final IntentFilter mHomeFilter; 39 DefaultHomePreference(Context context, AttributeSet attrs)40 public DefaultHomePreference(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 mHomeFilter = new IntentFilter(Intent.ACTION_MAIN); 43 mHomeFilter.addCategory(Intent.CATEGORY_HOME); 44 mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT); 45 refreshHomeOptions(); 46 } 47 48 @Override performClick()49 public void performClick() { 50 refreshHomeOptions(); 51 super.performClick(); 52 } 53 54 @Override persistString(String value)55 protected boolean persistString(String value) { 56 if (value != null) { 57 ComponentName component = ComponentName.unflattenFromString(value); 58 getContext().getPackageManager().replacePreferredActivity(mHomeFilter, 59 IntentFilter.MATCH_CATEGORY_EMPTY, 60 mAllHomeComponents.toArray(new ComponentName[0]), component); 61 setSummary(getEntry()); 62 } 63 return super.persistString(value); 64 } 65 refreshHomeOptions()66 public void refreshHomeOptions() { 67 final String myPkg = getContext().getPackageName(); 68 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>(); 69 PackageManager pm = getContext().getPackageManager(); 70 ComponentName currentDefaultHome = pm.getHomeActivities(homeActivities); 71 ArrayList<ComponentName> components = new ArrayList<>(); 72 mAllHomeComponents.clear(); 73 List<CharSequence> summaries = new ArrayList<>(); 74 75 boolean mustSupportManagedProfile = hasManagedProfile(); 76 for (int i = 0; i < homeActivities.size(); i++) { 77 final ResolveInfo candidate = homeActivities.get(i); 78 final ActivityInfo info = candidate.activityInfo; 79 ComponentName activityName = new ComponentName(info.packageName, info.name); 80 mAllHomeComponents.add(activityName); 81 if (info.packageName.equals(myPkg)) { 82 continue; 83 } 84 components.add(activityName); 85 if (mustSupportManagedProfile && !launcherHasManagedProfilesFeature(candidate, pm)) { 86 summaries.add(getContext().getString(R.string.home_work_profile_not_supported)); 87 } else { 88 summaries.add(null); 89 } 90 } 91 setComponentNames(components.toArray(new ComponentName[0]), currentDefaultHome, 92 summaries.toArray(new CharSequence[0])); 93 } 94 launcherHasManagedProfilesFeature(ResolveInfo resolveInfo, PackageManager pm)95 private boolean launcherHasManagedProfilesFeature(ResolveInfo resolveInfo, PackageManager pm) { 96 try { 97 ApplicationInfo appInfo = pm.getApplicationInfo( 98 resolveInfo.activityInfo.packageName, 0 /* default flags */); 99 return versionNumberAtLeastL(appInfo.targetSdkVersion); 100 } catch (PackageManager.NameNotFoundException e) { 101 return false; 102 } 103 } 104 versionNumberAtLeastL(int versionNumber)105 private boolean versionNumberAtLeastL(int versionNumber) { 106 return versionNumber >= Build.VERSION_CODES.LOLLIPOP; 107 } 108 hasManagedProfile()109 private boolean hasManagedProfile() { 110 UserManager userManager = getContext().getSystemService(UserManager.class); 111 List<UserInfo> profiles = userManager.getProfiles(getContext().getUserId()); 112 for (UserInfo userInfo : profiles) { 113 if (userInfo.isManagedProfile()) return true; 114 } 115 return false; 116 } 117 hasHomePreference(String pkg, Context context)118 public static boolean hasHomePreference(String pkg, Context context) { 119 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>(); 120 PackageManager pm = context.getPackageManager(); 121 pm.getHomeActivities(homeActivities); 122 for (int i = 0; i < homeActivities.size(); i++) { 123 if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) { 124 return true; 125 } 126 } 127 return false; 128 } 129 isHomeDefault(String pkg, Context context)130 public static boolean isHomeDefault(String pkg, Context context) { 131 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>(); 132 PackageManager pm = context.getPackageManager(); 133 ComponentName def = pm.getHomeActivities(homeActivities); 134 135 return def != null && def.getPackageName().equals(pkg); 136 } 137 } 138