Added help link, feedback email, recommendations through email, and (disabled) imprin...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
index d5f944f..508b7be 100644 (file)
@@ -137,9 +137,9 @@ public class FileDisplayActivity extends FileActivity implements
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         Log_OC.d(TAG, "onCreate() start");
-        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+        super.onCreate(savedInstanceState);
         
-        super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid
+        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
         
         mHandler = new Handler();
 
@@ -176,24 +176,12 @@ public class FileDisplayActivity extends FileActivity implements
         mRightFragmentContainer = findViewById(R.id.right_fragment_container);
         if (savedInstanceState == null) {
             createMinFragments();
-            if (!isRedirectingToSetupAccount()) {
-                initFragmentsWithFile();
-            }
         }
         
         // Action bar setup
         mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
-        OCFile currFile = getFile();
-        if (mStorageManager != null) {
-            while(currFile != null && currFile.getFileName() != OCFile.PATH_SEPARATOR) {
-                mDirectories.add(currFile.getFileName());
-                currFile = mStorageManager.getFileById(currFile.getParentId());
-            }
-        }
-        mDirectories.add(OCFile.PATH_SEPARATOR);
-        ActionBar actionBar = getSupportActionBar();
-        actionBar.setHomeButtonEnabled(true);   // mandatory since Android ICS, according to the official documentation
-        setSupportProgressBarIndeterminateVisibility(false);        // always AFTER setContentView(...) ; to workaround bug in its implementation
+        getSupportActionBar().setHomeButtonEnabled(true);       // mandatory since Android ICS, according to the official documentation
+        setSupportProgressBarIndeterminateVisibility(false);    // always AFTER setContentView(...) ; to work around bug in its implementation
         
         Log_OC.d(TAG, "onCreate() end");
     }
@@ -220,21 +208,39 @@ public class FileDisplayActivity extends FileActivity implements
             /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
             OCFile file = getFile();
             if (file != null) {
-                file = mStorageManager.getFileByPath(file.getRemotePath());   // currentDir = null if not in the current Account
+                if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
+                    // upload in progress - right now, files are not inserted in the local cache until the upload is successful
+                    if (mStorageManager.getFileById(file.getParentId()) == null) {
+                        file = null;    // not able to know the directory where the file is uploading
+                    }
+                } else {
+                    file = mStorageManager.getFileByPath(file.getRemotePath());   // currentDir = null if not in the current Account
+                }
             }
             if (file == null) {
                 // fall back to root folder
-                file = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);  // never should return null
+                file = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);  // never returns null
             }
             setFile(file);
-            
-            if (findViewById(android.R.id.content) != null && !stateWasRecovered) {
+            mDirectories.clear();
+            OCFile fileIt = file;
+            while(fileIt != null && fileIt.getFileName() != OCFile.PATH_SEPARATOR) {
+                if (fileIt.isDirectory()) {
+                    mDirectories.add(fileIt.getFileName());
+                }
+                fileIt = mStorageManager.getFileById(fileIt.getParentId());
+            }
+            mDirectories.add(OCFile.PATH_SEPARATOR);
+            if (!stateWasRecovered) {
                 Log_OC.e(TAG, "Initializing Fragments in onAccountChanged..");
                 initFragmentsWithFile();
+                
             } else {
-                Log_OC.e(TAG, "Fragment initializacion ignored in onAccountChanged due to lack of CONTENT VIEW");
+                updateFragmentsVisibility(!file.isDirectory());
+                updateNavigationElementsInActionBar(file.isDirectory() ? null : file);
             }
             
+            
         } else {
             Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
         }
