Home | History | Annotate | Download | only in facade
      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.googlecode.android_scripting.facade;
     18 
     19 import android.app.ActivityManager;
     20 import android.app.ActivityManager.RunningAppProcessInfo;
     21 import android.app.Service;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.pm.PackageManager;
     25 import android.content.pm.ResolveInfo;
     26 
     27 import com.googlecode.android_scripting.Log;
     28 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     29 import com.googlecode.android_scripting.rpc.Rpc;
     30 import com.googlecode.android_scripting.rpc.RpcParameter;
     31 
     32 import java.util.ArrayList;
     33 import java.util.Arrays;
     34 import java.util.HashMap;
     35 import java.util.HashSet;
     36 import java.util.List;
     37 import java.util.Map;
     38 import java.util.Set;
     39 
     40 /**
     41  * Facade for managing Applications.
     42  *
     43  */
     44 public class ApplicationManagerFacade extends RpcReceiver {
     45 
     46   private final Service mService;
     47   private final AndroidFacade mAndroidFacade;
     48   private final ActivityManager mActivityManager;
     49   private final PackageManager mPackageManager;
     50 
     51   public ApplicationManagerFacade(FacadeManager manager) {
     52     super(manager);
     53     mService = manager.getService();
     54     mAndroidFacade = manager.getReceiver(AndroidFacade.class);
     55     mActivityManager = (ActivityManager) mService.getSystemService(Context.ACTIVITY_SERVICE);
     56     mPackageManager = mService.getPackageManager();
     57   }
     58 
     59   @Rpc(description = "Returns a list of all launchable application class names.")
     60   public Map<String, String> getLaunchableApplications() {
     61     Intent intent = new Intent(Intent.ACTION_MAIN);
     62     intent.addCategory(Intent.CATEGORY_LAUNCHER);
     63     List<ResolveInfo> resolveInfos = mPackageManager.queryIntentActivities(intent, 0);
     64     Map<String, String> applications = new HashMap<String, String>();
     65     for (ResolveInfo info : resolveInfos) {
     66       applications.put(info.loadLabel(mPackageManager).toString(), info.activityInfo.name);
     67     }
     68     return applications;
     69   }
     70 
     71   @Rpc(description = "Start activity with the given class name.")
     72   public void launch(@RpcParameter(name = "className") String className) {
     73     Intent intent = new Intent(Intent.ACTION_MAIN);
     74     String packageName = className.substring(0, className.lastIndexOf("."));
     75     intent.setClassName(packageName, className);
     76     mAndroidFacade.startActivity(intent);
     77   }
     78 
     79   @Rpc(description = "Launch the specified app.")
     80   public void appLaunch(@RpcParameter(name = "name") String name) {
     81       Intent LaunchIntent = mPackageManager.getLaunchIntentForPackage(name);
     82       mService.startActivity(LaunchIntent);
     83   }
     84 
     85   @Rpc(description = "Kill the specified app.")
     86   public Boolean appKill(@RpcParameter(name = "name") String name) {
     87       for (RunningAppProcessInfo info : mActivityManager.getRunningAppProcesses()) {
     88           if (info.processName.contains(name)) {
     89               Log.d("Killing " + info.processName);
     90               android.os.Process.killProcess(info.pid);
     91               android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL);
     92               mActivityManager.killBackgroundProcesses(info.processName);
     93               return true;
     94           }
     95       }
     96       return false;
     97   }
     98 
     99   @Rpc(description = "Returns a list of packages running activities or services.", returns = "List of packages running activities.")
    100   public List<String> getRunningPackages() {
    101     Set<String> runningPackages = new HashSet<String>();
    102     List<ActivityManager.RunningAppProcessInfo> appProcesses =
    103         mActivityManager.getRunningAppProcesses();
    104     for (ActivityManager.RunningAppProcessInfo info : appProcesses) {
    105       runningPackages.addAll(Arrays.asList(info.pkgList));
    106     }
    107     List<ActivityManager.RunningServiceInfo> serviceProcesses =
    108         mActivityManager.getRunningServices(Integer.MAX_VALUE);
    109     for (ActivityManager.RunningServiceInfo info : serviceProcesses) {
    110       runningPackages.add(info.service.getPackageName());
    111     }
    112     return new ArrayList<String>(runningPackages);
    113   }
    114 
    115   @Rpc(description = "Force stops a package.")
    116   public void forceStopPackage(
    117       @RpcParameter(name = "packageName", description = "name of package") String packageName) {
    118     mActivityManager.restartPackage(packageName);
    119   }
    120 
    121   @Override
    122   public void shutdown() {
    123   }
    124 }
    125