Completing previous commit
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileDownloader.java
index cc13a67..d6d7780 100644 (file)
@@ -11,7 +11,6 @@ import android.app.PendingIntent;
 import android.app.Service;\r
 import android.content.ContentValues;\r
 import android.content.Intent;\r
-import android.net.Uri;\r
 import android.os.Environment;\r
 import android.os.Handler;\r
 import android.os.HandlerThread;\r
@@ -21,20 +20,18 @@ import android.os.Message;
 import android.os.Process;\r
 import android.util.Log;\r
 import android.widget.RemoteViews;\r
-import eu.alefzero.owncloud.AccountUtils;\r
 import eu.alefzero.owncloud.R;\r
-import eu.alefzero.owncloud.R.drawable;\r
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
 import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;\r
-import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
-import eu.alefzero.owncloud.utils.OwnCloudVersion;\r
 import eu.alefzero.webdav.WebdavClient;\r
 \r
 public class FileDownloader extends Service implements OnDatatransferProgressListener {\r
     public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";\r
+    public static final String BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD";    \r
     public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
     public static final String EXTRA_FILE_PATH = "FILE_PATH";\r
+    public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";\r
     public static final String EXTRA_FILE_SIZE = "FILE_SIZE";\r
     private static final String TAG = "FileDownloader";\r
 \r
@@ -43,6 +40,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private ServiceHandler mServiceHandler;\r
     private Account mAccount;\r
     private String mFilePath;\r
+    private String mRemotePath;\r
     private int mLastPercent;\r
     private long mTotalDownloadSize;\r
     private long mCurrentDownlodSize;\r
@@ -85,6 +83,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }\r
         mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);\r
         mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);\r
+        mRemotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);\r
         Message msg = mServiceHandler.obtainMessage();\r
         msg.arg1 = startId;\r
         mServiceHandler.sendMessage(msg);\r
@@ -96,13 +95,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 \r
     void downloadFile() {\r
         AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
-        String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);\r
-        OwnCloudVersion ocv = new OwnCloudVersion(am\r
-                .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
-        String webdav_path = AccountUtils.getWebdavPath(ocv);\r
-        Uri oc_url = Uri.parse(oc_base_url+webdav_path);\r
 \r
-        WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path));\r
+\r
+        WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext());\r
         \r
         String username = mAccount.name.split("@")[0];\r
         String password = "";\r
@@ -124,6 +119,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);\r
         mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1);\r
         mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);\r
+        // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;\r
+        //              BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar\r
+        mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);\r
         \r
         mNotificationMngr.notify(1, mNotification);\r
 \r
@@ -136,9 +134,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             e.printStackTrace();\r
         }\r
 \r
-        Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());\r
-        Log.e(TAG, mFilePath+"");\r
-        if (wdc.downloadFile(mFilePath, file)) {\r
+        String message;\r
+        if (wdc.downloadFile(mRemotePath, file)) {\r
             ContentValues cv = new ContentValues();\r
             cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
             getContentResolver().update(\r
@@ -149,9 +146,13 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                     new String[] {\r
                             mFilePath.substring(mFilePath.lastIndexOf('/') + 1),\r
                             mAccount.name });            \r
+            message = DOWNLOAD_FINISH_MESSAGE;\r
+        } else {\r
+            message = BAD_DOWNLOAD_MESSAGE;\r
         }\r
+        \r
         mNotificationMngr.cancel(1);\r
-        Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
+        Intent end = new Intent(message);\r
         end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath());\r
         sendBroadcast(end);\r
     }\r