ScanPackageAction.java revision 554d7ee0f5d177b6c0bce805f5a5917b6b211978
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
17package com.android.preload.actions;
18
19import com.android.ddmlib.Client;
20import com.android.ddmlib.IDevice;
21import com.android.preload.ClientUtils;
22import com.android.preload.DumpData;
23import com.android.preload.DumpTableModel;
24import com.android.preload.Main;
25
26import java.util.Date;
27import java.util.Map;
28
29public class ScanPackageAction extends AbstractThreadedDeviceSpecificAction {
30
31    private ClientUtils clientUtils;
32    private DumpTableModel dataTableModel;
33
34    public ScanPackageAction(ClientUtils utils, IDevice device, DumpTableModel dataTableModel) {
35        super("Scan package", device);
36        this.clientUtils = utils;
37        this.dataTableModel = dataTableModel;
38    }
39
40    @Override
41    public void run() {
42        Main.getUI().showWaitDialog();
43
44        try {
45            Client client = Main.getUI().getSelectedClient();
46            if (client != null) {
47                work(client);
48            } else {
49                Client[] clients = clientUtils.findAllClients(device);
50                if (clients.length > 0) {
51                    ClientWrapper[] clientWrappers = new ClientWrapper[clients.length];
52                    for (int i = 0; i < clientWrappers.length; i++) {
53                        clientWrappers[i] = new ClientWrapper(clients[i]);
54                    }
55                    Main.getUI().hideWaitDialog();
56
57                    ClientWrapper ret = Main.getUI().showChoiceDialog("Choose a package to scan",
58                            "Choose package",
59                            clientWrappers);
60                    if (ret != null) {
61                        work(ret.client);
62                    }
63                }
64            }
65        } finally {
66            Main.getUI().hideWaitDialog();
67        }
68    }
69
70    private void work(Client c) {
71        String pkg = c.getClientData().getClientDescription();
72        Main.getUI().showWaitDialog();
73        Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg);
74
75        try {
76            Map<String, String> data = Main.findAndGetClassData(device, pkg);
77            DumpData dumpData = new DumpData(pkg, data, new Date());
78            dataTableModel.addData(dumpData);
79        } catch (Exception e) {
80            e.printStackTrace();
81        }
82    }
83
84    private static class ClientWrapper {
85        private Client client;
86
87        public ClientWrapper(Client c) {
88            client = c;
89        }
90
91        @Override
92        public String toString() {
93            return client.getClientData().getClientDescription() + " (pid "
94                    + client.getClientData().getPid() + ")";
95        }
96    }
97}