From a2f508378ea405118cb00727e04d56e8b5c9e18a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 29 Feb 2016 22:51:28 +0100 Subject: [PATCH] delete empty directories after deleting all photos --- .../simplemobiletools/gallery/Helpers.java | 7 ++ .../gallery/activities/PhotosActivity.java | 49 ++++++++---- .../gallery/activities/ViewPagerActivity.java | 74 ++++++++++++------- app/src/main/res/values/strings.xml | 1 + 4 files changed, 90 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Helpers.java b/app/src/main/java/com/simplemobiletools/gallery/Helpers.java index 682fae675..8fbaab5c6 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Helpers.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Helpers.java @@ -1,7 +1,14 @@ package com.simplemobiletools.gallery; +import android.content.Context; +import android.widget.Toast; + public class Helpers { public static String getFilename(final String path) { return path.substring(path.lastIndexOf("/") + 1); } + + public static void showToast(Context context, int resId) { + Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); + } } 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 2489f1d54..3fc068427 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotosActivity.java @@ -31,7 +31,8 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; -public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener { +public class PhotosActivity extends AppCompatActivity + implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, MediaScannerConnection.OnScanCompletedListener { private final int STORAGE_PERMISSION = 1; private List photos; private int selectedItemsCnt; @@ -75,6 +76,9 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI private void initializeGallery() { path = getIntent().getStringExtra(Constants.DIRECTORY); photos = getPhotos(); + if (isDirEmpty()) + return; + adapter = new PhotosAdapter(this, photos); gridView = (GridView) findViewById(R.id.photos_grid); gridView.setAdapter(adapter); @@ -85,6 +89,13 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI setTitle(dirName); } + private void deleteDirectoryIfEmpty() { + final File file = new File(path); + if (file.isDirectory() && file.listFiles().length == 0) { + file.delete(); + } + } + private List getPhotos() { List myPhotos = new ArrayList<>(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; @@ -108,7 +119,17 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI return myPhotos; } + private boolean isDirEmpty() { + if (photos.size() <= 0) { + deleteDirectoryIfEmpty(); + finish(); + return true; + } + return false; + } + private void deleteSelectedItems() { + Helpers.showToast(this, R.string.deleting); final SparseBooleanArray items = gridView.getCheckedItemPositions(); int cnt = items.size(); for (int i = 0; i < cnt; i++) { @@ -117,18 +138,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI file.delete(); } - MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() { - @Override - public void onScanCompleted(final String path, final Uri uri) { - runOnUiThread(new Runnable() { - @Override - public void run() { - photos = getPhotos(); - adapter.updateItems(photos); - } - }); - } - }); + MediaScannerConnection.scanFile(this, new String[]{path}, null, this); } @Override @@ -177,4 +187,17 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI public void onDestroyActionMode(ActionMode mode) { selectedItemsCnt = 0; } + + @Override + public void onScanCompleted(String path, Uri uri) { + runOnUiThread(new Runnable() { + @Override + public void run() { + photos = getPhotos(); + if (!isDirEmpty()) { + adapter.updateItems(photos); + } + } + }); + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index da357ccf6..9c2f451cd 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -24,13 +24,15 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; -public class ViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { +public class ViewPagerActivity extends AppCompatActivity + implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, MediaScannerConnection.OnScanCompletedListener { private int pos; private boolean isFullScreen; private ActionBar actionbar; private List photos; private MyViewPager pager; private String path; + private String directory; @Override protected void onCreate(Bundle savedInstanceState) { @@ -43,23 +45,19 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On hideSystemUI(); path = getIntent().getStringExtra(Constants.PHOTO); - final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); + directory = new File(path).getParent(); pager = (MyViewPager) findViewById(R.id.view_pager); photos = getPhotos(); + if (isDirEmpty()) + return; + + final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); adapter.setPaths(photos); pager.setAdapter(adapter); pager.setCurrentItem(pos); pager.addOnPageChangeListener(this); - getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { - @Override - public void onSystemUiVisibilityChange(int visibility) { - if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { - isFullScreen = false; - } - } - }); - + getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this); updateActionbarTitle(); } @@ -95,31 +93,28 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On } private void deleteImage() { + Helpers.showToast(this, R.string.deleting); final String path = photos.get(pager.getCurrentItem()); final File file = new File(path); file.delete(); + MediaScannerConnection.scanFile(this, new String[]{path}, null, this); + } - MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() { - @Override - public void onScanCompleted(final String path, final Uri uri) { - runOnUiThread(new Runnable() { - @Override - public void run() { - reloadViewPager(); - } - }); - } - }); + private boolean isDirEmpty() { + if (photos.size() <= 0) { + deleteDirectoryIfEmpty(); + finish(); + return true; + } + return false; } private void reloadViewPager() { final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter(); final int pos = pager.getCurrentItem(); photos = getPhotos(); - if (photos.size() <= 0) { - finish(); + if (isDirEmpty()) return; - } pager.setAdapter(null); adapter.updateItems(photos); @@ -130,16 +125,22 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On updateActionbarTitle(); } + private void deleteDirectoryIfEmpty() { + final File file = new File(directory); + if (file.isDirectory() && file.listFiles().length == 0) { + file.delete(); + } + } + private List getPhotos() { final List photos = new ArrayList<>(); - final String fileDir = new File(path).getParent(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final String where = MediaStore.Images.Media.DATA + " like ? "; - final String[] args = new String[]{fileDir + "%"}; + final String[] args = new String[]{directory + "%"}; final String[] columns = {MediaStore.Images.Media.DATA}; final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC"; final Cursor cursor = getContentResolver().query(uri, columns, where, args, order); - final String pattern = Pattern.quote(fileDir) + "/[^/]*"; + final String pattern = Pattern.quote(directory) + "/[^/]*"; int i = 0; if (cursor != null && cursor.moveToFirst()) { @@ -208,4 +209,21 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On public void onPageScrollStateChanged(int state) { } + + @Override + public void onSystemUiVisibilityChange(int visibility) { + if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { + isFullScreen = false; + } + } + + @Override + public void onScanCompleted(String path, Uri uri) { + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadViewPager(); + } + }); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 91f6b62c1..01ab65910 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,4 +3,5 @@ Share via Not much to do in a gallery without accessing your photos Remove + Deleting