android: Avoid IllegalStateException when importing certificates
authorTobias Brunner <tobias@strongswan.org>
Fri, 6 May 2016 10:41:33 +0000 (12:41 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 6 May 2016 10:51:49 +0000 (12:51 +0200)
When certificates are imported via Storage Access Framework we did handle
the selection directly in onActivityResult().  However, at that point the
activity might apparently not yet be resumed.  So committing
FragmentTransactions could result in IllegalStateExceptions due to the
potential state loss.  To avoid that we cache the returned URI and wait
until onPostResume() to make sure the activity's state is fully restored
before showing the confirmation dialog.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/TrustedCertificateImportActivity.java

index 6b8eb2e..ae47e81 100644 (file)
@@ -52,6 +52,7 @@ public class TrustedCertificateImportActivity extends AppCompatActivity
                "application/x-pem-file",
                "application/pkix-cert"
        };
+       private Uri mCertificateUri;
 
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
@@ -82,18 +83,29 @@ public class TrustedCertificateImportActivity extends AppCompatActivity
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
+               super.onActivityResult(requestCode, resultCode, data);
                switch (requestCode)
                {
                        case OPEN_DOCUMENT:
                                if (resultCode == Activity.RESULT_OK && data != null)
                                {
-                                       importCertificate(data.getData());
+                                       mCertificateUri = data.getData();
                                        return;
                                }
                                finish();
                                return;
                }
-               super.onActivityResult(requestCode, resultCode, data);
+       }
+
+       @Override
+       protected void onPostResume()
+       {
+               super.onPostResume();
+               if (mCertificateUri != null)
+               {
+                       importCertificate(mCertificateUri);
+                       mCertificateUri = null;
+               }
        }
 
        /**