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