Search in sources :

Example 6 with DocumentFile

use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.

the class FileUtils method fetchDirectories.

public static DocumentFile fetchDirectories(DocumentFile directoryFile, String path, boolean createIfNotExists) throws IOException {
    DocumentFile currentDirectory = directoryFile;
    String[] pathArray = path.split(File.separator);
    for (String currentPath : pathArray) {
        if (currentDirectory == null)
            throw new IOException("Failed to create directories: " + path);
        DocumentFile existingOne = currentDirectory.findFile(currentPath);
        if (existingOne != null && !existingOne.isDirectory())
            throw new IOException("A file exists for of directory name: " + currentPath + " ; " + path);
        currentDirectory = existingOne == null && createIfNotExists ? currentDirectory.createDirectory(currentPath) : existingOne;
    }
    return currentDirectory;
}
Also used : LocalDocumentFile(com.genonbeta.TrebleShot.io.LocalDocumentFile) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) StreamDocumentFile(com.genonbeta.TrebleShot.io.StreamDocumentFile) TreeDocumentFile(com.genonbeta.TrebleShot.io.TreeDocumentFile) IOException(java.io.IOException)

Example 7 with DocumentFile

use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.

the class FileUtils method getApplicationDirectory.

public static DocumentFile getApplicationDirectory(Context context) {
    String defaultPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + context.getString(R.string.text_appName);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (sharedPreferences.contains("storage_path")) {
        try {
            DocumentFile savePath = fromUri(context, Uri.parse(sharedPreferences.getString("storage_path", null)));
            if (savePath != null && savePath.isDirectory() && savePath.canWrite())
                return savePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    File defaultFolder = new File(defaultPath);
    if (!defaultFolder.isDirectory())
        defaultFolder.mkdirs();
    return DocumentFile.fromFile(defaultFolder);
}
Also used : LocalDocumentFile(com.genonbeta.TrebleShot.io.LocalDocumentFile) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) StreamDocumentFile(com.genonbeta.TrebleShot.io.StreamDocumentFile) TreeDocumentFile(com.genonbeta.TrebleShot.io.TreeDocumentFile) SharedPreferences(android.content.SharedPreferences) LocalDocumentFile(com.genonbeta.TrebleShot.io.LocalDocumentFile) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) StreamDocumentFile(com.genonbeta.TrebleShot.io.StreamDocumentFile) File(java.io.File) TreeDocumentFile(com.genonbeta.TrebleShot.io.TreeDocumentFile) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 8 with DocumentFile

use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.

the class ShareActivity method createFolderStructure.

protected void createFolderStructure(DocumentFile file, String folderName) {
    DocumentFile[] files = file.listFiles();
    if (files != null) {
        for (DocumentFile thisFile : files) {
            if (getDefaultInterrupter().interrupted())
                break;
            getProgressDialog().setMax(getProgressDialog().getMax() + 1);
            getProgressDialog().setProgress(getProgressDialog().getMax() + 1);
            if (thisFile.isDirectory()) {
                createFolderStructure(thisFile, (folderName != null ? folderName + File.separator : null) + thisFile.getName());
                continue;
            }
            try {
                mFiles.add(new SelectableStream(thisFile, folderName));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Example 9 with DocumentFile

use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.

the class HomeActivity method sendThisApplication.

private void sendThisApplication() {
    new Handler(Looper.myLooper()).post(new Runnable() {

        @Override
        public void run() {
            try {
                Interrupter interrupter = new Interrupter();
                PackageManager pm = getPackageManager();
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                PackageInfo packageInfo = pm.getPackageInfo(getApplicationInfo().packageName, 0);
                String fileName = packageInfo.applicationInfo.loadLabel(pm) + "_" + packageInfo.versionName + ".apk";
                DocumentFile storageDirectory = FileUtils.getApplicationDirectory(getApplicationContext());
                DocumentFile codeFile = DocumentFile.fromFile(new File(getApplicationInfo().sourceDir));
                DocumentFile cloneFile = storageDirectory.createFile(null, FileUtils.getUniqueFileName(storageDirectory, fileName, true));
                FileUtils.copy(HomeActivity.this, codeFile, cloneFile, interrupter);
                try {
                    sendIntent.putExtra(ShareActivity.EXTRA_FILENAME_LIST, fileName).putExtra(Intent.EXTRA_STREAM, FileUtils.getSecureUri(HomeActivity.this, cloneFile)).setType(cloneFile.getType());
                    startActivity(Intent.createChooser(sendIntent, getString(R.string.text_fileShareAppChoose)));
                } catch (IllegalArgumentException e) {
                    Toast.makeText(HomeActivity.this, R.string.mesg_providerNotAllowedError, Toast.LENGTH_LONG).show();
                    openFolder(storageDirectory);
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : Interrupter(com.genonbeta.TrebleShot.util.Interrupter) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) Handler(android.os.Handler) Intent(android.content.Intent) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with DocumentFile

use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.

the class FileExplorerFragment method onCreateView.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fileexplorer, container, false);
    mDatabase = new AccessDatabase(getActivity());
    mPathView = view.findViewById(R.id.fragment_fileexplorer_pathresolver);
    mHomeButton = view.findViewById(R.id.fragment_fileexplorer_pathresolver_home);
    mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    mPathAdapter = new FilePathResolverRecyclerAdapter();
    mFileListFragment = (FileListFragment) getChildFragmentManager().findFragmentById(R.id.fragment_fileexplorer_fragment_files);
    mPathAdapter.setOnClickListener(new PathResolverRecyclerAdapter.OnClickListener<DocumentFile>() {

        @Override
        public void onClick(PathResolverRecyclerAdapter.Holder<DocumentFile> holder) {
            requestPath(holder.index.object);
        }
    });
    mHomeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            requestPath(null);
        }
    });
    mFileListFragment.setOnPathChangedListener(new FileListFragment.OnPathChangedListener() {

        @Override
        public void onPathChanged(DocumentFile file) {
            mPathAdapter.goTo(file);
            mPathAdapter.notifyDataSetChanged();
            if (mPathAdapter.getItemCount() > 0)
                mPathView.smoothScrollToPosition(mPathAdapter.getItemCount() - 1);
        }
    });
    mPathView.setHasFixedSize(true);
    mPathView.setLayoutManager(mLayoutManager);
    mLayoutManager.setStackFromEnd(true);
    mPathView.setAdapter(mPathAdapter);
    return view;
}
Also used : DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) PathResolverRecyclerAdapter(com.genonbeta.TrebleShot.adapter.PathResolverRecyclerAdapter) AccessDatabase(com.genonbeta.TrebleShot.database.AccessDatabase) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) Nullable(android.support.annotation.Nullable)

Aggregations

DocumentFile (com.genonbeta.TrebleShot.io.DocumentFile)11 IOException (java.io.IOException)5 LocalDocumentFile (com.genonbeta.TrebleShot.io.LocalDocumentFile)4 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 View (android.view.View)2 StreamDocumentFile (com.genonbeta.TrebleShot.io.StreamDocumentFile)2 TreeDocumentFile (com.genonbeta.TrebleShot.io.TreeDocumentFile)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Resources (android.content.res.Resources)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 Nullable (android.support.annotation.Nullable)1 AlertDialog (android.support.v7.app.AlertDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1