Refactoring of main Activities and Fragments to avoid inconsistent states leading...
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
index 9bcf9de..627c26a 100644 (file)
  */\r
 package eu.alefzero.owncloud.ui.fragment;\r
 \r
-import java.util.List;\r
-\r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
-import android.app.ActionBar.LayoutParams;\r
 import android.content.ActivityNotFoundException;\r
 import android.content.BroadcastReceiver;\r
 import android.content.Context;\r
 import android.content.Intent;\r
 import android.content.IntentFilter;\r
-import android.content.pm.PackageManager;\r
 import android.graphics.Bitmap;\r
 import android.graphics.BitmapFactory;\r
 import android.graphics.BitmapFactory.Options;\r
-import android.graphics.Path.FillType;\r
 import android.net.Uri;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
@@ -64,47 +59,79 @@ public class FileDetailFragment extends SherlockFragment implements
         OnClickListener {\r
 \r
     public static final String EXTRA_FILE = "FILE";\r
+    public static final String EXTRA_ACCOUNT = "ACCOUNT";\r
 \r
-    private DownloadFinishReceiver mDownloadFinishReceiver;\r
-    private Intent mIntent;\r
     private int mLayout;\r
     private View mView;\r
     private OCFile mFile;\r
+    private Account mAccount;\r
+    \r
+    private DownloadFinishReceiver mDownloadFinishReceiver;\r
+\r
     private static final String TAG = "FileDetailFragment";\r
+    public static final String FTAG = "FileDetails"; \r
 \r
+    \r
     /**\r
-     * Default constructor - contains real layout\r
+     * Creates an empty details fragment.\r
+     * \r
+     * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically. \r
      */\r
-    public FileDetailFragment(){\r
-        mLayout = R.layout.file_details_fragment;\r
+    public FileDetailFragment() {\r
+        mFile = null;\r
+        mAccount = null;\r
+        mLayout = R.layout.file_details_empty;\r
     }\r
     \r
+    \r
     /**\r
-     * Creates a dummy layout. For use if the user never has\r
-     * tapped on a file before\r
+     * Creates a details fragment.\r
+     * \r
+     * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).\r
      * \r
-     * @param useEmptyView If true, use empty layout\r
+     * @param fileToDetail      An {@link OCFile} to show in the fragment\r
+     * @param ocAccount         An ownCloud account; needed to start downloads\r
      */\r
-    public FileDetailFragment(boolean useEmptyView){\r
-        if(useEmptyView){\r
-            mLayout = R.layout.file_details_empty;\r
-        } else {\r
+    public FileDetailFragment(OCFile fileToDetail, Account ocAccount){\r
+        mFile = fileToDetail;\r
+        mAccount = ocAccount;\r
+        mLayout = R.layout.file_details_empty;\r
+        \r
+        if(fileToDetail != null && ocAccount != null) {\r
             mLayout = R.layout.file_details_fragment;\r
         }\r
     }\r
     \r
-    /**\r
-     * Use this when creating the fragment and display\r
-     * a file at the same time\r
-     * \r
-     * @param showDetailsIntent The Intent with the required parameters\r
-     * @see FileDetailFragment#updateFileDetails(Intent)\r
-     */\r
-    public FileDetailFragment(Intent showDetailsIntent) {\r
-        mIntent = showDetailsIntent;\r
-        mLayout = R.layout.file_details_fragment;\r
+    \r
+    @Override\r
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
+            Bundle savedInstanceState) {\r
+        super.onCreateView(inflater, container, savedInstanceState);\r
+        \r
+        if (savedInstanceState != null) {\r
+            mFile = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);\r
+            mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);\r
+        }\r
+        \r
+        View view = null;\r
+        view = inflater.inflate(mLayout, container, false);\r
+        mView = view;\r
+        \r
+        updateFileDetails();\r
+        return view;\r
+    }\r
+    \r
+\r
+    @Override\r
+    public void onSaveInstanceState(Bundle outState) {\r
+        Log.i(getClass().toString(), "onSaveInstanceState() start");\r
+        super.onSaveInstanceState(outState);\r
+        outState.putParcelable(FileDetailFragment.EXTRA_FILE, mFile);\r
+        outState.putParcelable(FileDetailFragment.EXTRA_ACCOUNT, mAccount);\r
+        Log.i(getClass().toString(), "onSaveInstanceState() end");\r
     }\r
 \r
+    \r
     @Override\r
     public void onResume() {\r
         super.onResume();\r
@@ -122,23 +149,6 @@ public class FileDetailFragment extends SherlockFragment implements
     }\r
 \r
     @Override\r
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
-            Bundle savedInstanceState) {\r
-        View view = null;\r
-        view = inflater.inflate(mLayout, container, false);\r
-        mView = view;\r
-        if(mLayout == R.layout.file_details_fragment){\r
-            // Phones will launch an activity with this intent\r
-            if(mIntent == null){\r
-                mIntent = getActivity().getIntent();\r
-            }\r
-            updateFileDetails();\r
-        }\r
-        \r
-        return view;\r
-    }\r
-\r
-    @Override\r
     public View getView() {\r
         return super.getView() == null ? mView : super.getView();\r
     }\r
@@ -147,8 +157,7 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onClick(View v) {\r
         Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();\r
         Intent i = new Intent(getActivity(), FileDownloader.class);\r
-        i.putExtra(FileDownloader.EXTRA_ACCOUNT,\r
-                mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));\r
+        i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
         i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());\r
         i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getURLDecodedRemotePath());\r
         i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());\r
