diff options
author | Joe Robinson <joe@lc8n.com> | 2013-02-02 22:18:05 +0000 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2013-02-02 22:18:05 +0000 |
commit | da2df98f789f2531142835dc43566ccda9583c22 (patch) | |
tree | 1e87bd9465192e3db33d33ff6c08e0dd0abeacf6 /src | |
parent | bffa5e5ea8a852bae02f724d9758edd54776b885 (diff) |
Updated file browser to show icons, size, and modified time. Also added
a button to go up a directory, and go to a specified directory
Diffstat (limited to 'src')
-rw-r--r-- | src/com/lc8n/blauploader/FileArrayAdapter.java | 165 | ||||
-rw-r--r-- | src/com/lc8n/blauploader/FileBrowser.java | 257 |
2 files changed, 279 insertions, 143 deletions
diff --git a/src/com/lc8n/blauploader/FileArrayAdapter.java b/src/com/lc8n/blauploader/FileArrayAdapter.java new file mode 100644 index 0000000..f15293f --- /dev/null +++ b/src/com/lc8n/blauploader/FileArrayAdapter.java @@ -0,0 +1,165 @@ +package com.lc8n.blauploader; + +import java.io.File; +import java.util.Calendar; + +import android.app.Activity; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +//This class is used to create a list of files +public class FileArrayAdapter extends ArrayAdapter<File> { + + private Context context; + private int layoutResourceId; + private File[] files = null; + + public FileArrayAdapter(Context context, int layoutResourceId, + File[] files) { + super(context, layoutResourceId, files); + this.context = context; + this.layoutResourceId = layoutResourceId; + this.files = files; + } + + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View row = convertView; + FileHolder holder = null; + + if(row == null) + { + LayoutInflater inflater = ((Activity)context).getLayoutInflater(); + row = inflater.inflate(layoutResourceId, parent, false); + + //Connect the holder to the GUI objects + holder = new FileHolder(); + holder.fileIcon = (ImageView)row.findViewById(R.id.fileicon); + holder.fileName = (TextView)row.findViewById(R.id.filetext); + holder.fileSize = (TextView)row.findViewById(R.id.filesize); + holder.fileModified = (TextView)row.findViewById(R.id.filemodified); + + row.setTag(holder); + } + else + { + holder = (FileHolder)row.getTag(); + } + + File file = files[position]; + + if (file.isDirectory()) { + holder.fileIcon.setImageResource(R.drawable.folder); + + //If it's a folder, the file size is irrelevant. Get the number of files inside instead + if (file.list()!= null) { + int numFiles = file.list().length; + if (numFiles == 1) { + holder.fileSize.setText("1 File"); + } else if (numFiles == 0) { + holder.fileSize.setText("Empty"); + } else { + holder.fileSize.setText(numFiles+" Files"); + } + } else { + holder.fileSize.setText("Empty"); + } + } else { + + //Check the file extension to set the icon + String[] fileParts = file.getName().split("\\."); + if (fileParts.length != 0) { + String extension = fileParts[fileParts.length-1]; + + if (extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("gif")) { + holder.fileIcon.setImageResource(R.drawable.image); + } else if (extension.equalsIgnoreCase("mp3") || extension.equalsIgnoreCase("wav") || extension.equalsIgnoreCase("aac") || extension.equalsIgnoreCase("wma")) { + holder.fileIcon.setImageResource(R.drawable.music); + } else if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("mkv") || extension.equalsIgnoreCase("3gp")) { + holder.fileIcon.setImageResource(R.drawable.movie); + } else if (extension.equalsIgnoreCase("txt") || extension.equalsIgnoreCase("pdf") || extension.equalsIgnoreCase("rtf") || extension.equalsIgnoreCase("log")) { + holder.fileIcon.setImageResource(R.drawable.text); + } else if (extension.equalsIgnoreCase("zip") || extension.equalsIgnoreCase("gz") || extension.equalsIgnoreCase("rar") || extension.equalsIgnoreCase("bz2")) { + holder.fileIcon.setImageResource(R.drawable.compressed); + } else if (extension.equalsIgnoreCase("exe") || extension.equalsIgnoreCase("apk") ) { + holder.fileIcon.setImageResource(R.drawable.binary); + } else { + holder.fileIcon.setImageResource(R.drawable.unknown); + } + + } else { + //We probably can't get here, but you never know + holder.fileIcon.setImageResource(R.drawable.generic); + } + + long fileSizeBytes = file.length(); + double floatFileSize = 0; + String strFileSize = ""; + + //Round it to the largest unit, and print it with 2 decimal places + if(fileSizeBytes > 1073741824) { + floatFileSize = (double)fileSizeBytes / 1073741824; + strFileSize = String.format("%.2f", floatFileSize) + " GB"; + } else if (fileSizeBytes > 1048576) { + floatFileSize = (double)fileSizeBytes / 1048576; + strFileSize = String.format("%.2f", floatFileSize) + " MB"; + } else if (fileSizeBytes > 1024) { + floatFileSize = (double)fileSizeBytes / 1024; + strFileSize = String.format("%.2f", floatFileSize) + " kB"; + } else { + strFileSize = fileSizeBytes + "B"; + } + + holder.fileSize.setText(strFileSize); + } + + holder.fileName.setText(file.getName()); + + //File modified time is a unix timestamp, turn it into a readable date/time + Calendar fileModTime = Calendar.getInstance(); + fileModTime.setTimeInMillis(file.lastModified()); + + //Add leading 0s + int hour = fileModTime.get(Calendar.HOUR_OF_DAY); + String time; + if (hour < 10) { + time = "0" + hour; + } else { + time = String.valueOf(hour); + } + + int minute = fileModTime.get(Calendar.HOUR); + if (minute < 10) { + time += ":0" + minute; + } else { + time += ":" + String.valueOf(minute); + } + + if (hour < 12) { + time += " AM"; + } else { + time += " PM"; + } + holder.fileModified.setText(fileModTime.get(Calendar.DAY_OF_MONTH)+"/"+(fileModTime.get(Calendar.MONTH)+1)+"/"+fileModTime.get(Calendar.YEAR)+" "+time); + + return row; + } + + static class FileHolder + { + ImageView fileIcon; + TextView fileName; + TextView fileSize; + TextView fileModified; + + } + +} + + diff --git a/src/com/lc8n/blauploader/FileBrowser.java b/src/com/lc8n/blauploader/FileBrowser.java index 890ee28..29f5af5 100644 --- a/src/com/lc8n/blauploader/FileBrowser.java +++ b/src/com/lc8n/blauploader/FileBrowser.java @@ -30,12 +30,16 @@ import android.os.Bundle; import android.os.Handler;
import android.os.Message;
import android.telephony.SmsManager;
+import android.util.Log;
+import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
-import android.view.View;
+import android.view.View;
+import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
+import android.widget.EditText;
+import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
@@ -43,11 +47,12 @@ public class FileBrowser extends Activity { /** Called when the activity is first created. */
private List<String> directoryEntries = new ArrayList<String>();
- private File currentDirectory = new File("/");
+ private File[] files;
+ private File currentDirectory = new File("/mnt/sdcard");
private float fileSize = 0;
private Handler pbHandle = null;
private ProgressDialog progressDialog;
- private ListView fileList;
+ private ListView fileListView;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -56,54 +61,88 @@ public class FileBrowser extends Activity { setContentView(R.layout.browser);
directoryEntries.clear();
+
+ files = currentDirectory.listFiles();
+// fileList = new ArrayList<File>(files);
+// //directoryEntries.add("Directory:"+currentDirectory.getAbsolutePath());
+// directoryEntries.add("Up One Level");
+// if (files != null)
+// {
+// for (File file: files)
+// {
+// fileList.add(file);
+// directoryEntries.add(file.getAbsolutePath());
+//// System.out.println(file.getPath());
+// }
+// }
- File[] files = currentDirectory.listFiles();
- //directoryEntries.add("Directory:"+currentDirectory.getAbsolutePath());
- directoryEntries.add("Up One Level");
- if (files != null)
- {
- for (File file: files)
- {
- directoryEntries.add(file.getAbsolutePath());
-// System.out.println(file.getPath());
- }
- }
-
- TextView header = (TextView)findViewById(R.id.directoryname);
+ final EditText header = (EditText)findViewById(R.id.directoryname);
header.setText(currentDirectory.getAbsolutePath());
- ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.filerow, this.directoryEntries);
- fileList = (ListView)findViewById(R.id.filelist);
+
+ //Listen for events from the file path box
+ header.setOnEditorActionListener(new TextView.OnEditorActionListener() {
+
+ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+
+ File checkFile = new File (header.getText().toString());
+
+ //When enter/confirm/go/done (how many possibilities are there) is hit, try and go to the path entered
+ if (actionId == EditorInfo.IME_ACTION_GO || actionId == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
+ if (checkFile.exists() && checkFile.isDirectory()) {
+ currentDirectory = checkFile;
+ browseTo(currentDirectory);
+ } else {
+ //TODO: Error message if it doesn't exist. Maybe upload if it's a file?
+ }
+ }
+ //I have no idea why I have to return something for this function
+ return true;
+ }
+ });
- fileList.setAdapter(directoryList);
+ //Go button does the same as hitting enter, try and go to the folder
+ ImageButton goToDir = (ImageButton) findViewById(R.id.goDirButton);
+ goToDir.setOnClickListener(new View.OnClickListener() {
+
+ public void onClick(View v) {
+ File checkFile = new File (header.getText().toString());
+ if (checkFile.exists() && checkFile.isDirectory()) {
+ currentDirectory = checkFile;
+ browseTo(currentDirectory);
+ } else {
+ //TODO: Error message if it doesn't exist. Maybe upload if it's a file?
+ }
+ }});
- fileList.setOnItemClickListener(new ListView.OnItemClickListener() {
-
+ //Up button goes to the folder above the current one
+ ImageButton upDir = (ImageButton) findViewById(R.id.upDirButton);
+ upDir.setOnClickListener(new View.OnClickListener() {
+
+ public void onClick(View v) {
+ //Go up unless there is no parent (if the result is null)
+ if (currentDirectory.getParentFile() != null) {
+ currentDirectory = currentDirectory.getParentFile();
+ browseTo(currentDirectory);
+ }
+ }});
+
+ //ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.filerow, this.directoryEntries);
+
+ //Use the FileArrayAdapter to apply the list of files to the list view
+ fileListView = (ListView)findViewById(R.id.filelist);
+ FileArrayAdapter fileAdapter = new FileArrayAdapter(this, R.layout.filerow, files);
+ fileListView.setAdapter(fileAdapter);
+
+ //Handler for clicks on the list
+ fileListView.setOnItemClickListener(new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
- String fileString = directoryEntries.get(position);
-
-
- if(fileString.contains("Directory:"))
- {
- browseTo(currentDirectory);
- }
- else if (fileString.equals("Up One Level"))
- {
- System.out.println(currentDirectory.getParentFile().getPath());
- if (currentDirectory.getParent() != null)
- {
- currentDirectory = currentDirectory.getParentFile();
- browseTo(currentDirectory);
- }
- }
- else
- {
-
-
- File chosenFile = new File(fileString);
+ File chosenFile = files[position];
+
+ //If it's a folder, open it, if not, upload the file
if (chosenFile.isDirectory())
{
currentDirectory = chosenFile;
@@ -111,17 +150,12 @@ public class FileBrowser extends Activity { }
else
{
-
- //upload it
+ //Upload it
+ //TODO: Ask for confirmation before uploading (maybe send them to the fileshare screen? or a popup to let them rename)
try {
fileSize = chosenFile.length();
-
-
-
-
-
FileUpload fu = new FileUpload(chosenFile,pbHandle);
Thread thread = new Thread(fu);
@@ -139,28 +173,21 @@ public class FileBrowser extends Activity { }
}
-
- }
}
-
-
+
});
+
+ //Show progress while it's uploading
+ //TODO: Make it use the filesize, not percent
progressDialog = new ProgressDialog(this);
-
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Blauploading...");
progressDialog.setCancelable(true);
progressDialog.setProgress(0);
-
-//
-//
-
-
-
pbHandle = new Handler(){
@Override
@@ -190,96 +217,40 @@ public class FileBrowser extends Activity { }
- public void browseTo(File dir)
- {
+ //Reload the screen with the specified folder
+ public void browseTo(File dir) {
directoryEntries.clear();
-
- File[] files = dir.listFiles();
- //directoryEntries.add("Current Directory:"+currentDirectory.getAbsolutePath());
- directoryEntries.add("Up One Level");
+
+ //Get a list of files
+ if (dir.listFiles() != null) {
+ files = dir.listFiles();
+ } else {
+ files = new File[0];
+ }
+
+ //Set the text box to the new folder
TextView header = (TextView)findViewById(R.id.directoryname);
header.setText(currentDirectory.getAbsolutePath());
// System.out.println(files.length);
- if(files != null)
- {
- for (File file: files)
- {
- directoryEntries.add(file.getAbsolutePath());
- System.out.println(file.getPath());
- }
- }
- ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.filerow, this.directoryEntries);
-
- fileList.setAdapter(directoryList);
+// if(files != null)
+// {
+// for (File file: files)
+// {
+// directoryEntries.add(file.getAbsolutePath());
+// System.out.println(file.getPath());
+// }
+// }
+// ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.filerow, this.directoryEntries);
+//
+// fileListView.setAdapter(directoryList);
+//
+ FileArrayAdapter fileAdapter = new FileArrayAdapter(this, R.layout.filerow, files);
+ fileListView.setAdapter(fileAdapter);
}
- //Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
-
- String fileString = directoryEntries.get(position);
-
-
- if(fileString.contains("Directory:"))
- {
- browseTo(currentDirectory);
- }
- else if (fileString.equals("Up One Level"))
- {
- System.out.println(currentDirectory.getParentFile().getPath());
- if (currentDirectory.getParent() != null)
- {
- currentDirectory = currentDirectory.getParentFile();
- browseTo(currentDirectory);
- }
- }
- else
- {
-
-
- File chosenFile = new File(fileString);
- if (chosenFile.isDirectory())
- {
- currentDirectory = chosenFile;
- browseTo(currentDirectory);
- }
- else
- {
-
- //upload it
- try {
-
- fileSize = chosenFile.length();
-
-
-
-
-
- FileUpload fu = new FileUpload(chosenFile,pbHandle);
- Thread thread = new Thread(fu);
-
- thread.start();
-
- SmsManager sms = SmsManager.getDefault();
- sms.sendTextMessage("07927278978", null, "New file blauploaded: http://www.blaupload.co.uk/"+chosenFile.getName(), null, null);
-
-
-
- } catch (Exception e) {
- // TODO Auto-generated catch block
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
-
- }
-
- }
-
-
-
- }
-
+ //Set up menu options to go to other screens
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
|