Changed OCFile to keep mRemotePath as a valid URL; CLEAR YOUR CACHE AFTER INSTALLING
authorDavid A. Velasco <dvelasco@solidgear.es>
Mon, 25 Jun 2012 13:18:27 +0000 (15:18 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Mon, 25 Jun 2012 13:18:27 +0000 (15:18 +0200)
12 files changed:
AndroidManifest.xml
src/eu/alefzero/owncloud/Uploader.java
src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java
src/eu/alefzero/owncloud/datamodel/OCFile.java
src/eu/alefzero/owncloud/files/services/FileDownloader.java
src/eu/alefzero/owncloud/files/services/FileUploader.java
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java
src/eu/alefzero/owncloud/ui/adapter/FileListActionListAdapter.java
src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java
src/eu/alefzero/webdav/WebdavClient.java

index a6b5e6a..b056899 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.131B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.132B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 3599579..757f803 100644 (file)
@@ -18,7 +18,6 @@
 package eu.alefzero.owncloud;\r
 \r
 import java.io.File;\r
-import java.net.URLEncoder;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 import java.util.LinkedList;\r
@@ -215,7 +214,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
         EditText mDirname;\r
 \r
         public a(String path, EditText dirname) {\r
-            mPath = path;\r
+            mPath = path; \r
             mDirname = dirname;\r
         }\r
 \r
@@ -239,6 +238,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
     }\r
 \r
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {\r
+        // click on folder in the list\r
         Log.d(TAG, "on item click");\r
         Vector<OCFile> tmpfiles = mStorageManager.getDirectoryContent(mFile);\r
         if (tmpfiles == null) return;\r
@@ -255,11 +255,13 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
     }\r
 \r
     public void onClick(View v) {\r
+        // click on button\r
         switch (v.getId()) {\r
         case R.id.uploader_choose_folder:\r
-            mUploadPath = "";\r
+            mUploadPath = "/";\r
             for (String p : mParents)\r
-                mUploadPath += URLEncoder.encode(p) + "/";\r
+                mUploadPath += p + "/";\r
+            mUploadPath = Uri.encode(mUploadPath, "/");\r
             Log.d(TAG, "Uploading file to dir " + mUploadPath);\r
 \r
             uploadFiles();\r
@@ -408,11 +410,11 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
                 final String display_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),\r
                              data = c.getString(c.getColumnIndex(Media.DATA));\r
                 local[i] = data;\r
-                remote[i] = mUploadPath + display_name;\r
+                remote[i] = mUploadPath + Uri.encode(display_name);\r
             } else if (uri.getScheme().equals("file")) {\r
                 final File file = new File(Uri.decode(uri.toString()).replace(uri.getScheme() + "://", ""));\r
                 local[i] = file.getAbsolutePath();\r
-                remote[i] = mUploadPath + file.getName();\r
+                remote[i] = mUploadPath + Uri.encode(file.getName());\r
             }\r
 \r
         }\r
index ed22d3e..0ad0d1b 100644 (file)
@@ -288,9 +288,9 @@ public class FileDataStorageManager implements DataStorageManager {
                 file.setStoragePath(c.getString(c
                         .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
                 if (file.getStoragePath() == null) {
-                    // try to find exisiting file and bind it with current account
+                    // try to find existing file and bind it with current account
                     File sdCard = Environment.getExternalStorageDirectory();
-                    File f = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + file.getRemotePath());
+                    File f = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + file.getURLDecodedRemotePath());
                     if (f.exists())
                         file.setStoragePath(f.getAbsolutePath());
                 }
index 6861933..3a720b9 100644 (file)
 package eu.alefzero.owncloud.datamodel;
 
 import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
 
+import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -52,10 +55,19 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
      * Create new {@link OCFile} with given path
      * 
      * @param path The remote path of the file
+     * @throws MalformedURLException 
      */
     public OCFile(String path) {
         resetData();
         mNeedsUpdating = false;
+        // dvelasco: let's make mandatory that mRemotePath is a valid URL always; this will make our life easier with the URL-encoding/decoding
+        if (path != null && path.length() > 0) {
+            try {
+                new URL("http://silly.test.com:8888" + path);
+            } catch (MalformedURLException e) {
+                throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path , e);
+            }
+        } else throw new RuntimeException("Trying to create a OCFile with a non valid remote path: " + path);
         mRemotePath = path;
     }
 
