Keep overflow menu button from closing on changing device orientation

Bug: 21650562
Change-Id: Ic2459b0768866e4a76734345ffa75edad8dac702
diff --git a/res/values/integers.xml b/res/values/integers.xml
index 86a8e26..a823a30 100644
--- a/res/values/integers.xml
+++ b/res/values/integers.xml
@@ -27,4 +27,7 @@
     <!-- Max suggestions limit showing in quick contact suggestion card [CHAR LIMIT=30]-->
     <integer name="quickcontact_suggestions_limit">10</integer>
 
+    <!-- The delay (in milliseconds) until the overflow options menu is shown automatically -->
+    <integer name="open_overflow_menu_delay_millis">250</integer>
+
 </resources>
diff --git a/src/com/android/contacts/activities/ActionBarAdapter.java b/src/com/android/contacts/activities/ActionBarAdapter.java
index 5a95c90..fd26035 100644
--- a/src/com/android/contacts/activities/ActionBarAdapter.java
+++ b/src/com/android/contacts/activities/ActionBarAdapter.java
@@ -73,11 +73,14 @@
     private static final String EXTRA_KEY_QUERY = "navBar.query";
     private static final String EXTRA_KEY_SELECTED_TAB = "navBar.selectedTab";
     private static final String EXTRA_KEY_SELECTED_MODE = "navBar.selectionMode";
+    private static final String EXTRA_KEY_SHOULD_OPEN_OVERFLOW = "navBar.shouldOpenOverflow";
 
     private static final String PERSISTENT_LAST_TAB = "actionBarAdapter.lastTab";
 
     private boolean mSelectionMode;
     private boolean mSearchMode;
+    private boolean mShouldOverflowOpen;
+    private boolean mIsOverflowOpen;
     private String mQueryString;
 
     private EditText mSearchView;
@@ -198,11 +201,12 @@
             mQueryString = request.getQueryString();
             mCurrentTab = loadLastTabPreference();
             mSelectionMode = false;
+            setShouldOpenOverflow(false);
         } else {
             mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
             mSelectionMode = savedState.getBoolean(EXTRA_KEY_SELECTED_MODE);
             mQueryString = savedState.getString(EXTRA_KEY_QUERY);
-
+            setShouldOpenOverflow(savedState.getBoolean(EXTRA_KEY_SHOULD_OPEN_OVERFLOW));
             // Just set to the field here.  The listener will be notified by update().
             mCurrentTab = savedState.getInt(EXTRA_KEY_SELECTED_TAB);
         }
@@ -319,6 +323,22 @@
         }
     }
 
+    public void setShouldOpenOverflow(boolean shouldOpenOverflow) {
+        mShouldOverflowOpen = shouldOpenOverflow;
+    }
+
+    public boolean shouldOpenOverflow() {
+        return mShouldOverflowOpen;
+    }
+
+    public void setOverflowOpen(boolean isOverflowOpen) {
+        mIsOverflowOpen = isOverflowOpen;
+    }
+
+    public boolean isOverflowOpen() {
+        return mIsOverflowOpen;
+    }
+
     public String getQueryString() {
         return mSearchMode ? mQueryString : null;
     }
@@ -547,6 +567,7 @@
     public void onSaveInstanceState(Bundle outState) {
         outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
         outState.putBoolean(EXTRA_KEY_SELECTED_MODE, mSelectionMode);
+        outState.putBoolean(EXTRA_KEY_SHOULD_OPEN_OVERFLOW, mShouldOverflowOpen);
         outState.putString(EXTRA_KEY_QUERY, mQueryString);
         outState.putInt(EXTRA_KEY_SELECTED_TAB, mCurrentTab);
     }
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index 473ed5c..d7c074e 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -28,6 +28,7 @@
 import android.os.Bundle;
 import android.os.Parcelable;
 import android.os.UserManager;
+import android.os.Handler;
 import android.preference.PreferenceActivity;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.Contacts;
@@ -436,6 +437,16 @@
         // Current tab may have changed since the last onSaveInstanceState().  Make sure
         // the actual contents match the tab.
         updateFragmentsVisibility();
+
+        if (mActionBarAdapter.shouldOpenOverflow() && !mActionBarAdapter.isOverflowOpen()) {
+            new Handler().postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    openOptionsMenu();
+                }
+            }, /* delayMillis = */ getResources().getInteger(R.integer.
+                    open_overflow_menu_delay_millis));
+        }
     }
 
     @Override
@@ -1422,4 +1433,24 @@
         }
         return position;
     }
+
+    @Override
+    public boolean onMenuOpened(int featureId, Menu menu) {
+        // When the overflow menu button opens (both manually and automatically), we need to
+        // update both variables; same for closing event.
+        mActionBarAdapter.setOverflowOpen(true);
+        mActionBarAdapter.setShouldOpenOverflow(true);
+        return super.onMenuOpened(featureId, menu);
+    }
+
+    @Override
+    public void onPanelClosed(int featureId, Menu menu) {
+        // Since onPanelClosed will be called when the activity is destroyed on rotation even if
+        // user leaves the menu open, we are relying on onSaveInstanceState being called before
+        // onDestroy and onPanelClosed are invoked in order to store the "opening" status of the
+        // menu.
+        mActionBarAdapter.setOverflowOpen(false);
+        mActionBarAdapter.setShouldOpenOverflow(false);
+        super.onMenuOpened(featureId, menu);
+    }
 }