Maybe fix issue #5405788: Device continuously opening and closing...

...the "Complete action using" dialog

I have never been able to reproduce this consistently, but here is
another stab in the twilight.  It looks like during boot we have
a potential race where we could reset the config sequence number after
we had gone through a config change, causing ActivityThread to ignore
a following config change.  Maybe this change will help.

Change-Id: I4b731df5fd5c63894ca2e9bd34693b31ab1c0565
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index cd63090..0228181 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -1479,6 +1479,7 @@
 
         mConfiguration.setToDefaults();
         mConfiguration.locale = Locale.getDefault();
+        mConfigurationSeq = mConfiguration.seq = 1;
         mProcessStats.init();
         
         mCompatModePackages = new CompatModePackages(this, systemDir);
@@ -2436,7 +2437,7 @@
                     r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
             if (config != null) {
                 r.frozenBeforeDestroy = true;
-                if (!updateConfigurationLocked(config, r, false)) {
+                if (!updateConfigurationLocked(config, r, false, false)) {
                     mMainStack.resumeTopActivityLocked(null);
                 }
             }
@@ -3797,7 +3798,7 @@
                     app.instrumentationClass, profileFile, profileFd, profileAutoStop,
                     app.instrumentationArguments, app.instrumentationWatcher, testMode, 
                     isRestrictedBackupMode || !normalMode, app.persistent,
-                    mConfiguration, app.compat, getCommonServicesLocked(),
+                    new Configuration(mConfiguration), app.compat, getCommonServicesLocked(),
                     mCoreSettingsObserver.getCoreSettingsLocked());
             updateLruProcessLocked(app, false, true);
             app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
@@ -6707,8 +6708,7 @@
             mAlwaysFinishActivities = alwaysFinishActivities;
             // This happens before any activities are started, so we can
             // change mConfiguration in-place.
-            mConfiguration.updateFrom(configuration);
-            mConfigurationSeq = mConfiguration.seq = 1;
+            updateConfigurationLocked(configuration, null, false, true);
             if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
         }
     }
@@ -12934,7 +12934,7 @@
 
         synchronized(this) {
             final long origId = Binder.clearCallingIdentity();
-            updateConfigurationLocked(values, null, true);
+            updateConfigurationLocked(values, null, true, false);
             Binder.restoreCallingIdentity(origId);
         }
     }
@@ -12957,7 +12957,7 @@
             if (values != null) {
                 Settings.System.clearConfiguration(values);
             }
-            updateConfigurationLocked(values, null, false);
+            updateConfigurationLocked(values, null, false, false);
             Binder.restoreCallingIdentity(origId);
         }
     }
@@ -12971,7 +12971,7 @@
      * @param persistent TODO
      */
     public boolean updateConfigurationLocked(Configuration values,
-            ActivityRecord starting, boolean persistent) {
+            ActivityRecord starting, boolean persistent, boolean initLocale) {
         int changes = 0;
         
         boolean kept = true;
@@ -12986,7 +12986,7 @@
                 
                 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
 
-                if (values.locale != null) {
+                if (values.locale != null && !initLocale) {
                     saveLocaleLocked(values.locale, 
                                      !values.locale.equals(mConfiguration.locale),
                                      values.userSetLocale);
@@ -12999,10 +12999,12 @@
                 newConfig.seq = mConfigurationSeq;
                 mConfiguration = newConfig;
                 Slog.i(TAG, "Config changed: " + newConfig);
-                
+
+                final Configuration configCopy = new Configuration(mConfiguration);
+
                 AttributeCache ac = AttributeCache.instance();
                 if (ac != null) {
-                    ac.updateConfiguration(mConfiguration);
+                    ac.updateConfiguration(configCopy);
                 }
 
                 // Make sure all resources in our process are updated
@@ -13012,11 +13014,11 @@
                 // boot, where the first config change needs to guarantee
                 // all resources have that config before following boot
                 // code is executed.
-                mSystemThread.applyConfigurationToResources(newConfig);
+                mSystemThread.applyConfigurationToResources(configCopy);
 
                 if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
                     Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
-                    msg.obj = new Configuration(mConfiguration);
+                    msg.obj = new Configuration(configCopy);
                     mHandler.sendMessage(msg);
                 }
         
@@ -13026,7 +13028,7 @@
                         if (app.thread != null) {
                             if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
                                     + app.processName + " new config " + mConfiguration);
-                            app.thread.scheduleConfigurationChanged(mConfiguration);
+                            app.thread.scheduleConfigurationChanged(configCopy);
                         }
                     } catch (Exception e) {
                     }