Fixed undesired video loop after completion, caused by bugs in GINGERBREAD
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index 57667aa..6d1524f 100644 (file)
@@ -1,5 +1,6 @@
 /* ownCloud Android client application\r
  *   Copyright (C) 2011  Bartek Przybylski\r
+ *   Copyright (C) 2012-2013 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License as published by\r
@@ -21,7 +22,6 @@ package com.owncloud.android.ui.activity;
 import java.io.File;\r
 \r
 import android.accounts.Account;\r
-import android.app.Activity;\r
 import android.app.AlertDialog;\r
 import android.app.ProgressDialog;\r
 import android.app.AlertDialog.Builder;\r
@@ -86,12 +86,15 @@ import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.operations.RemoteOperationResult.ResultCode;\r
 import com.owncloud.android.syncadapter.FileSyncService;\r
 import com.owncloud.android.ui.dialog.ChangelogDialog;\r
+import com.owncloud.android.ui.dialog.EditNameDialog;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog;\r
+import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;\r
 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
 import com.owncloud.android.ui.fragment.FileFragment;\r
 import com.owncloud.android.ui.fragment.OCFileListFragment;\r
 import com.owncloud.android.ui.preview.PreviewImageActivity;\r
+import com.owncloud.android.ui.preview.PreviewImageFragment;\r
 import com.owncloud.android.ui.preview.PreviewMediaFragment;\r
 \r
 import com.owncloud.android.R;\r
@@ -101,11 +104,11 @@ import eu.alefzero.webdav.WebdavClient;
  * Displays, what files the user has available in his ownCloud.\r
  * \r
  * @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\r
  */\r
 \r
 public class FileDisplayActivity extends SherlockFragmentActivity implements\r
-    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener {\r
+    OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener, EditNameDialogListener {\r
     \r
     private ArrayAdapter<String> mDirectories;\r
     private OCFile mCurrentDir = null;\r
@@ -123,6 +126,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     private OCFileListFragment mFileList;\r
     \r
     private boolean mDualPane;\r
+    private boolean mBackFromCreatingFirstAccount;\r
     \r
     private static final int DIALOG_SETUP_ACCOUNT = 0;\r
     private static final int DIALOG_CREATE_DIR = 1;\r
@@ -151,10 +155,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         super.onCreate(savedInstanceState);\r
 \r
         /// Load of parameters from received intent\r
-        mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); // no check necessary, mCurrenDir == null if the parameter is not in the intent\r
         Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
-        if (account != null)\r
-            AccountUtils.setCurrentOwnCloudAccount(this, account.name);\r
+        if (account != null && AccountUtils.setCurrentOwnCloudAccount(this, account.name)) {\r
+            mCurrentDir = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); \r
+        }\r
         \r
         /// Load of saved instance state: keep this always before initDataFromCurrentAccount()\r
         if(savedInstanceState != null) {\r
@@ -199,7 +203,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         // Drop-down navigation \r
         mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);\r
         OCFile currFile = mCurrentDir;\r
-        while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {\r
+        while(mStorageManager != null && currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {\r
             mDirectories.add(currFile.getFileName());\r
             currFile = mStorageManager.getFileById(currFile.getParentId());\r
         }\r
@@ -211,6 +215,19 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         mDualPane = (findViewById(R.id.file_details_container) != null);\r
         if (mDualPane) {\r
             initFileDetailsInDualPane();\r
+        } else {\r
+            // quick patchES to fix problem in turn from landscape to portrait, when a file is selected in the right pane\r
+            // TODO serious refactorization in activities and fragments providing file browsing and handling \r
+            if (mCurrentFile != null) {\r
+                onFileClick(mCurrentFile);\r
+                mCurrentFile = null;\r
+            }\r
+            Fragment rightPanel = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+            if (rightPanel != null) {\r
+                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                transaction.remove(rightPanel);\r
+                transaction.commit();\r
+            }\r
         }\r
             \r
         // Action bar setup\r
@@ -224,7 +241,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         \r
         \r
         // show changelog, if needed\r
-        showChangeLog();\r
+        //showChangeLog();\r
+        mBackFromCreatingFirstAccount = false;\r
         \r
         Log.d(getClass().toString(), "onCreate() end");\r
     }\r
@@ -265,7 +283,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         startActivity(intent);  // the new activity won't be created until this.onStart() and this.onResume() are finished;\r
     }\r
 \r
