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