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 ApplicationManagerFacade(FacadeManager manager)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.") getLaunchableApplications()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.") launch(@pcParametername = "className") String className)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 = "Start activity with the given class name with result") launchForResult(@pcParametername = "className") String className)80 public Intent launchForResult(@RpcParameter(name = "className") String className) { 81 Intent intent = new Intent(Intent.ACTION_MAIN); 82 String packageName = className.substring(0, className.lastIndexOf(".")); 83 intent.setClassName(packageName, className); 84 return mAndroidFacade.startActivityForResult(intent); 85 } 86 87 @Rpc(description = "Launch the specified app.") appLaunch(@pcParametername = "name") String name)88 public void appLaunch(@RpcParameter(name = "name") String name) { 89 Intent LaunchIntent = mPackageManager.getLaunchIntentForPackage(name); 90 mService.startActivity(LaunchIntent); 91 } 92 93 @Rpc(description = "Kill the specified app.") appKill(@pcParametername = "name") String name)94 public Boolean appKill(@RpcParameter(name = "name") String name) { 95 for (RunningAppProcessInfo info : mActivityManager.getRunningAppProcesses()) { 96 if (info.processName.contains(name)) { 97 Log.d("Killing " + info.processName); 98 android.os.Process.killProcess(info.pid); 99 android.os.Process.sendSignal(info.pid, android.os.Process.SIGNAL_KILL); 100 mActivityManager.killBackgroundProcesses(info.processName); 101 return true; 102 } 103 } 104 return false; 105 } 106 107 @Rpc(description = "Returns a list of packages running activities or services.", 108 returns = "List of packages running activities.") getRunningPackages()109 public List<String> getRunningPackages() { 110 Set<String> runningPackages = new HashSet<String>(); 111 List<ActivityManager.RunningAppProcessInfo> appProcesses = 112 mActivityManager.getRunningAppProcesses(); 113 for (ActivityManager.RunningAppProcessInfo info : appProcesses) { 114 runningPackages.addAll(Arrays.asList(info.pkgList)); 115 } 116 List<ActivityManager.RunningServiceInfo> serviceProcesses = 117 mActivityManager.getRunningServices(Integer.MAX_VALUE); 118 for (ActivityManager.RunningServiceInfo info : serviceProcesses) { 119 runningPackages.add(info.service.getPackageName()); 120 } 121 return new ArrayList<String>(runningPackages); 122 } 123 124 /** 125 * Force stops a package. Equivalent to calling `am force-stop "package.name"` as root. 126 * <p> 127 * If you have access to adb, it is preferred to use the above command instead. 128 * 129 * @param packageName the name of the package to force stop 130 */ 131 @Rpc(description = "Force stops a package. Equivalent to `adb shell am force-stop " 132 + "\"package.name\"`. If possible, use that command instead.") forceStopPackage( @pcParametername = "packageName", description = "name of package") String packageName)133 public void forceStopPackage( 134 @RpcParameter(name = "packageName", description = "name of package") String packageName) { 135 mActivityManager.forceStopPackage(packageName); 136 } 137 138 @Override shutdown()139 public void shutdown() { 140 } 141 } 142