@SuppressLint("NewApi")
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK
        && requestCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {
      String dir = data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR);

      File path;
      if (dir != null) {
        path = new File(dir);
      } else {
        path = ui.getActivity().getExternalFilesDir(null);
      }
      String message = null;
      final Context context = ui.getActivity().getApplicationContext();
      if (!path.exists()) {
        message = String.format(context.getString(R.string.folder_does_not_exist_error), dir);
      } else if (!path.canRead()) {
        message = String.format(context.getString(R.string.folder_not_readable_error), dir);
      } else if (!path.canWrite()) {
        message = String.format(context.getString(R.string.folder_not_writable_error), dir);
      }

      if (message == null) {
        Log.d(TAG, "Setting data folder: " + dir);
        UserPreferences.setDataFolder(dir);
        setDataFolderText();
      } else {
        AlertDialog.Builder ab = new AlertDialog.Builder(ui.getActivity());
        ab.setMessage(message);
        ab.setPositiveButton(android.R.string.ok, null);
        ab.show();
      }
    }
  }
 private void showChooseDataFolderDialog() {
   Context context = ui.getActivity();
   File dataFolder = UserPreferences.getDataFolder(null);
   if (dataFolder == null) {
     new MaterialDialog.Builder(ui.getActivity())
         .title(R.string.error_label)
         .content(R.string.external_storage_error_msg)
         .neutralText(android.R.string.ok)
         .show();
     return;
   }
   String dataFolderPath = dataFolder.getAbsolutePath();
   int selectedIndex = -1;
   File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
   List<String> folders = new ArrayList<>(mediaDirs.length);
   List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
   for (int i = 0; i < mediaDirs.length; i++) {
     File dir = mediaDirs[i];
     if (dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
       continue;
     }
     String path = mediaDirs[i].getAbsolutePath();
     folders.add(path);
     if (dataFolderPath.equals(path)) {
       selectedIndex = i;
     }
     int index = path.indexOf("Android");
     String choice;
     if (index >= 0) {
       choice = path.substring(0, index);
     } else {
       choice = path;
     }
     long bytes = StorageUtils.getFreeSpaceAvailable(path);
     String freeSpace =
         String.format(
             context.getString(R.string.free_space_label), Converter.byteToString(bytes));
     choices.add(
         Html.fromHtml("<html><small>" + choice + " [" + freeSpace + "]" + "</small></html>"));
   }
   if (choices.size() == 0) {
     new MaterialDialog.Builder(ui.getActivity())
         .title(R.string.error_label)
         .content(R.string.external_storage_error_msg)
         .neutralText(android.R.string.ok)
         .show();
     return;
   }
   MaterialDialog dialog =
       new MaterialDialog.Builder(ui.getActivity())
           .title(R.string.choose_data_directory)
           .content(R.string.choose_data_directory_message)
           .items(choices.toArray(new CharSequence[choices.size()]))
           .itemsCallbackSingleChoice(
               selectedIndex,
               (dialog1, itemView, which, text) -> {
                 String folder = folders.get(which);
                 Log.d(TAG, "data folder: " + folder);
                 UserPreferences.setDataFolder(folder);
                 setDataFolderText();
                 return true;
               })
           .negativeText(R.string.cancel_label)
           .cancelable(true)
           .build();
   dialog.show();
 }