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 ScanAllPackagesAction extends AbstractThreadedDeviceSpecificAction {
30
31    private ClientUtils clientUtils;
32    private DumpTableModel dataTableModel;
33
34    public ScanAllPackagesAction(ClientUtils utils, IDevice device, DumpTableModel dataTableModel) {
35        super("Scan all packages", 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[] clients = clientUtils.findAllClients(device);
46            for (Client c : clients) {
47                String pkg = c.getClientData().getClientDescription();
48                Main.getUI().showWaitDialog();
49                Main.getUI().updateWaitDialog("Retrieving heap data for " + pkg);
50
51                try {
52                    Map<String, String> data = Main.getClassDataRetriever().getClassData(c);
53                    DumpData dumpData = new DumpData(pkg, data, new Date());
54                    dataTableModel.addData(dumpData);
55                } catch (Exception e) {
56                    e.printStackTrace();
57                }
58            }
59        } finally {
60            Main.getUI().hideWaitDialog();
61        }
62    }
63}