Added view pager for scrolling image previews with horizontal swipe
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
index a36c972..6994c09 100644 (file)
@@ -40,10 +40,8 @@ import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.ui.fragment.FileDetailFragment;\r
-import com.owncloud.android.ui.fragment.FileFragment;\r
 import com.owncloud.android.ui.fragment.FilePreviewFragment;\r
 \r
 import com.owncloud.android.ui.fragment.FilePreviewFragment;\r
 \r
-import com.owncloud.android.AccountUtils;\r
 import com.owncloud.android.R;\r
 \r
 /**\r
 import com.owncloud.android.R;\r
 \r
 /**\r
@@ -51,7 +49,7 @@ import com.owncloud.android.R;
  * on.\r
  * \r
  * @author Bartek Przybylski\r
  * on.\r
  * \r
  * @author Bartek Przybylski\r
- * \r
+ * @author David A. Velasco\r
  */\r
 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {\r
     \r
  */\r
 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {\r
     \r
@@ -62,17 +60,26 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     public static final String EXTRA_MODE = "MODE";\r
     public static final int MODE_DETAILS = 0;\r
     public static final int MODE_PREVIEW = 1;\r
     public static final String EXTRA_MODE = "MODE";\r
     public static final int MODE_DETAILS = 0;\r
     public static final int MODE_PREVIEW = 1;\r
+\r
+    public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";\r
     \r
     private boolean mConfigurationChangedToLandscape = false;\r
     private FileDownloaderBinder mDownloaderBinder = null;\r
     private ServiceConnection mDownloadConnection, mUploadConnection = null;\r
     private FileUploaderBinder mUploaderBinder = null;\r
     \r
     private boolean mConfigurationChangedToLandscape = false;\r
     private FileDownloaderBinder mDownloaderBinder = null;\r
     private ServiceConnection mDownloadConnection, mUploadConnection = null;\r
     private FileUploaderBinder mUploaderBinder = null;\r
+    private boolean mWaitingToPreview;\r
+    \r
+    private OCFile mFile;\r
+    private Account mAccount;\r
     \r
 \r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
 \r
     \r
 \r
     @Override\r
     protected void onCreate(Bundle savedInstanceState) {\r
         super.onCreate(savedInstanceState);\r
 \r
+        mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
+        mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
+        \r
         // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity \r
         Configuration conf = getResources().getConfiguration();\r
         mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && \r
         // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity \r
         Configuration conf = getResources().getConfiguration();\r
         mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && \r
@@ -80,39 +87,50 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
                                            );\r
 \r
         if (!mConfigurationChangedToLandscape) {\r
                                            );\r
 \r
         if (!mConfigurationChangedToLandscape) {\r
-            mDownloadConnection = new DetailsServiceConnection();\r
-            bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
-            mUploadConnection = new DetailsServiceConnection();\r
-            bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
-            \r
             setContentView(R.layout.file_activity_details);\r
         \r
             ActionBar actionBar = getSupportActionBar();\r
             actionBar.setDisplayHomeAsUpEnabled(true);\r
 \r
             if (savedInstanceState == null) {\r
             setContentView(R.layout.file_activity_details);\r
         \r
             ActionBar actionBar = getSupportActionBar();\r
             actionBar.setDisplayHomeAsUpEnabled(true);\r
 \r
             if (savedInstanceState == null) {\r
+                mWaitingToPreview = false;\r
                 createChildFragment();\r
                 createChildFragment();\r
+            } else {\r
+                mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);\r
             }\r
             \r
             }\r
             \r
+            mDownloadConnection = new DetailsServiceConnection();\r
+            bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
+            mUploadConnection = new DetailsServiceConnection();\r
+            bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
+            \r
+            \r
         }  else {\r
             backToDisplayActivity();   // the 'back' won't be effective until this.onStart() and this.onResume() are completed;\r
         }\r
         \r
         \r
     }\r
         }  else {\r
             backToDisplayActivity();   // the 'back' won't be effective until this.onStart() and this.onResume() are completed;\r
         }\r
         \r
         \r
     }\r
-    \r
-    \r
+\r
+    /**\r
+     * Creates the proper fragment depending upon the state of the handled {@link OCFile} and\r
+     * the requested {@link Intent}.\r
+     */\r
     private void createChildFragment() {\r
     private void createChildFragment() {\r
-        OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
-        Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
         int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW); \r
         \r
         Fragment newFragment = null;\r
         int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW); \r
         \r
         Fragment newFragment = null;\r