-\r
+    \r
     /**\r
      *  Load of state dependent of the existence of an ownCloud account\r
      */\r
@@ -367,7 +385,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         boolean retval = true;\r
         switch (item.getItemId()) {\r
             case R.id.action_create_dir: {\r
-                showDialog(DIALOG_CREATE_DIR);\r
+                EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.uploader_info_dirname), "", this);\r
+                dialog.show(getSupportFragmentManager(), "createdirdialog");\r
                 break;\r
             }\r
             case R.id.action_sync_account: {\r
@@ -561,7 +580,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     }\r
 \r
     @Override\r
-    protected void onResume() {\r
+    public void onResume() {\r
         Log.d(getClass().toString(), "onResume() start");\r
         super.onResume();\r
 \r
@@ -573,6 +592,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 if (mDualPane) {\r
                     initFileDetailsInDualPane();\r
                 }\r
+                mBackFromCreatingFirstAccount = true;\r
             }\r
             \r
             // Listen for sync messages\r
@@ -587,7 +607,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             \r
             // Listen for download messages\r
             IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);\r
-            //downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
+            downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
             mDownloadFinishReceiver = new DownloadFinishReceiver();\r
             registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
         \r
@@ -605,7 +625,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
     \r
     @Override\r
-    protected void onPause() {\r
+    public void onPause() {\r
         Log.d(getClass().toString(), "onPause() start");\r
         super.onPause();\r
         if (mSyncBroadcastReceiver != null) {\r
@@ -736,27 +756,45 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             break;\r
         }\r
         case DIALOG_CHOOSE_UPLOAD_SOURCE: {\r
-            final String [] items = {   getString(R.string.actionbar_upload_files), \r
-                                        getString(R.string.actionbar_upload_from_apps) }; \r
+            \r
+            String[] items = null;\r
+            \r
+            String[] allTheItems = { getString(R.string.actionbar_upload_files),\r
+                                     getString(R.string.actionbar_upload_from_apps),\r
+                                     getString(R.string.actionbar_failed_instant_upload) };\r
+            \r
+            String[] commonItems = { getString(R.string.actionbar_upload_files),\r
+                                     getString(R.string.actionbar_upload_from_apps) };\r
+            \r
+            if (InstantUploadActivity.IS_ENABLED)\r
+                items = allTheItems;\r
+            else \r
+                items = commonItems;\r
+            \r
             builder = new AlertDialog.Builder(this);\r
             builder.setTitle(R.string.actionbar_upload);\r
             builder.setItems(items, new DialogInterface.OnClickListener() {\r
                 public void onClick(DialogInterface dialog, int item) {\r
                     if (item == 0) {\r
-                        //if (!mDualPane) { \r
-                            Intent action = new Intent(FileDisplayActivity.this, UploadFilesActivity.class);\r
-                            action.putExtra(UploadFilesActivity.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this));\r
-                            startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);\r
-                        //} else {\r
-                            // TODO create and handle new fragment LocalFileListFragment\r
-                        //}\r
+                        // if (!mDualPane) {\r
+                        Intent action = new Intent(FileDisplayActivity.this, UploadFilesActivity.class);\r
+                        action.putExtra(UploadFilesActivity.EXTRA_ACCOUNT,\r
+                                AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this));\r
+                        startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);\r
+                        // } else {\r
+                        // TODO create and handle new fragment\r
+                        // LocalFileListFragment\r
+                        // }\r
                     } else if (item == 1) {\r
                         Intent action = new Intent(Intent.ACTION_GET_CONTENT);\r
-                        action = action.setType("*/*")\r
-                                .addCategory(Intent.CATEGORY_OPENABLE);\r
-                        startActivityForResult(\r
-                                Intent.createChooser(action, getString(R.string.upload_chooser_title)),\r
+                        action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);\r
+                        startActivityForResult(Intent.createChooser(action, getString(R.string.upload_chooser_title)),\r
                                 ACTION_SELECT_CONTENT_FROM_APPS);\r
+                    } else if (item == 2 && InstantUploadActivity.IS_ENABLED) {\r
+                        Account account = AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this);\r
+                        Intent action = new Intent(FileDisplayActivity.this, InstantUploadActivity.class);\r
+                        action.putExtra(FileUploader.KEY_ACCOUNT, account);\r
+                        startActivity(action);\r
                     }\r
                 }\r
             });\r
