Пример #1
0
 private static void openLocal(
     final Activity context, final File file, String mime, final CloudFile remoteSource) {
   List<String> textExts =
       Arrays.asList(context.getResources().getStringArray(R.array.other_text_extensions));
   List<String> codeExts =
       Arrays.asList(context.getResources().getStringArray(R.array.code_extensions));
   String ext = file.getExtension().toLowerCase(Locale.getDefault());
   if (textExts.contains(ext) || codeExts.contains(ext)) {
     mime = "text/plain";
   }
   if (mime == null) {
     new MaterialDialog.Builder(context)
         .title(R.string.open_as)
         .items(R.array.open_as_array)
         .itemsCallback(
             new MaterialDialog.ListCallback() {
               @Override
               public void onSelection(
                   MaterialDialog dialog, View view, int which, CharSequence text) {
                 String newMime;
                 switch (which) {
                   default:
                     newMime = "text/*";
                     break;
                   case 1:
                     newMime = "image/*";
                     break;
                   case 2:
                     newMime = "audio/*";
                     break;
                   case 3:
                     newMime = "video/*";
                     break;
                   case 4:
                     newMime = "*/*";
                     break;
                 }
                 openLocal(context, file, newMime, remoteSource);
               }
             })
         .show();
     return;
   }
   try {
     context.startActivity(
         new Intent(Intent.ACTION_VIEW)
             .setDataAndType(Uri.fromFile(file.toJavaFile()), mime)
             .putExtra("remote", remoteSource));
   } catch (ActivityNotFoundException e) {
     Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
   }
 }
Пример #2
0
 public static void openFile(final MainActivity context, final File item, final boolean openAs) {
   if (item.isRemote()) {
     downloadFile(
         context,
         item,
         new FileCallback() {
           @Override
           public void onFile(File file) {
             openLocal(context, file, openAs ? null : item.getMimeType(), (CloudFile) item);
           }
         });
     return;
   }
   openLocal(context, item, openAs ? null : item.getMimeType(), null);
 }
Пример #3
0
  private static void checkDuplicates(
      final Activity context,
      final File file,
      final String originalNameNoExt,
      final int checks,
      final DuplicateCheckResult callback) {
    Log.v("checkDuplicates", "Checking: " + file.getPath());
    file.exists(
        new File.BooleanCallback() {
          @Override
          public void onComplete(final boolean result) {
            context.runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    if (result) {
                      String newName = originalNameNoExt;
                      if (checks > 0) newName += " (" + checks + ")";
                      if (!file.isDirectory()) newName += "." + file.getExtension();
                      File newFile =
                          file.isRemote()
                              ? new CloudFile(
                                  context,
                                  (CloudFile) file.getParent(),
                                  newName,
                                  file.isDirectory())
                              : new LocalFile(context, file.getParent(), newName);
                      checkDuplicates(context, newFile, originalNameNoExt, 1 + checks, callback);
                    } else {
                      callback.onResult(file);
                    }
                  }
                });
          }

          @Override
          public void onError(Exception e) {
            if (e != null) showErrorDialog(context, e.getMessage());
          }
        });
  }
Пример #4
0
 private static File checkDuplicatesSync(
     final Activity context, final File file, final String originalNameNoExt, final int checks)
     throws Exception {
   Log.v("checkDuplicatesSync", "Checking: " + file.getPath());
   if (file.existsSync()) {
     String newName = originalNameNoExt;
     if (checks > 0) newName += " (" + checks + ")";
     if (!file.isDirectory()) newName += "." + file.getExtension();
     File newFile =
         file.isRemote()
             ? new CloudFile(context, (CloudFile) file.getParent(), newName, file.isDirectory())
             : new LocalFile(context, file.getParent(), newName);
     return checkDuplicatesSync(context, newFile, originalNameNoExt, 1 + checks);
   } else return file;
 }
Пример #5
0
 public static void checkDuplicates(
     final Activity context, final File file, final DuplicateCheckResult callback) {
   checkDuplicates(context, file, file.getNameNoExtension(), 0, callback);
 }
Пример #6
0
  public static void downloadFile(
      final MainActivity context, final File item, final FileCallback callback) {
    final java.io.File downloadDir =
        new java.io.File(Environment.getExternalStorageDirectory(), "Cabinet");
    if (!downloadDir.exists()) downloadDir.mkdir();
    java.io.File tester = new java.io.File(downloadDir, item.getName());
    if (tester.exists() && tester.length() == item.length()) {
      callback.onFile(new LocalFile(context, tester));
      return;
    }
    final java.io.File dest = new java.io.File(downloadDir, item.getName());
    cancelledDownload = false;
    final ProgressDialog connectDialog =
        Utils.showProgressDialog(
            context,
            R.string.connecting,
            new DialogInterface.OnCancelListener() {
              @Override
              public void onCancel(DialogInterface dialogInterface) {
                cancelledDownload = true;
              }
            });
    context
        .getNetworkService()
        .getSftpClient(
            new NetworkService.SftpGetCallback() {
              @Override
              public void onSftpClient(final SftpClient client) {
                if (cancelledDownload) return;
                context.runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        connectDialog.dismiss();
                        final ProgressDialog downloadDialog =
                            Utils.showProgressDialog(
                                context,
                                R.string.downloading,
                                new DialogInterface.OnCancelListener() {
                                  @Override
                                  public void onCancel(DialogInterface dialogInterface) {
                                    cancelledDownload = true;
                                  }
                                });
                        client.get(
                            item.getPath(),
                            dest.getPath(),
                            new SftpClient.CancelableCompletionCallback() {
                              @Override
                              public void onComplete() {
                                context.runOnUiThread(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        if (!cancelledDownload) {
                                          downloadDialog.dismiss();
                                          callback.onFile(new LocalFile(context, dest));
                                        } else if (dest.exists()) dest.delete();
                                      }
                                    });
                              }

                              @Override
                              public boolean shouldCancel() {
                                return cancelledDownload;
                              }

                              @Override
                              public void onError(final Exception e) {
                                context.runOnUiThread(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        downloadDialog.dismiss();
                                        Utils.showErrorDialog(
                                            context, R.string.failed_download_file, e);
                                      }
                                    });
                              }
                            });
                      }
                    });
              }

              @Override
              public void onError(final Exception e) {
                context.runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        connectDialog.dismiss();
                        Utils.showErrorDialog(context, R.string.failed_connect_server, e);
                      }
                    });
              }
            },
            (CloudFile) item);
  }
Пример #7
0
 public static File checkDuplicatesSync(final Activity context, final File file) throws Exception {
   return checkDuplicatesSync(context, file, file.getNameNoExtension(), 0);
 }