• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 import com.android.settings.applications.PackageManagerWrapper;
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
getDefaultAppInfo()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(mPackageManager, mUserId, currentDefaultHome);
65         }
66         final ActivityInfo onlyAppInfo = getOnlyAppInfo();
67         if (onlyAppInfo != null) {
68             return new DefaultAppInfo(mPackageManager, mUserId, onlyAppInfo.getComponentName());
69         }
70         return null;
71     }
72 
getOnlyAppInfo()73     private ActivityInfo getOnlyAppInfo() {
74         final List<ResolveInfo> homeActivities = new ArrayList<>();
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 
hasHomePreference(String pkg, Context context)90     public static boolean hasHomePreference(String pkg, Context context) {
91         ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
92         PackageManager pm = context.getPackageManager();
93         pm.getHomeActivities(homeActivities);
94         for (int i = 0; i < homeActivities.size(); i++) {
95             if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) {
96                 return true;
97             }
98         }
99         return false;
100     }
101 
isHomeDefault(String pkg, PackageManagerWrapper pm)102     public static boolean isHomeDefault(String pkg, PackageManagerWrapper pm) {
103         final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
104         ComponentName def = pm.getHomeActivities(homeActivities);
105 
106         return def == null || def.getPackageName().equals(pkg);
107     }
108 }
109