lets really delete the selected files

This commit is contained in:
tibbi 2016-02-28 23:45:10 +01:00
parent ceecb21fa1
commit 07cbc6bedb
3 changed files with 29 additions and 4 deletions

View file

@ -3,7 +3,7 @@
package="com.simplemobiletools.gallery" package="com.simplemobiletools.gallery"
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View file

@ -2,10 +2,12 @@ package com.simplemobiletools.gallery.activities;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
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.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -19,6 +21,7 @@ import com.simplemobiletools.gallery.Helpers;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.adapters.PhotosAdapter; import com.simplemobiletools.gallery.adapters.PhotosAdapter;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -27,6 +30,8 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
private List<String> photos; private List<String> photos;
private int selectedItemsCnt; private int selectedItemsCnt;
private GridView gridView; private GridView gridView;
private String path;
private PhotosAdapter adapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -34,8 +39,8 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
setContentView(R.layout.activity_photos); setContentView(R.layout.activity_photos);
photos = new ArrayList<>(); photos = new ArrayList<>();
final String path = getIntent().getStringExtra(Constants.DIRECTORY); path = getIntent().getStringExtra(Constants.DIRECTORY);
final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos(path)); adapter = new PhotosAdapter(this, getPhotos());
gridView = (GridView) findViewById(R.id.photos_grid); gridView = (GridView) findViewById(R.id.photos_grid);
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this); gridView.setOnItemClickListener(this);
@ -45,7 +50,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
setTitle(dirName); setTitle(dirName);
} }
private List<String> getPhotos(final String path) { private List<String> getPhotos() {
photos.clear(); photos.clear();
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 ? ";
@ -68,6 +73,19 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
return photos; return photos;
} }
private void deleteSelectedItems() {
final SparseBooleanArray items = gridView.getCheckedItemPositions();
int cnt = items.size();
for (int i = 0; i < cnt; i++) {
final int id = items.keyAt(i);
new File(photos.get(id)).delete();
}
MediaScannerConnection.scanFile(this, new String[]{path}, null, null);
adapter.updateItems(getPhotos());
adapter.notifyDataSetChanged();
}
@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);
@ -102,6 +120,7 @@ public class PhotosActivity extends AppCompatActivity implements AdapterView.OnI
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();
mode.finish(); mode.finish();
return true; return true;
default: default:

View file

@ -55,6 +55,12 @@ public class PhotosAdapter extends BaseAdapter {
return 0; return 0;
} }
public void updateItems(List<String> newPhotos) {
photos.clear();
photos.addAll(newPhotos);
notifyDataSetChanged();
}
static class ViewHolder { static class ViewHolder {
ImageView photoThumbnail; ImageView photoThumbnail;