@@ -95,6 +107,15 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
 
     /**
+     * Returns the remote path of the file on ownCloud
+     * 
+     * @return The remote path to the file
+     */
+    public String getURLDecodedRemotePath() {
+        return Uri.decode(mRemotePath);
+    }
+
+    /**
      * Can be used to check, whether or not this file exists in the database
      * already
      * 
@@ -182,11 +203,8 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
      * @return The name of the file
      */
     public String getFileName() {
-        if (mRemotePath != null) {
-            File f = new File(mRemotePath);
-            return f.getName().equals("") ? "/" : f.getName();
-        }
-        return null;
+        File f = new File(getURLDecodedRemotePath());
+        return f.getName().length() == 0 ? "/" : f.getName();
     }
 
     /**
@@ -324,13 +342,13 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     @Override
     public int compareTo(OCFile another) {
         if (isDirectory() && another.isDirectory()) {
-            return getFileName().toLowerCase().compareTo(another.getFileName().toLowerCase());
+            return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
         } else if (isDirectory()) {
             return -1;
         } else if (another.isDirectory()) {
             return 1;
         }
-        return getFileName().toLowerCase().compareTo(another.getFileName().toLowerCase());
+        return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
     }
 
     public boolean equals(Object o) {
index c051fc1..323faf4 100644 (file)
@@ -35,6 +35,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH";\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 +44,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 +87,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
@@ -141,7 +144,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 \r
         Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString());\r
         Log.e(TAG, mFilePath+"");\r
-        if (wdc.downloadFile(mFilePath, file)) {\r
+        if (wdc.downloadFile(mRemotePath, file)) {\r
             ContentValues cv = new ContentValues();\r
             cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath());\r
             getContentResolver().update(\r
index c34e79c..5d748c9 100644 (file)
@@ -1,7 +1,6 @@
 package eu.alefzero.owncloud.files.services;
 
 import java.io.File;
-import java.net.URLDecoder;
 
 import eu.alefzero.owncloud.AccountUtils;
 import eu.alefzero.owncloud.R;
@@ -124,7 +123,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             Toast.makeText(this, "Upload successfull", Toast.LENGTH_SHORT)
                     .show();
         } else {
-            Toast.makeText(this, "No i kupa", Toast.LENGTH_SHORT).show();
+            Toast.makeText(this, "Upload could not be completed", Toast.LENGTH_SHORT).show();
         }
     }
 
@@ -176,14 +175,13 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             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
+                OCFile new_file = new OCFile(mRemotePaths[i]);
                 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]));
+                File f = new File(mRemotePaths[i]);
                 new_file.setParentId(storageManager.getFileByPath(f.getParent().endsWith("/")?f.getParent():f.getParent()+"/").getFileId());
                 storageManager.saveFile(new_file);
             }
index 85f9cbb..d5512a1 100644 (file)
@@ -19,7 +19,6 @@
 package eu.alefzero.owncloud.syncadapter;\r
 \r
 import java.io.IOException;\r
-import java.net.URLDecoder;\r
 import java.util.Vector;\r
 \r
 import org.apache.jackrabbit.webdav.DavException;\r
@@ -140,7 +139,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     }\r
 \r
     private OCFile fillOCFile(WebdavEntry we) {\r
-        OCFile file = new OCFile(URLDecoder.decode(we.path()));\r
+        OCFile file = new OCFile(we.path());\r
         file.setCreationTimestamp(we.createTimestamp());\r
         file.setFileLength(we.contentLength());\r
         file.setMimetype(we.contentType());\r
index b08c3ab..25cb714 100644 (file)
@@ -22,7 +22,6 @@ import java.io.BufferedReader;
 import java.io.File;\r
 import java.io.InputStreamReader;\r
 import java.lang.Thread.UncaughtExceptionHandler;\r
-import java.net.URLEncoder;\r
 import java.util.ArrayList;\r
 \r
 import android.accounts.Account;\r
@@ -223,11 +222,12 @@ 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
-                    remotepath += "/" + URLEncoder.encode(mDirectories.getItem(j));\r
+                    remotepath += "/" + mDirectories.getItem(j);\r
                 }\r
                 if (!remotepath.endsWith("/"))\r
                     remotepath += "/";\r
-                remotepath += URLEncoder.encode(new File(filepath).getName());\r
+                remotepath += new File(filepath).getName();\r
+                remotepath = Uri.encode(remotepath, "/");\r
     \r
                 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);\r
                 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);\r
@@ -309,9 +309,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             \r
                 // Clear intent extra, so rotating the screen will not return us to this directory\r
                 getIntent().removeExtra(FileDetailFragment.EXTRA_FILE);\r
-            } else {\r
-                mCurrentDir = mStorageManager.getFileByPath("/");\r
             }\r
+            \r
+            if (mCurrentDir == null)\r
+                mCurrentDir = mStorageManager.getFileByPath("/");\r
                 \r
             // Drop-Down navigation and file list restore\r
             mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);\r
@@ -428,7 +429,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                             path = FileDisplayActivity.this.mCurrentDir.getRemotePath();\r
                             \r
                             // Create directory\r
-                            path += directoryName + "/";\r
+                            path += Uri.encode(directoryName) + "/";\r
                             Thread thread = new Thread(new DirectoryCreator(path, a));\r
                             thread.start();\r
     \r
index 3673155..d0d5b5f 100644 (file)
@@ -84,7 +84,7 @@ public class FileListActionListAdapter implements ListAdapter {
                         .getSystemService(Context.ACCOUNT_SERVICE);
                 String ocurl = accm.getUserData(mAccount,
                         AccountAuthenticator.KEY_OC_URL);
-                ocurl += mFilePath + mFilename;
+                ocurl += mFilePath + Uri.encode(mFilename);
                 intent.setData(Uri.parse(ocurl));
             } else {
                 intent.putExtra("toDownload", false);
index 7e10265..fc28e1f 100644 (file)
@@ -17,7 +17,6 @@
  */\r
 package eu.alefzero.owncloud.ui.adapter;\r
 \r