@@ -250,11 +256,24 @@ public class FileDisplayActivity extends FileActivity implements
 
     private void initFragmentsWithFile() {
         if (getAccount() != null && getFile() != null) {
+            /// First fragment
+            OCFileListFragment listOfFiles = getListOfFilesFragment(); 
+            if (listOfFiles != null) {
+                listOfFiles.listDirectory(getCurrentDir());   
+            } else {
+                Log.e(TAG, "Still have a chance to lose the initializacion of list fragment >(");
+            }
+            
             /// Second fragment
             OCFile file = getFile(); 
             Fragment secondFragment = chooseInitialSecondFragment(file);
             if (secondFragment != null) {
                 setSecondFragment(secondFragment);
+                updateFragmentsVisibility(true);
+                updateNavigationElementsInActionBar(file);
+                
+            } else {
+                cleanSecondFragment();
             }
             
         } else {
@@ -271,7 +290,9 @@ public class FileDisplayActivity extends FileActivity implements
     private Fragment chooseInitialSecondFragment(OCFile file) {
         Fragment secondFragment = null;
         if (file != null && !file.isDirectory()) {
-            if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)) {
+            if (file.isDown() && PreviewMediaFragment.canBePreviewed(file) 
+                    && file.getLastSyncDateForProperties() > 0  // temporal fix
+                    ) {
                 int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
                 boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
                 secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay);
@@ -294,7 +315,7 @@ public class FileDisplayActivity extends FileActivity implements
      */
     private void setSecondFragment(Fragment fragment) {
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
-        transaction.add(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
+        transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
         transaction.commit();
     }
     
@@ -350,8 +371,9 @@ public class FileDisplayActivity extends FileActivity implements
             FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
             tr.remove(second);
             tr.commit();
-            updateFragmentsVisibility(false);
         }
+        updateFragmentsVisibility(false);
+        updateNavigationElementsInActionBar(null);
     }
     
     protected void refeshListOfFilesFragment() {
@@ -377,19 +399,21 @@ public class FileDisplayActivity extends FileActivity implements
                 detailsFragment.updateFileDetails(true, false);
                 
             } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
-                //  update the right panel 
-                if (success && waitedPreview) {
-                    mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId());   // update the file from database, for the local storage path
-                    if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
-                        startMediaPreview(mWaitingToPreview, 0, true);
-                       
-                    } else {
-                        detailsFragment.updateFileDetails(false, (success));
-                        openFile(mWaitingToPreview);
+                //  update the right panel
+                boolean detailsFragmentChanged = false;
+                if (waitedPreview) {
+                    if (success) {
+                        mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId());   // update the file from database, for the local storage path
+                        if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
+                            startMediaPreview(mWaitingToPreview, 0, true);
+                            detailsFragmentChanged = true;
+                        } else {
+                            openFile(mWaitingToPreview);
+                        }
                     }
                     mWaitingToPreview = null;
-                    
-                } else {
+                }
+                if (!detailsFragmentChanged) {
                     detailsFragment.updateFileDetails(false, (success));
                 }
             }
@@ -428,7 +452,8 @@ public class FileDisplayActivity extends FileActivity implements
             }
             case android.R.id.home: {
                 FileFragment second = getSecondFragment();
-                if((getFile() != null && getFile().getParentId() != 0) || 
+                OCFile currentDir = getCurrentDir();
+                if((currentDir != null && currentDir.getParentId() != 0) || 
                         (second != null && second.getFile() != null)) {
                     onBackPressed(); 
                 }
@@ -558,8 +583,8 @@ public class FileDisplayActivity extends FileActivity implements
 
     @Override
     public void onBackPressed() {
+        OCFileListFragment listOfFiles = getListOfFilesFragment(); 
         if (mDualPane || getSecondFragment() == null) {
-            OCFileListFragment listOfFiles = getListOfFilesFragment(); 
             if (listOfFiles != null) {  // should never be null, indeed
                 if (mDirectories.getCount() <= 1) {
                     finish();
@@ -567,11 +592,12 @@ public class FileDisplayActivity extends FileActivity implements
                 }
                 popDirname();
                 listOfFiles.onBrowseUp();
-                setFile(listOfFiles.getCurrentFile());
             }
         }
+        if (listOfFiles != null) {  // should never be null, indeed
+            setFile(listOfFiles.getCurrentFile());
+        }
         cleanSecondFragment();
-        updateNavigationElementsInActionBar(getFile(), null);
     }
 
     @Override
@@ -582,14 +608,7 @@ public class FileDisplayActivity extends FileActivity implements
         outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
         Log_OC.d(TAG, "onSaveInstanceState() end");
     }
-    
-    @Override
-    protected void onStart() {
-        super.onStart();
-        FileFragment second = getSecondFragment();
-        updateFragmentsVisibility(second != null);
-        updateNavigationElementsInActionBar(getFile(), (second == null) ? null : second.getFile());
-    }
+
     
     @Override
     protected void onResume() {
@@ -612,12 +631,6 @@ public class FileDisplayActivity extends FileActivity implements
         mDownloadFinishReceiver = new DownloadFinishReceiver();
         registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
     
-        // List current directory
-        OCFileListFragment listOfFiles = getListOfFilesFragment(); 
-        if (listOfFiles != null) {
-            listOfFiles.listDirectory(getFile());   // TODO we should find the way to avoid the need of this (maybe it's not necessary yet; to check)
-        }
-    
         Log_OC.d(TAG, "onResume() end");
     }
 
@@ -818,7 +831,7 @@ public class FileDisplayActivity extends FileActivity implements
                 String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); 
                  
                 boolean fillBlankRoot = false;
