|
1 | 1 | package com.aditya.filebrowser; |
2 | 2 |
|
| 3 | +import android.app.ProgressDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.pm.PackageManager; |
| 8 | +import android.content.pm.ResolveInfo; |
| 9 | +import android.net.Uri; |
| 10 | +import android.support.v7.app.AlertDialog; |
| 11 | +import android.view.View; |
| 12 | + |
| 13 | +import com.aditya.filebrowser.adapters.CustomAdapter; |
| 14 | +import com.aditya.filebrowser.interfaces.FuncPtr; |
| 15 | +import com.aditya.filebrowser.models.FileItem; |
| 16 | +import com.aditya.filebrowser.utils.UIUtils; |
| 17 | +import com.beardedhen.androidbootstrap.BootstrapProgressBar; |
| 18 | + |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.text.SimpleDateFormat; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.Executors; |
| 28 | + |
3 | 29 | /** |
4 | 30 | * Created by Aditya on 4/15/2017. |
5 | 31 | */ |
6 | 32 | public class FileIO { |
| 33 | + |
| 34 | + ExecutorService executor; |
| 35 | + MainActivity mActivity; |
| 36 | + UIHelper helper; |
| 37 | + |
| 38 | + FileIO (final MainActivity mActivity) { |
| 39 | + this.mActivity = mActivity; |
| 40 | + helper = new UIHelper(mActivity); |
| 41 | + executor = Executors.newFixedThreadPool(1); |
| 42 | + } |
| 43 | + |
| 44 | + public void createDirectory(final File path) { |
| 45 | + executor.execute(new Runnable() { |
| 46 | + @Override |
| 47 | + public void run() { |
| 48 | + try { |
| 49 | + FileUtils.forceMkdir(path); |
| 50 | + mActivity.runOnUiThread(helper.updateRunner()); |
| 51 | + } catch (IOException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + mActivity.runOnUiThread(helper.errorRunner("An error occurred while creating a new folder")); |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public void deleteItems(final List<FileItem> selectedItems) { |
| 60 | + if(selectedItems!=null && selectedItems.size()>0) { |
| 61 | + AlertDialog confirmDialog = new AlertDialog.Builder(mActivity) |
| 62 | + .setTitle("Delete Files") |
| 63 | + .setMessage("Are you sure you want to delete " + selectedItems.size() + " items?") |
| 64 | + .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { |
| 65 | + public void onClick(DialogInterface dialog, int which) { |
| 66 | + // continue with delete |
| 67 | + final ProgressDialog progressDialog = new ProgressDialog(mActivity); |
| 68 | + progressDialog.setTitle("Deleting Please Wait... "); |
| 69 | + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
| 70 | + progressDialog.setCancelable(false); |
| 71 | + progressDialog.setProgress(0); |
| 72 | + progressDialog.setMessage(""); |
| 73 | + progressDialog.show(); |
| 74 | + |
| 75 | + executor.execute(new Runnable() { |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + int i = 0; |
| 79 | + float TOTAL_ITEMS = selectedItems.size(); |
| 80 | + try { |
| 81 | + for (; i < selectedItems.size(); i++) { |
| 82 | + mActivity.runOnUiThread(helper.progressUpdater(progressDialog, (int)((i/TOTAL_ITEMS)*100), "File: "+selectedItems.get(i).getFile().getName())); |
| 83 | + if (selectedItems.get(i).getFile().isDirectory()) { |
| 84 | + FileUtils.deleteDirectory(selectedItems.get(i).getFile()); |
| 85 | + } else { |
| 86 | + FileUtils.forceDelete(selectedItems.get(i).getFile()); |
| 87 | + } |
| 88 | + } |
| 89 | + mActivity.runOnUiThread(helper.toggleProgressBarVisibility(progressDialog)); |
| 90 | + mActivity.runOnUiThread(helper.updateRunner()); |
| 91 | + } catch (IOException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + mActivity.runOnUiThread(helper.toggleProgressBarVisibility(progressDialog)); |
| 94 | + mActivity.runOnUiThread(helper.errorRunner("An error occurred while deleting ")); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + mActivity.switchMode(CustomAdapter.CHOICE_MODE.SINGLE_CHOICE); |
| 100 | + } |
| 101 | + }) |
| 102 | + .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { |
| 103 | + public void onClick(DialogInterface dialog, int which) { |
| 104 | + // do nothing |
| 105 | + dialog.dismiss(); |
| 106 | + } |
| 107 | + }) |
| 108 | + .setIcon(android.R.drawable.ic_dialog_alert) |
| 109 | + .show(); |
| 110 | + } else { |
| 111 | + UIUtils.ShowToast("No Items Selected!",mActivity); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void pasteFiles(final File destination) { |
| 116 | + |
| 117 | + final Operations op = Operations.getInstance(mActivity); |
| 118 | + final List<FileItem> selectedItems = op.getSelectedFiles(); |
| 119 | + final Operations.FILE_OPERATIONS operation = op.getOperation(); |
| 120 | + |
| 121 | + if(selectedItems!=null && selectedItems.size()>0) { |
| 122 | + final ProgressDialog progressDialog = new ProgressDialog(mActivity); |
| 123 | + String title = "Please Wait... "; |
| 124 | + progressDialog.setTitle(title); |
| 125 | + if (operation == Operations.FILE_OPERATIONS.COPY) |
| 126 | + progressDialog.setTitle("Copying " + title); |
| 127 | + else if (operation == Operations.FILE_OPERATIONS.CUT) |
| 128 | + progressDialog.setTitle("Moving " + title); |
| 129 | + |
| 130 | + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
| 131 | + progressDialog.setCancelable(false); |
| 132 | + progressDialog.setMessage(""); |
| 133 | + progressDialog.setProgress(0); |
| 134 | + progressDialog.show(); |
| 135 | + |
| 136 | + executor.execute(new Runnable() { |
| 137 | + @Override |
| 138 | + public void run() { |
| 139 | + int i = 0; |
| 140 | + float TOTAL_ITEMS = selectedItems.size(); |
| 141 | + try { |
| 142 | + for (; i < selectedItems.size(); i++) { |
| 143 | + mActivity.runOnUiThread(helper.progressUpdater(progressDialog, (int)((i/TOTAL_ITEMS)*100), "File: "+selectedItems.get(i).getFile().getName())); |
| 144 | + if (selectedItems.get(i).getFile().isDirectory()) { |
| 145 | + if (operation == Operations.FILE_OPERATIONS.CUT) |
| 146 | + FileUtils.moveDirectory(selectedItems.get(i).getFile(), new File(destination, selectedItems.get(i).getFile().getName())); |
| 147 | + else if (operation == Operations.FILE_OPERATIONS.COPY) |
| 148 | + FileUtils.copyDirectory(selectedItems.get(i).getFile(), new File(destination, selectedItems.get(i).getFile().getName())); |
| 149 | + } else { |
| 150 | + if (operation == Operations.FILE_OPERATIONS.CUT) |
| 151 | + FileUtils.moveFile(selectedItems.get(i).getFile(), new File(destination, selectedItems.get(i).getFile().getName())); |
| 152 | + else if (operation == Operations.FILE_OPERATIONS.COPY) |
| 153 | + FileUtils.copyFile(selectedItems.get(i).getFile(), new File(destination, selectedItems.get(i).getFile().getName())); |
| 154 | + } |
| 155 | + } |
| 156 | + mActivity.runOnUiThread(new Runnable() { |
| 157 | + @Override |
| 158 | + public void run() { |
| 159 | + op.resetOperation(); |
| 160 | + } |
| 161 | + }); |
| 162 | + mActivity.runOnUiThread(helper.toggleProgressBarVisibility(progressDialog)); |
| 163 | + mActivity.runOnUiThread(helper.updateRunner()); |
| 164 | + } catch (IOException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + mActivity.runOnUiThread(helper.toggleProgressBarVisibility(progressDialog)); |
| 167 | + mActivity.runOnUiThread(helper.errorRunner("An error occurred while pasting ")); |
| 168 | + } |
| 169 | + } |
| 170 | + }); |
| 171 | + } else { |
| 172 | + UIUtils.ShowToast("No Items Selected!",mActivity); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public void renameFile(final FileItem fileItem) { |
| 177 | + UIUtils.showEditTextDialog(mActivity, "Rename", fileItem.getFile().getName() ,new FuncPtr() { |
| 178 | + @Override |
| 179 | + public void execute(final String val) { |
| 180 | + executor.execute(new Runnable() { |
| 181 | + @Override |
| 182 | + public void run() { |
| 183 | + try { |
| 184 | + if(fileItem.getFile().isDirectory()) |
| 185 | + FileUtils.moveDirectory(fileItem.getFile(),new File(fileItem.getFile().getParentFile(), val.trim())); |
| 186 | + else |
| 187 | + FileUtils.moveFile(fileItem.getFile(),new File(fileItem.getFile().getParentFile(), val.trim())); |
| 188 | + mActivity.runOnUiThread(helper.updateRunner()); |
| 189 | + } catch (Exception e) { |
| 190 | + e.printStackTrace(); |
| 191 | + mActivity.runOnUiThread(helper.errorRunner("An error occurred while renaming ")); |
| 192 | + } |
| 193 | + } |
| 194 | + }); |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + public void getProperties(List<FileItem> selectedItems) { |
| 200 | + |
| 201 | + StringBuilder msg = new StringBuilder(); |
| 202 | + if(selectedItems.size()==1) { |
| 203 | + boolean isDirectory = (selectedItems.get(0).getFile().isDirectory()); |
| 204 | + String type = isDirectory?"Directory":"File"; |
| 205 | + String size = FileUtils.byteCountToDisplaySize(isDirectory?FileUtils.sizeOfDirectory(selectedItems.get(0).getFile()):FileUtils.sizeOf(selectedItems.get(0).getFile())); |
| 206 | + String lastModified = new SimpleDateFormat(Constants.DATE_FORMAT).format(selectedItems.get(0).getFile().lastModified()); |
| 207 | + msg.append("Type : " + type + "\n\n"); |
| 208 | + msg.append("Size : " + size + "\n\n"); |
| 209 | + msg.append("Last Modified : " + lastModified + "\n\n"); |
| 210 | + msg.append("Path : "+selectedItems.get(0).getFile().getAbsolutePath()); |
| 211 | + } else { |
| 212 | + long totalSize = 0; |
| 213 | + for(int i=0;i<selectedItems.size();i++) { |
| 214 | + boolean isDirectory = (selectedItems.get(i).getFile().isDirectory()); |
| 215 | + totalSize += isDirectory?FileUtils.sizeOfDirectory(selectedItems.get(i).getFile()):FileUtils.sizeOf(selectedItems.get(i).getFile()); |
| 216 | + } |
| 217 | + msg.append("Type : " + "Multiple Files" + "\n\n"); |
| 218 | + msg.append("Size : " + FileUtils.byteCountToDisplaySize(totalSize) + "\n\n"); |
| 219 | + } |
| 220 | + UIUtils.ShowMsg(msg.toString(),"Properties",mActivity); |
| 221 | + } |
| 222 | + |
| 223 | + public void shareMultipleFiles(List<FileItem> filesToBeShared){ |
| 224 | + |
| 225 | + ArrayList<Uri> uris = new ArrayList<>(); |
| 226 | + for(FileItem file: filesToBeShared){ |
| 227 | + uris.add(Uri.fromFile(file.getFile())); |
| 228 | + } |
| 229 | + final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 230 | + intent.setType("*/*"); |
| 231 | + intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 232 | + PackageManager manager = mActivity.getPackageManager(); |
| 233 | + List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0); |
| 234 | + if (infos.size() > 0) { |
| 235 | + mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share))); |
| 236 | + } else { |
| 237 | + UIUtils.ShowToast("No app found to handle sharing",mActivity); |
| 238 | + } |
| 239 | + } |
7 | 240 | } |
0 commit comments