Java tutorial
/* * Copyright 2015 Elia Zammuto * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.theelix.libreexplorer; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.support.v4.provider.DocumentFile; import android.support.v7.app.AlertDialog; import android.view.MenuItem; import com.theelix.librefilemanager.R; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; /** * LibreFM Low Level Class. It Manages the basic IO and File Manipulation Actions */ public class FileManager { public static final int REQUEST_GET_DOCUMENT = 0; private static File currentDirectory; private static File homeDirectory = Environment.getExternalStorageDirectory(); private static Context mContext; private static Boolean isMoving; private static File[] selectedFiles; private static String[] destFiles; private static File targetFile; /** * This method chooses the appropriate Apps and Open the file * * @param file The target file */ public static void openFile(File file) { try { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); String mimeType = FileUtilties.getMimeType(uri); if (mimeType == null) { mimeType = "*/*"; } intent.setDataAndType(uri, mimeType); mContext.startActivity(Intent.createChooser(intent, null)); } catch (ActivityNotFoundException e) { //Activity is not found so App'll start an intent with generic Mimetype Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "*/*"); mContext.startActivity(Intent.createChooser(intent, null)); } } /** * This method calls FileManagerActivity refresh() Method */ public static void refresh() { FileManagerActivity fma = (FileManagerActivity) mContext; fma.refresh(); } /** * This method prepares the copy of the selected files * * @param files The Selected Files */ public static void prepareCopy(File[] files) { selectedFiles = files; isMoving = false; setPasteVisible(true); } /** * This method prepares the copy of the selected files * * @param files The Selected Files */ public static void prepareCut(File[] files) { selectedFiles = files; isMoving = true; setPasteVisible(true); } private static void paste(File sourceFile, File destFile) throws IOException, FileNotCreatedException { try { if (sourceFile.isDirectory()) { FileUtils.copyDirectory(sourceFile, destFile); } else { FileUtils.copyFile(sourceFile, destFile); } if (isMoving) { if (sourceFile.isDirectory()) { FileUtils.deleteDirectory(sourceFile); } else { sourceFile.delete(); } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new FileNotCreatedException(); } } /** * This Method use the file set by prepareCopy or PrepareCut and then paste the files */ public static void preparePaste() { //If we are running KK or later, show the "Choose Folder" dialog if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && getCurrentDirectory().getPath().contains(System.getenv("SECONDARY_STORAGE"))) { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage( "As Android 5.0, a new system is used for writing files to External Storage. Please press OK and Select THE EXACT FOLDER you pasted the files"); builder.setPositiveButton(mContext.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); ((Activity) mContext).startActivityForResult(intent, REQUEST_GET_DOCUMENT); } }); builder.create().show(); } else { createDestFiles(); } } public static void multiplePaste() throws IOException, FileNotCreatedException { for (int i = 0; i < selectedFiles.length; i++) { setTargetFile(selectedFiles[i]); paste(getTargetFile(), new File(getCurrentDirectory() + File.separator + destFiles[i])); } } public static void createDestFiles() { ArrayList<String> destFiles = new ArrayList<>(); for (File selectedFile : selectedFiles) { String origFileName; String extension; try { origFileName = selectedFile.getName().substring(0, selectedFile.getName().lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { origFileName = selectedFile.getName(); } String fileName = selectedFile.getName(); try { extension = fileName.substring(fileName.lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { extension = ""; } int lastIndex = 1; while (new File(getCurrentDirectory() + File.separator + fileName).exists()) { fileName = origFileName + " (" + lastIndex + ")" + extension; lastIndex++; } destFiles.add(fileName); FileManager.destFiles = destFiles.toArray(new String[destFiles.size()]); } PasteTask task = new PasteTask(mContext); task.execute(); } public static void createDestFiles(DocumentFile folder) throws IOException, FileNotCreatedException { ArrayList<String> destFiles = new ArrayList<>(); for (File selectedFile : selectedFiles) { String origFileName; String extension; try { origFileName = selectedFile.getName().substring(0, selectedFile.getName().lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { origFileName = selectedFile.getName(); } String fileName = selectedFile.getName(); try { extension = fileName.substring(fileName.lastIndexOf(".")); } catch (IndexOutOfBoundsException e) { extension = ""; } int lastIndex = 1; while (new File(getCurrentDirectory() + File.separator + fileName).exists()) { fileName = origFileName + " (" + lastIndex + ")" + extension; lastIndex++; } if (selectedFile.isDirectory()) { folder.createDirectory(fileName); } else { folder.createFile(FileUtilties.getMimeType(selectedFile), fileName); } destFiles.add(fileName); } FileManager.destFiles = destFiles.toArray(new String[destFiles.size()]); PasteTask task = new PasteTask(mContext); task.execute(); } /** * Deletes the given file * * @param selectedFiles The Selected Files * @throws IOException */ public static void delete(Object[] selectedFiles) throws IOException { for (Object selectedFile : selectedFiles) { File file = (File) selectedFile; if (file.isDirectory()) { FileUtils.deleteDirectory(file); } else { file.delete(); if (file.equals(targetFile)) { targetFile = null; setPasteVisible(false); } } } } /** * Rename the given file * * @param sourceFile The Target File * @param newFileName The New File Name * @throws IOException */ public static void rename(File sourceFile, String newFileName) throws IOException { sourceFile.renameTo(new File(sourceFile.getParent() + File.separator + newFileName)); } //Getter and Setters public static File getCurrentDirectory() { return currentDirectory; } public static void setCurrentDirectory(File currentDirectory) { FileManager.currentDirectory = currentDirectory; refresh(); } public static void setContext(Context mContext) { FileManager.mContext = mContext; } public static File getTargetFile() { return targetFile; } public static void setTargetFile(File targetFile) { FileManager.targetFile = targetFile; } public static void setPasteVisible(Boolean visible) { MenuItem item = ((FileManagerActivity) mContext).mMenu.findItem(R.id.toolbar_menu_paste); item.setVisible(visible); } public static File getHomeDirectory() { return homeDirectory; } }