Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues related to Delete Icon inside album and TrashBin issue. #2918

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3541,13 +3541,33 @@ private void addTrashObjectsToRealm(ArrayList<Media> 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();
}
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ private void trashEmptyViewSetup() {
private ArrayList<TrashBinRealmModel> getTrashObjects() {
ArrayList<TrashBinRealmModel> list = new ArrayList<>();
final ArrayList<TrashBinRealmModel> 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));

Expand All @@ -161,14 +163,15 @@ private ArrayList<TrashBinRealmModel> 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<TrashBinRealmModel> 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();
}
});
Expand Down Expand Up @@ -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;
}
Expand Down