1 /* 2 * Copyright (C) 2015 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.preload.actions; 18 19 import com.android.ddmlib.IDevice; 20 import com.android.preload.DeviceUtils; 21 import com.android.preload.DumpData; 22 import com.android.preload.DumpTableModel; 23 import com.android.preload.Main; 24 25 import java.awt.event.ActionEvent; 26 import java.util.Date; 27 import java.util.Map; 28 import java.util.concurrent.TimeUnit; 29 30 import javax.swing.AbstractAction; 31 32 public class RunMonkeyAction extends AbstractAction implements DeviceSpecific { 33 34 private final static String DEFAULT_MONKEY_PACKAGES = 35 "com.android.calendar,com.android.gallery3d"; 36 37 private IDevice device; 38 private DumpTableModel dataTableModel; 39 RunMonkeyAction(IDevice device, DumpTableModel dataTableModel)40 public RunMonkeyAction(IDevice device, DumpTableModel dataTableModel) { 41 super("Run monkey"); 42 this.device = device; 43 this.dataTableModel = dataTableModel; 44 } 45 46 @Override setDevice(IDevice device)47 public void setDevice(IDevice device) { 48 this.device = device; 49 } 50 51 @Override actionPerformed(ActionEvent e)52 public void actionPerformed(ActionEvent e) { 53 String packages = Main.getUI().showInputDialog("Please enter packages name to run with" 54 + " the monkey, or leave empty for default."); 55 if (packages == null) { 56 return; 57 } 58 if (packages.isEmpty()) { 59 packages = DEFAULT_MONKEY_PACKAGES; 60 } 61 Runnable r = new RunMonkeyRunnable(packages); 62 if (Main.getUI().isSingleThreaded()) { 63 r.run(); 64 } else { 65 new Thread(r).start(); 66 } 67 } 68 69 private class RunMonkeyRunnable implements Runnable { 70 71 private String packages; 72 private final static int ITERATIONS = 1000; 73 RunMonkeyRunnable(String packages)74 public RunMonkeyRunnable(String packages) { 75 this.packages = packages; 76 } 77 78 @Override run()79 public void run() { 80 Main.getUI().showWaitDialog(); 81 82 try { 83 String pkgs[] = packages.split(","); 84 85 for (String pkg : pkgs) { 86 Main.getUI().updateWaitDialog("Running monkey on " + pkg); 87 88 try { 89 // Stop running app. 90 forceStop(pkg); 91 92 // Little bit of breather here. 93 try { 94 Thread.sleep(1000); 95 } catch (Exception e) { 96 } 97 98 DeviceUtils.doShell(device, "monkey -p " + pkg + " " + ITERATIONS, 1, 99 TimeUnit.MINUTES); 100 101 Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg); 102 Map<String, String> data = Main.findAndGetClassData(device, pkg); 103 DumpData dumpData = new DumpData(pkg, data, new Date()); 104 dataTableModel.addData(dumpData); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } finally { 108 // Stop running app. 109 forceStop(pkg); 110 } 111 } 112 } finally { 113 Main.getUI().hideWaitDialog(); 114 } 115 } 116 forceStop(String packageName)117 private void forceStop(String packageName) { 118 // Stop running app. 119 DeviceUtils.doShell(device, "force-stop " + packageName, 5, TimeUnit.SECONDS); 120 DeviceUtils.doShell(device, "kill " + packageName, 5, TimeUnit.SECONDS); 121 DeviceUtils.doShell(device, "kill `pid " + packageName + "`", 5, TimeUnit.SECONDS); 122 } 123 } 124 }