From 01c75a7aa09d0d9fd7a4c61f952aaa93972e7c99 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 27 Oct 2010 21:31:44 +0100 Subject: Reset eclipse workspace (commit hopefully changes nothing) --- src/com/lc8n/blauploader/FileBrowser.java | 198 ++++++++++++++++++++++ src/com/lc8n/blauploader/FileUpload.java | 76 +++++++++ src/com/lc8n/blauploader/ProgressInputStream.java | 97 +++++++++++ 3 files changed, 371 insertions(+) create mode 100644 src/com/lc8n/blauploader/FileBrowser.java create mode 100644 src/com/lc8n/blauploader/FileUpload.java create mode 100644 src/com/lc8n/blauploader/ProgressInputStream.java (limited to 'src/com') diff --git a/src/com/lc8n/blauploader/FileBrowser.java b/src/com/lc8n/blauploader/FileBrowser.java new file mode 100644 index 0000000..6546327 --- /dev/null +++ b/src/com/lc8n/blauploader/FileBrowser.java @@ -0,0 +1,198 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + Copyright 2010 Joe Robinson +*/ + +package com.lc8n.blauploader; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import android.app.ListActivity; +import android.app.ProgressDialog; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +public class FileBrowser extends ListActivity { + /** Called when the activity is first created. */ + + private List directoryEntries = new ArrayList(); + private File currentDirectory = new File("/"); + private float fileSize = 0; + private Handler pbHandle = null; + private ProgressDialog progressDialog; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + + directoryEntries.clear(); + + File[] files = currentDirectory.listFiles(); + directoryEntries.add("Directory:"+currentDirectory.getAbsolutePath()); + directoryEntries.add("Up One Level"); + if (files != null) + { + for (File file: files) + { + directoryEntries.add(file.getPath()); +// System.out.println(file.getPath()); + } + } + ArrayAdapter directoryList = new ArrayAdapter(this, R.layout.filerow, this.directoryEntries); + + this.setListAdapter(directoryList); + + progressDialog = new ProgressDialog(this); + + + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.setMessage("Blauploading..."); + progressDialog.setCancelable(true); + progressDialog.setProgress(0); + + +// +// + + + + pbHandle = new Handler(){ + + @Override + public void handleMessage(Message msg) { + + /* get the value from the Message */ + + long progress = msg.getData().getLong("progress_update"); + System.out.println(progress+"/"+fileSize); + if(progress>(fileSize-10240)) + { + progressDialog.dismiss(); + } + float percent = (progress/fileSize)*100; + Integer intProgress = Math.round(percent); + if(intProgress==100) + { + progressDialog.dismiss(); + } + else + { + progressDialog.show(); + progressDialog.setProgress(intProgress); + } + } + }; + + } + + public void browseTo(File dir) + { + directoryEntries.clear(); + + File[] files = dir.listFiles(); + directoryEntries.add("Current Directory:"+currentDirectory.getAbsolutePath()); + directoryEntries.add("Up One Level"); + +// System.out.println(files.length); + if(files != null) + { + for (File file: files) + { + directoryEntries.add(file.getPath()); + System.out.println(file.getPath()); + } + } + ArrayAdapter directoryList = new ArrayAdapter(this, R.layout.filerow, this.directoryEntries); + + this.setListAdapter(directoryList); + } + + + @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(); + + + + + } catch (Exception e) { + // TODO Auto-generated catch block + System.err.println(e.getMessage()); + e.printStackTrace(); + } + + } + + } + + + + } + + + +} \ No newline at end of file diff --git a/src/com/lc8n/blauploader/FileUpload.java b/src/com/lc8n/blauploader/FileUpload.java new file mode 100644 index 0000000..b81ec2d --- /dev/null +++ b/src/com/lc8n/blauploader/FileUpload.java @@ -0,0 +1,76 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + Copyright 2010 Joe Robinson +*/ + +package com.lc8n.blauploader; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.SocketException; + +import org.apache.commons.net.ftp.FTP; +import org.apache.commons.net.ftp.FTPClient; + +import android.os.Handler; + +public class FileUpload implements Runnable{ + + private File file; + private Handler pbHandle; + public FileUpload(File file, Handler pbHandle) + { + this.file = file; + this.pbHandle = pbHandle; + } + + public void uploadFile(File file, Handler pbHandle) throws SocketException, IOException + { + + + FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + ProgressInputStream pis = new ProgressInputStream(bis,pbHandle); + String fileName = file.getName(); + FTPClient ftpClient = new FTPClient(); + + ftpClient.connect("tghost.co.uk"); + ftpClient.login("blaupload", "lemons"); + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + ftpClient.enterLocalPassiveMode(); + ftpClient.changeWorkingDirectory("/"); + ftpClient.storeFile(fileName, pis); + bis.close(); + pis.close(); + ftpClient.logout(); + ftpClient.disconnect(); + + } + public void run() { + try { + uploadFile(file, pbHandle); + } catch (Exception e) { + + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} diff --git a/src/com/lc8n/blauploader/ProgressInputStream.java b/src/com/lc8n/blauploader/ProgressInputStream.java new file mode 100644 index 0000000..bda5980 --- /dev/null +++ b/src/com/lc8n/blauploader/ProgressInputStream.java @@ -0,0 +1,97 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + Copyright 2010 Joe Robinson +*/ + +package com.lc8n.blauploader; + +import java.io.IOException; +import java.io.InputStream; + +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; + +public class ProgressInputStream extends InputStream { + + /* Key to retrieve progress value from message bundle passed to handler */ + public static final String PROGRESS_UPDATE = "progress_update"; + + private static final int TEN_KILOBYTES = 1024 * 10; + + private InputStream inputStream; + private Handler handler; + + private long progress; + private long lastUpdate; + + private boolean closed; + + public ProgressInputStream(InputStream inputStream, Handler handler) { + this.inputStream = inputStream; + this.handler = handler; + + this.progress = 0; + this.lastUpdate = 0; + + this.closed = false; + } + + @Override + public int read() throws IOException { + int count = inputStream.read(); + return incrementCounterAndUpdateDisplay(count); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int count = inputStream.read(b, off, len); + return incrementCounterAndUpdateDisplay(count); + } + + @Override + public void close() throws IOException { + super.close(); + if (closed) + throw new IOException("already closed"); + closed = true; + } + + private int incrementCounterAndUpdateDisplay(int count) { + if (count > 0) + progress += count; + lastUpdate = maybeUpdateDisplay(progress, lastUpdate); + return count; + } + + private long maybeUpdateDisplay(long progress, long lastUpdate) { + if (progress - lastUpdate > TEN_KILOBYTES) { + lastUpdate = progress; + sendLong(PROGRESS_UPDATE, progress); + } + return lastUpdate; + } + + public void sendLong(String key, long value) { + Bundle data = new Bundle(); + data.putLong(key, value); + + Message message = Message.obtain(); + message.setData(data); + handler.sendMessage(message); + } +} \ No newline at end of file -- cgit v1.2.3