From e03cf077618b7fac89cf4174c92675032d3cc091 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Mar 2016 21:45:48 +0100 Subject: [PATCH] allow deleting full directories --- .../gallery/activities/MainActivity.java | 103 +++++++++++++++++- .../gallery/activities/PhotosActivity.java | 5 +- .../gallery/adapters/DirectoryAdapter.java | 8 +- app/src/main/res/layout/activity_main.xml | 27 +++-- app/src/main/res/values/strings.xml | 5 + 5 files changed, 132 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index e7287bfda..ee0e5d473 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -3,18 +3,24 @@ package com.simplemobiletools.gallery.activities; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.res.Resources; import android.database.Cursor; +import android.graphics.Color; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; 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.content.ContextCompat; import android.support.v7.app.AppCompatActivity; +import android.util.SparseBooleanArray; import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; @@ -33,11 +39,14 @@ import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity - implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener { + implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener { private final int STORAGE_PERMISSION = 1; private List dirs; private GridView gridView; private int selectedItemsCnt; + private Snackbar snackbar; + private boolean isSnackbarShown; + private List toBeDeleted; @Override protected void onCreate(Bundle savedInstanceState) { @@ -51,6 +60,12 @@ public class MainActivity extends AppCompatActivity tryloadGallery(); } + @Override + protected void onPause() { + super.onPause(); + deleteDirs(); + } + private void tryloadGallery() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { initializeGallery(); @@ -73,6 +88,7 @@ public class MainActivity extends AppCompatActivity } private void initializeGallery() { + toBeDeleted = new ArrayList<>(); dirs = new ArrayList<>(getDirectories().values()); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); @@ -80,6 +96,7 @@ public class MainActivity extends AppCompatActivity gridView.setAdapter(adapter); gridView.setOnItemClickListener(this); gridView.setMultiChoiceModeListener(this); + gridView.setOnTouchListener(this); } private Map getDirectories() { @@ -100,7 +117,7 @@ public class MainActivity extends AppCompatActivity final Directory directory = directories.get(fileDir); final int newImageCnt = directory.getPhotoCnt() + 1; directory.setPhotoCnt(newImageCnt); - } else { + } else if (!toBeDeleted.contains(fileDir)) { final String dirName = Helpers.getFilename(fileDir); directories.put(fileDir, new Directory(fileDir, path, dirName, 1)); } @@ -111,6 +128,73 @@ public class MainActivity extends AppCompatActivity return directories; } + private void prepareForDeleting() { + Helpers.showToast(this, R.string.deleting); + final SparseBooleanArray items = gridView.getCheckedItemPositions(); + int cnt = items.size(); + for (int i = 0; i < cnt; i++) { + final int id = items.keyAt(i); + final String path = dirs.get(id).getPath(); + toBeDeleted.add(path); + } + + notifyDeletion(cnt); + } + + private void notifyDeletion(int cnt) { + dirs = new ArrayList<>(getDirectories().values()); + + final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); + final Resources res = getResources(); + final String msg = res.getQuantityString(R.plurals.folders_deleted, cnt, cnt); + snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE); + snackbar.setAction(res.getString(R.string.undo), undoDeletion); + snackbar.setActionTextColor(Color.WHITE); + snackbar.show(); + isSnackbarShown = true; + updateGridView(); + } + + private void deleteDirs() { + if (toBeDeleted.isEmpty()) + return; + + if (snackbar != null) { + snackbar.dismiss(); + } + + isSnackbarShown = false; + + final List updatedFiles = new ArrayList<>(); + for (String delPath : toBeDeleted) { + final File dir = new File(delPath); + final File[] files = dir.listFiles(); + for (File f : files) { + updatedFiles.add(f.getAbsolutePath()); + f.delete(); + } + } + + final String[] deletedPaths = updatedFiles.toArray(new String[updatedFiles.size()]); + MediaScannerConnection.scanFile(this, deletedPaths, null, null); + } + + private View.OnClickListener undoDeletion = new View.OnClickListener() { + @Override + public void onClick(View v) { + snackbar.dismiss(); + isSnackbarShown = false; + toBeDeleted.clear(); + dirs = new ArrayList<>(getDirectories().values()); + updateGridView(); + } + }; + + private void updateGridView() { + final DirectoryAdapter adapter = (DirectoryAdapter) gridView.getAdapter(); + adapter.updateItems(dirs); + } + @Override public void onItemClick(AdapterView parent, View view, int position, long id) { final Intent intent = new Intent(this, PhotosActivity.class); @@ -145,7 +229,14 @@ public class MainActivity extends AppCompatActivity @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - return false; + switch (item.getItemId()) { + case R.id.cab_remove: + prepareForDeleting(); + mode.finish(); + return true; + default: + return false; + } } @Override @@ -154,7 +245,11 @@ public class MainActivity extends AppCompatActivity } @Override - public void onScanCompleted(String path, Uri uri) { + public boolean onTouch(View v, MotionEvent event) { + if (isSnackbarShown) { + deleteDirs(); + } + return false; } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java index c19f0193e..4b286b971 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -114,7 +114,7 @@ public class PhotosActivity extends AppCompatActivity } private List getPhotos() { - List myPhotos = new ArrayList<>(); + final List myPhotos = new ArrayList<>(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final String where = MediaStore.Images.Media.DATA + " like ? "; final String[] args = new String[]{path + "%"}; @@ -177,6 +177,9 @@ public class PhotosActivity extends AppCompatActivity } private void deleteFiles() { + if (toBeDeleted.isEmpty()) + return; + if (snackbar != null) { snackbar.dismiss(); } diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java b/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java index 3396062c0..1ebf38202 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/DirectoryAdapter.java @@ -36,7 +36,7 @@ public class DirectoryAdapter extends BaseAdapter { holder = (ViewHolder) view.getTag(); } - Directory dir = dirs.get(position); + final Directory dir = dirs.get(position); holder.dirName.setText(dir.getName()); holder.photoCnt.setText(String.valueOf(dir.getPhotoCnt())); Glide.with(context).load(dir.getThumbnail()).placeholder(R.color.tmb_background).centerCrop().crossFade().into(holder.dirThumbnail); @@ -59,6 +59,12 @@ public class DirectoryAdapter extends BaseAdapter { return 0; } + public void updateItems(List newDirs) { + dirs.clear(); + dirs.addAll(newDirs); + notifyDataSetChanged(); + } + static class ViewHolder { TextView dirName; TextView photoCnt; diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5e7593f66..8ce1ae914 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,13 +1,20 @@ - + android:layout_height="match_parent"> + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index caf630520..6caf8ae99 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,6 +12,11 @@ Extension deleted + + 1 folder deleted + %1$d folders deleted + + 1 file deleted %1$d files deleted