1554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe/*
2554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
3554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
4554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
5554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * you may not use this file except in compliance with the License.
6554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * You may obtain a copy of the License at
7554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
8554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
9554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
10554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Unless required by applicable law or agreed to in writing, software
11554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
12554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * See the License for the specific language governing permissions and
14554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * limitations under the License.
15554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe */
16554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
17554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepackage com.android.preload.actions;
18554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
19554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.ddmlib.Client;
20554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.ddmlib.IDevice;
21554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.ClientUtils;
22554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpData;
23554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpTableModel;
24554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.Main;
25554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
26554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Date;
27554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Map;
28554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
29554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepublic class ScanPackageAction extends AbstractThreadedDeviceSpecificAction {
30554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
31554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private ClientUtils clientUtils;
32554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private DumpTableModel dataTableModel;
33554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
34554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public ScanPackageAction(ClientUtils utils, IDevice device, DumpTableModel dataTableModel) {
35554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        super("Scan package", device);
36554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.clientUtils = utils;
37554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.dataTableModel = dataTableModel;
38554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
39554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
40554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    @Override
41554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public void run() {
42554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Main.getUI().showWaitDialog();
43554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
44554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        try {
45554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            Client client = Main.getUI().getSelectedClient();
46554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            if (client != null) {
47554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                work(client);
48554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            } else {
49554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                Client[] clients = clientUtils.findAllClients(device);
50554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                if (clients.length > 0) {
51554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    ClientWrapper[] clientWrappers = new ClientWrapper[clients.length];
52554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    for (int i = 0; i < clientWrappers.length; i++) {
53554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                        clientWrappers[i] = new ClientWrapper(clients[i]);
54554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    }
55554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    Main.getUI().hideWaitDialog();
56554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
57554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    ClientWrapper ret = Main.getUI().showChoiceDialog("Choose a package to scan",
58554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                            "Choose package",
59554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                            clientWrappers);
60554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    if (ret != null) {
61554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                        work(ret.client);
62554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    }
63554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                }
64554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            }
65554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        } finally {
66554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            Main.getUI().hideWaitDialog();
67554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
68554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
69554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
70554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private void work(Client c) {
71554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        String pkg = c.getClientData().getClientDescription();
72554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Main.getUI().showWaitDialog();
73554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg);
74554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
75554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        try {
76554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            Map<String, String> data = Main.findAndGetClassData(device, pkg);
77554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            DumpData dumpData = new DumpData(pkg, data, new Date());
78554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            dataTableModel.addData(dumpData);
79554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        } catch (Exception e) {
80554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            e.printStackTrace();
81554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
82554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
83554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
84554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private static class ClientWrapper {
85554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        private Client client;
86554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
87554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        public ClientWrapper(Client c) {
88554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            client = c;
89554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
90554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
91554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        @Override
92554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        public String toString() {
93554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            return client.getClientData().getClientDescription() + " (pid "
94554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    + client.getClientData().getPid() + ")";
95554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
96554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
97554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe}