adding open file button
authorBartek Przybylski <bart.p.pl@gmail.com>
Sat, 19 May 2012 19:05:53 +0000 (21:05 +0200)
committerBartek Przybylski <bart.p.pl@gmail.com>
Sat, 19 May 2012 19:05:53 +0000 (21:05 +0200)
src/eu/alefzero/owncloud/FileDownloader.java
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java
src/eu/alefzero/webdav/WebdavClient.java

index b8fffbc..3b5f198 100644 (file)
@@ -1,6 +1,7 @@
 package eu.alefzero.owncloud;\r
 \r
 import java.io.File;\r
 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
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
@@ -22,6 +23,7 @@ import android.util.Log;
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;\r
 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\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 {\r
 import eu.alefzero.webdav.WebdavClient;\r
 \r
 public class FileDownloader extends Service {\r
@@ -82,10 +84,13 @@ public class FileDownloader extends Service {
 \r
     void downloadFile() {\r
         AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
 \r
     void downloadFile() {\r
         AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);\r
-        Uri oc_url = Uri.parse(am.getUserData(mAccount,\r
-                AccountAuthenticator.KEY_OC_URL));\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
 \r
-        WebdavClient wdc = new WebdavClient(oc_url);\r
+        WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path));\r
 \r
         String username = mAccount.name.split("@")[0];\r
         String password = "";\r
 \r
         String username = mAccount.name.split("@")[0];\r
         String password = "";\r
@@ -94,6 +99,7 @@ public class FileDownloader extends Service {
                     AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
         } catch (Exception e) {\r
             e.printStackTrace();\r
                     AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
         } catch (Exception e) {\r
             e.printStackTrace();\r
+            return;\r
         }\r
 \r
         wdc.setCredentials(username, password);\r
         }\r
 \r
         wdc.setCredentials(username, password);\r
@@ -113,17 +119,19 @@ public class FileDownloader extends Service {
         File file = new File(dir, mFilePath.replace('/', '.'));\r
 \r
         Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());\r
         File file = new File(dir, mFilePath.replace('/', '.'));\r
 \r
         Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());\r
-        wdc.downloadFile(mFilePath, file);\r
-        ContentValues cv = new ContentValues();\r
-        cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.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
+        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
+                    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
+        }\r
         nm.cancel(1);\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
         sendBroadcast(end);\r
         nm.cancel(1);\r
         Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE);\r
         sendBroadcast(end);\r
index 1e1a5e3..8bd0a57 100644 (file)
@@ -47,6 +47,8 @@ import eu.alefzero.webdav.WebdavEntry;
  */\r
 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
 \r
  */\r
 public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {\r
 \r
+    private final static String TAG = "FileSyncAdapter"; \r
+    \r
     private long mCurrentSyncTime;\r
     \r
     public FileSyncAdapter(Context context, boolean autoInitialize) {\r
     private long mCurrentSyncTime;\r
     \r
     public FileSyncAdapter(Context context, boolean autoInitialize) {\r
@@ -63,7 +65,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         this.setStorageManager(new FileDataStorageManager(account,\r
                 getContentProvider()));\r
 \r
         this.setStorageManager(new FileDataStorageManager(account,\r
                 getContentProvider()));\r
 \r
-        Log.d("ASD", "syncing owncloud account " + account.name);\r
+        Log.d(TAG, "syncing owncloud account " + account.name);\r
 \r
         Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
         i.putExtra(FileSyncService.IN_PROGRESS, true);\r
 \r
         Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
         i.putExtra(FileSyncService.IN_PROGRESS, true);\r
index 4ca240d..e7ee4cc 100644 (file)
@@ -19,6 +19,7 @@
 package eu.alefzero.owncloud.ui.activity;\r
 \r
 import java.io.File;\r
 package eu.alefzero.owncloud.ui.activity;\r
 \r
 import java.io.File;\r
+import java.net.URLDecoder;\r
 import java.net.URLEncoder;\r
 \r
 import android.accounts.Account;\r
 import java.net.URLEncoder;\r
 \r
 import android.accounts.Account;\r
@@ -298,7 +299,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                         AccountUtils.getCurrentOwnCloudAccount(this));\r
                 String remotepath = new String();\r
                 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {\r
                         AccountUtils.getCurrentOwnCloudAccount(this));\r
                 String remotepath = new String();\r
                 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {\r
-                    remotepath += "/" + mDirectories.getItem(j);\r
+                    remotepath += "/" + URLDecoder.decode(mDirectories.getItem(j));\r
                 }\r
                 if (!remotepath.endsWith("/"))\r
                     remotepath += "/";\r
                 }\r
                 if (!remotepath.endsWith("/"))\r
                     remotepath += "/";\r
index 5fd4003..7d10eba 100644 (file)
@@ -23,7 +23,9 @@ import android.content.Intent;
 import android.content.IntentFilter;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
 import android.content.IntentFilter;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
+import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.os.Bundle;\r
+import android.util.Log;\r
 import android.view.LayoutInflater;\r
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
 import android.view.LayoutInflater;\r
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
@@ -56,6 +58,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private int mLayout;\r
     private View mView;\r
     private OCFile mFile;\r
     private int mLayout;\r
     private View mView;\r
     private OCFile mFile;\r
+    private static final String TAG = "FileDetailFragment";\r
 \r
     /**\r
      * Default constructor - contains real layout\r
 \r
     /**\r
      * Default constructor - contains real layout\r
@@ -124,6 +127,7 @@ public class FileDetailFragment extends SherlockFragment implements
 \r
     private void updateFileDetails() {\r
         mFile = mIntent.getParcelableExtra(FILE);\r
 \r
     private void updateFileDetails() {\r
         mFile = mIntent.getParcelableExtra(FILE);\r
+        Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
 \r
         if (mFile != null) {\r
             // set file details\r
 \r
         if (mFile != null) {\r
             // set file details\r
@@ -131,20 +135,31 @@ public class FileDetailFragment extends SherlockFragment implements
             setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
                     .getMimetype()));\r
             setFilesize(mFile.getFileLength());\r
             setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
                     .getMimetype()));\r
             setFilesize(mFile.getFileLength());\r
-            \r
             // Update preview\r
             if (mFile.getStoragePath() != null) {\r
             // Update preview\r
             if (mFile.getStoragePath() != null) {\r
-                if (mFile.getMimetype().startsWith("image/")) {\r
-                    ImageView preview = (ImageView) getView().findViewById(\r
-                            R.id.fdPreview);\r
-                    Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
-                    preview.setImageBitmap(bmp);\r
+                try {\r
+                    if (mFile.getMimetype().startsWith("image/")) {\r
+                        ImageView preview = (ImageView) getView().findViewById(\r
+                                R.id.fdPreview);\r
+                        Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
+                        preview.setImageBitmap(bmp);\r
+                    }\r
+                } catch (OutOfMemoryError e) {\r
+                    Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());\r
                 }\r
                 }\r
+                downloadButton.setText("Open file");\r
+                downloadButton.setOnClickListener(new OnClickListener() {\r
+                    @Override\r
+                    public void onClick(View v) {\r
+                        Intent i = new Intent(Intent.ACTION_VIEW);\r
+                        i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());\r
+                        startActivity(i);\r
+                    }\r
+                });\r
+            } else {\r
+                // Make download button effective\r
+                downloadButton.setOnClickListener(this);\r
             }\r
             }\r
-            \r
-            // Make download button effective\r
-            Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
-            downloadButton.setOnClickListener(this);\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r
index 6e1d287..3338f01 100644 (file)
@@ -75,16 +75,16 @@ public class WebdavClient extends HttpClient {
         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
         // "%20"));\r
 \r
         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
         // "%20"));\r
 \r
-        Log.e("ASD", mUri.toString() + URLDecoder.decode(filepath) + "");\r
+        Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");\r
         GetMethod get = new GetMethod(mUri.toString()\r
         GetMethod get = new GetMethod(mUri.toString()\r
-                + URLEncoder.encode(filepath));\r
+                + filepath.replace(" ", "%20"));\r
 \r
         // get.setHeader("Host", mUri.getHost());\r
         // get.setHeader("User-Agent", "Android-ownCloud");\r
 \r
         try {\r
 \r
         // get.setHeader("Host", mUri.getHost());\r
         // get.setHeader("User-Agent", "Android-ownCloud");\r
 \r
         try {\r
-            Log.e("ASD", get.toString());\r
             int status = executeMethod(get);\r
             int status = executeMethod(get);\r
+            Log.e(TAG, "status return: " + status);\r
             if (status != HttpStatus.SC_OK) {\r
                 return false;\r
             }\r
             if (status != HttpStatus.SC_OK) {\r
                 return false;\r
             }\r