detailed upload progress
authorBartek Przybylski <bart.p.pl@gmail.com>
Sat, 19 May 2012 21:30:23 +0000 (23:30 +0200)
committerBartek Przybylski <bart.p.pl@gmail.com>
Sat, 19 May 2012 21:30:23 +0000 (23:30 +0200)
src/eu/alefzero/owncloud/FileDownloader.java
src/eu/alefzero/owncloud/files/services/FileUploader.java
src/eu/alefzero/webdav/FileRequestEntity.java
src/eu/alefzero/webdav/WebdavClient.java

index 3b5f198..1936e8e 100644 (file)
@@ -1,7 +1,6 @@
 package eu.alefzero.owncloud;\r
 \r
 import java.io.File;\r
-import java.net.URLEncoder;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
index 7357477..a814fa8 100644 (file)
@@ -1,11 +1,12 @@
 package eu.alefzero.owncloud.files.services;
 
-import java.net.URLEncoder;
+import java.io.File;
 
 import eu.alefzero.owncloud.AccountUtils;
 import eu.alefzero.owncloud.R;
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
 import eu.alefzero.owncloud.utils.OwnCloudVersion;
+import eu.alefzero.webdav.OnUploadProgressListener;
 import eu.alefzero.webdav.WebdavClient;
 import android.accounts.Account;
 import android.accounts.AccountManager;
@@ -25,7 +26,7 @@ import android.webkit.MimeTypeMap;
 import android.widget.RemoteViews;
 import android.widget.Toast;
 
-public class FileUploader extends Service {
+public class FileUploader extends Service implements OnUploadProgressListener {
 
     public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
     public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
@@ -44,6 +45,9 @@ public class FileUploader extends Service {
     private String[] mLocalPaths, mRemotePaths;
     private boolean mResult;
     private int mUploadType;
+    private Notification mNotification;
+    private int mTotalDataToSend, mSendData;
+    private int mCurrentIndexUpload;
 
     @Override
     public IBinder onBind(Intent arg0) {
@@ -129,39 +133,55 @@ public class FileUploader extends Service {
         String username = mAccount.name.substring(0,
                 mAccount.name.lastIndexOf('@'));
         String password = mAccountManager.getPassword(mAccount);
-        Notification notification = new Notification(
+        
+        mTotalDataToSend = mSendData = 0;
+        
+        mNotification = new Notification(
                 eu.alefzero.owncloud.R.drawable.icon, "Uploading...",
                 System.currentTimeMillis());
-        notification.flags |= Notification.FLAG_ONGOING_EVENT;
-        notification.contentView = new RemoteViews(getApplicationContext()
-                .getPackageName(), R.layout.progressbar_layout);
-        notification.contentView.setProgressBar(R.id.status_progress,
-                mLocalPaths.length - 1, 0, false);
-        notification.contentView.setImageViewResource(R.id.status_icon,
-                R.drawable.icon);
+        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
+        mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
+        mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
+        mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
 
-        mNotificationManager.notify(42, notification);
+        mNotificationManager.notify(42, mNotification);
 
         WebdavClient wc = new WebdavClient(ocUri);
+        wc.allowUnsignedCertificates();
+        wc.setUploadListener(this);
         wc.setCredentials(username, password);
 
         for (int i = 0; i < mLocalPaths.length; ++i) {
+            File f = new File(mLocalPaths[i]);
+            mTotalDataToSend += f.length();
+        }
+        
+        Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files");
+        
+        for (int i = 0; i < mLocalPaths.length; ++i) {
             String mimeType = MimeTypeMap.getSingleton()
                     .getMimeTypeFromExtension(
                             mLocalPaths[i].substring(mLocalPaths[i]
                                     .lastIndexOf('.') + 1));
             mResult = false;
+            mCurrentIndexUpload = i;
             if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
                 mResult |= true;
             }
-            notification.contentView.setProgressBar(R.id.status_progress,
-                    mLocalPaths.length - 1, i + 1, false);
-
-            mNotificationManager.notify(42, notification);
         }
         // notification.contentView.setProgressBar(R.id.status_progress,
         // mLocalPaths.length-1, mLocalPaths.length-1, false);
         mNotificationManager.cancel(42);
         run();
     }
+
+    @Override
+    public void OnUploadProgress(long currentProgress) {
+        mSendData += currentProgress;
+        int percent = (int)(100*mSendData/mTotalDataToSend);
+        String text = String.format("%d%% Uploading %s file", percent, new File(mLocalPaths[mCurrentIndexUpload]).getName());
+        mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false);
+        mNotification.contentView.setTextViewText(R.id.status_text, text);
+        mNotificationManager.notify(42, mNotification);
+    }
 }
index 3fc0ab0..e466aa1 100644 (file)
@@ -16,6 +16,7 @@ public class FileRequestEntity implements RequestEntity {
 
     final File file;
     final String contentType;
+    OnUploadProgressListener listener;
 
     public FileRequestEntity(final File file, final String contentType) {
         super();
@@ -25,7 +26,7 @@ public class FileRequestEntity implements RequestEntity {
         this.file = file;
         this.contentType = contentType;
     }
-
+    
     public long getContentLength() {
         return this.file.length();
     }
@@ -37,6 +38,10 @@ public class FileRequestEntity implements RequestEntity {
     public boolean isRepeatable() {
         return true;
     }
+    
+    public void setOnUploadProgressListener(OnUploadProgressListener listener) {
+        this.listener = listener;
+    }
 
     public void writeRequest(final OutputStream out) throws IOException {
         byte[] tmp = new byte[4096];
@@ -45,6 +50,8 @@ public class FileRequestEntity implements RequestEntity {
         try {
             while ((i = instream.read(tmp)) >= 0) {
                 out.write(tmp, 0, i);
+                if (listener != null) 
+                    listener.OnUploadProgress(i);
             }
         } finally {
             instream.close();
index 3338f01..66f27e4 100644 (file)
@@ -47,6 +47,7 @@ public class WebdavClient extends HttpClient {
     private Credentials mCredentials;\r
     final private static String TAG = "WebdavClient";\r
     private static final String USER_AGENT = "Android-ownCloud";\r
+    private OnUploadProgressListener mUploadProgressListener;\r
 \r
     public WebdavClient(Uri uri) {\r
         mUri = uri;\r
@@ -104,6 +105,10 @@ public class WebdavClient extends HttpClient {
         return true;\r
     }\r
 \r
+    public void setUploadListener(OnUploadProgressListener listener) {\r
+        mUploadProgressListener = listener;\r
+    }\r
+    \r
     public boolean putFile(String localFile, String remoteTarget,\r
             String contentType) {\r
         boolean result = true;\r
@@ -111,7 +116,8 @@ public class WebdavClient extends HttpClient {
         try {\r
             Log.e("ASD", contentType + "");\r
             File f = new File(localFile);\r
-            RequestEntity entity = new FileRequestEntity(f, contentType);\r
+            FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
+            entity.setOnUploadProgressListener(mUploadProgressListener);\r
             Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
             PutMethod put = new PutMethod(mUri.toString() + remoteTarget);\r
             put.setRequestEntity(entity);\r