[Android] Use accessors with FileWrapper.java. Maintains encapsulation. Also make FileWrapper.java implement Comparable, since it already implements compareTo.

This commit is contained in:
Lioncash 2013-10-11 11:58:19 -04:00
parent 4b3f15c904
commit b69059629b
2 changed files with 50 additions and 20 deletions

View File

@ -6,25 +6,24 @@ import org.retroarch.R;
import android.graphics.drawable.Drawable;
public final class FileWrapper implements IconAdapterItem {
public final File file;
public final boolean parentItem;
public final boolean dirSelectItem;
protected final boolean enabled;
public final class FileWrapper implements IconAdapterItem, Comparable<FileWrapper> {
public static final int DIRSELECT = 0;
public static final int PARENT = 1;
public static final int FILE = 2;
protected final int typeIndex;
private final File file;
private final boolean parentItem;
private final boolean dirSelectItem;
private final boolean enabled;
private final int typeIndex;
public FileWrapper(File file, int type, boolean isEnabled) {
this.file = file;
this.parentItem = type == PARENT;
this.dirSelectItem = type == DIRSELECT;
this.typeIndex = type == FILE ? (FILE + (file.isDirectory() ? 0 : 1)) : type;
this.parentItem = (type == PARENT);
this.dirSelectItem = (type == DIRSELECT);
this.typeIndex = (type == FILE) ? (FILE + (file.isDirectory() ? 0 : 1)) : type;
this.enabled = parentItem || dirSelectItem || isEnabled;
}
@ -43,7 +42,7 @@ public final class FileWrapper implements IconAdapterItem {
else
return file.getName();
}
@Override
public String getSubText() {
return null;
@ -63,6 +62,38 @@ public final class FileWrapper implements IconAdapterItem {
return null;
}
/**
* Checks whether or not the wrapped {@link File} is
* the "Parent Directory" item in the file browser.
*
* @return true if the wrapped {@link File} is the "Parent Directory"
* item in the file browser; false otherwise.
*/
public boolean isParentItem() {
return parentItem;
}
/**
* Checks whether or not the wrapped {@link File}
* is the "use this directory" item.
*
* @return true if the wrapped {@link File} is the "Use this directory"
* item in the file browser; false otherwise.
*/
public boolean isDirSelectItem() {
return dirSelectItem;
}
/**
* Gets the file wrapped by this FileWrapper.
*
* @return the file wrapped by this FileWrapper.
*/
public File getFile() {
return file;
}
@Override
public int compareTo(FileWrapper other) {
if (other != null) {
// Who says ternary is hard to follow

View File

@ -122,21 +122,20 @@ public class DirectoryActivity extends ListActivity {
public void onListItemClick(ListView listView, View aView, int position, long id) {
final FileWrapper item = adapter.getItem(position);
if (item.parentItem && backStack.get(backStack.size() - 1).parentIsBack) {
if (item.isParentItem() && backStack.get(backStack.size() - 1).parentIsBack) {
backStack.remove(backStack.size() - 1);
wrapFiles();
return;
} else if (item.dirSelectItem) {
} else if (item.isDirSelectItem()) {
finishWithPath(listedDirectory.getAbsolutePath());
return;
}
final File selected = item.parentItem ? listedDirectory.getParentFile()
: item.file;
final File selected = item.isParentItem() ? listedDirectory.getParentFile() : item.getFile();
if (selected.isDirectory()) {
backStack.add(new BackStackItem(selected.getAbsolutePath(),
!item.parentItem));
!item.isParentItem()));
wrapFiles();
} else {
String filePath = selected.getAbsolutePath();
@ -228,8 +227,8 @@ public class DirectoryActivity extends ListActivity {
// Sort items
adapter.sort(new Comparator<FileWrapper>() {
@Override
public int compare(FileWrapper aLeft, FileWrapper aRight) {
return aLeft.compareTo(aRight);
public int compare(FileWrapper left, FileWrapper right) {
return left.compareTo(right);
};
});