@@ -167,27 +176,22 @@ public class FileDetailFragment extends SherlockFragment implements
     /**\r
      * Use this method to signal this Activity that it shall update its view.\r
      * \r
-     * @param intent The {@link Intent} that contains extra information about\r
-     *            this file The intent needs to have these extras:\r
-     *            <p>\r
-     * \r
-     *            {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}\r
-     *            {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file\r
-     *            belongs to (required for downloading)\r
+     * @param file : An {@link OCFile}\r
      */\r
-    public void updateFileDetails(Intent intent) {\r
-        mIntent = intent;\r
+    public void updateFileDetails(OCFile file, Account ocAccount) {\r
+        mFile = file;\r
+        mAccount = ocAccount;\r
         updateFileDetails();\r
     }\r
+    \r
 \r
     /**\r
      * Updates the view with all relevant details about that file.\r
      */\r
-    private void updateFileDetails() {\r
-        mFile = mIntent.getParcelableExtra(EXTRA_FILE);\r
-        Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
+    public void updateFileDetails() {\r
 \r
-        if (mFile != null) {\r
+        if (mFile != null && mLayout == R.layout.file_details_fragment) {\r
+            Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
             // set file details\r
             setFilename(mFile.getFileName());\r
             setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
@@ -350,18 +354,15 @@ public class FileDetailFragment extends SherlockFragment implements
      * the time that the file was created. There is a chance that this will\r
      * be fixed in future versions. Use this method to check if this version of\r
      * ownCloud has this fix.\r
-     * @return True, if ownCloud the ownCloud version is supporting creationg time\r
+     * @return True, if ownCloud the ownCloud version is supporting creation time\r
      */\r
     private boolean ocVersionSupportsTimeCreated(){\r
-       /* if(mIntent != null){\r
-            Account ocAccount = mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);\r
-            if(ocAccount != null){\r
-                AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);\r
-                OwnCloudVersion ocVersion = new OwnCloudVersion(accManager\r
-                        .getUserData(ocAccount, AccountAuthenticator.KEY_OC_VERSION));\r
-                if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {\r
-                    return true;\r
-                }\r
+        /*if(mAccount != null){\r
+            AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);\r
+            OwnCloudVersion ocVersion = new OwnCloudVersion(accManager\r
+                    .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));\r
+            if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {\r
+                return true;\r
             }\r
         }*/\r
         return false;\r
@@ -379,11 +380,12 @@ public class FileDetailFragment extends SherlockFragment implements
                 Toast.makeText(context, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show();\r
                 \r
             } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
-                ((OCFile)mIntent.getParcelableExtra(EXTRA_FILE)).setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
-                updateFileDetails(mIntent);\r
+                mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));\r
+                updateFileDetails();\r
             }\r
         }\r
         \r
     }\r
+    \r
 \r
 }\r