public void dump(IndentingPrintWriter pw) {
    synchronized (mLock) {
      pw.print("USB Port State:");
      if (!mSimulatedPorts.isEmpty()) {
        pw.print(" (simulation active; end with 'dumpsys usb reset')");
      }
      pw.println();

      if (mPorts.isEmpty()) {
        pw.println("  <no ports>");
      } else {
        for (PortInfo portInfo : mPorts.values()) {
          pw.println("  " + portInfo.mUsbPort.getId() + ": " + portInfo);
        }
      }
    }
  }
 public void resetSimulation(IndentingPrintWriter pw) {
   synchronized (mLock) {
     pw.println("Removing all simulated ports and ending simulation.");
     if (!mSimulatedPorts.isEmpty()) {
       mSimulatedPorts.clear();
       updatePortsLocked(pw);
     }
   }
 }
  private void updatePortsLocked(IndentingPrintWriter pw) {
    // Assume all ports are gone unless informed otherwise.
    // Kind of pessimistic but simple.
    for (int i = mPorts.size(); i-- > 0; ) {
      mPorts.valueAt(i).mDisposition = PortInfo.DISPOSITION_REMOVED;
    }

    // Enumerate all extant ports.
    if (!mSimulatedPorts.isEmpty()) {
      final int count = mSimulatedPorts.size();
      for (int i = 0; i < count; i++) {
        final SimulatedPortInfo portInfo = mSimulatedPorts.valueAt(i);
        addOrUpdatePortLocked(
            portInfo.mPortId,
            portInfo.mSupportedModes,
            portInfo.mCurrentMode,
            portInfo.mCanChangeMode,
            portInfo.mCurrentPowerRole,
            portInfo.mCanChangePowerRole,
            portInfo.mCurrentDataRole,
            portInfo.mCanChangeDataRole,
            pw);
      }
    } else if (mHaveKernelSupport) {
      final File[] portDirs = new File(SYSFS_CLASS).listFiles();
      if (portDirs != null) {
        for (File portDir : portDirs) {
          if (!portDir.isDirectory()) {
            continue;
          }

          // Parse the sysfs file contents.
          final String portId = portDir.getName();
          final int supportedModes = readSupportedModes(portDir);
          final int currentMode = readCurrentMode(portDir);
          final boolean canChangeMode = canChangeMode(portDir);
          final int currentPowerRole = readCurrentPowerRole(portDir);
          final boolean canChangePowerRole = canChangePowerRole(portDir);
          final int currentDataRole = readCurrentDataRole(portDir);
          final boolean canChangeDataRole = canChangeDataRole(portDir);
          addOrUpdatePortLocked(
              portId,
              supportedModes,
              currentMode,
              canChangeMode,
              currentPowerRole,
              canChangePowerRole,
              currentDataRole,
              canChangeDataRole,
              pw);
        }
      }
    }

    // Process the updates.
    // Once finished, the list of ports will only contain ports in DISPOSITION_READY.
    for (int i = mPorts.size(); i-- > 0; ) {
      final PortInfo portInfo = mPorts.valueAt(i);
      switch (portInfo.mDisposition) {
        case PortInfo.DISPOSITION_ADDED:
          handlePortAddedLocked(portInfo, pw);
          portInfo.mDisposition = PortInfo.DISPOSITION_READY;
          break;
        case PortInfo.DISPOSITION_CHANGED:
          handlePortChangedLocked(portInfo, pw);
          portInfo.mDisposition = PortInfo.DISPOSITION_READY;
          break;
        case PortInfo.DISPOSITION_REMOVED:
          mPorts.removeAt(i);
          portInfo.mUsbPortStatus = null; // must do this early
          handlePortRemovedLocked(portInfo, pw);
          break;
      }
    }
  }