-        if (FilePreviewFragment.canBePreviewed(file) && mode == MODE_PREVIEW) {\r
-            newFragment = new FilePreviewFragment(file, account);\r
+        if (FilePreviewFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {\r
+            if (mFile.isDown()) {\r
+                newFragment = new FilePreviewFragment(mFile, mAccount);\r
+            \r
+            } else {\r
+                newFragment = new FileDetailFragment(mFile, mAccount);\r
+                mWaitingToPreview = true;\r
+            }\r
             \r
         } else {\r
             \r
         } else {\r
-            newFragment = new FileDetailFragment(file, account);\r
+            newFragment = new FileDetailFragment(mFile, mAccount);\r
         }\r
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();\r
         ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);\r
         }\r
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();\r
         ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);\r
@@ -120,24 +138,38 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     }\r
     \r
 \r
     }\r
     \r
 \r
+    @Override\r
+    protected void onSaveInstanceState(Bundle outState) {\r
+        super.onSaveInstanceState(outState);\r
+        outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\r
+    }\r
+\r
 \r
     /** Defines callbacks for service binding, passed to bindService() */\r
     private class DetailsServiceConnection implements ServiceConnection {\r
 \r
         @Override\r
         public void onServiceConnected(ComponentName component, IBinder service) {\r
 \r
     /** Defines callbacks for service binding, passed to bindService() */\r
     private class DetailsServiceConnection implements ServiceConnection {\r
 \r
         @Override\r
         public void onServiceConnected(ComponentName component, IBinder service) {\r
+                \r
             if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {\r
                 Log.d(TAG, "Download service connected");\r
                 mDownloaderBinder = (FileDownloaderBinder) service;\r
             if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {\r
                 Log.d(TAG, "Download service connected");\r
                 mDownloaderBinder = (FileDownloaderBinder) service;\r
+                if (mWaitingToPreview) {\r
+                    requestForDownload();\r
+                }\r
+                    \r
             } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {\r
                 Log.d(TAG, "Upload service connected");\r
                 mUploaderBinder = (FileUploaderBinder) service;\r
             } else {\r
                 return;\r
             }\r
             } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {\r
                 Log.d(TAG, "Upload service connected");\r
                 mUploaderBinder = (FileUploaderBinder) service;\r
             } else {\r
                 return;\r
             }\r
+            \r
             Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
             Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
-            if (fragment != null && fragment instanceof FileDetailFragment) {\r
-                ((FileDetailFragment) fragment).updateFileDetails(false);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
+            FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;\r
+            if (detailsFragment != null) {\r
+                detailsFragment.listenForTransferProgress();\r
+                detailsFragment.updateFileDetails(mWaitingToPreview);   // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
             }\r
         }\r
 \r
             }\r
         }\r
 \r
@@ -153,7 +185,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
         }\r
     };    \r
     \r
         }\r
     };    \r
     \r
-\r
+    \r
     @Override\r
     public void onDestroy() {\r
         super.onDestroy();\r
     @Override\r
     public void onDestroy() {\r
         super.onDestroy();\r
@@ -202,8 +234,8 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     private void backToDisplayActivity() {\r
         Intent intent = new Intent(this, FileDisplayActivity.class);\r
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
     private void backToDisplayActivity() {\r
         Intent intent = new Intent(this, FileDisplayActivity.class);\r
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
-        intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));\r
-        intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));\r
+        intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);\r
+        intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);\r
         startActivity(intent);\r
         finish();\r
     }\r
         startActivity(intent);\r
         finish();\r
     }\r
@@ -256,8 +288,30 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
     @Override\r
     public void showFragmentWithDetails(OCFile file) {\r
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
     @Override\r
     public void showFragmentWithDetails(OCFile file) {\r
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
-        transaction.replace(R.id.fragment, new FileDetailFragment(file, (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT)), FileDetailFragment.FTAG); \r
+        transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG); \r
         transaction.commit();\r
     }\r
         transaction.commit();\r
     }\r
+\r
+    \r
+    private void requestForDownload() {\r
+        if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {\r
+            Intent i = new Intent(this, FileDownloader.class);\r
+            i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
+            i.putExtra(FileDownloader.EXTRA_FILE, mFile);\r
+            startService(i);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
+        if (success) {\r
+            if (mWaitingToPreview) {\r
+                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+                transaction.replace(R.id.fragment, new FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG); \r
+                transaction.commit();\r
+                mWaitingToPreview = false;\r
+            }\r
+        }\r
+    }\r
     \r
 }\r
     \r
 }\r