From 09732cd15d53641de94cf38ab8ebd75a2836dd13 Mon Sep 17 00:00:00 2001 From: Satyajit Pradhan Date: Mon, 18 Nov 2019 04:38:10 +0530 Subject: [PATCH] Fixed Image Delete Issue , TrashBin Issue and More Clicking on Delete Icon on the Toolbar now deletes all the images. Fixed issue when images of same name are added by changing the PrimaryKey from binPath to an auto_increment id. Fixed an issue in which files are not deleted from ".nomedia" directory. Also Updated some external library versions. Fixed Image Delete Issue , TrashBin Issue and More Clicking on Delete Icon on the Toolbar now deletes all the images. Fixed issue when images of same name are added by changing the PrimaryKey from binPath to an auto_increment id. Fixed an issue in which files are not deleted from ".nomedia" directory. Also Updated some external library versions. Update SingleMediaActivity.java Changed PrimaryKey from trashpath to id. And applied auto_increment of the id Removed Updated Comment Reverted Changes to build.gradle Removed Updated Comment Fixed Image Delete Issue , TrashBin Issue and More Clicking on Delete Icon on the Toolbar now deletes all the images. Fixed issue when images of same name are added by changing the PrimaryKey from binPath to an auto_increment id. Fixed an issue in which files are not deleted from ".nomedia" directory. Also Updated some external library versions. Fixed Image Delete Issue , TrashBin Issue and More Clicking on Delete Icon on the Toolbar now deletes all the images. Fixed issue when images of same name are added by changing the PrimaryKey from binPath to an auto_increment id. Fixed an issue in which files are not deleted from ".nomedia" directory. Also Updated some external library versions. Update SingleMediaActivity.java Changed PrimaryKey from trashpath to id. And applied auto_increment of the id Removed Updated Comment Removed Updated Comment Update build.gradle --- app/build.gradle | 2 +- .../data/local/TrashBinRealmModel.java | 21 +++++++++++++++- .../gallery/activities/LFMainActivity.java | 25 ++++++++++++++++--- .../activities/SingleMediaActivity.java | 10 +++++++- .../fossasia/phimpme/gallery/data/Album.java | 3 ++- .../phimpme/trashbin/TrashBinActivity.java | 16 +++++++++--- build.gradle | 2 +- 7 files changed, 68 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 19a777a0f..77c781e8d 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,7 +31,7 @@ android { defaultConfig { applicationId "org.fossasia.phimpme" minSdkVersion 21 - targetSdkVersion 29 + targetSdkVersion 28 versionCode 12 versionName '1.9.0' multiDexEnabled true diff --git a/app/src/main/java/org/fossasia/phimpme/data/local/TrashBinRealmModel.java b/app/src/main/java/org/fossasia/phimpme/data/local/TrashBinRealmModel.java index 997e343fc..f3b77f2a1 100644 --- a/app/src/main/java/org/fossasia/phimpme/data/local/TrashBinRealmModel.java +++ b/app/src/main/java/org/fossasia/phimpme/data/local/TrashBinRealmModel.java @@ -4,15 +4,34 @@ import io.realm.annotations.PrimaryKey; /** Created by saurav on 21/6/18. */ + +/* Used ID as PrimaryKey instead of filePath + * Since there might be cases that user deletes a file + * which has same name then that would cause a Duplicate PrimaryKey Exception + */ + public class TrashBinRealmModel extends RealmObject { - @PrimaryKey private String trashbinpath; + @PrimaryKey private int id; + private String trashbinpath; private String oldpath; private String datetime; private String timeperiod; public TrashBinRealmModel() {} + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public void setTrashbinpath(String trashbinpath) { + this.trashbinpath = trashbinpath; + } + public String getTrashbinpath() { return trashbinpath; } diff --git a/app/src/main/java/org/fossasia/phimpme/gallery/activities/LFMainActivity.java b/app/src/main/java/org/fossasia/phimpme/gallery/activities/LFMainActivity.java index 31789fda3..e62517cd9 100644 --- a/app/src/main/java/org/fossasia/phimpme/gallery/activities/LFMainActivity.java +++ b/app/src/main/java/org/fossasia/phimpme/gallery/activities/LFMainActivity.java @@ -3541,13 +3541,33 @@ private void addTrashObjectsToRealm(ArrayList media) { int index = media.get(i).getPath().lastIndexOf("/"); String name = media.get(i).getPath().substring(index + 1); realm.beginTransaction(); + Number currentIdNum = realm.where(TrashBinRealmModel.class).max("id"); + int nextId; + if (currentIdNum == null) { + nextId = 1; + } else { + nextId = currentIdNum.intValue() + 1; + } + String trashpath = trashbinpath + "/" + name; - TrashBinRealmModel trashBinRealmModel = - realm.createObject(TrashBinRealmModel.class, trashpath); + + /* Used ID as PrimaryKey instead of filePath + * Since there might be cases that user deletes a file + * which has same name then that would cause a Duplicate PrimaryKey Exception + */ + + TrashBinRealmModel trashBinRealmModel = realm.createObject(TrashBinRealmModel.class, nextId); + + // Sets the File Bin Path here + trashBinRealmModel.setTrashbinpath(trashpath); + trashBinRealmModel.setOldpath(media.get(i).getPath()); + trashBinRealmModel.setDatetime( new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date())); + trashBinRealmModel.setTimeperiod("null"); + realm.commitTransaction(); } } @@ -3661,7 +3681,6 @@ public Bitmap getBitmap(String path) { } in.close(); - Log.d(TAG, "bitmap size - width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight()); return bitmap; } catch (IOException e) { Log.e(TAG, e.getMessage(), e); diff --git a/app/src/main/java/org/fossasia/phimpme/gallery/activities/SingleMediaActivity.java b/app/src/main/java/org/fossasia/phimpme/gallery/activities/SingleMediaActivity.java index 1b086098a..8d50ef21a 100644 --- a/app/src/main/java/org/fossasia/phimpme/gallery/activities/SingleMediaActivity.java +++ b/app/src/main/java/org/fossasia/phimpme/gallery/activities/SingleMediaActivity.java @@ -1042,9 +1042,17 @@ private void addTrashObjectsToRealm(String mediaPath) { String trashbinpath = Environment.getExternalStorageDirectory() + "/" + ".nomedia"; realm = Realm.getDefaultInstance(); realm.beginTransaction(); + Number currentIdNum = realm.where(TrashBinRealmModel.class).max("id"); + int nextId; + if (currentIdNum == null) { + nextId = 1; + } else { + nextId = currentIdNum.intValue() + 1; + } String name = mediaPath.substring(mediaPath.lastIndexOf("/") + 1); String trashpath = trashbinpath + "/" + name; - TrashBinRealmModel trashBinRealmModel = realm.createObject(TrashBinRealmModel.class, trashpath); + TrashBinRealmModel trashBinRealmModel = realm.createObject(TrashBinRealmModel.class, nextId); + trashBinRealmModel.setTrashbinpath(trashpath); trashBinRealmModel.setOldpath(mediaPath); trashBinRealmModel.setDatetime(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date())); trashBinRealmModel.setTimeperiod("null"); diff --git a/app/src/main/java/org/fossasia/phimpme/gallery/data/Album.java b/app/src/main/java/org/fossasia/phimpme/gallery/data/Album.java index df96ff15e..1d690c62a 100644 --- a/app/src/main/java/org/fossasia/phimpme/gallery/data/Album.java +++ b/app/src/main/java/org/fossasia/phimpme/gallery/data/Album.java @@ -460,7 +460,8 @@ public void onScanCompleted(String s, Uri uri) { Log.d("scanFile", "onScanCompleted: " + s); } }); - media.remove(albummedia.get(i)); + // This was Causing a change in the length of albummedia + // media.remove(albummedia.get(i)); n++; } } diff --git a/app/src/main/java/org/fossasia/phimpme/trashbin/TrashBinActivity.java b/app/src/main/java/org/fossasia/phimpme/trashbin/TrashBinActivity.java index ff57a6628..c253d6513 100644 --- a/app/src/main/java/org/fossasia/phimpme/trashbin/TrashBinActivity.java +++ b/app/src/main/java/org/fossasia/phimpme/trashbin/TrashBinActivity.java @@ -152,7 +152,9 @@ private void trashEmptyViewSetup() { private ArrayList getTrashObjects() { ArrayList list = new ArrayList<>(); final ArrayList toDelete = new ArrayList<>(); + for (int i = 0; i < trashBinRealmModelRealmQuery.count(); i++) { + if (new File(trashBinRealmModelRealmQuery.findAll().get(i).getTrashbinpath()).exists()) { list.add(trashBinRealmModelRealmQuery.findAll().get(i)); @@ -161,14 +163,15 @@ private ArrayList getTrashObjects() { } } for (int i = 0; i < toDelete.size(); i++) { - final String path = toDelete.get(i).getTrashbinpath(); + final int id = toDelete.get(i).getId(); Realm realm = Realm.getDefaultInstance(); realm.executeTransaction( new Realm.Transaction() { @Override public void execute(Realm realm) { RealmResults realmResults = - realm.where(TrashBinRealmModel.class).equalTo("trashbinpath", path).findAll(); + realm.where(TrashBinRealmModel.class).equalTo("id", id).findAll(); + // Deleting using the id since its the new PrimaryKey realmResults.deleteAllFromRealm(); } }); @@ -297,8 +300,15 @@ public void execute(Realm realm) { } }); File binfolder = new File(Environment.getExternalStorageDirectory() + "/" + ".nomedia"); + if (binfolder.exists()) { - binfolder.delete(); + + // Since bin is not empty we will perform recursive delete on the folder + + String[] children = binfolder.list(); + for (int i = 0; i < children.length; i++) { + new File(binfolder, children[i]).delete(); + } } return null; } diff --git a/build.gradle b/build.gradle index 464b00e68..8b49850e0 100755 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' + classpath 'com.android.tools.build:gradle:3.5.2' classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4' classpath "io.realm:realm-gradle-plugin:$realm_version" classpath "gradle.plugin.com.github.b3er.local.properties:local-properties-plugin:1.1"