Skip to content

Commit

Permalink
Cut, copy and paste implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciotogneri committed Oct 1, 2016
1 parent 4580555 commit 9ed5fdf
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,42 @@ public void onCopy()

public void onPaste()
{
// TODO: show dialog
mainActivity.clipboard().paste();
unselectAll();
final Clipboard clipboard = mainActivity.clipboard();

String message = "";

if (clipboard.isCut())
{
message = getString(R.string.clipboard_cut);
}
else if (clipboard.isCopy())
{
message = getString(R.string.clipboard_copy);
}

final ProgressDialog dialog = new ProgressDialog(getContext());
dialog.setMessage(message);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();

new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
clipboard.paste(new FileInfo(folder()));

return null;
}

@Override
protected void onPostExecute(Void result)
{
dialog.dismiss();
refreshFolder();
}
}.execute();
}

public void onSelectAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public Clipboard()
{
}

public boolean isCut()
{
return mode == Mode.CUT;
}

public boolean isCopy()
{
return mode == Mode.COPY;
}

public void cut(List<FileInfo> items)
{
mode = Mode.CUT;
Expand All @@ -32,12 +42,32 @@ public void copy(List<FileInfo> items)
this.items = items;
}

public void paste()
public boolean paste(FileInfo target)
{
// TODO
boolean allPasted = true;

try
{
for (FileInfo fileInfo : items)
{
allPasted &= fileInfo.copy(target);

if (mode == Mode.CUT)
{
fileInfo.delete();
}
}
}
catch (Exception e)
{
// ignore
e.printStackTrace();
}

items.clear();
mode = Mode.NONE;

return allPasted;
}

public boolean isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -64,6 +69,82 @@ public boolean rename(String newName)
return !newFile.exists() && file.renameTo(newFile);
}

public boolean copy(FileInfo target)
{
if (isDirectory())
{
boolean allCopied = true;

for (File currentFile : children())
{
if (currentFile != null)
{
FileInfo fileInfo = new FileInfo(currentFile);

allCopied &= fileInfo.copy(target);
}
}

return allCopied;
}
else
{
return copy(file, target.file);
}
}

private boolean copy(File source, File destination)
{
File target = new File(destination, source.getName());

InputStream inputStream = null;
OutputStream outputStream = null;

try
{
inputStream = new FileInputStream(source);
outputStream = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}

outputStream.flush();

return true;
}
catch (Exception e)
{
// ignore
e.printStackTrace();

return false;
}
finally
{
close(inputStream);
close(outputStream);
}
}

private void close(Closeable closeable)
{
try
{
if (closeable != null)
{
closeable.close();
}
}
catch (Exception e)
{
// ignore
}
}

public boolean delete()
{
if (isDirectory())
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

<string name="rename.error">Error renaming</string>

<string name="clipboard.cut">Moving…</string>
<string name="clipboard.copy">Copying…</string>

<string name="delete.confirm">Do you want to delete the selected items?</string>
<string name="delete.deleting">Deleting…</string>
<string name="delete.error">Error deleting</string>
Expand Down

0 comments on commit 9ed5fdf

Please sign in to comment.