Home | History | Annotate | Download | only in logcat
      1 /*
      2  * Copyright (C) 2011 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 package com.android.ddmuilib.logcat;
     17 
     18 import com.android.ddmlib.AndroidDebugBridge;
     19 import com.android.ddmlib.AndroidDebugBridge.IClientChangeListener;
     20 import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
     21 import com.android.ddmlib.Client;
     22 import com.android.ddmlib.ClientData;
     23 import com.android.ddmlib.IDevice;
     24 
     25 import java.util.HashMap;
     26 import java.util.Map;
     27 
     28 /**
     29  * This class maintains a mapping between the PID and the application name for all
     30  * running apps on a device. It does this by implementing callbacks to two events:
     31  * {@link AndroidDebugBridge.IDeviceChangeListener} and
     32  * {@link AndroidDebugBridge.IClientChangeListener}.
     33  */
     34 public class LogCatPidToNameMapper {
     35     /** Default name used when the actual name cannot be determined. */
     36     public static final String UNKNOWN_APP = "";
     37 
     38     private IClientChangeListener mClientChangeListener;
     39     private IDeviceChangeListener mDeviceChangeListener;
     40     private IDevice mDevice;
     41     private Map<String, String> mPidToName;
     42 
     43     public LogCatPidToNameMapper(IDevice device) {
     44         mDevice = device;
     45         mClientChangeListener = constructClientChangeListener();
     46         AndroidDebugBridge.addClientChangeListener(mClientChangeListener);
     47 
     48         mDeviceChangeListener = constructDeviceChangeListener();
     49         AndroidDebugBridge.addDeviceChangeListener(mDeviceChangeListener);
     50 
     51         mPidToName = new HashMap<String, String>();
     52 
     53         updateClientList(device);
     54     }
     55 
     56     private IClientChangeListener constructClientChangeListener() {
     57         return new IClientChangeListener() {
     58             @Override
     59             public void clientChanged(Client client, int changeMask) {
     60                 if ((changeMask & Client.CHANGE_NAME) == Client.CHANGE_NAME) {
     61                     ClientData cd = client.getClientData();
     62                     updateClientName(cd);
     63                 }
     64             }
     65         };
     66     }
     67 
     68     private void updateClientName(ClientData cd) {
     69         String name = cd.getClientDescription();
     70         if (name != null) {
     71             int pid = cd.getPid();
     72             if (mPidToName != null) {
     73                 mPidToName.put(Integer.toString(pid), name);
     74             }
     75         }
     76     }
     77 
     78     private IDeviceChangeListener constructDeviceChangeListener() {
     79         return new IDeviceChangeListener() {
     80             @Override
     81             public void deviceDisconnected(IDevice device) {
     82             }
     83 
     84             @Override
     85             public void deviceConnected(IDevice device) {
     86             }
     87 
     88             @Override
     89             public void deviceChanged(IDevice device, int changeMask) {
     90                 if (changeMask == IDevice.CHANGE_CLIENT_LIST) {
     91                     updateClientList(device);
     92                 }
     93             }
     94         };
     95     }
     96 
     97     private void updateClientList(IDevice device) {
     98         if (mDevice == null) {
     99             return;
    100         }
    101 
    102         if (!mDevice.equals(device)) {
    103             return;
    104         }
    105 
    106         mPidToName = new HashMap<String, String>();
    107         for (Client c : device.getClients()) {
    108             ClientData cd = c.getClientData();
    109             String name = cd.getClientDescription();
    110             int pid = cd.getPid();
    111 
    112             /* The name will be null for apps that have just been created.
    113              * In such a case, we fill in the default name, and wait for the
    114              * clientChangeListener to do the update with the correct name.
    115              */
    116             if (name == null) {
    117                 name = UNKNOWN_APP;
    118             }
    119 
    120             mPidToName.put(Integer.toString(pid), name);
    121         }
    122     }
    123 
    124     /**
    125      * Get the application name corresponding to given pid.
    126      * @param pid application's pid
    127      * @return application name if available, else {@link LogCatPidToNameMapper#UNKNOWN_APP}.
    128      */
    129     public String getName(String pid) {
    130         String name = mPidToName.get(pid);
    131         return name != null ? name : UNKNOWN_APP;
    132     }
    133 }
    134