• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         new Thread(new RunMonkeyRunnable(packages)).start();
62     }
63 
64     private class RunMonkeyRunnable implements Runnable {
65 
66         private String packages;
67         private final static int ITERATIONS = 1000;
68 
RunMonkeyRunnable(String packages)69         public RunMonkeyRunnable(String packages) {
70             this.packages = packages;
71         }
72 
73         @Override
run()74         public void run() {
75             Main.getUI().showWaitDialog();
76 
77             try {
78                 String pkgs[] = packages.split(",");
79 
80                 for (String pkg : pkgs) {
81                     Main.getUI().updateWaitDialog("Running monkey on " + pkg);
82 
83                     try {
84                         // Stop running app.
85                         forceStop(pkg);
86 
87                         // Little bit of breather here.
88                         try {
89                             Thread.sleep(1000);
90                         } catch (Exception e) {
91                         }
92 
93                         DeviceUtils.doShell(device, "monkey -p " + pkg + " " + ITERATIONS, 1,
94                                 TimeUnit.MINUTES);
95 
96                         Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg);
97                         Map<String, String> data = Main.findAndGetClassData(device, pkg);
98                         DumpData dumpData = new DumpData(pkg, data, new Date());
99                         dataTableModel.addData(dumpData);
100                     } catch (Exception e) {
101                         e.printStackTrace();
102                     } finally {
103                         // Stop running app.
104                         forceStop(pkg);
105                     }
106                 }
107             } finally {
108                 Main.getUI().hideWaitDialog();
109             }
110         }
111 
forceStop(String packageName)112         private void forceStop(String packageName) {
113             // Stop running app.
114             DeviceUtils.doShell(device, "force-stop " + packageName, 5, TimeUnit.SECONDS);
115             DeviceUtils.doShell(device, "kill " + packageName, 5, TimeUnit.SECONDS);
116             DeviceUtils.doShell(device, "kill `pid " + packageName + "`", 5, TimeUnit.SECONDS);
117         }
118     }
119 }