Search in sources :

Example 16 with DocumentFile

use of android.support.v4.provider.DocumentFile in project AmazeFileManager by TeamAmaze.

the class OTGUtil method getDocumentFiles.

/**
 * Get the files at a specific path in OTG
 *
 * @param path    the path to the directory tree, starts with prefix 'otg:/'
 *                Independent of URI (or mount point) for the OTG
 * @param context context for loading
 * @return an array of list of files at the path
 */
public static void getDocumentFiles(String path, Context context, OnFileFound fileFound) {
    SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context);
    String rootUriString = manager.getString(MainActivity.KEY_PREF_OTG, null);
    DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString));
    String[] parts = path.split("/");
    for (String part : parts) {
        // first omit 'otg:/' before iterating through DocumentFile
        if (path.equals(OTGUtil.PREFIX_OTG + "/"))
            break;
        if (part.equals("otg:") || part.equals(""))
            continue;
        // iterating through the required path to find the end point
        rootUri = rootUri.findFile(part);
    }
    // we have the end point DocumentFile, list the files inside it and return
    for (DocumentFile file : rootUri.listFiles()) {
        if (file.exists()) {
            long size = 0;
            if (!file.isDirectory())
                size = file.length();
            Log.d(context.getClass().getSimpleName(), "Found file: " + file.getName());
            HybridFileParcelable baseFile = new HybridFileParcelable(path + "/" + file.getName(), RootHelper.parseDocumentFilePermission(file), file.lastModified(), size, file.isDirectory());
            baseFile.setName(file.getName());
            baseFile.setMode(OpenMode.OTG);
            fileFound.onFileFound(baseFile);
        }
    }
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) DocumentFile(android.support.v4.provider.DocumentFile) SharedPreferences(android.content.SharedPreferences)

Example 17 with DocumentFile

use of android.support.v4.provider.DocumentFile in project AmazeFileManager by TeamAmaze.

the class DeleteTask method doInBackground.

protected Boolean doInBackground(ArrayList<HybridFileParcelable>... p1) {
    files = p1[0];
    boolean wasDeleted = true;
    if (files.size() == 0)
        return true;
    if (files.get(0).isOtgFile()) {
        for (HybridFileParcelable file : files) {
            DocumentFile documentFile = OTGUtil.getDocumentFile(file.getPath(), cd, false);
            wasDeleted = documentFile.delete();
        }
    } else if (files.get(0).isDropBoxFile()) {
        CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
        for (HybridFileParcelable baseFile : files) {
            try {
                cloudStorageDropbox.delete(CloudUtil.stripPath(OpenMode.DROPBOX, baseFile.getPath()));
            } catch (Exception e) {
                e.printStackTrace();
                wasDeleted = false;
                break;
            }
        }
    } else if (files.get(0).isBoxFile()) {
        CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
        for (HybridFileParcelable baseFile : files) {
            try {
                cloudStorageBox.delete(CloudUtil.stripPath(OpenMode.BOX, baseFile.getPath()));
            } catch (Exception e) {
                e.printStackTrace();
                wasDeleted = false;
                break;
            }
        }
    } else if (files.get(0).isGoogleDriveFile()) {
        CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
        for (HybridFileParcelable baseFile : files) {
            try {
                cloudStorageGdrive.delete(CloudUtil.stripPath(OpenMode.GDRIVE, baseFile.getPath()));
            } catch (Exception e) {
                e.printStackTrace();
                wasDeleted = false;
                break;
            }
        }
    } else if (files.get(0).isOneDriveFile()) {
        CloudStorage cloudStorageOnedrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
        for (HybridFileParcelable baseFile : files) {
            try {
                cloudStorageOnedrive.delete(CloudUtil.stripPath(OpenMode.ONEDRIVE, baseFile.getPath()));
            } catch (Exception e) {
                e.printStackTrace();
                wasDeleted = false;
                break;
            }
        }
    } else {
        for (HybridFileParcelable file : files) {
            try {
                if (file.delete(cd, rootMode)) {
                    wasDeleted = true;
                } else {
                    wasDeleted = false;
                    break;
                }
            } catch (ShellNotRunningException e) {
                e.printStackTrace();
                wasDeleted = false;
                break;
            }
        }
    }
    // delete file from media database
    if (!files.get(0).isSmb()) {
        try {
            for (HybridFileParcelable f : files) {
                delete(cd, f.getPath());
            }
        } catch (Exception e) {
            for (HybridFileParcelable f : files) {
                FileUtils.scanFile(f.getPath(), cd);
            }
        }
    }
    // delete file entry from encrypted database
    for (HybridFileParcelable file : files) {
        if (file.getName().endsWith(CryptUtil.CRYPT_EXTENSION)) {
            CryptHandler handler = new CryptHandler(cd);
            handler.clear(file.getPath());
        }
    }
    return wasDeleted;
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) DocumentFile(android.support.v4.provider.DocumentFile) CryptHandler(com.amaze.filemanager.database.CryptHandler) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException)

Example 18 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class StorageUtil method createDocumentDir.

public static DocumentFile createDocumentDir(Context context, Uri treeUri, String path) {
    int index = path.lastIndexOf("/");
    String dirPath = path.substring(0, index);
    DocumentFile file = parseDocumentFile(context, treeUri, new File(dirPath));
    if (file != null) {
        String name = path.substring(index + 1);
        file = file.createDirectory(name);
    }
    return file;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 19 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class Delete method deleteFileOnRemovableStorage.

boolean deleteFileOnRemovableStorage(Context context, Uri treeUri, String path) {
    boolean success = false;
    DocumentFile file = StorageUtil.parseDocumentFile(context, treeUri, new File(path));
    if (file != null) {
        success = file.delete();
    }
    // remove from MediaStore
    addPathToScan(path);
    return success;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File)

Example 20 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Camera-Roll-Android-App by kollerlukas.

the class FileOperation method getTreeUri.

public Uri getTreeUri(Intent workIntent, String path) {
    Log.d("FileOperation", "getTreeUri");
    Uri treeUri;
    String treeUriExtra = workIntent.getStringExtra(FileOperation.REMOVABLE_STORAGE_TREE_URI);
    if (treeUriExtra != null) {
        treeUri = Uri.parse(treeUriExtra);
    } else {
        Settings s = Settings.getInstance(getApplicationContext());
        treeUri = s.getRemovableStorageTreeUri();
    }
    if (path != null) {
        // check if path is child of the treeUri
        DocumentFile file = StorageUtil.parseDocumentFile(getApplicationContext(), treeUri, new File(path));
        if (file != null) {
            return treeUri;
        } else {
            requestPermissionForRemovableStorageBroadcast(workIntent);
        }
    } else {
        return treeUri;
    }
    return null;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) Uri(android.net.Uri) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) Settings(us.koller.cameraroll.data.Settings)

Aggregations

DocumentFile (android.support.v4.provider.DocumentFile)65 File (java.io.File)36 IOException (java.io.IOException)22 Uri (android.net.Uri)21 ContentResolver (android.content.ContentResolver)12 SmbFile (jcifs.smb.SmbFile)11 MalformedURLException (java.net.MalformedURLException)10 SmbException (jcifs.smb.SmbException)10 FileOutputStream (java.io.FileOutputStream)9 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)8 OutputStream (java.io.OutputStream)8 InputStream (java.io.InputStream)7 SuppressLint (android.annotation.SuppressLint)6 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)6 FileInputStream (java.io.FileInputStream)6 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)4 BufferedOutputStream (java.io.BufferedOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 FileChannel (java.nio.channels.FileChannel)4 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)4