Quick patch to fix operations not ready for HTTPS
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / FileDisplayActivity.java
index 2c4ae38..163fea4 100644 (file)
@@ -33,11 +33,13 @@ import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
+import android.content.SharedPreferences;\r
 import android.content.pm.PackageInfo;\r
 import android.content.pm.PackageManager.NameNotFoundException;\r
 import android.database.Cursor;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
+import android.preference.PreferenceManager;\r
 import android.provider.MediaStore;\r
 import android.support.v4.app.FragmentTransaction;\r
 import android.util.Log;\r
@@ -46,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
@@ -86,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
@@ -109,14 +113,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         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
-            mDirectories.add("/");\r
+            mDirectories.add(OCFile.PATH_SEPARATOR);\r
             if (mDirs != null)\r
                 for (String s : mDirs)\r
                     mDirectories.insert(s, 0);\r
@@ -129,12 +132,21 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             \r
             initDelayedTilAccountAvailabe();\r
             \r
+            // PIN CODE request ;  best location is to decide, let's try this first\r
+            //if (savedInstanceState == null) {\r
+            if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) {\r
+                requestPinCode();\r
+            }\r
+            \r
+            \r
         } else {\r
             \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
@@ -160,6 +172,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             case R.id.startSync: {\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
@@ -212,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
@@ -233,12 +255,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
-                    remotepath += "/" + mDirectories.getItem(j);\r
+                    remotepath += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);\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 = Uri.encode(remotepath, "/");\r
     \r
                 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);\r
                 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);\r
@@ -309,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
@@ -358,7 +384,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 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
@@ -390,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
@@ -445,16 +476,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
-                                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
-                                mCurrentDir = mStorageManager.getFileByPath("/");\r
+                                mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);\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
@@ -608,34 +639,37 @@ 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
-            Log.d("FileDisplay", "sync of account " + account_name\r
+\r
+            Log.d("FileDisplay", "sync of account " + accountName\r
                     + " is in_progress: " + inProgress);\r
-            setSupportProgressBarIndeterminateVisibility(inProgress);\r
+\r
+            if (accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {  \r
             \r
-            long OCDirId = intent.getLongExtra(FileSyncService.SYNC_FOLDER, -1);\r
-            if (OCDirId >= 0) {\r
-                OCFile syncDir = mStorageManager.getFileById(OCDirId);\r
-                if (syncDir != null && (\r
-                        (mCurrentDir == null && syncDir.getFileName().equals("/")) ||\r
-                         syncDir.equals(mCurrentDir))\r
-                    ) {\r
-                    FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
-                    if (fileListFragment != null) { \r
-                        fileListFragment.listDirectory();\r
+                String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); \r
+                 \r
+                boolean fillBlankRoot = false;\r
+                if (mCurrentDir == null) {\r
+                    mCurrentDir = mStorageManager.getFileByPath("/");\r
+                    fillBlankRoot = (mCurrentDir != null);\r
+                }\r
+\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
+                    if (fileListFragment != null) {\r
+                        fileListFragment.listDirectory(mCurrentDir);  \r
                     }\r
                 }\r
-            }\r
-            \r
-            if (!inProgress) {\r
-                FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager()\r
-                        .findFragmentById(R.id.fileList);\r
-                if (fileListFragment != null)\r
-                    fileListFragment.listDirectory();\r
+                \r
+                setSupportProgressBarIndeterminateVisibility(inProgress);\r
+                \r
             }\r
         }\r
-\r
     }\r
     \r
 \r
@@ -649,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
@@ -662,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
@@ -717,17 +774,11 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         \r
         // If we are on a large device -> update fragment\r
         if (mDualPane) {\r
-            FileDetailFragment fileDetails = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-            if (fileDetails == null) {\r
-                // first selected file since the current directory was listed\r
-                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-                transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
-                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);\r
-                transaction.commit();\r
-            } else {\r
-                // another select file\r
-                fileDetails.updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));\r
-            }   \r
+            // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'\r
+            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+            transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);\r
+            transaction.commit();\r
             \r
         } else {    // small or medium screen device -> new Activity\r
             Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);\r
@@ -756,7 +807,22 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment\r
             transaction.commit();\r
         }\r
+        setSupportProgressBarIndeterminateVisibility(false);\r
+    }\r
+    \r
 \r
+    /**\r
+     * Launch an intent to request the PIN code to the user before letting him use the app\r
+     */\r
+    private void requestPinCode() {\r
+        boolean pinStart = false;\r
+        SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());\r
+        pinStart = appPrefs.getBoolean("set_pincode", false);\r
+        if (pinStart) {\r
+            Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);\r
+            i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "FileDisplayActivity");\r
+            startActivity(i);\r
+        }\r
     }\r
 \r
     \r