-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow user to pick any file for file transfer
- Loading branch information
1 parent
8bd1543
commit eba2b3e
Showing
8 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package im.tox.antox.utils; | ||
|
||
import android.app.Activity; | ||
import android.app.AlertDialog; | ||
import android.app.Dialog; | ||
import android.content.DialogInterface; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileDialog { | ||
private static final String PARENT_DIR = ".."; | ||
private final String TAG = getClass().getName(); | ||
private String[] fileList; | ||
private File currentPath; | ||
|
||
public interface FileSelectedListener { | ||
void fileSelected(File file); | ||
} | ||
|
||
public interface DirectorySelectedListener { | ||
void directorySelected(File directory); | ||
} | ||
|
||
private ListenerList<FileSelectedListener> fileListenerList = new ListenerList<FileDialog.FileSelectedListener>(); | ||
private ListenerList<DirectorySelectedListener> dirListenerList = new ListenerList<FileDialog.DirectorySelectedListener>(); | ||
private final Activity activity; | ||
private boolean selectDirectoryOption; | ||
private String fileEndsWith; | ||
|
||
public FileDialog(Activity activity, File path) { | ||
this.activity = activity; | ||
if (!path.exists()) path = Environment.getExternalStorageDirectory(); | ||
loadFileList(path); | ||
} | ||
|
||
public Dialog createFileDialog() { | ||
Dialog dialog = null; | ||
AlertDialog.Builder builder = new AlertDialog.Builder(activity); | ||
|
||
builder.setTitle(currentPath.getPath()); | ||
if (selectDirectoryOption) { | ||
builder.setPositiveButton("Select directory", new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int which) { | ||
Log.d(TAG, currentPath.getPath()); | ||
fireDirectorySelectedEvent(currentPath); | ||
} | ||
}); | ||
} | ||
|
||
builder.setItems(fileList, new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int which) { | ||
String fileChosen = fileList[which]; | ||
File chosenFile = getChosenFile(fileChosen); | ||
if (chosenFile.isDirectory()) { | ||
loadFileList(chosenFile); | ||
dialog.cancel(); | ||
dialog.dismiss(); | ||
showDialog(); | ||
} else fireFileSelectedEvent(chosenFile); | ||
} | ||
}); | ||
|
||
dialog = builder.show(); | ||
return dialog; | ||
} | ||
|
||
|
||
public void addFileListener(FileSelectedListener listener) { | ||
fileListenerList.add(listener); | ||
} | ||
|
||
public void showDialog() { | ||
createFileDialog().show(); | ||
} | ||
|
||
private void fireFileSelectedEvent(final File file) { | ||
fileListenerList.fireEvent(new ListenerList.FireHandler<FileSelectedListener>() { | ||
public void fireEvent(FileSelectedListener listener) { | ||
listener.fileSelected(file); | ||
} | ||
}); | ||
} | ||
|
||
private void fireDirectorySelectedEvent(final File directory) { | ||
dirListenerList.fireEvent(new ListenerList.FireHandler<DirectorySelectedListener>() { | ||
public void fireEvent(DirectorySelectedListener listener) { | ||
listener.directorySelected(directory); | ||
} | ||
}); | ||
} | ||
|
||
private void loadFileList(File path) { | ||
this.currentPath = path; | ||
List<String> r = new ArrayList<String>(); | ||
if (path.exists()) { | ||
if (path.getParentFile() != null) r.add(PARENT_DIR); | ||
FilenameFilter filter = new FilenameFilter() { | ||
public boolean accept(File dir, String filename) { | ||
File sel = new File(dir, filename); | ||
if (!sel.canRead()) return false; | ||
if (selectDirectoryOption) return sel.isDirectory(); | ||
else { | ||
boolean endsWith = fileEndsWith != null ? filename.toLowerCase().endsWith(fileEndsWith) : true; | ||
return endsWith || sel.isDirectory(); | ||
} | ||
} | ||
}; | ||
String[] fileList1 = path.list(filter); | ||
for (String file : fileList1) { | ||
r.add(file); | ||
} | ||
} | ||
fileList = (String[]) r.toArray(new String[]{}); | ||
} | ||
|
||
private File getChosenFile(String fileChosen) { | ||
if (fileChosen.equals(PARENT_DIR)) return currentPath.getParentFile(); | ||
else return new File(currentPath, fileChosen); | ||
} | ||
} | ||
|
||
class ListenerList<L> { | ||
private List<L> listenerList = new ArrayList<L>(); | ||
|
||
public interface FireHandler<L> { | ||
void fireEvent(L listener); | ||
} | ||
|
||
public void add(L listener) { | ||
listenerList.add(listener); | ||
} | ||
|
||
public void fireEvent(FireHandler<L> fireHandler) { | ||
List<L> copy = new ArrayList<L>(listenerList); | ||
for (L l : copy) { | ||
fireHandler.fireEvent(l); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eba2b3e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to have broken sending the other types of file transfers