delete empty directories after deleting all photos

This commit is contained in:
tibbi 2016-02-29 22:51:28 +01:00
parent 52051ca77a
commit a2f508378e
4 changed files with 90 additions and 41 deletions

View file

@ -1,7 +1,14 @@
package com.simplemobiletools.gallery; package com.simplemobiletools.gallery;
import android.content.Context;
import android.widget.Toast;
public class Helpers { public class Helpers {
public static String getFilename(final String path) { public static String getFilename(final String path) {
return path.substring(path.lastIndexOf("/") + 1); 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();
}
} }

View file

@ -31,7 +31,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; 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 final int STORAGE_PERMISSION = 1;
private List<String> photos; private List<String> photos;
private int selectedItemsCnt; private int selectedItemsCnt;
@ -75,6 +76,9 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
private void initializeGallery() { private void initializeGallery() {
path = getIntent().getStringExtra(Constants.DIRECTORY); path = getIntent().getStringExtra(Constants.DIRECTORY);
photos = getPhotos(); photos = getPhotos();
if (isDirEmpty())
return;
adapter = new PhotosAdapter(this, photos); adapter = new PhotosAdapter(this, photos);
gridView = (GridView) findViewById(R.id.photos_grid); gridView = (GridView) findViewById(R.id.photos_grid);
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
@ -85,6 +89,13 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
setTitle(dirName); setTitle(dirName);
} }
private void deleteDirectoryIfEmpty() {
final File file = new File(path);
if (file.isDirectory() && file.listFiles().length == 0) {
file.delete();
}
}
private List<String> getPhotos() { private List<String> getPhotos() {
List<String> myPhotos = new ArrayList<>(); List<String> myPhotos = new ArrayList<>();
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
@ -108,7 +119,17 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
return myPhotos; return myPhotos;
} }
private boolean isDirEmpty() {
if (photos.size() <= 0) {
deleteDirectoryIfEmpty();
finish();
return true;
}
return false;
}
private void deleteSelectedItems() { private void deleteSelectedItems() {
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++) {
@ -117,18 +138,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
file.delete(); file.delete();
} }
MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() { MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
@Override
public void onScanCompleted(final String path, final Uri uri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
photos = getPhotos();
adapter.updateItems(photos);
}
});
}
});
} }
@Override @Override
@ -177,4 +187,17 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
public void onDestroyActionMode(ActionMode mode) { public void onDestroyActionMode(ActionMode mode) {
selectedItemsCnt = 0; selectedItemsCnt = 0;
} }
@Override
public void onScanCompleted(String path, Uri uri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
photos = getPhotos();
if (!isDirEmpty()) {
adapter.updateItems(photos);
}
}
});
}
} }

View file

@ -24,13 +24,15 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; 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 int pos;
private boolean isFullScreen; private boolean isFullScreen;
private ActionBar actionbar; private ActionBar actionbar;
private List<String> photos; private List<String> photos;
private MyViewPager pager; private MyViewPager pager;
private String path; private String path;
private String directory;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -43,23 +45,19 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
hideSystemUI(); hideSystemUI();
path = getIntent().getStringExtra(Constants.PHOTO); path = getIntent().getStringExtra(Constants.PHOTO);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); directory = new File(path).getParent();
pager = (MyViewPager) findViewById(R.id.view_pager); pager = (MyViewPager) findViewById(R.id.view_pager);
photos = getPhotos(); photos = getPhotos();
if (isDirEmpty())
return;
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
adapter.setPaths(photos); adapter.setPaths(photos);
pager.setAdapter(adapter); pager.setAdapter(adapter);
pager.setCurrentItem(pos); pager.setCurrentItem(pos);
pager.addOnPageChangeListener(this); pager.addOnPageChangeListener(this);
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
isFullScreen = false;
}
}
});
updateActionbarTitle(); updateActionbarTitle();
} }
@ -95,31 +93,28 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
} }
private void deleteImage() { private void deleteImage() {
Helpers.showToast(this, R.string.deleting);
final String path = photos.get(pager.getCurrentItem()); final String path = photos.get(pager.getCurrentItem());
final File file = new File(path); final File file = new File(path);
file.delete(); file.delete();
MediaScannerConnection.scanFile(this, new String[]{path}, null, this);
}
MediaScannerConnection.scanFile(this, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() { private boolean isDirEmpty() {
@Override if (photos.size() <= 0) {
public void onScanCompleted(final String path, final Uri uri) { deleteDirectoryIfEmpty();
runOnUiThread(new Runnable() { finish();
@Override return true;
public void run() { }
reloadViewPager(); return false;
}
});
}
});
} }
private void reloadViewPager() { private void reloadViewPager() {
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter(); final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
final int pos = pager.getCurrentItem(); final int pos = pager.getCurrentItem();
photos = getPhotos(); photos = getPhotos();
if (photos.size() <= 0) { if (isDirEmpty())
finish();
return; return;
}
pager.setAdapter(null); pager.setAdapter(null);
adapter.updateItems(photos); adapter.updateItems(photos);
@ -130,16 +125,22 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
updateActionbarTitle(); updateActionbarTitle();
} }
private void deleteDirectoryIfEmpty() {
final File file = new File(directory);
if (file.isDirectory() && file.listFiles().length == 0) {
file.delete();
}
}
private List<String> getPhotos() { private List<String> getPhotos() {
final List<String> photos = new ArrayList<>(); final List<String> photos = new ArrayList<>();
final String fileDir = new File(path).getParent();
final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String where = MediaStore.Images.Media.DATA + " like ? "; 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[] columns = {MediaStore.Images.Media.DATA};
final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC"; final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC";
final Cursor cursor = getContentResolver().query(uri, columns, where, args, order); 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; int i = 0;
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@ -208,4 +209,21 @@ public class ViewPagerActivity extends AppCompatActivity implements ViewPager.On
public void onPageScrollStateChanged(int state) { 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();
}
});
}
} }

View file

@ -3,4 +3,5 @@
<string name="share_via">Share via</string> <string name="share_via">Share via</string>
<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>
</resources> </resources>