Quick patch to fix operations not ready for HTTPS
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
index 45f7c77..163fea4 100644 (file)
@@ -48,6 +48,7 @@ import android.view.ViewGroup;
 import android.widget.ArrayAdapter;\r
 import android.widget.EditText;\r
 import android.widget.TextView;\r
+import android.widget.Toast;\r
 \r
 import com.actionbarsherlock.app.ActionBar;\r
 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;\r
@@ -88,6 +89,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private DataStorageManager mStorageManager;\r
     private SyncBroadcastReceiver mSyncBroadcastReceiver;\r
     private UploadFinishReceiver mUploadFinishReceiver;\r
+    private DownloadFinishReceiver mDownloadFinishReceiver;\r
     \r
     private View mLayoutView = null;\r
     private FileListFragment mFileList;\r
@@ -223,20 +225,29 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     public void onActivityResult(int requestCode, int resultCode, Intent data) {\r
         if (requestCode == ACTION_SELECT_FILE) {\r
             if (resultCode == RESULT_OK) {\r
-                Uri selectedImageUri = data.getData();\r
-    \r
-                String filemanagerstring = selectedImageUri.getPath();\r
-                String selectedImagePath = getPath(selectedImageUri);\r
-                String filepath;\r
-    \r
-                if (selectedImagePath != null)\r
-                    filepath = selectedImagePath;\r
-                else\r
-                    filepath = filemanagerstring;\r
-    \r
-                if (filepath == null) {\r
-                    Log.e("FileDisplay", "Couldnt resolve path to file");\r
-                    return;\r
+                String filepath = null;\r
+                try {\r
+                    Uri selectedImageUri = data.getData();\r
+    \r
+                    String filemanagerstring = selectedImageUri.getPath();\r
+                    String selectedImagePath = getPath(selectedImageUri);\r
+    \r
+                    if (selectedImagePath != null)\r
+                        filepath = selectedImagePath;\r
+                    else\r
+                        filepath = filemanagerstring;\r
+                    \r
+                } catch (Exception e) {\r
+                    Log.e("FileDisplay", "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e);\r
+                    e.printStackTrace();\r
+                    \r
+                } finally {\r
+                    if (filepath == null) {\r
+                        Log.e("FileDisplay", "Couldnt resolve path to file");\r
+                        Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content), Toast.LENGTH_LONG);\r
+                        t.show();\r
+                        return;\r
+                    }\r
                 }\r
     \r
                 Intent i = new Intent(this, FileUploader.class);\r
@@ -319,6 +330,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);\r
             mUploadFinishReceiver = new UploadFinishReceiver();\r
             registerReceiver(mUploadFinishReceiver, uploadIntentFilter);\r
+            \r
+            // Listen for download messages\r
+            IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
+            mDownloadFinishReceiver = new DownloadFinishReceiver();\r
+            registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
         \r
             // Storage manager initialization\r
             mStorageManager = new FileDataStorageManager(\r
@@ -400,6 +416,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             unregisterReceiver(mUploadFinishReceiver);\r
             mUploadFinishReceiver = null;\r
         }\r
+        if (mDownloadFinishReceiver != null) {\r
+            unregisterReceiver(mDownloadFinishReceiver);\r
+            mDownloadFinishReceiver = null;\r
+        }\r
+        \r
         getIntent().putExtra(FileDetailFragment.EXTRA_FILE, mCurrentDir);\r
         Log.i(getClass().toString(), "onPause() end");\r
     }\r
@@ -618,13 +639,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         public void onReceive(Context context, Intent intent) {\r
             boolean inProgress = intent.getBooleanExtra(\r
                     FileSyncService.IN_PROGRESS, false);\r
-            String account_name = intent\r
+            String accountName = intent\r
                     .getStringExtra(FileSyncService.ACCOUNT_NAME);\r
 \r
-            Log.d("FileDisplay", "sync of account " + account_name\r
+            Log.d("FileDisplay", "sync of account " + accountName\r
                     + " is in_progress: " + inProgress);\r
 \r
-            if (account_name.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {  \r
+            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {  \r
             \r
                 String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); \r
                  \r
@@ -634,10 +655,12 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                     fillBlankRoot = (mCurrentDir != null);\r
                 }\r
 \r
-                if (synchFolderRemotePath != null && mCurrentDir != null && (mCurrentDir.getRemotePath().equals(synchFolderRemotePath) || fillBlankRoot) ) {\r
+                if ((synchFolderRemotePath != null && mCurrentDir != null && (mCurrentDir.getRemotePath().equals(synchFolderRemotePath)))\r
+                        || fillBlankRoot ) {\r
+                    if (!fillBlankRoot) \r
+                        mCurrentDir = getStorageManager().getFileByPath(synchFolderRemotePath);\r
                     FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager()\r
                             .findFragmentById(R.id.fileList);\r
-                    mCurrentDir = getStorageManager().getFileByPath(synchFolderRemotePath);\r
                     if (fileListFragment != null) {\r
                         fileListFragment.listDirectory(mCurrentDir);  \r
                     }\r
@@ -660,10 +683,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         public void onReceive(Context context, Intent intent) {\r
             long parentDirId = intent.getLongExtra(FileUploader.EXTRA_PARENT_DIR_ID, -1);\r
             OCFile parentDir = mStorageManager.getFileById(parentDirId);\r
-            \r
-            if (parentDir != null && (\r
-                    (mCurrentDir == null && parentDir.getFileName().equals("/")) ||\r
-                     parentDir.equals(mCurrentDir))\r
+            String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);\r
+\r
+            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) &&\r
+                    parentDir != null && \r
+                    (   (mCurrentDir == null && parentDir.getFileName().equals("/")) ||\r
+                            parentDir.equals(mCurrentDir)\r
+                    )\r
                 ) {\r
                 FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
                 if (fileListFragment != null) { \r
@@ -673,6 +699,26 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         }\r
         \r
     }\r
+    \r
+    \r
+    /**\r
+     * Once the file download has finished -> update view\r
+     */\r
+    private class DownloadFinishReceiver extends BroadcastReceiver {\r
+        @Override\r
+        public void onReceive(Context context, Intent intent) {\r
+            String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
+            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
+\r
+            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name) &&\r
+                     mCurrentDir != null && mCurrentDir.getFileId() == mStorageManager.getFileByPath(downloadedRemotePath).getParentId()) {\r
+                FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+                if (fileListFragment != null) { \r
+                    fileListFragment.listDirectory();\r
+                }\r
+            }\r
+        }\r
+    }\r
 \r
     \r
     @Override\r