allow Undoing file deletion
This commit is contained in:
parent
a2f508378e
commit
b8f4bcbd0a
4 changed files with 90 additions and 20 deletions
|
@ -23,6 +23,7 @@ dependencies {
|
||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
compile 'com.android.support:appcompat-v7:23.1.1'
|
compile 'com.android.support:appcompat-v7:23.1.1'
|
||||||
|
compile 'com.android.support:design:23.1.1'
|
||||||
compile 'com.github.bumptech.glide:glide:3.7.0'
|
compile 'com.github.bumptech.glide:glide:3.7.0'
|
||||||
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.4.1'
|
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.4.1'
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,13 @@ import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
import android.support.design.widget.CoordinatorLayout;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
@ -16,6 +19,7 @@ import android.view.ActionMode;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.GridView;
|
import android.widget.GridView;
|
||||||
|
@ -32,13 +36,17 @@ import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class PhotosActivity extends AppCompatActivity
|
public class PhotosActivity extends AppCompatActivity
|
||||||
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener {
|
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener,
|
||||||
|
GridView.OnTouchListener {
|
||||||
private final int STORAGE_PERMISSION = 1;
|
private final int STORAGE_PERMISSION = 1;
|
||||||
private List<String> photos;
|
private List<String> photos;
|
||||||
private int selectedItemsCnt;
|
private int selectedItemsCnt;
|
||||||
private GridView gridView;
|
private GridView gridView;
|
||||||
private String path;
|
private String path;
|
||||||
private PhotosAdapter adapter;
|
private PhotosAdapter adapter;
|
||||||
|
private Snackbar snackbar;
|
||||||
|
private boolean isSnackbarShown;
|
||||||
|
private List<String> toBeDeleted;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -74,6 +82,7 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeGallery() {
|
private void initializeGallery() {
|
||||||
|
toBeDeleted = new ArrayList<>();
|
||||||
path = getIntent().getStringExtra(Constants.DIRECTORY);
|
path = getIntent().getStringExtra(Constants.DIRECTORY);
|
||||||
photos = getPhotos();
|
photos = getPhotos();
|
||||||
if (isDirEmpty())
|
if (isDirEmpty())
|
||||||
|
@ -84,6 +93,8 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
gridView.setAdapter(adapter);
|
gridView.setAdapter(adapter);
|
||||||
gridView.setOnItemClickListener(this);
|
gridView.setOnItemClickListener(this);
|
||||||
gridView.setMultiChoiceModeListener(this);
|
gridView.setMultiChoiceModeListener(this);
|
||||||
|
gridView.setOnTouchListener(this);
|
||||||
|
isSnackbarShown = false;
|
||||||
|
|
||||||
final String dirName = Helpers.getFilename(path);
|
final String dirName = Helpers.getFilename(path);
|
||||||
setTitle(dirName);
|
setTitle(dirName);
|
||||||
|
@ -110,7 +121,7 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
|
||||||
do {
|
do {
|
||||||
final String curPath = cursor.getString(pathIndex);
|
final String curPath = cursor.getString(pathIndex);
|
||||||
if (curPath.matches(pattern)) {
|
if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) {
|
||||||
myPhotos.add(cursor.getString(pathIndex));
|
myPhotos.add(cursor.getString(pathIndex));
|
||||||
}
|
}
|
||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
|
@ -128,19 +139,56 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteSelectedItems() {
|
private void prepareForDeleting() {
|
||||||
Helpers.showToast(this, R.string.deleting);
|
Helpers.showToast(this, R.string.deleting);
|
||||||
final SparseBooleanArray items = gridView.getCheckedItemPositions();
|
final SparseBooleanArray items = gridView.getCheckedItemPositions();
|
||||||
int cnt = items.size();
|
int cnt = items.size();
|
||||||
for (int i = 0; i < cnt; i++) {
|
for (int i = 0; i < cnt; i++) {
|
||||||
final int id = items.keyAt(i);
|
final int id = items.keyAt(i);
|
||||||
final File file = new File(photos.get(id));
|
toBeDeleted.add(photos.get(id));
|
||||||
file.delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notifyDeletion(toBeDeleted.size());
|
||||||
MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
|
MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void notifyDeletion(int cnt) {
|
||||||
|
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
|
||||||
|
final String msg = getResources().getQuantityString(R.plurals.files_deleted, cnt, cnt);
|
||||||
|
snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE);
|
||||||
|
snackbar.setAction(getResources().getString(R.string.undo), undoDeletion);
|
||||||
|
snackbar.setActionTextColor(Color.WHITE);
|
||||||
|
snackbar.show();
|
||||||
|
isSnackbarShown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteFiles() {
|
||||||
|
for (String delPath : toBeDeleted) {
|
||||||
|
final File file = new File(delPath);
|
||||||
|
if (file.exists())
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
MediaScannerConnection.scanFile(this, new String[]{path}, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private View.OnClickListener undoDeletion = new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
snackbar.dismiss();
|
||||||
|
isSnackbarShown = false;
|
||||||
|
toBeDeleted.clear();
|
||||||
|
updateGridView();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private void updateGridView() {
|
||||||
|
photos = getPhotos();
|
||||||
|
if (!isDirEmpty()) {
|
||||||
|
adapter.updateItems(photos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
final Intent intent = new Intent(this, ViewPagerActivity.class);
|
final Intent intent = new Intent(this, ViewPagerActivity.class);
|
||||||
|
@ -175,7 +223,7 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.cab_remove:
|
case R.id.cab_remove:
|
||||||
deleteSelectedItems();
|
prepareForDeleting();
|
||||||
mode.finish();
|
mode.finish();
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
|
@ -193,11 +241,19 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
photos = getPhotos();
|
updateGridView();
|
||||||
if (!isDirEmpty()) {
|
|
||||||
adapter.updateItems(photos);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
if (isSnackbarShown) {
|
||||||
|
deleteFiles();
|
||||||
|
snackbar.dismiss();
|
||||||
|
isSnackbarShown = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,20 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<GridView
|
<android.support.design.widget.CoordinatorLayout
|
||||||
android:id="@+id/photos_grid"
|
android:id="@+id/coordinator_layout"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent">
|
||||||
android:background="@android:color/black"
|
|
||||||
android:choiceMode="multipleChoiceModal"
|
<GridView
|
||||||
android:columnWidth="@dimen/photo_tmb_size"
|
android:id="@+id/photos_grid"
|
||||||
android:horizontalSpacing="1dp"
|
android:layout_width="match_parent"
|
||||||
android:numColumns="auto_fit"
|
android:layout_height="match_parent"
|
||||||
android:stretchMode="columnWidth"
|
android:background="@android:color/black"
|
||||||
android:verticalSpacing="1dp"/>
|
android:choiceMode="multipleChoiceModal"
|
||||||
|
android:columnWidth="@dimen/photo_tmb_size"
|
||||||
|
android:horizontalSpacing="1dp"
|
||||||
|
android:numColumns="auto_fit"
|
||||||
|
android:stretchMode="columnWidth"
|
||||||
|
android:verticalSpacing="1dp"/>
|
||||||
|
|
||||||
|
</android.support.design.widget.CoordinatorLayout>
|
||||||
|
|
|
@ -4,4 +4,10 @@
|
||||||
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
|
<string name="no_permissions">Not much to do in a gallery without accessing your photos</string>
|
||||||
<string name="remove">Remove</string>
|
<string name="remove">Remove</string>
|
||||||
<string name="deleting">Deleting</string>
|
<string name="deleting">Deleting</string>
|
||||||
|
<string name="undo">Undo</string>
|
||||||
|
|
||||||
|
<plurals name="files_deleted">
|
||||||
|
<item quantity="one">1 file deleted</item>
|
||||||
|
<item quantity="other">%1$d files deleted</item>
|
||||||
|
</plurals>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in a new issue