@@ -943,6 +981,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 }\r
                 \r
                 setSupportProgressBarIndeterminateVisibility(inProgress);\r
+                if (mBackFromCreatingFirstAccount) {\r
+                    // awful patch to fix problem with visibility of progress circle with the first refresh of the first account\r
+                    // TODO - kill this Activity when the first account has to be created instead of stack the account creation on it\r
+                    getSupportActionBar().hide();\r
+                    getSupportActionBar().show();\r
+                    mBackFromCreatingFirstAccount = false;\r
+                }\r
                 removeStickyBroadcast(intent);\r
                 \r
             }\r
@@ -982,42 +1027,78 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     \r
     \r
     /**\r
-     * Once the file download has finished -> update view\r
+     * Class waiting for broadcast events from the {@link FielDownloader} service.\r
+     * \r
+     * Updates the UI when a download is started or finished, provided that it is relevant for the\r
+     * current folder.\r
      */\r
     private class DownloadFinishReceiver extends BroadcastReceiver {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
+            boolean sameAccount = isSameAccount(context, intent);\r
             String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
-            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-            if (accountName != null && AccountUtils.getCurrentOwnCloudAccount(context) != null) {\r
-                boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);\r
-                boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
-                if (sameAccount && isDescendant) {\r
-                    OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
-                    if (fileListFragment != null) { \r
-                        fileListFragment.listDirectory();\r
-                        if (    mWaitingToPreview != null && \r
-                                mWaitingToPreview.getRemotePath().equals(downloadedRemotePath) && \r
-                                intent.getAction().equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)    ) {\r
-                            \r
-                            Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-                            if (fragment != null && fragment instanceof FileDetailFragment ) {\r
-                                FileDetailFragment detailFragment = (FileDetailFragment) fragment;\r
-                                if (detailFragment.getFile().getRemotePath().equals(downloadedRemotePath)) {\r
-                                    detailFragment.listenForTransferProgress();\r
-                                    detailFragment.updateFileDetails(true);\r
-                                }\r
-                            }\r
-                        }\r
-                    }\r
+            boolean isDescendant = isDescendant(downloadedRemotePath);\r
+            \r
+            if (sameAccount && isDescendant) {\r
+                updateLeftPanel();\r
+                if (mDualPane) {\r
+                    updateRightPanel(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));\r
                 }\r
             }\r
+            \r
+            removeStickyBroadcast(intent);\r
+        }\r
+\r
+        private boolean isDescendant(String downloadedRemotePath) {\r
+            return (mCurrentDir != null && downloadedRemotePath != null && downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));\r
+        }\r
+\r
+        private boolean isSameAccount(Context context, Intent intent) {\r
+            String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
+            return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));\r
         }\r
     }\r
     \r
     \r
