Avoid crashes when an external app makes a bad response for the selection of a file...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
index c94c27f..81a8da1 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.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
 \r
 import com.actionbarsherlock.app.ActionBar;\r
 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;\r
@@ -111,14 +112,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         super.onCreate(savedInstanceState);\r
 \r
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
         super.onCreate(savedInstanceState);\r
 \r
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
-        setSupportProgressBarIndeterminateVisibility(false);\r
 \r
         Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));\r
 \r
         if(savedInstanceState != null) {\r
             mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY);\r
             mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);\r
 \r
         Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));\r
 \r
         if(savedInstanceState != null) {\r
             mDirs = savedInstanceState.getStringArray(KEY_DIR_ARRAY);\r
             mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);\r
-            mDirectories.add("/");\r
+            mDirectories.add(OCFile.PATH_SEPARATOR);\r
             if (mDirs != null)\r
                 for (String s : mDirs)\r
                     mDirectories.insert(s, 0);\r
             if (mDirs != null)\r
                 for (String s : mDirs)\r
                     mDirectories.insert(s, 0);\r
@@ -143,7 +143,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             setContentView(R.layout.no_account_available);\r
             getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE);\r
             findViewById(R.id.setup_account).setOnClickListener(this);\r
             setContentView(R.layout.no_account_available);\r
             getSupportActionBar().setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE);\r
             findViewById(R.id.setup_account).setOnClickListener(this);\r
-            \r
+\r
+            setSupportProgressBarIndeterminateVisibility(false);\r
+\r
             Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);\r
             intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });\r
             startActivity(intent);  // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished;\r
             Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);\r
             intent.putExtra(android.provider.Settings.EXTRA_AUTHORITIES, new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });\r
             startActivity(intent);  // although the code is here, the activity won't be created until this.onStart() and this.onResume() are finished;\r
@@ -169,8 +171,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             case R.id.startSync: {\r
                 break;\r
             }\r
             case R.id.startSync: {\r
-                // This could be interesting\r
-                //ContentResolver.cancelSync(null, "org.owncloud");   // cancel the current synchronizations of any other ownCloud account\r
+                ContentResolver.cancelSync(null, "org.owncloud");   // cancel the current synchronizations of any ownCloud account\r
                 Bundle bundle = new Bundle();\r
                 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
                 ContentResolver.requestSync(\r
                 Bundle bundle = new Bundle();\r
                 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
                 ContentResolver.requestSync(\r
@@ -223,20 +224,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
     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
                 }\r
     \r
                 Intent i = new Intent(this, FileUploader.class);\r
@@ -244,12 +254,11 @@ 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 += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);\r
                 }\r
                 }\r
-                if (!remotepath.endsWith("/"))\r
-                    remotepath += "/";\r
+                if (!remotepath.endsWith(OCFile.PATH_SEPARATOR))\r
+                    remotepath += OCFile.PATH_SEPARATOR;\r
                 remotepath += 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
     \r
                 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);\r
                 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);\r
@@ -369,7 +378,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 for (String s : mDirs)\r
                     mDirectories.add(s);\r
             } else {\r
                 for (String s : mDirs)\r
                     mDirectories.add(s);\r
             } else {\r
-                mDirectories.add("/");\r
+                mDirectories.add(OCFile.PATH_SEPARATOR);\r
             }\r
                \r
             // Actionbar setup\r
             }\r
                \r
             // Actionbar setup\r
@@ -456,16 +465,16 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                             String path;\r
                             if (mCurrentDir == null) {\r
                                 // this is just a patch; we should ensure that mCurrentDir never is null\r
                             String path;\r
                             if (mCurrentDir == null) {\r
                                 // this is just a patch; we should ensure that mCurrentDir never is null\r
-                                if (!mStorageManager.fileExists("/")) {\r
-                                    OCFile file = new OCFile("/");\r
+                                if (!mStorageManager.fileExists(OCFile.PATH_SEPARATOR)) {\r
+                                    OCFile file = new OCFile(OCFile.PATH_SEPARATOR);\r
                                     mStorageManager.saveFile(file);\r
                                 }\r
                                     mStorageManager.saveFile(file);\r
                                 }\r
-                                mCurrentDir = mStorageManager.getFileByPath("/");\r
+                                mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);\r
                             }\r
                             path = FileDisplayActivity.this.mCurrentDir.getRemotePath();\r
                             \r
                             // Create directory\r
                             }\r
                             path = FileDisplayActivity.this.mCurrentDir.getRemotePath();\r
                             \r
                             // Create directory\r
-                            path += Uri.encode(directoryName) + "/";\r
+                            path += directoryName + OCFile.PATH_SEPARATOR;\r
                             Thread thread = new Thread(new DirectoryCreator(path, a));\r
                             thread.start();\r
     \r
                             Thread thread = new Thread(new DirectoryCreator(path, a));\r
                             thread.start();\r
     \r
@@ -762,7 +771,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
             transaction.commit();\r
         }\r
             transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
             transaction.commit();\r
         }\r
-\r
+        setSupportProgressBarIndeterminateVisibility(false);\r
     }\r
     \r
 \r
     }\r
     \r
 \r