DefaultHomePreferenceController.java revision efa763624a86f77c99b5cac53d8cf81e474ffc4b
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();
67        if (onlyAppInfo != null) {
68            return new DefaultAppInfo(mContext, mPackageManager, mUserId,
69                    onlyAppInfo.getComponentName());
70        }
71        return null;
72    }
73
74    private ActivityInfo getOnlyAppInfo() {
75        final List<ResolveInfo> homeActivities = new ArrayList<>();
76        final List<ActivityInfo> appLabels = new ArrayList<>();
77
78        mPackageManager.getHomeActivities(homeActivities);
79        for (ResolveInfo candidate : homeActivities) {
80            final ActivityInfo info = candidate.activityInfo;
81            if (info.packageName.equals(mPackageName)) {
82                continue;
83            }
84            appLabels.add(info);
85        }
86        return appLabels.size() == 1
87                ? appLabels.get(0)
88                : null;
89    }
90
91    public static boolean hasHomePreference(String pkg, Context context) {
92        ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
93        PackageManager pm = context.getPackageManager();
94        pm.getHomeActivities(homeActivities);
95        for (int i = 0; i < homeActivities.size(); i++) {
96            if (homeActivities.get(i).activityInfo.packageName.equals(pkg)) {
97                return true;
98            }
99        }
100        return false;
101    }
102
103    public static boolean isHomeDefault(String pkg, PackageManagerWrapper pm) {
104        final ArrayList<ResolveInfo> homeActivities = new ArrayList<>();
105        ComponentName def = pm.getHomeActivities(homeActivities);
106
107        return def == null || def.getPackageName().equals(pkg);
108    }
109}
110