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.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.content.pm.UserInfo; 28 import android.os.Build; 29 import android.text.TextUtils; 30 31 import com.android.internal.logging.nano.MetricsProto; 32 import com.android.settings.R; 33 import com.android.settingslib.applications.DefaultAppInfo; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 public class DefaultHomePicker extends DefaultAppPickerFragment { 39 40 private String mPackageName; 41 42 @Override 43 public void onAttach(Context context) { 44 super.onAttach(context); 45 mPackageName = context.getPackageName(); 46 } 47 48 @Override 49 protected int getPreferenceScreenResId() { 50 return R.xml.default_home_settings; 51 } 52 53 @Override 54 public int getMetricsCategory() { 55 return MetricsProto.MetricsEvent.DEFAULT_HOME_PICKER; 56 } 57 58 @Override 59 protected List<DefaultAppInfo> getCandidates() { 60 final boolean mustSupportManagedProfile = hasManagedProfile(); 61 final List<DefaultAppInfo> candidates = new ArrayList<>(); 62 final List<ResolveInfo> homeActivities = new ArrayList<>(); 63 final Context context = getContext(); 64 mPm.getHomeActivities(homeActivities); 65 66 for (ResolveInfo resolveInfo : homeActivities) { 67 final ActivityInfo info = resolveInfo.activityInfo; 68 final ComponentName activityName = new ComponentName(info.packageName, info.name); 69 if (info.packageName.equals(mPackageName)) { 70 continue; 71 } 72 73 final String summary; 74 boolean enabled = true; 75 if (mustSupportManagedProfile && !launcherHasManagedProfilesFeature(resolveInfo)) { 76 summary = getContext().getString(R.string.home_work_profile_not_supported); 77 enabled = false; 78 } else { 79 summary = null; 80 } 81 final DefaultAppInfo candidate = 82 new DefaultAppInfo(context, mPm, mUserId, activityName, summary, enabled); 83 candidates.add(candidate); 84 } 85 return candidates; 86 } 87 88 @Override 89 protected String getDefaultKey() { 90 final ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 91 final ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities); 92 if (currentDefaultHome != null) { 93 return currentDefaultHome.flattenToString(); 94 } 95 return null; 96 } 97 98 @Override 99 protected boolean setDefaultKey(String key) { 100 if (!TextUtils.isEmpty(key)) { 101 final ComponentName component = ComponentName.unflattenFromString(key); 102 final List<ResolveInfo> homeActivities = new ArrayList<>(); 103 mPm.getHomeActivities(homeActivities); 104 final List<ComponentName> allComponents = new ArrayList<>(); 105 for (ResolveInfo info : homeActivities) { 106 final ActivityInfo appInfo = info.activityInfo; 107 ComponentName activityName = new ComponentName(appInfo.packageName, appInfo.name); 108 allComponents.add(activityName); 109 } 110 mPm.replacePreferredActivity( 111 DefaultHomePreferenceController.HOME_FILTER, 112 IntentFilter.MATCH_CATEGORY_EMPTY, 113 allComponents.toArray(new ComponentName[0]), 114 component); 115 116 // Launch the new Home app so the change is immediately visible even if 117 // the Home button is not pressed. 118 final Context context = getContext(); 119 Intent i = new Intent(Intent.ACTION_MAIN); 120 i.addCategory(Intent.CATEGORY_HOME); 121 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 122 context.startActivity(i); 123 return true; 124 } 125 return false; 126 } 127 128 private boolean hasManagedProfile() { 129 final Context context = getContext(); 130 List<UserInfo> profiles = mUserManager.getProfiles(context.getUserId()); 131 for (UserInfo userInfo : profiles) { 132 if (userInfo.isManagedProfile()) { 133 return true; 134 } 135 } 136 return false; 137 } 138 139 private boolean launcherHasManagedProfilesFeature(ResolveInfo resolveInfo) { 140 try { 141 ApplicationInfo appInfo = mPm.getPackageManager().getApplicationInfo( 142 resolveInfo.activityInfo.packageName, 0 /* default flags */); 143 return versionNumberAtLeastL(appInfo.targetSdkVersion); 144 } catch (PackageManager.NameNotFoundException e) { 145 return false; 146 } 147 } 148 149 private boolean versionNumberAtLeastL(int versionNumber) { 150 return versionNumber >= Build.VERSION_CODES.LOLLIPOP; 151 } 152 } 153