@Override
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public void onShowAdvanced() {
    if (selectDownloadFolderFragment != null) selectDownloadFolderFragment.dismiss();

    Intent selectFolderIntent;
    // Create intent depending on system version
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      // Use the system UI for folder selection on Android 5.0 and later
      selectFolderIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    else {
      // Use old custom UI on older Android versions
      selectFolderIntent = new Intent(this, SelectFileActivity.class);
      selectFolderIntent.putExtra(
          SelectFileActivity.SELECTION_MODE_KEY, SelectFileActivity.SelectionMode.FOLDER);
      selectFolderIntent.putExtra(
          SelectFileActivity.INITIAL_PATH_KEY,
          getPreferences(MODE_PRIVATE)
              .getString(
                  SettingsActivity.KEY_DOWNLOAD_FOLDER,
                  EpisodeDownloadManager.getDefaultDownloadFolder().getAbsolutePath()));
    }

    // Start activity. Result will be caught below and pushed down to the SettingsActivity.
    startActivityForResult(selectFolderIntent, DownloadFolderPreference.REQUEST_CODE);
  }
  private void cleanMetadata(Map<String, EpisodeMetadata> result) {
    // Find download folder
    File podcastDir =
        new File(
            PreferenceManager.getDefaultSharedPreferences(context)
                .getString(
                    SettingsActivity.KEY_DOWNLOAD_FOLDER,
                    EpisodeDownloadManager.getDefaultDownloadFolder().getAbsolutePath()));

    // Handle the case where the download finished while the application was
    // not running. In this case, there would be a downloadId but no
    // filePath while the episode media file is actually there.
    Iterator<Entry<String, EpisodeMetadata>> iterator = result.entrySet().iterator();

    while (iterator.hasNext()) {
      Entry<String, EpisodeMetadata> entry = iterator.next();
      // Skip all entries without a download id
      if (entry.getValue().downloadId == null) continue;

      final File downloadPath =
          new File(
              podcastDir,
              EpisodeDownloadManager.sanitizeAsFilePath(
                  entry.getValue().podcastName, entry.getValue().episodeName, entry.getKey()));

      if (entry.getValue().filePath == null && downloadPath.exists())
        entry.getValue().filePath = downloadPath.getAbsolutePath();
    }

    // Handle the case that the media file has been delete from outside the
    // app. In this case, downloadId and and filePath would be there, but no
    // file.
    iterator = result.entrySet().iterator();

    while (iterator.hasNext()) {
      Entry<String, EpisodeMetadata> entry = iterator.next();
      // Skip all entries without a download id
      if (entry.getValue().downloadId == null) continue;

      // Invalidate file path and download id data
      if (entry.getValue().filePath != null && !new File(entry.getValue().filePath).exists()) {
        entry.getValue().downloadId = null;
        entry.getValue().filePath = null;
      }
    }
  }