/* returns true if USB is in accessory mode */
 private boolean initFunctions(File dir, boolean useEnableFiles, char[] buffer) {
   boolean inAccessoryMode = false;
   try {
     File[] files = dir.listFiles();
     for (int i = 0; i < files.length; i++) {
       File file = useEnableFiles ? new File(files[i], "enable") : files[i];
       FileReader reader = new FileReader(file);
       int len = reader.read(buffer, 0, 1024);
       reader.close();
       int value = Integer.valueOf((new String(buffer, 0, len)).trim());
       String functionName = files[i].getName();
       if (value == 1) {
         mEnabledFunctions.add(functionName);
         if (UsbManager.USB_FUNCTION_ACCESSORY.equals(functionName)) {
           // The USB accessory driver is on by default, but it might have been
           // enabled before the USB service has initialized.
           inAccessoryMode = true;
         } else if (!UsbManager.USB_FUNCTION_ADB.equals(functionName)) {
           // adb is enabled/disabled automatically by the adbd daemon,
           // so don't treat it as a default function.
           mDefaultFunctions.add(functionName);
         }
       } else {
         mDisabledFunctions.add(functionName);
       }
     }
   } catch (Exception e) {
     Slog.e(TAG, "", e);
   }
   return inAccessoryMode;
 }
  private final void init() {
    char[] buffer = new char[1024];
    boolean inAccessoryMode = false;

    // Read initial USB state (device mode)
    mConfiguration = -1;
    try {
      FileReader file = new FileReader(USB_CONNECTED_PATH);
      int len = file.read(buffer, 0, 1024);
      file.close();
      mConnected = Integer.valueOf((new String(buffer, 0, len)).trim());

      file = new FileReader(USB_CONFIGURATION_PATH);
      len = file.read(buffer, 0, 1024);
      file.close();
      mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim());

    } catch (FileNotFoundException e) {
      Slog.i(TAG, "This kernel does not have USB configuration switch support");
      Slog.i(TAG, "Trying legacy USB configuration switch support");
      try {
        FileReader file = new FileReader(USB_LEGACY_PATH);
        int len = file.read(buffer, 0, 1024);
        file.close();
        mConnected = ("online".equals((new String(buffer, 0, len))) ? 1 : 0);
        mLegacy = true;
        mConfiguration = 0;
      } catch (FileNotFoundException f) {
        Slog.i(TAG, "This kernel does not have legacy USB configuration switch support");
      } catch (Exception f) {
        Slog.e(TAG, "", f);
      }
    } catch (Exception e) {
      Slog.e(TAG, "", e);
    }
    if (mConfiguration < 0) {
      // This may happen in the emulator or devices without USB device mode support
      return;
    }

    if (mLegacy) {
      mDisabledFunctions.add(UsbManager.USB_FUNCTION_MASS_STORAGE);
    } else {
      // Read initial list of enabled and disabled functions (device mode)
      try {
        File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
        for (int i = 0; i < files.length; i++) {
          File file = new File(files[i], "enable");
          FileReader reader = new FileReader(file);
          int len = reader.read(buffer, 0, 1024);
          reader.close();
          int value = Integer.valueOf((new String(buffer, 0, len)).trim());
          String functionName = files[i].getName();
          if (value == 1) {
            mEnabledFunctions.add(functionName);
            if (UsbManager.USB_FUNCTION_ACCESSORY.equals(functionName)) {
              // The USB accessory driver is on by default, but it might have been
              // enabled before the USB service has initialized.
              inAccessoryMode = true;
            } else if (!UsbManager.USB_FUNCTION_ADB.equals(functionName)) {
              // adb is enabled/disabled automatically by the adbd daemon,
              // so don't treat it as a default function.
              mDefaultFunctions.add(functionName);
            }
          } else {
            mDisabledFunctions.add(functionName);
          }
        }
      } catch (FileNotFoundException e) {
        Slog.w(TAG, "This kernel does not have USB composite class support");
      } catch (Exception e) {
        Slog.e(TAG, "", e);
      }
    }

    // handle the case where an accessory switched the driver to accessory mode
    // before the framework finished booting
    if (inAccessoryMode && !mLegacy) {
      readCurrentAccessoryLocked();

      // FIXME - if we booted in accessory mode, then we have no way to figure out
      // which functions are enabled by default.
      // For now, assume that MTP or mass storage are the only possibilities
      if (mDisabledFunctions.contains(UsbManager.USB_FUNCTION_MTP)) {
        mDefaultFunctions.add(UsbManager.USB_FUNCTION_MTP);
      } else if (mDisabledFunctions.contains(UsbManager.USB_FUNCTION_MASS_STORAGE)) {
        mDefaultFunctions.add(UsbManager.USB_FUNCTION_MASS_STORAGE);
      }
    }
  }