-    \r
-    \r
+    protected void updateLeftPanel() {\r
+        OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);\r
+        if (fileListFragment != null) { \r
+            fileListFragment.listDirectory();\r
+        }\r
+    }\r
+\r
+    protected void updateRightPanel(String downloadEvent, String downloadedRemotePath, boolean success) {\r
+        Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+        boolean waitedPreview = (mWaitingToPreview != null && mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));\r
+        if (fragment != null && fragment instanceof FileDetailFragment) {\r
+            FileDetailFragment detailsFragment = (FileDetailFragment) fragment;\r
+            OCFile fileInFragment = detailsFragment.getFile();\r
+            if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {\r
+                // the user browsed to other file ; forget the automatic preview \r
+                mWaitingToPreview = null;\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {\r
+                // grant that the right panel updates the progress bar\r
+                detailsFragment.listenForTransferProgress();\r
+                detailsFragment.updateFileDetails(true, false);\r
+                \r
+            } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+                //  update the right panel \r
+                if (success && waitedPreview) {\r
+                    mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId());   // update the file from database, for the local storage path\r
+                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                    transaction.replace(R.id.file_details_container, new PreviewMediaFragment(mWaitingToPreview, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
+                    transaction.commit();\r
+                    mWaitingToPreview = null;\r
+                    \r
+                } else {\r
+                    detailsFragment.updateFileDetails(false, (success));\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
     /**\r
      * {@inheritDoc}\r
      */\r
@@ -1026,7 +1107,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         return mStorageManager;\r
     }\r
     \r
-    \r
+\r
     /**\r
      * {@inheritDoc}\r
      */\r
@@ -1053,15 +1134,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
      */\r
     @Override\r
     public void onFileClick(OCFile file) {\r
-\r
-        if (file != null && PreviewMediaFragment.canBePreviewed(file)) {\r
-            if (file.isImage()) {\r
-                // preview image - it handles the download, if needed\r
-                startPreviewImage(file);\r
-                \r
-            } else if (file.isDown()) {\r
+        if (file != null && PreviewImageFragment.canBePreviewed(file)) {\r
+            // preview image - it handles the download, if needed\r
+            startPreviewImage(file);\r
+            \r
+        } else if (file != null && PreviewMediaFragment.canBePreviewed(file)) {\r
+            if (file.isDown()) {\r
                 // general preview\r
-                startOtherPreview(file);\r
+                startMediaPreview(file);\r
                 \r
             } else {\r
                 // automatic download, preview on finish\r
@@ -1081,7 +1161,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         startActivity(showDetailsIntent);\r
     }\r
     \r
-    private void startOtherPreview(OCFile file) {\r
+    private void startMediaPreview(OCFile file) {\r
         if (mDualPane) {\r
             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
             transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
@@ -1191,7 +1271,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 if (fragment != null && fragment instanceof FileDetailFragment) {\r
                     FileDetailFragment detailFragment = (FileDetailFragment)fragment;\r
                     detailFragment.listenForTransferProgress();\r
-                    detailFragment.updateFileDetails(false);\r
+                    detailFragment.updateFileDetails(false, false);\r
                 }\r
             }\r
         }\r
@@ -1374,7 +1454,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 if (downloading || uploading) {\r
                     ((FileDetailFragment)details).updateFileDetails(file, AccountUtils.getCurrentOwnCloudAccount(this));\r
                 } else {\r
-                    ((FileDetailFragment)details).updateFileDetails(downloading || uploading);\r
+                    ((FileDetailFragment)details).updateFileDetails(false, true);\r
                 }\r
             }\r
         }\r
@@ -1397,21 +1477,33 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         }\r
     }\r
 \r
-\r
-    @Override\r
-    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
-        if (success) {\r
-            if (mWaitingToPreview != null) {\r
-                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-                transaction.replace(R.id.file_details_container, new PreviewMediaFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);\r
-                transaction.commit();\r
-                mWaitingToPreview = null;\r
+    public void onDismiss(EditNameDialog dialog) {\r
+        //dialog.dismiss();\r
+        if (dialog.getResult()) {\r
+            String newDirectoryName = dialog.getNewFilename().trim();\r
+            Log.d(TAG, "'create directory' dialog dismissed with new name " + newDirectoryName);\r
+            if (newDirectoryName.length() > 0) {\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(OCFile.PATH_SEPARATOR)) {\r
+                        OCFile file = new OCFile(OCFile.PATH_SEPARATOR);\r
+                        mStorageManager.saveFile(file);\r
+                    }\r
+                    mCurrentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);\r
+                }\r
+                path = FileDisplayActivity.this.mCurrentDir.getRemotePath();\r
+                \r
+                // Create directory\r
+                path += newDirectoryName + OCFile.PATH_SEPARATOR;\r
+                Thread thread = new Thread(new DirectoryCreator(path,  AccountUtils.getCurrentOwnCloudAccount(FileDisplayActivity.this), new Handler()));\r
+                thread.start();\r
+                \r
+                showDialog(DIALOG_SHORT_WAIT);\r
             }\r
         }\r
-        mDownloadFinishReceiver.onReceive(this, intent);\r
     }\r
 \r
-\r
     private void requestForDownload() {\r
         Account account = AccountUtils.getCurrentOwnCloudAccount(this);\r
         if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {\r
@@ -1424,3 +1516,4 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 \r
     \r
 }\r
+