Skip to content

Commit f4a8154

Browse files
committed
fix copy without remove old file
1 parent d304363 commit f4a8154

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

PowerFileExplorer/src/main/java/com/amaze/filemanager/services/CopyService.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class CopyService extends Service {
7373
private ArrayList<DataPackage> dataPackages = new ArrayList<>();
7474
private NotificationManager mNotifyManager;
7575
private NotificationCompat.Builder mBuilder;
76-
private Context c;
76+
private Context ctx;
7777

7878
private ProgressListener progressListener;
7979
private final IBinder mBinder = new LocalBinder();
@@ -87,7 +87,7 @@ public class CopyService extends Service {
8787
@Override
8888
public void onCreate() {
8989
super.onCreate();
90-
c = getApplicationContext();
90+
ctx = getApplicationContext();
9191
registerReceiver(receiver3, new IntentFilter(TAG_BROADCAST_COPY_CANCEL));
9292
}
9393

@@ -108,7 +108,7 @@ public int onStartCommand(Intent intent, int flags, final int startId) {
108108
notificationIntent.putExtra("from", "CopyService");
109109
Log.i("CopyService", "notificationIntent " + notificationIntent + ", " + targetPath + ", " + notificationIntent.getExtras());
110110
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
111-
mBuilder = new NotificationCompat.Builder(c);
111+
mBuilder = new NotificationCompat.Builder(ctx);
112112
mBuilder.setContentIntent(pendingIntent);
113113
mBuilder.setContentTitle(getResources().getString(R.string.copying))
114114
.setSmallIcon(R.drawable.ic_content_copy_white_36dp);
@@ -148,7 +148,7 @@ protected Integer doInBackground(Bundle... p1) {
148148
}
149149
// setting up service watchers and initial data packages
150150
// finding total size on background thread (this is necessary condition for SMB!)
151-
totalSize = Futils.getTotalBytes(sourceFiles, c);
151+
totalSize = Futils.getTotalBytes(sourceFiles, ctx);
152152
totalSourceFiles = sourceFiles.size();
153153
progressHandler = new ProgressHandler(totalSourceFiles, totalSize);
154154

@@ -275,7 +275,7 @@ public void execute(final ArrayList<BaseFile> sourceFiles, final String targetPa
275275
// initial start of copy, initiate the watcher
276276
watcherUtil.watch();
277277

278-
if (FileUtil.checkFolder((targetPath), c) == 1) {
278+
if (FileUtil.checkFolder((targetPath), ctx) == 1) {
279279
for (int i = 0; i < sourceFiles.size(); i++) {
280280
sourceProgress = i;
281281
BaseFile f1 = (sourceFiles.get(i));
@@ -352,21 +352,23 @@ public void execute(final ArrayList<BaseFile> sourceFiles, final String targetPa
352352
if (!failedFOps.contains(a))
353353
toDelete.add(a);
354354
}
355-
new DeleteTask(c, null).execute((toDelete));//getContentResolver(),
355+
new DeleteTask(ctx, null).execute((toDelete));//getContentResolver(),
356356
}
357357
}
358358

359359
void copyRoot(BaseFile sourceFile, HFile targetFile, boolean move) {
360360

361361
try {
362-
if (!move) RootUtils.copy(sourceFile.getPath(), targetFile.getPath());
363-
else if (move) RootUtils.move(sourceFile.getPath(), targetFile.getPath());
362+
if (!move)
363+
RootUtils.copy(sourceFile.getPath(), targetFile.getPath());
364+
else if (move)
365+
RootUtils.move(sourceFile.getPath(), targetFile.getPath());
364366
ServiceWatcherUtil.POSITION += sourceFile.size;
365367
} catch (RootNotPermittedException e) {
366368
failedFOps.add(sourceFile);
367369
e.printStackTrace();
368370
}
369-
Futils.scanFile(targetFile.getPath(), c);
371+
Futils.scanFile(targetFile.getPath(), ctx);
370372
}
371373

372374
private void copyFiles(final BaseFile sourceFile, final HFile targetFile,
@@ -375,7 +377,7 @@ private void copyFiles(final BaseFile sourceFile, final HFile targetFile,
375377
if (sourceFile.isDirectory()) {
376378
if (progressHandler.getCancelled()) return;
377379

378-
if (!targetFile.exists()) targetFile.mkdir(c);
380+
if (!targetFile.exists()) targetFile.mkdir(ctx);
379381

380382
// various checks
381383
// 1. source file and target file doesn't end up in loop
@@ -388,7 +390,7 @@ private void copyFiles(final BaseFile sourceFile, final HFile targetFile,
388390
targetFile.setLastModified(sourceFile.lastModified());
389391

390392
if(progressHandler.getCancelled()) return;
391-
ArrayList<BaseFile> filePaths = sourceFile.listFiles(c, false);
393+
ArrayList<BaseFile> filePaths = sourceFile.listFiles(ctx, false);
392394
for (BaseFile file : filePaths) {
393395
HFile destFile = new HFile(targetFile.getMode(), targetFile.getPath(),
394396
file.getName(), file.isDirectory());
@@ -401,7 +403,7 @@ private void copyFiles(final BaseFile sourceFile, final HFile targetFile,
401403
return;
402404
}
403405

404-
GenericCopyUtil copyUtil = new GenericCopyUtil(c);
406+
GenericCopyUtil copyUtil = new GenericCopyUtil(ctx);
405407

406408
progressHandler.setFileName(sourceFile.getName());
407409
copyUtil.copy(sourceFile, targetFile, progressHandler);
@@ -424,10 +426,10 @@ void generateNotification(ArrayList<HFile> failedOps, boolean move) {
424426
if(failedOps.size()==0) return;
425427
Log.e("CopyService.generateNotification", failedOps + ", " + move);
426428

427-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(c);
428-
mBuilder.setContentTitle(c.getString(R.string.operationunsuccesful));
429-
mBuilder.setContentText(c.getString(R.string.copy_error).replace("%s",
430-
move ? c.getString(R.string.moved) : c.getString(R.string.copied)));
429+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
430+
mBuilder.setContentTitle(ctx.getString(R.string.operationunsuccesful));
431+
mBuilder.setContentText(ctx.getString(R.string.copy_error).replace("%s",
432+
move ? ctx.getString(R.string.moved) : ctx.getString(R.string.copied)));
431433
mBuilder.setAutoCancel(true);
432434

433435
progressHandler.setCancelled(true);
@@ -475,9 +477,9 @@ private void publishResults(int id, String fileName, int sourceFiles, int source
475477
mBuilder.setOngoing(true);
476478
int title = R.string.copying;
477479
if (move) title = R.string.moving;
478-
mBuilder.setContentTitle(c.getResources().getString(title));
479-
mBuilder.setContentText(fileName + " " + Formatter.formatFileSize(c, writtenSize) + "/" +
480-
Formatter.formatFileSize(c, totalSize));
480+
mBuilder.setContentTitle(ctx.getResources().getString(title));
481+
mBuilder.setContentText(fileName + " " + Formatter.formatFileSize(ctx, writtenSize) + "/" +
482+
Formatter.formatFileSize(ctx, totalSize));
481483
int id1 = Integer.parseInt("456" + id);
482484
mNotifyManager.notify(id1, mBuilder.build());
483485
if (writtenSize == totalSize || totalSize == 0) {

PowerFileExplorer/src/main/java/com/amaze/filemanager/services/asynctasks/CopyFileCheck.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,15 @@ class CopyNode {
302302
private ArrayList<BaseFile> filesToCopy, conflictingFiles;
303303
private ArrayList<CopyNode> nextNodes = new ArrayList<>();
304304

305-
CopyNode(String p, ArrayList<BaseFile> filesToCopy) {
305+
CopyNode(final String p, final ArrayList<BaseFile> filesToCopy) {
306306
path = p;
307307
this.filesToCopy = filesToCopy;
308308

309-
HFile destination = new HFile(openMode, path);
309+
final HFile destination = new HFile(openMode, path);
310310
conflictingFiles = checkConflicts(filesToCopy, destination);
311311

312-
for (int i = 0; i < conflictingFiles.size(); i++) {
312+
final int size = conflictingFiles.size();
313+
for (int i = 0; i < size; i++) {
313314
if (conflictingFiles.get(i).isDirectory()) {
314315
if (deleteCopiedFolder == null)
315316
deleteCopiedFolder = new ArrayList<>();

PowerFileExplorer/src/main/java/com/amaze/filemanager/services/asynctasks/MoveFiles.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ public MoveFiles(ArrayList<ArrayList<BaseFile>> files, Frag ma, Context context,
7373
protected Boolean doInBackground(ArrayList<String>... strings) {
7474
paths = strings[0];
7575

76-
if (files.size() == 0) return true;
77-
76+
if (files.size() == 0)
77+
return true;
78+
79+
final int size = paths.size();
7880
switch (mode) {
7981
case SMB:
80-
for (int i = 0; i < paths.size(); i++) {
82+
for (int i = 0; i < size; i++) {
8183
for (BaseFile f : files.get(i)) {
8284
try {
83-
SmbFile source = new SmbFile(f.getPath());
84-
SmbFile dest = new SmbFile(paths.get(i) + "/" + f.getName());
85+
final SmbFile source = new SmbFile(f.getPath());
86+
final SmbFile dest = new SmbFile(paths.get(i) + "/" + f.getName());
8587
source.renameTo(dest);
8688
} catch (MalformedURLException e) {
8789
e.printStackTrace();
@@ -94,10 +96,10 @@ protected Boolean doInBackground(ArrayList<String>... strings) {
9496
}
9597
break;
9698
case FILE:
97-
for (int i = 0; i < paths.size(); i++) {
99+
for (int i = 0; i < size; i++) {
98100
for (BaseFile f : files.get(i)) {
99-
File dest = new File(paths.get(i) + "/" + f.getName());
100-
File source = new File(f.getPath());
101+
final File dest = new File(paths.get(i) + "/" + f.getName());
102+
final File source = new File(f.getPath());
101103
if (!source.renameTo(dest)) {
102104

103105
// check if we have root
@@ -118,7 +120,7 @@ protected Boolean doInBackground(ArrayList<String>... strings) {
118120
case BOX:
119121
case ONEDRIVE:
120122
case GDRIVE:
121-
for (int i=0; i<paths.size(); i++) {
123+
for (int i=0; i < size; i++) {
122124
for (BaseFile baseFile : files.get(i)) {
123125

124126
DataUtils dataUtils = DataUtils.getInstance();

PowerFileExplorer/src/main/java/com/amaze/filemanager/utils/files/GenericCopyUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.amaze.filemanager.utils.ProgressHandler;
3333
import com.amaze.filemanager.services.DeleteTask;
3434
import java.util.ArrayList;
35+
import com.amaze.filemanager.exceptions.*;
3536

3637
/**
3738
* Created by vishal on 26/10/16.
@@ -209,6 +210,16 @@ private void startCopy(boolean lowOnMemory) throws IOException {
209210
} else {
210211
// copying normal file, target not in OTG
211212
targetFile = new File(mTargetFile.getPath());
213+
214+
///
215+
try {
216+
mTargetFile.delete(mContext, mTargetFile.isRoot());
217+
}
218+
catch (RootNotPermittedException e) {
219+
e.printStackTrace();
220+
}
221+
///
222+
212223
if (FileUtil.isWritable(targetFile)) {
213224

214225
if (lowOnMemory) {

0 commit comments

Comments
 (0)