Rework the loader activity base class so it is compatible with ICS.
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Thu, 14 Jun 2012 13:17:55 +0000 (13:17 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Thu, 14 Jun 2012 13:17:55 +0000 (13:17 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@779 180e2498-e6e9-4542-8430-84ac67f01cd8

android/src/net/sf/openrocket/android/rocket/OpenRocketLoaderActivity.java

index 606d4a06d57864c6d4521ea5c72fa3ee4637cbe5..6724744c56277dfa5c323de8dfe155f4b4cbefbb 100644 (file)
@@ -34,7 +34,14 @@ implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnO
        private final static String MISSING_MOTOR_DIAG_FRAGMENT_TAG = "missingmotordialog";\r
        private final static String MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG = "missingmotortask";\r
 \r
+       /*\r
+        * Set to true when we have started to load a file.  Is saved in InstanceState.\r
+        */\r
        private boolean isLoading = false;\r
+       /*\r
+        * Set to the Uri of the file we are supposed to load.  Is saved in InstanceState.\r
+        */\r
+       private Uri fileToLoad = null;\r
 \r
        @Override\r
        protected void onPostCreate(Bundle savedInstanceState) {\r
@@ -42,15 +49,40 @@ implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnO
                Intent i = getIntent();\r
                if (Intent.ACTION_VIEW.equals(i.getAction()) && i.getData() != null ) {\r
                        Uri file = i.getData();\r
-                       loadOrkFile(file);\r
+                       fileToLoad = file;\r
+                       loadOrkFile();\r
                } else {\r
                }\r
        }\r
 \r
        @Override\r
        protected void onSaveInstanceState(Bundle outState) {\r
-               super.onSaveInstanceState(outState);\r
+               AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onSaveInstanceState");\r
                outState.putBoolean("isLoading", isLoading);\r
+               if ( fileToLoad != null ) {\r
+                       outState.putParcelable("fileToLoad", fileToLoad);\r
+               }\r
+               super.onSaveInstanceState(outState);\r
+       }\r
+\r
+       @Override\r
+       protected void onRestoreInstanceState(Bundle savedInstanceState) {\r
+               AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onRestoreInstanceState");\r
+               isLoading = savedInstanceState.getBoolean("isLoading",false);\r
+               if ( savedInstanceState.containsKey("fileToLoad") ) {\r
+                       fileToLoad = savedInstanceState.getParcelable("fileToLoad");\r
+               }\r
+               super.onRestoreInstanceState(savedInstanceState);\r
+       }\r
+\r
+       @Override\r
+       protected void onResume() {\r
+               AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onResume");\r
+               super.onResume();\r
+               // Start loading a file if we have a file and are not already loading one.\r
+               if ( fileToLoad != null && !isLoading ) {\r
+                       loadOrkFile();\r
+               }\r
        }\r
 \r
        /* (non-Javadoc)\r
@@ -58,11 +90,22 @@ implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnO
         */\r
        @Override\r
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {\r
+               AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onActivityResult");\r
                switch ( requestCode ) {\r
                case PICK_ORK_FILE_RESULT:\r
                        if(resultCode==RESULT_OK){\r
                                Uri file = data.getData();\r
-                               loadOrkFile(file);\r
+                               fileToLoad = file;\r
+                               // It would be nice to just start loading the file - but that doesn't work correctly.\r
+                               // I'm uncertain if it is a bug in Android 14/15 or a bug in the v4 support library.\r
+                               // essentially what happens is, when the FileBrowserActivity is brought up,\r
+                               // this activity goes through the saveInstanceState calls to push it to the background.\r
+                               // When the FileBrowserActivity returns the result, this.onActivityResult is called\r
+                               // prior to any of the other lifecycle methods (onRestoreInstanceState as documented, but onStart is\r
+                               // a bug. Since onStart hasn't been called, this activity is not able to create fragments - which \r
+                               // are used to indicate progress etc.\r
+                               // Instead of calling loadOrkFile() here, we push the file Uri into a member variable,\r
+                               // then check the member variable in onResume to actuall kick off the work.\r
                        }\r
                        break;\r
                default:\r
@@ -96,14 +139,21 @@ implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnO
                }               \r
        }\r
 \r
-       private void loadOrkFile( Uri file ) {\r
+       private void loadOrkFile( ) {\r
+               // a little protection.\r
+               if ( fileToLoad == null ) {\r
+                       return;\r
+               }\r
                isLoading = true;\r
-               CurrentRocketHolder.getCurrentRocket().setFileUri( file );\r
-               AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + file);\r
-               String path = file.getPath();\r
+               CurrentRocketHolder.getCurrentRocket().setFileUri( fileToLoad );\r
+               AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + fileToLoad);\r
+               String path = fileToLoad.getPath();\r
                File orkFile = new File(path);\r
 \r
-               getSupportFragmentManager().beginTransaction().add( OpenRocketLoaderFragment.newInstance(orkFile), "loader").commit();\r
+               // Also need commitAllowingState loss because of a bug in v4 dialog show.\r
+               getSupportFragmentManager().beginTransaction()\r
+                 .add( OpenRocketLoaderFragment.newInstance(orkFile), "loader")\r
+                 .commitAllowingStateLoss();\r
 \r
        }\r
 \r