private void init() {
    Log.v(TAG, "Scanning directory " + currentDirectory);

    if (cancelled) {
      Log.v(TAG, "Scan aborted");
      return;
    }

    totalCount = 0;
    progress = 0;
    files = currentDirectory.listFiles();
    noMedia = false;
    displayHidden = PreferenceActivity.getDisplayHiddenFiles(context);
    sdIcon = context.getResources().getDrawable(R.drawable.ic_launcher_sdcard);
    folderIcon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
    genericFileIcon = context.getResources().getDrawable(R.drawable.ic_launcher_file);

    operationStartTime = SystemClock.uptimeMillis();

    if (files == null) {
      Log.v(TAG, "Returned null - inaccessible directory?");
    } else {
      totalCount = files.length;
    }
    Log.v(TAG, "Total count=" + totalCount + ")");

    /** Directory container */
    listDir = new ArrayList<>(totalCount);
    /** File container */
    listFile = new ArrayList<>(totalCount);
    /** External storage container */
    listSdCard = new ArrayList<>(3);
  }
  public void run() {
    running = true;
    init();

    // Scan files
    if (files != null) {
      for (File currentFile : files) {
        if (cancelled) {
          Log.v(TAG, "Scan aborted while checking files");
          return;
        }

        progress++;
        updateProgress(progress, totalCount);

        // It's the noMedia file. Raise the flag.
        if (currentFile.getName().equalsIgnoreCase(FileUtils.NOMEDIA_FILE_NAME)) noMedia = true;

        // If the user doesn't want to display hidden files and the file is hidden, ignore this
        // file.
        if (!displayHidden && currentFile.isHidden()) {
          continue;
        }

        // It's a directory. Handle it.
        if (currentFile.isDirectory()) {
          // It's the sd card.
          if (currentFile.getAbsolutePath().equals(mSdCardPath)) {
            listSdCard.add(
                new FileHolder(
                    currentFile, mMimeTypes.getMimeType(currentFile.getName()), sdIcon, context));
          }
          // It's a normal directory.
          else {
            if (!mWriteableOnly || currentFile.canWrite())
              listDir.add(
                  new FileHolder(
                      currentFile,
                      mMimeTypes.getMimeType(currentFile.getName()),
                      folderIcon,
                      context));
          }
          // It's a file. Handle it too :P
        } else {
          String fileName = currentFile.getName();

          // Get the file's mimetype.
          String mimetype = mMimeTypes.getMimeType(fileName);
          String filetype = FileUtils.getExtension(fileName);

          boolean ext_allow = filetype.equalsIgnoreCase(mFilterFiletype) || mFilterFiletype == "";
          boolean mime_allow =
              mFilterMimetype != null
                  && (mimetype.contentEquals(mFilterMimetype)
                      || mFilterMimetype.contentEquals("*/*")
                      || mFilterFiletype == null);
          if (!mDirectoriesOnly && (ext_allow || mime_allow)) {
            // Take advantage of the already parsed mimeType to set a specific icon.
            listFile.add(new FileHolder(currentFile, mimetype, genericFileIcon, context));
          }
        }
      }
    }

    Log.v(TAG, "Sorting results...");
    int sortBy = PreferenceActivity.getSortBy(context);
    boolean ascending = PreferenceActivity.getAscending(context);

    // Sort lists
    if (!cancelled) {
      Collections.sort(listSdCard);
      Collections.sort(listDir, Comparators.getForDirectory(sortBy, ascending));
      Collections.sort(listFile, Comparators.getForFile(sortBy, ascending));
    }

    // Return lists
    if (!cancelled) {
      Log.v(TAG, "Sending data back to main thread");

      DirectoryContents contents = new DirectoryContents();

      contents.listDir = listDir;
      contents.listFile = listFile;
      contents.listSdCard = listSdCard;
      contents.noMedia = noMedia;

      Message msg = handler.obtainMessage(MESSAGE_SHOW_DIRECTORY_CONTENTS);
      msg.obj = contents;
      msg.sendToTarget();
    }

    running = false;
  }