Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldsht committed Jun 16, 2016
1 parent 0510f25 commit 1df15a8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 128 deletions.
123 changes: 44 additions & 79 deletions app/src/main/java/com/horaapps/leafpic/Base/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

import com.horaapps.leafpic.Adapters.PhotosAdapter;
import com.horaapps.leafpic.MyApplication;
import com.horaapps.leafpic.R;
import com.horaapps.leafpic.utils.ContentHelper;
import com.horaapps.leafpic.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* Created by dnld on 26/04/16.
Expand All @@ -33,38 +27,35 @@ public class Album {
private String path = null;
private int count = -1;
private boolean selected = false;
private int filter_photos;
private int filter;
public AlbumSettings settings = new AlbumSettings();
private int current = -1;
private int currentMediaIndex = 0;

private String storageRootPath;

public ArrayList<Media> media = new ArrayList<Media>();
public ArrayList<Media> selectedMedias = new ArrayList<Media>();

public Album() {
media = new ArrayList<Media>();
selectedMedias = new ArrayList<Media>();
}
public Album() { }

public Album(String path, String name, int count, String storageRootPath) {
public Album(String path, String name) {
media = new ArrayList<Media>();
selectedMedias = new ArrayList<Media>();
this.path = path;
this.name = name;
this.count = count;
}

public Album(String path, String name, int count, String storageRootPath) {
this(path, name, count);
this.storageRootPath = storageRootPath;
}

public Album(String path, String name, int count) {
media = new ArrayList<Media>();
selectedMedias = new ArrayList<Media>();
this.path = path;
this.name = name;
this(path, name);
this.count = count;
}

public Album(Context context, File mediaPath) {
public Album(Context context, @NotNull File mediaPath) {
File folder = mediaPath.getParentFile();
media = new ArrayList<Media>();
selectedMedias = new ArrayList<Media>();
Expand All @@ -81,17 +72,10 @@ public Album(Context context, Uri mediaUri) {
setCurrentPhotoIndex(0);
}

public Album(String path, String name) {
media = new ArrayList<Media>();
selectedMedias = new ArrayList<Media>();
this.path = path;
this.name = name;
}

public void updatePhotos(Context context) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(context);
ArrayList<Media> mediaArrayList = new ArrayList<Media>();
File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter_photos, SP.getBoolean("set_include_video",true)));
File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter, SP.getBoolean("set_include_video",true)));
for (File image : images)
mediaArrayList.add(0, new Media(image));
media = mediaArrayList;
Expand All @@ -118,23 +102,8 @@ public int compare(String lhs, String rhs) {
return result;
}

/* public void updatePhotos(PhotosAdapter adapter) {
media = new ArrayList<Media>();
File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter_photos));
for (int i = 0; i < images.length; i++) {
media.add(0, new Media(images[i].getAbsolutePath(), images[i].lastModified(), images[i].length()));
adapter.notifyItemInserted(i);
}
sortPhotos();
adapter.notifyDataSetChanged();
}*/

public boolean areFiltersActive() {
return filter_photos != ImageFileFilter.FILTER_ALL;
}

public void filterMedias(Context context, int filter) {
filter_photos = filter;
this.filter = filter;
updatePhotos(context);
}