-import java.net.URLDecoder;\r
 import java.util.Vector;\r
 \r
 import eu.alefzero.owncloud.DisplayUtils;\r
index 932fa51..21d1241 100644 (file)
@@ -144,7 +144,8 @@ public class FileDetailFragment extends SherlockFragment implements
         Intent i = new Intent(getActivity(), FileDownloader.class);\r
         i.putExtra(FileDownloader.EXTRA_ACCOUNT,\r
                 mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));\r
-        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());\r
+        i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
+        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getURLDecodedRemotePath());\r
         i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
         getActivity().startService(i);\r
     }\r
index a8d22ce..907de67 100644 (file)
@@ -21,7 +21,6 @@ import java.io.BufferedInputStream;
 import java.io.File;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
-import java.net.URLEncoder;\r
 \r
 import org.apache.commons.httpclient.Credentials;\r
 import org.apache.commons.httpclient.HttpClient;\r
@@ -71,19 +70,23 @@ public class WebdavClient extends HttpClient {
                 new EasySSLSocketFactory(), 443));\r
     }\r
 \r
-    public boolean downloadFile(String filepath, File targetPath) {\r
+    public boolean downloadFile(String remoteFilepath, File targetPath) {\r
         // HttpGet get = new HttpGet(mUri.toString() + filepath.replace(" ",\r
         // "%20"));\r
-        String[] splitted_filepath = filepath.split("/");\r
-        filepath = "";\r
+        /* dvelasco - this is not necessary anymore; OCFile.mRemotePath (the origin of remoteFielPath) keeps valid URL strings\r
+        String[] splitted_filepath = remoteFilepath.split("/");\r
+        remoteFilepath = "";\r
         for (String s : splitted_filepath) {\r
             if (s.equals("")) continue;\r
-            filepath += "/" + URLEncoder.encode(s);\r
+            remoteFilepath += "/" + URLEncoder.encode(s);\r
         }\r
 \r
-        Log.e("ASD", mUri.toString() + filepath.replace(" ", "%20") + "");\r
+        Log.e("ASD", mUri.toString() + remoteFilepath.replace(" ", "%20") + "");\r
         GetMethod get = new GetMethod(mUri.toString()\r
-                + filepath.replace(" ", "%20"));\r
+                + remoteFilepath.replace(" ", "%20"));\r
+        */\r
+        \r
+        GetMethod get = new GetMethod(mUri.toString() + remoteFilepath);\r
 \r
         // get.setHeader("Host", mUri.getHost());\r
         // get.setHeader("User-Agent", "Android-ownCloud");\r
@@ -155,8 +158,7 @@ public class WebdavClient extends HttpClient {
 \r
     public boolean createDirectory(String path) {\r
         try {\r
-            MkColMethod mkcol = new MkColMethod(mUri.toString() + "/" + path\r
-                    + "/");\r
+            MkColMethod mkcol = new MkColMethod(mUri.toString() + path);\r
             int status = executeMethod(mkcol);\r
             Log.d(TAG, "Status returned " + status);\r
             Log.d(TAG, "uri: " + mkcol.getURI().toString());\r