-                OCFile currentDir = getFile();
+                OCFile currentDir = getCurrentDir();
                 if (currentDir == null) {
                     currentDir = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
                     fillBlankRoot = (currentDir != null);
@@ -832,8 +845,9 @@ public class FileDisplayActivity extends FileActivity implements
                     if (fileListFragment != null) {
                         fileListFragment.listDirectory(currentDir);
                     }
+                    if (getSecondFragment() == null)
+                        setFile(currentDir);
                 }
-                setFile(currentDir);
                 
                 setSupportProgressBarIndeterminateVisibility(inProgress);
                 removeStickyBroadcast(intent);
@@ -862,7 +876,8 @@ public class FileDisplayActivity extends FileActivity implements
             String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
             String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
             boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name);
-            boolean isDescendant = (getFile() != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(getFile().getRemotePath()));
+            OCFile currentDir = getCurrentDir();
+            boolean isDescendant = (currentDir != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(currentDir.getRemotePath()));
             if (sameAccount && isDescendant) {
                 refeshListOfFilesFragment();
             }
@@ -893,7 +908,8 @@ public class FileDisplayActivity extends FileActivity implements
         }
 
         private boolean isDescendant(String downloadedRemotePath) {
-            return (getFile() != null && downloadedRemotePath != null && downloadedRemotePath.startsWith(getFile().getRemotePath()));
+            OCFile currentDir = getCurrentDir();
+            return (currentDir != null && downloadedRemotePath != null && downloadedRemotePath.startsWith(currentDir.getRemotePath()));
         }
 
         private boolean isSameAccount(Context context, Intent intent) {
@@ -921,7 +937,6 @@ public class FileDisplayActivity extends FileActivity implements
     public void onBrowsedDownTo(OCFile directory) {
         pushDirname(directory);
         cleanSecondFragment();
-        updateNavigationElementsInActionBar(directory, null);
     }
     
     /**
@@ -949,7 +964,8 @@ public class FileDisplayActivity extends FileActivity implements
         Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay);
         setSecondFragment(mediaFragment);
         updateFragmentsVisibility(true);
-        updateNavigationElementsInActionBar(getFile(), file);
+        updateNavigationElementsInActionBar(file);
+        setFile(file);
     }
     
     /**
@@ -966,7 +982,8 @@ public class FileDisplayActivity extends FileActivity implements
         mWaitingToPreview = file;
         requestForDownload();
         updateFragmentsVisibility(true);
-        updateNavigationElementsInActionBar(getFile(), file);
+        updateNavigationElementsInActionBar(file);
+        setFile(file);
     }
 
     
@@ -981,17 +998,19 @@ public class FileDisplayActivity extends FileActivity implements
         Fragment detailFragment = new FileDetailFragment(file, getAccount());
         setSecondFragment(detailFragment);
         updateFragmentsVisibility(true);
-        updateNavigationElementsInActionBar(getFile(), file);
+        updateNavigationElementsInActionBar(file);
+        setFile(file);
     }
     
     
     /**
      * TODO
      */
-    private void updateNavigationElementsInActionBar(OCFile currentDir, OCFile currentFile) {
+    private void updateNavigationElementsInActionBar(OCFile chosenFile) {
         ActionBar actionBar = getSupportActionBar(); 
-        if (currentFile == null || mDualPane) {
+        if (chosenFile == null || mDualPane) {
             // only list of files - set for browsing through folders
+            OCFile currentDir = getCurrentDir();
             actionBar.setDisplayHomeAsUpEnabled(currentDir != null && currentDir.getParentId() != 0);
             actionBar.setDisplayShowTitleEnabled(false);
             actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
@@ -1000,7 +1019,7 @@ public class FileDisplayActivity extends FileActivity implements
         } else {
             actionBar.setDisplayHomeAsUpEnabled(true);
             actionBar.setDisplayShowTitleEnabled(true);
-            actionBar.setTitle(currentFile.getFileName());
+            actionBar.setTitle(chosenFile.getFileName());
             actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
         }
     }
@@ -1010,17 +1029,9 @@ public class FileDisplayActivity extends FileActivity implements
      * {@inheritDoc}
      */
     @Override
-    public OCFile getInitialDirectory() {
-        return getFile();
-    }
-    
-    
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public void onFileStateChanged() {
         refeshListOfFilesFragment();
+        updateNavigationElementsInActionBar(getSecondFragment().getFile());
     }
 
     
@@ -1156,7 +1167,7 @@ public class FileDisplayActivity extends FileActivity implements
             if (second != null && removedFile.equals(second.getFile())) {
                 cleanSecondFragment();
             }
-            if (mStorageManager.getFileById(removedFile.getParentId()).equals(getFile())) {
+            if (mStorageManager.getFileById(removedFile.getParentId()).equals(getCurrentDir())) {
                 refeshListOfFilesFragment();
             }
                 
@@ -1211,7 +1222,7 @@ public class FileDisplayActivity extends FileActivity implements
                     ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount());
                 }
             }
-            if (mStorageManager.getFileById(renamedFile.getParentId()).equals(getFile())) {
+            if (mStorageManager.getFileById(renamedFile.getParentId()).equals(getCurrentDir())) {
                 refeshListOfFilesFragment();
             }
             
@@ -1283,20 +1294,11 @@ public class FileDisplayActivity extends FileActivity implements
             String newDirectoryName = dialog.getNewFilename().trim();
             Log_OC.d(TAG, "'create directory' dialog dismissed with new name " + newDirectoryName);
             if (newDirectoryName.length() > 0) {
-                String path;
-                if (getFile() == null) {
-                    // this is just a patch; we should ensure that mCurrentDir never is null
-                    if (!mStorageManager.fileExists(OCFile.PATH_SEPARATOR)) {
-                        OCFile file = new OCFile(OCFile.PATH_SEPARATOR);
-                        mStorageManager.saveFile(file);
-                    }
-                    setFile(mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR));
-                }
-                path = FileDisplayActivity.this.getFile().getRemotePath();
+                String path = getCurrentDir().getRemotePath();
                 
                 // Create directory
                 path += newDirectoryName + OCFile.PATH_SEPARATOR;
-                RemoteOperation operation = new CreateFolderOperation(path, getFile().getFileId(), mStorageManager);
+                RemoteOperation operation = new CreateFolderOperation(path, getCurrentDir().getFileId(), mStorageManager);
                 operation.execute(  getAccount(), 
                                     FileDisplayActivity.this, 
                                     FileDisplayActivity.this, 
@@ -1318,6 +1320,18 @@ public class FileDisplayActivity extends FileActivity implements
             startService(i);
         }
     }
-
+    
+    
+    private OCFile getCurrentDir() {
+        OCFile file = getFile();
+        if (file != null) {
+            if (file.isDirectory()) {
+                return file;
+            } else if (mStorageManager != null) {
+                return mStorageManager.getFileById(file.getParentId());
+            }
+        }
+        return null;
+    }
 
 }