Expand All @@ -157,11 +126,11 @@ public boolean isSelected() {

public Media getMedia(int index) { return media.get(index); }

public void setCurrentPhotoIndex(int index){ current = index; }
public void setCurrentPhotoIndex(int index){ currentMediaIndex = index; }

public Media getCurrentMedia() { return getMedia(current); }
public Media getCurrentMedia() { return getMedia(currentMediaIndex); }

public int getCurrentMediaIndex() { return current; }
public int getCurrentMediaIndex() { return currentMediaIndex; }

public String getContentDescription(Context c) {
return c.getString(R.string.media);
Expand Down Expand Up @@ -209,19 +178,22 @@ public void setSelectedPhotoAsPreview(Context context) {

private void setCurrentPhoto(String path) {
for (int i = 0; i < media.size(); i++)
if (media.get(i).getPath().equals(path)) current = i;
if (media.get(i).getPath().equals(path)) currentMediaIndex = i;
}

public int getSelectedCount() {
return selectedMedias.size();
}

public boolean areMediaSelected() { return getSelectedCount() != 0;}

public void selectAllPhotos() {
for (int i = 0; i < media.size(); i++)
for (int i = 0; i < media.size(); i++) {
if (!media.get(i).isSelected()) {
media.get(i).setSelected(true);
selectedMedias.add(media.get(i));
}
}
}

public int toggleSelectPhoto(int index) {
Expand All @@ -241,31 +213,32 @@ public void setDefaultSortingMode(Context context, int column) {
settings.columnSortingMode = column;
}

public void renameCurrentMedia(Context context, String newName) {
public boolean renameCurrentMedia(Context context, String newName) {
boolean success = false;
try {
File from = new File(getCurrentMedia().getPath());
File to = new File(StringUtils.getPhotoPathRenamed(getCurrentMedia().getPath(), newName));
if (from.renameTo(to)) {
if (success = ContentHelper.moveFile(context, from, to)) {
scanFile(context, new String[]{ to.getAbsolutePath(), from.getAbsolutePath() });
getCurrentMedia().path = to.getAbsolutePath();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) { e.printStackTrace(); }
return success;
}

public int moveCurrentPhoto(Context context, String newName) {
public boolean moveCurrentPhoto(Context context, String newName) {
boolean success = false;
try {
File from = new File(getCurrentMedia().getPath());
File to = new File(StringUtils.getPhotoPathMoved(getCurrentMedia().getPath(), newName));
if (ContentHelper.moveFile(context, from, to)) {
if (success = ContentHelper.moveFile(context, from, to)) {
scanFile(context, new String[]{ to.getAbsolutePath(), from.getAbsolutePath() });
getCurrentMedia().path = to.getAbsolutePath();
media.remove(getCurrentMediaIndex());
setCount(media.size());
}
} catch (Exception e) { e.printStackTrace(); }
return getCurrentMediaIndex();
return success;
}

public void setDefaultSortingAscending(Context context, Boolean ascending) {
Expand Down Expand Up @@ -311,9 +284,8 @@ public void selectAllPhotosUpTo(int targetIndex, PhotosAdapter adapter) {
}

public void clearSelectedPhotos() {
for (Media m : media) {
for (Media m : media)
m.setSelected(false);
}
selectedMedias.clear();
}

Expand Down Expand Up @@ -341,39 +313,30 @@ public void copySelectedPhotos(Context context, String folderPath) {
copyPhoto(context, media.getPath(), folderPath);
}

public void copyPhoto(Context context, String olderPath, String folderPath) {
public boolean copyPhoto(Context context, String olderPath, String folderPath) {
boolean success = false;
try {
File from = new File(olderPath);
File to = new File(StringUtils.getPhotoPathMoved(olderPath, folderPath));

if (ContentHelper.copyFile(context, from, to)) {
if (success = ContentHelper.copyFile(context, from, to))
scanFile(context, new String[]{ to.getAbsolutePath() });
}
/*InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();*/

//scanFile(context, new String[]{to.getAbsolutePath()});
} catch (Exception e) { e.printStackTrace(); }
return success;
}

public void deleteCurrentMedia(Context context) {
deleteMedia(context, media.get(getCurrentMediaIndex()));
deleteMedia(context, getCurrentMedia());
media.remove(getCurrentMediaIndex());
setCount(media.size());
}

private void deleteMedia(Context context, Media media) {
private boolean deleteMedia(Context context, Media media) {
boolean success;
File file = new File(media.getPath());
if (ContentHelper.deleteFile(context, file))
if (success = ContentHelper.deleteFile(context, file))
scanFile(context, new String[]{ file.getAbsolutePath() });
return success;
}

public void deleteSelectedMedia(Context context) {
Expand All @@ -393,7 +356,6 @@ public void renameAlbum(Context context, String newName) {
for (int i = 0; i < media.size(); i++) {
File from = new File(media.get(i).getPath());
File to = new File(StringUtils.getPhotoPathRenamedAlbumChange(media.get(i).getPath(), newName));
//File to = new File(StringUtils.getPhotoPathRenamedAlbumChange(media.get(i).getPath(), newName));
if (ContentHelper.moveFile(context, from, to)) {
MediaScannerConnection.scanFile(context,
new String[]{from.getAbsolutePath(), to.getAbsolutePath()}, null, null);
Expand All @@ -405,4 +367,7 @@ public void renameAlbum(Context context, String newName) {

public void scanFile(Context context, String[] path) { MediaScannerConnection.scanFile(context, path, null, null); }

public boolean areFiltersActive() {
return filter != ImageFileFilter.FILTER_ALL;
}
}
Loading

0 comments on commit 1df15a8

Please sign in to comment.