Close photo selection activity on config change

This activity doesn't handle orientation changes right.  Let's just close it
on any configuration changes.   This activity is kinda like a popup, so it
should be okay.

Now this activity handles all config changes by itself, so we can remove a lot
of things, such as onSaveInstanceState and PendingPhotoResult.

Bug 6236774

Change-Id: Ib7037a66d092c8d9a285224ea4cf70e7da8ff1b5
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 56c7b9f..a9c781a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -551,11 +551,16 @@
             </intent-filter>
         </activity>
 
-        <!-- Internal photo selection activity -->
+        <!--
+            Internal photo selection activity.  This activity handles all configuration changes by
+            itself.
+        -->
         <activity android:name=".activities.PhotoSelectionActivity"
             android:theme="@style/Theme.PhotoSelector"
             android:launchMode="singleTop"
-            android:windowSoftInputMode="stateUnchanged">
+            android:windowSoftInputMode="stateUnchanged"
+            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|screenSize|smallestScreenSize"
+            >
         </activity>
 
         <!-- Interstitial activity that shows a phone disambig dialog -->
diff --git a/src/com/android/contacts/activities/PhotoSelectionActivity.java b/src/com/android/contacts/activities/PhotoSelectionActivity.java
index d443782..509bd48 100644
--- a/src/com/android/contacts/activities/PhotoSelectionActivity.java
+++ b/src/com/android/contacts/activities/PhotoSelectionActivity.java
@@ -30,6 +30,7 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.graphics.Rect;
 import android.net.Uri;
@@ -59,10 +60,6 @@
     /** Number of ms for the animation to hide the backdrop on finish. */
     private static final int BACKDROP_FADEOUT_DURATION = 100;
 
-    private static final String KEY_CURRENT_PHOTO_FILE = "currentphotofile";
-
-    private static final String KEY_SUB_ACTIVITY_IN_PROGRESS = "subinprogress";
-
     /** Intent extra to get the photo URI. */
     public static final String PHOTO_URI = "photo_uri";
 
@@ -132,10 +129,7 @@
     /** Whether a sub-activity is currently in progress. */
     private boolean mSubActivityInProgress;
 
-    /**
-     * A photo result received by the activity, persisted across activity lifecycle.
-     */
-    private PendingPhotoResult mPendingPhotoResult;
+    private boolean mCloseActivityWhenCameBackFromSubActivity;
 
     /**
      * The photo file being interacted with, if any.  Saved/restored between activity instances.
@@ -146,13 +140,6 @@
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.photoselection_activity);
-        if (savedInstanceState != null) {
-            String fileName = savedInstanceState.getString(KEY_CURRENT_PHOTO_FILE);
-            if (fileName != null) {
-                mCurrentPhotoFile = new File(fileName);
-            }
-            mSubActivityInProgress = savedInstanceState.getBoolean(KEY_SUB_ACTIVITY_IN_PROGRESS);
-        }
 
         // Pull data out of the intent.
         final Intent intent = getIntent();
@@ -187,11 +174,26 @@
     }
 
     @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+
+        // The current look may not seem right on the new configuration, so let's just close self.
+
+        if (!mSubActivityInProgress) {
+            finishImmediatelyWithNoAnimation();
+        } else {
+            // A sub-activity is in progress, so don't close it yet, but close it when we come back
+            // to this activity.
+            mCloseActivityWhenCameBackFromSubActivity = true;
+        }
+    }
+
+    @Override
     public void finish() {
         if (!mSubActivityInProgress) {
             closePhotoAndFinish();
         } else {
-            activityFinish();
+            finishImmediatelyWithNoAnimation();
         }
     }
 
@@ -227,7 +229,7 @@
         return intent;
     }
 
-    private void activityFinish() {
+    private void finishImmediatelyWithNoAnimation() {
         super.finish();
     }
 
@@ -360,15 +362,13 @@
                 anim.addListener(new AnimatorListenerAdapter() {
                     @Override
                     public void onAnimationEnd(Animator animation) {
-                        activityFinish();
+                        finishImmediatelyWithNoAnimation();
                     }
                 });
                 anim.start();
             }
         };
 
-        // TODO: This won't animate in the right way if the rotation has changed since the activity
-        // was first started.
         animatePhoto(mPhotoStartParams);
         animateAwayBackground();
     }
@@ -395,28 +395,23 @@
     }
 
     @Override
-    protected void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        if (mCurrentPhotoFile != null) {
-            outState.putString(KEY_CURRENT_PHOTO_FILE, mCurrentPhotoFile.toString());
-        }
-        outState.putBoolean(KEY_SUB_ACTIVITY_IN_PROGRESS, mSubActivityInProgress);
-    }
-
-    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (mPhotoHandler != null) {
             mSubActivityInProgress = false;
             if (mPhotoHandler.handlePhotoActivityResult(requestCode, resultCode, data)) {
-                // Clear out any pending photo result.
-                mPendingPhotoResult = null;
+                // Result was handled.  We'll get a callback later.
             } else {
-                // User returning to the photo selection activity.  Re-display options.
-                mPhotoHandler.onClick(mPhotoView);
+                // User cancelled the sub-activity and returning to the photo selection activity.
+                if (mCloseActivityWhenCameBackFromSubActivity) {
+                    finishImmediatelyWithNoAnimation();
+                } else {
+                    // Re-display options.
+                    mPhotoHandler.onClick(mPhotoView);
+                }
             }
         } else {
-            // Create a pending photo result to be handled when the photo handler is created.
-            mPendingPhotoResult = new PendingPhotoResult(requestCode, resultCode, data);
+            // The result comes back before we prepare the handler?  This activity won't get
+            // re-created for orientation changes, so this shouldn't happen.
         }
     }
 
@@ -432,24 +427,19 @@
         mode &= ~PhotoActionPopup.Flags.REMOVE_PHOTO;
 
         mPhotoHandler = new PhotoHandler(this, mPhotoView, mode, mState);
-        if (mPendingPhotoResult != null) {
-            mPhotoHandler.handlePhotoActivityResult(mPendingPhotoResult.mRequestCode,
-                    mPendingPhotoResult.mResultCode, mPendingPhotoResult.mData);
-            mPendingPhotoResult = null;
-        } else {
-            // Setting the photo in displayPhoto() resulted in a relayout
-            // request... to avoid jank, wait until this layout has happened.
-            SchedulingUtils.doAfterLayout(mBackdrop, new Runnable() {
-                @Override
-                public void run() {
-                    animatePhotoOpen();
-                }
-            });
-        }
+
+        // Setting the photo in displayPhoto() resulted in a relayout
+        // request... to avoid jank, wait until this layout has happened.
+        SchedulingUtils.doAfterLayout(mBackdrop, new Runnable() {
+            @Override
+            public void run() {
+                animatePhotoOpen();
+            }
+        });
     }
 
     private final class PhotoHandler extends PhotoSelectionHandler {
-        private PhotoActionListener mListener;
+        private final PhotoActionListener mListener;
 
         private PhotoHandler(
                 Context context, View photoView, int photoMode, EntityDeltaList state) {
@@ -496,15 +486,4 @@
             }
         }
     }
-
-    private static class PendingPhotoResult {
-        private int mRequestCode;
-        private int mResultCode;
-        private Intent mData;
-        private PendingPhotoResult(int requestCode, int resultCode, Intent data) {
-            mRequestCode = requestCode;
-            mResultCode = resultCode;
-            mData = data;
-        }
-    }
 }