Skip to content
This repository has been archived by the owner on Sep 8, 2019. It is now read-only.

Commit

Permalink
- Bug fixes in FileExplorer
Browse files Browse the repository at this point in the history
- minor ui changes in FileExplorer
- code clean up
  • Loading branch information
kollerlukas committed Feb 21, 2017
1 parent d4de9cf commit d7f0ba3
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package us.koller.cameraroll.adapter.fileExplorer;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;

import us.koller.cameraroll.R;
import us.koller.cameraroll.adapter.fileExplorer.ViewHolder.FileHolder;
import us.koller.cameraroll.data.Album;
import us.koller.cameraroll.data.AlbumItem;
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.data.Provider.MediaProvider;
import us.koller.cameraroll.data.StorageRoot;
import us.koller.cameraroll.ui.FileExplorerActivity;
import us.koller.cameraroll.ui.ItemActivity;

Expand Down Expand Up @@ -92,22 +88,10 @@ public void onClick(View view) {
String path = file.getPath().substring(0, index);

//load Album
final Album album;
AlbumItem albumItem = null;
if (!Environment.isExternalStorageRemovable(new File(file.getPath()))) {
album = MediaProvider.loadAlbum(path);
for (int i = 0; i < album.getAlbumItems().size(); i++) {
if (album.getAlbumItems().get(i).getPath().equals(file.getPath())) {
albumItem = album.getAlbumItems().get(i);
break;
}
}
} else {
album = new Album().setPath(path);
albumItem = AlbumItem.getInstance(holder.itemView.getContext(), file.getPath());
if (albumItem != null) {
album.getAlbumItems().add(albumItem);
}
final Album album = new Album().setPath(path);
AlbumItem albumItem = AlbumItem.getInstance(holder.itemView.getContext(), file.getPath());
if (albumItem != null) {
album.getAlbumItems().add(albumItem);
}

if (album != null && albumItem != null) {
Expand Down Expand Up @@ -152,12 +136,18 @@ public void onClick(View view) {
}

private void onItemSelect(File_POJO file) {
if (file instanceof StorageRoot) {
return;
}

if (mode == NORMAL_MODE) {
mode = SELECTOR_MODE;
//no preselected Items
enterSelectorMode(new File_POJO[0]);
/*mode = SELECTOR_MODE;
selected_items = new boolean[files.getChildren().size()];
if (callback != null) {
callback.onSelectorModeEnter();
}
}*/
}

int position = files.getChildren().indexOf(file);
Expand All @@ -179,7 +169,7 @@ public int getMode() {
return mode;
}

private int getSelectedCount() {
public int getSelectedCount() {
int selected_items_count = 0;
for (int i = 0; i < selected_items.length; i++) {
selected_items_count += selected_items[i] ? 1 : 0;
Expand All @@ -198,14 +188,7 @@ public void cancelMode() {
if (mode == SELECTOR_MODE) {
mode = NORMAL_MODE;
if (callback != null) {
File_POJO[] files = new File_POJO[getSelectedCount()];
int index = 0;
for (int i = 0; i < selected_items.length; i++) {
if (selected_items[i]) {
files[index] = this.files.getChildren().get(i);
index++;
}
}
File_POJO[] files = getSelectedItems();
callback.onSelectorModeExit(files);
}
selected_items = new boolean[files.getChildren().size()];
Expand All @@ -218,6 +201,35 @@ public void cancelMode() {
notifyDataSetChanged();
}

public File_POJO[] getSelectedItems() {
File_POJO[] files = new File_POJO[getSelectedCount()];
int index = 0;
for (int i = 0; i < selected_items.length; i++) {
if (selected_items[i]) {
files[index] = this.files.getChildren().get(i);
index++;
}
}
return files;
}

public void enterSelectorMode(File_POJO[] selectedItems) {
mode = SELECTOR_MODE;
selected_items = new boolean[files.getChildren().size()];
//select items
for (int i = 0; i < selectedItems.length; i++) {
for (int k = 0; k < files.getChildren().size(); k++) {
if (selectedItems[i].getPath()
.equals(files.getChildren().get(k).getPath())) {
onItemSelect(files.getChildren().get(k));
}
}
}
if (callback != null) {
callback.onSelectorModeEnter();
}
}

public void pickTarget() {
mode = PICK_TARGET_MODE;
if (callback != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import us.koller.cameraroll.R;
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.data.StorageRoot;
import us.koller.cameraroll.util.MediaType;

public class FileHolder extends RecyclerView.ViewHolder {
Expand All @@ -25,7 +26,9 @@ public void setFile(File_POJO file) {
this.file = file;

ImageView folderIndicator = (ImageView) itemView.findViewById(R.id.folder_indicator);
if (!file.isMedia) {
if (file instanceof StorageRoot) {
folderIndicator.setImageResource(R.drawable.ic_sd_storage_white_24dp);
} else if (!file.isMedia) {
folderIndicator.setImageResource(R.drawable.ic_folder_white_24dp);
} else if (MediaType.isVideo(folderIndicator.getContext(), file.getPath())) {
folderIndicator.setImageResource(R.drawable.ic_videocam_white_24dp);
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/us/koller/cameraroll/data/File_POJO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Parcel;
import android.os.Parcelable;

import java.io.File;
import java.util.ArrayList;

import us.koller.cameraroll.util.SortUtil;
Expand Down Expand Up @@ -36,7 +37,7 @@ public String getName() {
@Override
public long getDate(Activity context) {
//not needed
return 0;
return new File(getPath()).lastModified();
}

@Override
Expand Down Expand Up @@ -68,7 +69,7 @@ public void writeToParcel(Parcel parcel, int i) {
parcel.writeTypedArray(children, 0);
}

private File_POJO(Parcel parcel) {
public File_POJO(Parcel parcel) {
path = parcel.readString();
isMedia = Boolean.valueOf(parcel.readString());
children = parcel.createTypedArrayList(CREATOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.data.Provider.Retriever.Retriever;
import us.koller.cameraroll.data.Provider.Retriever.StorageRetriever;
import us.koller.cameraroll.data.StorageRoot;

public class FilesProvider extends Provider {

/*public interface Callback {
void onFilesLoaded(File_POJO files);
void timeout();
void needPermission();
}*/

public interface Callback {
void onDirLoaded(File_POJO dir);

Expand All @@ -30,19 +23,9 @@ public FilesProvider() {
retriever = new StorageRetriever();
}

/*public void loadFiles(Activity context, final Callback callback) {
if (!MediaProvider.checkPermission(context)) {
callback.needPermission();
return;
}
retriever = new StorageRetriever();
((StorageRetriever) retriever).loadFiles(context, callback);
}*/

public File_POJO[] getRoots() {
return ((StorageRetriever) retriever).loadRoots();
public static StorageRoot[] getRoots(Activity context) {
return StorageRetriever.loadRoots(context);
}

public void loadDir(Activity context, String dirPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import java.util.ArrayList;
import java.util.Arrays;

import us.koller.cameraroll.R;
import us.koller.cameraroll.data.Album;
import us.koller.cameraroll.data.File_POJO;
import us.koller.cameraroll.data.Provider.FilesProvider;
import us.koller.cameraroll.data.Provider.ItemLoader.AlbumLoader;
import us.koller.cameraroll.data.Provider.ItemLoader.FileLoader;
import us.koller.cameraroll.data.Provider.ItemLoader.ItemLoader;
import us.koller.cameraroll.data.Provider.MediaProvider;
import us.koller.cameraroll.data.StorageRoot;
import us.koller.cameraroll.util.MediaType;
import us.koller.cameraroll.util.SortUtil;

//loading media by searching through Storage
Expand Down Expand Up @@ -110,22 +113,24 @@ public void done() {
});
}

public File_POJO[] loadRoots() {
public static StorageRoot[] loadRoots(Activity context) {
ArrayList<StorageRoot> temp = new ArrayList<>();

ArrayList<File_POJO> temp = new ArrayList<>();

temp.add(new File_POJO(Environment.getExternalStorageDirectory().getPath(), false));
StorageRoot externalStorage
= new StorageRoot(Environment.getExternalStorageDirectory().getPath());
externalStorage.setName(context.getString(R.string.storage));
temp.add(externalStorage);

File[] removableStorageRoots = getRemovableStorageRoots();
for (int i = 0; i < removableStorageRoots.length; i++) {
temp.add(new File_POJO(removableStorageRoots[i].getPath(), false));
temp.add(new StorageRoot(removableStorageRoots[i].getPath()));
}

File_POJO[] roots = new File_POJO[temp.size()];
StorageRoot[] roots = new StorageRoot[temp.size()];
return temp.toArray(roots);
}

public void loadDir(Activity context, String dirPath,
public void loadDir(final Activity context, String dirPath,
final FilesProvider.Callback callback) {

if (new File(dirPath).isFile()) {
Expand All @@ -142,7 +147,19 @@ public void loadDir(Activity context, String dirPath,
public void done(AdaptableThread thread, ItemLoader.Result result,
ArrayList<File> filesToSearch) {
File_POJO files = result.files;
SortUtil.sortByName(files.getChildren());
boolean filesContainMedia = false;
for (int i = 0; i < files.getChildren().size(); i++) {
if (MediaType.isMedia(context, files.getChildren().get(i).getPath())) {
filesContainMedia = true;
break;
}
}

if (filesContainMedia) {
SortUtil.sortByDate(context, files.getChildren());
} else {
SortUtil.sortByName(files.getChildren());
}
callback.onDirLoaded(files);
thread.cancel();
threads = null;
Expand Down Expand Up @@ -226,8 +243,8 @@ private static File[] getRemovableStorageRoots() {
return root.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
File[] files1 = file.listFiles();
return files1 != null && files1.length > 0;
File[] files = file.listFiles();
return files != null && files.length > 0;
}
});
}
Expand Down Expand Up @@ -504,12 +521,14 @@ private void searchDir(final Activity context, final File file) {
final ArrayList<File> filesToSearch = new ArrayList<>();

File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files != null) {
for (int i = 0; i < files.length; i++) {

itemLoader.onFile(context, files[i]);
itemLoader.onFile(context, files[i]);

if (files[i].isDirectory()) {
filesToSearch.add(files[i]);
if (files[i].isDirectory()) {
filesToSearch.add(files[i]);
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/us/koller/cameraroll/data/StorageRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package us.koller.cameraroll.data;

import android.os.Parcel;
import android.os.Parcelable;

public class StorageRoot extends File_POJO {

private String name;

public StorageRoot(String path) {
super(path, false);
}

public void setName(String name) {
this.name = name;
}

public String getName() {
if (name == null) {
return super.getName();
}
return name;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
super.writeToParcel(parcel, i);
parcel.writeString(name);
}

private StorageRoot(Parcel parcel) {
super(parcel);
name = parcel.readString();
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public StorageRoot createFromParcel(Parcel parcel) {
return new StorageRoot(parcel);
}

@Override
public StorageRoot[] newArray(int i) {
return new StorageRoot[i];
}
};
}
Loading

0 comments on commit d7f0ba3

Please sign in to comment.