make confirmation dialog more universal
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / FileDownloader.java
index a5ee28f..cb5d0c8 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,21 +20,22 @@ 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 android.widget.Toast;\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.owncloud.syncadapter.FileSyncService;\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 EXTRA_DOWNLOAD_RESULT = "RESULT";    \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
+    public static final String ACCOUNT_NAME = "ACCOUNT_NAME";    \r
     private static final String TAG = "FileDownloader";\r
 \r
     private NotificationManager mNotificationMngr;\r
@@ -43,6 +43,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
@@ -59,6 +60,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             stopSelf(msg.arg1);\r
         }\r
     }\r
+    \r
+    public static final String getSavePath() {\r
+        File sdCard = Environment.getExternalStorageDirectory();\r
+        return sdCard.getAbsolutePath() + "/owncloud/";\r
+    }\r
+    \r
+    public static final String getTemporalPath() {\r
+        File sdCard = Environment.getExternalStorageDirectory();\r
+        return sdCard.getAbsolutePath() + "/owncloud.tmp/";\r
+    }\r
 \r
     @Override\r
     public void onCreate() {\r
@@ -85,6 +96,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 +108,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,34 +132,56 @@ 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
-        File sdCard = Environment.getExternalStorageDirectory();\r
-        File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath);\r
-        try {\r
-            file.createNewFile();\r
-        } catch (IOException e) {\r
-            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
-            ContentValues cv = new ContentValues();\r
-            cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
-            getContentResolver().update(\r
+        // download in a temporal file\r
+        File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath);\r
+        tmpFile.getParentFile().mkdirs();\r
+\r
+        boolean download_result = false;\r
+        File newFile = null;\r
+        if (wdc.downloadFile(mRemotePath, tmpFile)) {\r
+            newFile = new File(getSavePath() + mAccount.name + mFilePath);\r
+            newFile.getParentFile().mkdirs();\r
+            boolean moved = tmpFile.renameTo(newFile);\r
+            \r
+            if (moved) {\r
+                ContentValues cv = new ContentValues();\r
+                cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newFile.getAbsolutePath());\r
+                getContentResolver().update(\r
                     ProviderTableMeta.CONTENT_URI,\r
                     cv,\r
                     ProviderTableMeta.FILE_NAME + "=? AND "\r
                             + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",\r
                     new String[] {\r
                             mFilePath.substring(mFilePath.lastIndexOf('/') + 1),\r
-                            mAccount.name });            \r
+                            mAccount.name });\r
+                download_result = true;\r
+            }\r
+        }\r
+        \r
+        if (!download_result) {\r
+            tmpFile.delete();\r
         }\r
+        \r
         mNotificationMngr.cancel(1);\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
+        end.putExtra(EXTRA_REMOTE_PATH, mRemotePath);\r
+        end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath());\r
+        end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result);\r
+        end.putExtra(ACCOUNT_NAME, mAccount.name);\r
         sendBroadcast(end);\r
+\r
+        if (download_result) {\r
+            Toast.makeText(this, R.string.downloader_download_succeed , Toast.LENGTH_SHORT).show();\r
+        } else {\r
+            Toast.makeText(this, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
+        }\r
+        \r
     }\r
 \r
     @Override\r
@@ -166,5 +196,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         \r
         mLastPercent = percent;\r
     }\r
-\r
+    \r
+    \r
 }\r