DefaultHomePreferenceController.java revision ea63e4c6c6b0b89d50be8b804f4b27715ad534c4
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 17package com.android.settings.applications.defaultapps; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.content.pm.ActivityInfo; 24import android.content.pm.PackageManager; 25import android.content.pm.ResolveInfo; 26 27import com.android.settingslib.wrapper.PackageManagerWrapper; 28 29import java.util.ArrayList; 30import java.util.List; 31 32public 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 44 public DefaultHomePreferenceController(Context context) { 45 super(context); 46 mPackageName = mContext.getPackageName(); 47 } 48 49 @Override 50 public String getPreferenceKey() { 51 return "default_home"; 52 } 53 54 @Override 55 public boolean isAvailable() { 56 return true; 57 } 58 59 @Override 60 protected DefaultAppInfo getDefaultAppInfo() { 61 final ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 62 final ComponentName currentDefaultHome = mPackageManager.getHomeActivities(homeActivities); 63 if (currentDefaultHome != null) { 64 return new DefaultAppInfo(mContext, mPackageManager, mUserId, currentDefaultHome); 65 } 66 final ActivityInfo onlyAppInfo = getOnlyAppInfo(homeActivities); 67 if (onlyAppInfo != null) { 68 return new DefaultAppInfo(mContext, mPackageManager, mUserId, 69 onlyAppInfo.getComponentName()); 70 } 71 return null; 72 } 73 74 private ActivityInfo getOnlyAppInfo(List<ResolveInfo> homeActivities) { 75 final List<ActivityInfo> appLabels = new ArrayList<>(); 76 77 mPackageManager.getHomeActivities(homeActivities); 78 for (ResolveInfo candidate : homeActivities) { 79 final ActivityInfo info = candidate.activityInfo; 80 if (info.packageName.equals(mPackageName)) { 81 continue; 82 } 83 appLabels.add(info); 84 } 85 return appLabels.size() == 1 86 ? appLabels.get(0) 87 : null; 88 } 89 90 @Override 91 protected Intent getSettingIntent(DefaultAppInfo info) { 92 final String packageName; 93 if (info.componentName != null) { 94 packageName = info.componentName.getPackageName(); 95 } else if (info.packageItemInfo != null) { 96 packageName = info.packageItemInfo.packageName; 97 } else { 98 return null; 99 } 100 101 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES) 102 .setPackage(packageName) 103 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 104 return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null; 105 } 106 107 public static boolean hasHomePreference(String pkg, Context context) { 108 ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 109 PackageManager pm = context.getPackageManager(); 110 pm.getHomeActivities(homeActivities); 111 for (int i = 0; i < homeActivities.size(); i++) { 112 if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) { 113 return true; 114 } 115 } 116 return false; 117 } 118 119 public static boolean isHomeDefault(String pkg, PackageManagerWrapper pm) { 120 final ArrayList<ResolveInfo> homeActivities = new ArrayList<>(); 121 ComponentName def = pm.getHomeActivities(homeActivities); 122 123 return def == null || def.getPackageName().equals(pkg); 124 } 125} 126