version bump
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileUploader.java
index a814fa8..c34e79c 100644 (file)
@@ -1,10 +1,14 @@
 package eu.alefzero.owncloud.files.services;
 
 import java.io.File;
+import java.net.URLDecoder;
 
 import eu.alefzero.owncloud.AccountUtils;
 import eu.alefzero.owncloud.R;
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
+import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
+import eu.alefzero.owncloud.datamodel.OCFile;
+import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
 import eu.alefzero.owncloud.utils.OwnCloudVersion;
 import eu.alefzero.webdav.OnUploadProgressListener;
 import eu.alefzero.webdav.WebdavClient;
@@ -12,6 +16,7 @@ import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.Notification;
 import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Intent;
 import android.net.Uri;
@@ -26,7 +31,7 @@ import android.webkit.MimeTypeMap;
 import android.widget.RemoteViews;
 import android.widget.Toast;
 
-public class FileUploader extends Service implements OnUploadProgressListener {
+public class FileUploader extends Service implements OnDatatransferProgressListener {
 
     public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
     public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
@@ -47,7 +52,7 @@ public class FileUploader extends Service implements OnUploadProgressListener {
     private int mUploadType;
     private Notification mNotification;
     private int mTotalDataToSend, mSendData;
-    private int mCurrentIndexUpload;
+    private int mCurrentIndexUpload, mPreviousPercent;
 
     @Override
     public IBinder onBind(Intent arg0) {
@@ -133,8 +138,9 @@ public class FileUploader extends Service implements OnUploadProgressListener {
         String username = mAccount.name.substring(0,
                 mAccount.name.lastIndexOf('@'));
         String password = mAccountManager.getPassword(mAccount);
+        FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, getContentResolver());
         
-        mTotalDataToSend = mSendData = 0;
+        mTotalDataToSend = mSendData = mPreviousPercent = 0;
         
         mNotification = new Notification(
                 eu.alefzero.owncloud.R.drawable.icon, "Uploading...",
@@ -143,12 +149,15 @@ public class FileUploader extends Service implements OnUploadProgressListener {
         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);
-
+        // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;
+        //              BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on an upload in the notification bar
+        mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
+        
         mNotificationManager.notify(42, mNotification);
 
         WebdavClient wc = new WebdavClient(ocUri);
-        wc.allowUnsignedCertificates();
-        wc.setUploadListener(this);
+        wc.allowSelfsignedCertificates();
+        wc.setDataTransferProgressListener(this);
         wc.setCredentials(username, password);
 
         for (int i = 0; i < mLocalPaths.length; ++i) {
@@ -167,6 +176,16 @@ public class FileUploader extends Service implements OnUploadProgressListener {
             mCurrentIndexUpload = i;
             if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
                 mResult |= true;
+                String decRemotePath = URLDecoder.decode(mRemotePaths[i]);
+                OCFile new_file = new OCFile(decRemotePath);    // FyleSyncAdapter and this MUST use the same encoding when creating a new OCFile
+                new_file.setMimetype(mimeType);
+                new_file.setFileLength(new File(mLocalPaths[i]).length());
+                new_file.setModificationTimestamp(System.currentTimeMillis());
+                new_file.setLastSyncDate(0);
+                new_file.setStoragePath(mLocalPaths[i]);         
+                File f = new File(URLDecoder.decode(mRemotePaths[i]));
+                new_file.setParentId(storageManager.getFileByPath(f.getParent().endsWith("/")?f.getParent():f.getParent()+"/").getFileId());
+                storageManager.saveFile(new_file);
             }
         }
         // notification.contentView.setProgressBar(R.id.status_progress,
@@ -176,12 +195,15 @@ public class FileUploader extends Service implements OnUploadProgressListener {
     }
 
     @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);
+    public void transferProgress(long progressRate) {
+        mSendData += progressRate;
+        int percent = (int)(100*((double)mSendData)/((double)mTotalDataToSend));
+        if (percent != mPreviousPercent) {
+            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);
+        }
+        mPreviousPercent = percent;
     }
 }