Home | History | Annotate | Download | only in actions
      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.Client;
     20 import com.android.ddmlib.IDevice;
     21 import com.android.preload.ClientUtils;
     22 import com.android.preload.DumpData;
     23 import com.android.preload.DumpTableModel;
     24 import com.android.preload.Main;
     25 
     26 import java.util.Date;
     27 import java.util.Map;
     28 
     29 public 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 }