1/*
2 * Copyright 2014, 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.managedprovisioning.task;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ComponentInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25
26import com.android.managedprovisioning.ProvisionLogger;
27import com.android.managedprovisioning.Utils;
28
29import java.util.List;
30import java.util.Set;
31
32
33/**
34 * Disables all system app components that listen to ACTION_INSTALL_SHORTCUT.
35 */
36public class DisableInstallShortcutListenersTask {
37    private final PackageManager mPm;
38    private final int mUserId;
39
40    public DisableInstallShortcutListenersTask(Context context, int userId) {
41        mUserId = userId;
42        mPm = context.getPackageManager();
43    }
44
45    public void run() {
46        ProvisionLogger.logd("Disabling install shortcut listeners.");
47        Intent actionShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
48        Set<String> systemApps = Utils.getCurrentSystemApps(mUserId);
49        for (String systemApp : systemApps) {
50            actionShortcut.setPackage(systemApp);
51            disableReceivers(actionShortcut);
52        }
53    }
54
55    /**
56     * Disable all components that can handle the specified broadcast intent.
57     */
58    private void disableReceivers(Intent intent) {
59        List<ResolveInfo> receivers = mPm.queryBroadcastReceivers(intent, 0, mUserId);
60        for (ResolveInfo ri : receivers) {
61            // One of ri.activityInfo, ri.serviceInfo, ri.providerInfo is not null. Let's find which
62            // one.
63            ComponentInfo ci;
64            if (ri.activityInfo != null) {
65                ci = ri.activityInfo;
66            } else if (ri.serviceInfo != null) {
67                ci = ri.serviceInfo;
68            } else {
69                ci = ri.providerInfo;
70            }
71            Utils.disableComponent(new ComponentName(ci.packageName, ci.name), mUserId);
72        }
73    }
74}
75