private String getDocIdForFile(File file) throws FileNotFoundException {
    String path = file.getAbsolutePath();

    // Find the most-specific root path
    String mostSpecificId = null;
    String mostSpecificPath = null;
    synchronized (mRootsLock) {
      for (int i = 0; i < mRoots.size(); i++) {
        final String rootId = mRoots.keyAt(i);
        final String rootPath = mRoots.valueAt(i).path.getAbsolutePath();
        if (path.startsWith(rootPath)
            && (mostSpecificPath == null || rootPath.length() > mostSpecificPath.length())) {
          mostSpecificId = rootId;
          mostSpecificPath = rootPath;
        }
      }
    }

    if (mostSpecificPath == null) {
      throw new FileNotFoundException("Failed to find root that contains " + path);
    }

    // Start at first char of path under root
    final String rootPath = mostSpecificPath;
    if (rootPath.equals(path)) {
      path = "";
    } else if (rootPath.endsWith("/")) {
      path = path.substring(rootPath.length());
    } else {
      path = path.substring(rootPath.length() + 1);
    }

    return mostSpecificId + ':' + path;
  }
 public UsbPort[] getPorts() {
   synchronized (mLock) {
     final int count = mPorts.size();
     final UsbPort[] result = new UsbPort[count];
     for (int i = 0; i < count; i++) {
       result[i] = mPorts.valueAt(i).mUsbPort;
     }
     return result;
   }
 }
Exemplo n.º 3
0
 /**
  * Perform a {@link #put(Object, Object)} of all key/value pairs in <var>array</var>
  *
  * @param array The array whose contents are to be retrieved.
  */
 public void putAll(ArrayMap<? extends K, ? extends V> array) {
   final int N = array.mSize;
   ensureCapacity(mSize + N);
   if (mSize == 0) {
     if (N > 0) {
       System.arraycopy(array.mHashes, 0, mHashes, 0, N);
       System.arraycopy(array.mArray, 0, mArray, 0, N << 1);
       mSize = N;
     }
   } else {
     for (int i = 0; i < N; i++) {
       put(array.keyAt(i), array.valueAt(i));
     }
   }
 }
 @Override
 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
   final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ", 160);
   synchronized (mRootsLock) {
     for (int i = 0; i < mRoots.size(); i++) {
       final RootInfo root = mRoots.valueAt(i);
       pw.println("Root{" + root.rootId + "}:");
       pw.increaseIndent();
       pw.printPair("flags", DebugUtils.flagsToString(Root.class, "FLAG_", root.flags));
       pw.println();
       pw.printPair("title", root.title);
       pw.printPair("docId", root.docId);
       pw.println();
       pw.printPair("path", root.path);
       pw.printPair("visiblePath", root.visiblePath);
       pw.decreaseIndent();
       pw.println();
     }
   }
 }
  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;
      }
    }
  }
 boolean dumpMap(
     PrintWriter out,
     String titlePrefix,
     String title,
     String prefix,
     ArrayMap<String, F[]> map,
     String packageName,
     boolean printFilter,
     boolean collapseDuplicates) {
   final String eprefix = prefix + "  ";
   final String fprefix = prefix + "    ";
   final ArrayMap<Object, MutableInt> found = new ArrayMap<>();
   boolean printedSomething = false;
   Printer printer = null;
   for (int mapi = 0; mapi < map.size(); mapi++) {
     F[] a = map.valueAt(mapi);
     final int N = a.length;
     boolean printedHeader = false;
     F filter;
     if (collapseDuplicates && !printFilter) {
       found.clear();
       for (int i = 0; i < N && (filter = a[i]) != null; i++) {
         if (packageName != null && !isPackageForFilter(packageName, filter)) {
           continue;
         }
         Object label = filterToLabel(filter);
         int index = found.indexOfKey(label);
         if (index < 0) {
           found.put(label, new MutableInt(1));
         } else {
           found.valueAt(index).value++;
         }
       }
       for (int i = 0; i < found.size(); i++) {
         if (title != null) {
           out.print(titlePrefix);
           out.println(title);
           title = null;
         }
         if (!printedHeader) {
           out.print(eprefix);
           out.print(map.keyAt(mapi));
           out.println(":");
           printedHeader = true;
         }
         printedSomething = true;
         dumpFilterLabel(out, fprefix, found.keyAt(i), found.valueAt(i).value);
       }
     } else {
       for (int i = 0; i < N && (filter = a[i]) != null; i++) {
         if (packageName != null && !isPackageForFilter(packageName, filter)) {
           continue;
         }
         if (title != null) {
           out.print(titlePrefix);
           out.println(title);
           title = null;
         }
         if (!printedHeader) {
           out.print(eprefix);
           out.print(map.keyAt(mapi));
           out.println(":");
           printedHeader = true;
         }
         printedSomething = true;
         dumpFilter(out, fprefix, filter);
         if (printFilter) {
           if (printer == null) {
             printer = new PrintWriterPrinter(out);
           }
           filter.dump(printer, fprefix + "  ");
         }
       }
     }
   }
   return printedSomething;
 }
Exemplo n.º 7
0
  private void handleReadPrintJobsLocked() {
    // Make a map with the files for a print job since we may have
    // to delete some. One example of getting orphan files if the
    // spooler crashes while constructing a print job. We do not
    // persist partially populated print jobs under construction to
    // avoid special handling for various attributes missing.
    ArrayMap<PrintJobId, File> fileForJobMap = null;
    File[] files = getFilesDir().listFiles();
    if (files != null) {
      final int fileCount = files.length;
      for (int i = 0; i < fileCount; i++) {
        File file = files[i];
        if (file.isFile() && file.getName().startsWith(PRINT_JOB_FILE_PREFIX)) {
          if (fileForJobMap == null) {
            fileForJobMap = new ArrayMap<PrintJobId, File>();
          }
          String printJobIdString =
              file.getName().substring(PRINT_JOB_FILE_PREFIX.length(), file.getName().indexOf('.'));
          PrintJobId printJobId = PrintJobId.unflattenFromString(printJobIdString);
          fileForJobMap.put(printJobId, file);
        }
      }
    }

    final int printJobCount = mPrintJobs.size();
    for (int i = 0; i < printJobCount; i++) {
      PrintJobInfo printJob = mPrintJobs.get(i);

      // We want to have only the orphan files at the end.
      if (fileForJobMap != null) {
        fileForJobMap.remove(printJob.getId());
      }

      switch (printJob.getState()) {
        case PrintJobInfo.STATE_QUEUED:
        case PrintJobInfo.STATE_STARTED:
        case PrintJobInfo.STATE_BLOCKED:
          {
            // We have a print job that was queued or started or blocked in
            // the past but the device battery died or a crash occurred. In
            // this case we assume the print job failed and let the user
            // decide whether to restart the job or just cancel it.
            setPrintJobState(
                printJob.getId(),
                PrintJobInfo.STATE_FAILED,
                getString(R.string.no_connection_to_printer));
          }
          break;
      }
    }

    if (!mPrintJobs.isEmpty()) {
      // Update the notification.
      mNotificationController.onUpdateNotifications(mPrintJobs);
    }

    // Delete the orphan files.
    if (fileForJobMap != null) {
      final int orphanFileCount = fileForJobMap.size();
      for (int i = 0; i < orphanFileCount; i++) {
        File file = fileForJobMap.valueAt(i);
        file.delete();
      }
    }
  }