1. 程式人生 > >Android 螢幕旋轉事件流程分析

Android 螢幕旋轉事件流程分析

WindowManagerService.java (android-6.0\frameworks\base\services\core\java\com\android\server\wm) [java] view plain copy print?
  1. privatevoid initPolicy() {  
  2.     UiThread.getHandler().runWithScissors(new Runnable() {  
  3.         @Override
  4.         publicvoid run() {  
  5.             WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());  
  6.             mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);  
  7.         }  
  8.     }, 0);  
  9. }  
    private void initPolicy() {
        UiThread.getHandler().runWithScissors(new Runnable() {
            @Override
            public void run() {
                WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());

                mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);
            }
        }, 0);
    }
在WMS構造方法中就呼叫initPolicy方法。在這個方法中,很明顯是在初始化mPolicy:WindowManagerPolicy這個變數。 在WMS中,該變數實質是PhoneWindowManager物件。PhoneWindowManager類實現了WindowManagerPolicy介面類。 PhoneWindowManager.java (android-6.0\frameworks\base\services\core\java\com\android\server\policy) [java] view plain copy print?
  1. /** {@inheritDoc} */
  2.  @Override
  3.  publicvoid init(Context context, IWindowManager windowManager,  
  4.          WindowManagerFuncs windowManagerFuncs) {  
  5.      mContext = context;  
  6.      mWindowManager = windowManager;  
  7.      mWindowManagerFuncs = windowManagerFuncs;  
  8.      mOrientationListener = new MyOrientationListener(mContext, mHandler);  
  9.      try {  
  10.          mOrientationListener.setCurrentRotation(windowManager.getRotation());  
  11.      } catch (RemoteException ex) { }  
  12.  }  
   /** {@inheritDoc} */
    @Override
    public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {
        mContext = context;
        mWindowManager = windowManager;
        mWindowManagerFuncs = windowManagerFuncs;

        mOrientationListener = new MyOrientationListener(mContext, mHandler);
        try {
            mOrientationListener.setCurrentRotation(windowManager.getRotation());
        } catch (RemoteException ex) { }

    }
發現在初始化的過程中,例項化一個方位監聽物件MyOrientationListener,並且給它設定了初始值。該初始值從WMS中獲取,預設為0。 MyOrientationListener類是PhoneWindowManager的一個內部類,它繼承了WindowOrientationListener類。 WindowOrientationListener.java (android-6.0\frameworks\base\services\core\java\com\android\server\policy) [java] view plain copy print?
  1. private WindowOrientationListener(Context context, Handler handler, int rate) {  
  2.     mHandler = handler;  
  3.     mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);  
  4.     mRate = rate;  
  5.     mSensor = mSensorManager.getDefaultSensor(USE_GRAVITY_SENSOR  
  6.             ? Sensor.TYPE_GRAVITY : Sensor.TYPE_ACCELEROMETER);  
  7.     if (mSensor != null) {  
  8.         // Create listener only if sensors do exist
  9.         mSensorEventListener = new SensorEventListenerImpl(context);  
  10.     }  
  11. }  
    private WindowOrientationListener(Context context, Handler handler, int rate) {
        mHandler = handler;
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mRate = rate;
        mSensor = mSensorManager.getDefaultSensor(USE_GRAVITY_SENSOR
                ? Sensor.TYPE_GRAVITY : Sensor.TYPE_ACCELEROMETER);
        if (mSensor != null) {
            // Create listener only if sensors do exist
            mSensorEventListener = new SensorEventListenerImpl(context);
        }
    }
看到這邊,一下子就明白旋屏事件上報的大致流程。首先由感測器計算資料確認是否上報,然後通過Handler或者回調方法來處理。這麼想是因為構造器中傳遞進來一個Handler物件,另外本身就是通過其子類呼叫才進入WindowOrientationListener。具體是怎麼一個流程,還要分析接下來的程式碼。 在這裡,預設採用的感測器是加速度感測器,USE_GRAVITY_SENSOR:false 在Android7.0中,預設採用的是方向感測器。 WindowOrientationListener構造器中,mSensorManager、mSensor、mSensorEventListener物件。在後面的enable方法中,通過mSensorManager呼叫registerListener為mSensor註冊監聽事件mSensorEventListener。 [java] view plain copy print?
  1.  * Enables the WindowOrientationListener so it will monitor the sensor and call  
  2.  * {@link #onProposedRotationChanged(int)} when the device orientation changes.  
  3.  */  
  4. publicvoid enable() {  
  5.             mSensorManager.registerListener(mSensorEventListener, mSensor, mRate, mHandler);  
  6.             mEnabled = true;  
  7.         }  
  8.     }  
  9. }  
     * Enables the WindowOrientationListener so it will monitor the sensor and call
     * {@link #onProposedRotationChanged(int)} when the device orientation changes.
     */
    public void enable() {

                mSensorManager.registerListener(mSensorEventListener, mSensor, mRate, mHandler);
                mEnabled = true;
            }
        }
    }
這裡不關注這些個點,還是去看感測器監聽處理這一塊內容。 SensorEventListenerImpl是WindowOrientationListener的內部類,它實現了介面。 @Override [java] view plain copy print?
  1. publicvoid onSensorChanged(SensorEvent event) {  
  2.     int proposedRotation;  
  3.     int oldProposedRotation;  
  4.     synchronized (mLock) {  
  5.         // The vector given in the SensorEvent points straight up (towards the sky) under
  6.         // ideal conditions (the phone is not accelerating).  I’ll call this up vector
  7.         // elsewhere.
  8.         float x = event.values[ACCELEROMETER_DATA_X];  
  9.         float y = event.values[ACCELEROMETER_DATA_Y];  
  10.         float z = event.values[ACCELEROMETER_DATA_Z];  
  11.         }  
  12.     }  
  13.     // Tell the listener.
  14.     if (proposedRotation != oldProposedRotation && proposedRotation >= 0) {  
  15.         if (LOG) {  
  16.             Slog.v(TAG, ”Proposed rotation changed!  proposedRotation=” + proposedRotation  
  17.                     + ”, oldProposedRotation=” + oldProposedRotation);  
  18.         }  
  19.         onProposedRotationChanged(proposedRotation);  
  20.     }  
  21. }  
        public void onSensorChanged(SensorEvent event) {
            int proposedRotation;
            int oldProposedRotation;

            synchronized (mLock) {
                // The vector given in the SensorEvent points straight up (towards the sky) under
                // ideal conditions (the phone is not accelerating).  I'll call this up vector
                // elsewhere.
                float x = event.values[ACCELEROMETER_DATA_X];
                float y = event.values[ACCELEROMETER_DATA_Y];
                float z = event.values[ACCELEROMETER_DATA_Z];
                }
            }

            // Tell the listener.
            if (proposedRotation != oldProposedRotation && proposedRotation >= 0) {
                if (LOG) {
                    Slog.v(TAG, "Proposed rotation changed!  proposedRotation=" + proposedRotation
                            + ", oldProposedRotation=" + oldProposedRotation);
                }
                onProposedRotationChanged(proposedRotation);
            }
        }
感測器資料計算過程這裡省略了,在onSensorChanged方法的最後通過函式回撥上報旋屏事件。回顧上面的內容,驗證的確如此。 [java] view plain copy print?
  1. /** 
  2.  * Called when the rotation view of the device has changed. 
  3.  * 
  4.  * This method is called whenever the orientation becomes certain of an orientation. 
  5.  * It is called each time the orientation determination transitions from being 
  6.  * uncertain to being certain again, even if it is the same orientation as before. 
  7.  * 
  8.  * @param rotation The new orientation of the device, one of the Surface.ROTATION_* constants. 
  9.  * @see android.view.Surface 
  10.  */
  11. publicabstractvoid onProposedRotationChanged(int rotation);  
    /**
     * Called when the rotation view of the device has changed.
     *
     * This method is called whenever the orientation becomes certain of an orientation.
     * It is called each time the orientation determination transitions from being
     * uncertain to being certain again, even if it is the same orientation as before.
     *
     * @param rotation The new orientation of the device, one of the Surface.ROTATION_* constants.
     * @see android.view.Surface
     */
    public abstract void onProposedRotationChanged(int rotation);
onProposedRotationChanged方法是宣告在WindowOrientationListener類的一個抽象方法,它具體實現在PhoneWindowManager的一個內部類,即MyOrientationListener。 PhoneWindowManager.java (android-6.0\frameworks\base\services\core\java\com\android\server\policy) [java] view plain copy print?
  1. @Override
  2. publicvoid onProposedRotationChanged(int rotation) {  
  3.     if (localLOGV) Slog.v(TAG, “onProposedRotationChanged, rotation=” + rotation);  
  4.     updateRotation(false);  
  5. }  
        @Override
        public void onProposedRotationChanged(int rotation) {
            if (localLOGV) Slog.v(TAG, "onProposedRotationChanged, rotation=" + rotation);
            updateRotation(false);
        }

[java] view plain copy print?
  1. void updateRotation(boolean alwaysSendConfiguration) {  
  2.     try {  
  3.         //set orientation on WindowManager
  4.         mWindowManager.updateRotation(alwaysSendConfiguration, false); //false、false
  5.     } catch (RemoteException e) {  
  6.         // Ignore
  7.     }  
  8. }  
    void updateRotation(boolean alwaysSendConfiguration) {
        try {
            //set orientation on WindowManager
            mWindowManager.updateRotation(alwaysSendConfiguration, false); //false、false
        } catch (RemoteException e) {
            // Ignore
        }
    }
很明顯,是通知WMS更新rotation。 WindowManagerService.java (android-6.0\frameworks\base\services\core\java\com\android\server\wm) [java] view plain copy print?
  1. /** 
  2.  * Recalculate the current rotation. 
  3.  * 
  4.  * Called by the window manager policy whenever the state of the system changes 
  5.  * such that the current rotation might need to be updated, such as when the 
  6.  * device is docked or rotated into a new posture. 
  7.  */
  8. @Override
  9. publicvoid updateRotation(boolean alwaysSendConfiguration, boolean forceRelayout) {  
  10.     updateRotationUnchecked(alwaysSendConfiguration, forceRelayout);  
  11. }  
    /**
     * Recalculate the current rotation.
     *
     * Called by the window manager policy whenever the state of the system changes
     * such that the current rotation might need to be updated, such as when the
     * device is docked or rotated into a new posture.
     */
    @Override
    public void updateRotation(boolean alwaysSendConfiguration, boolean forceRelayout) {
        updateRotationUnchecked(alwaysSendConfiguration, forceRelayout);
    }

[java] view plain copy print?
  1. publicvoid updateRotationUnchecked(boolean alwaysSendConfiguration, boolean forceRelayout) {  
  2.     boolean changed;  
  3.     synchronized(mWindowMap) {  
  4.         changed = updateRotationUncheckedLocked(false);  
  5.     if (changed || alwaysSendConfiguration) {  
  6.         sendNewConfiguration();  
  7.     }  
  8. }  
    public void updateRotationUnchecked(boolean alwaysSendConfiguration, boolean forceRelayout) {

        boolean changed;
        synchronized(mWindowMap) {
            changed = updateRotationUncheckedLocked(false);

        if (changed || alwaysSendConfiguration) {
            sendNewConfiguration();
        }

    }
一般情況,rotation都是發生變化的,也就是說updateRotationUncheckedLocked返回值通常為true,故會呼叫sendNewConfiguration。 [java] view plain copy print?
  1. /* 
  2.  * Instruct the Activity Manager to fetch the current configuration and broadcast 
  3.  * that to config-changed listeners if appropriate. 
  4.  */
  5. void sendNewConfiguration() {  
  6.     try {  
  7.         mActivityManager.updateConfiguration(null);  
  8.     } catch (RemoteException e) {  
  9.     }  
  10. }  
    /*
     * Instruct the Activity Manager to fetch the current configuration and broadcast
     * that to config-changed listeners if appropriate.
     */
    void sendNewConfiguration() {
        try {
            mActivityManager.updateConfiguration(null);
        } catch (RemoteException e) {
        }
    }
mActivityManager本質是AMS Server端,這裡從WMS執行至AMS, ActivityManagerService.java (android-6.0\frameworks\base\services\core\java\com\android\server\am) [java] view plain copy print?
  1. publicvoid updateConfiguration(Configuration values) {  
  2.     enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,  
  3.             ”updateConfiguration()”);  
  4.     synchronized(this) {  
  5.         if (values == null && mWindowManager != null) {  
  6.             // sentinel: fetch the current configuration from the window manager
  7.             values = mWindowManager.computeNewConfiguration();  
  8.         }  
  9.         updateConfigurationLocked(values, nullfalsefalse);  
  10.     }  
  11. }  
    public void updateConfiguration(Configuration values) {
        enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
                "updateConfiguration()");

        synchronized(this) {
            if (values == null && mWindowManager != null) {
                // sentinel: fetch the current configuration from the window manager
                values = mWindowManager.computeNewConfiguration();
            }

            updateConfigurationLocked(values, null, false, false);

        }
    }
AMS中,第一步:檢查許可權,沒有許可權則拋一個異常;第二步:從WMS中獲取Configuration值;第三步:去真正更新Configuration值。 [java] view plain copy print?
  1. /** 
  2.  * Do either or both things: (1) change the current configuration, and (2) 
  3.  * make sure the given activity is running with the (now) current 
  4.  * configuration.  Returns true if the activity has been left running, or 
  5.  * false if <var>starting</var> is being destroyed to match the new 
  6.  * configuration. 
  7.  * @param persistent TODO 
  8.  */
  9. boolean updateConfigurationLocked(Configuration values,  
  10.         ctivityRecord starting, boolean persistent, boolean initLocale) {  
  11.     int changes = 0;  
  12.     if (values != null) {  
  13.         Configuration newConfig = new Configuration(mConfiguration);  
  14.         changes = newConfig.updateFrom(values);  
  15.             for (int i=mLruProcesses.size()-1; i>=0; i–) {  
  16.                 ProcessRecord app = mLruProcesses.get(i);  
  17.                 try {  
  18.                     if (app.thread != null) {  
  19.                         if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, “Sending to proc ”
  20.                                 + app.processName + ” new config ” + mConfiguration);  
  21.                         app.thread.scheduleConfigurationChanged(configCopy);  
  22.                     }  
  23.                 } catch (Exception e) {  
  24.                 }  
  25.             }  
  26.     boolean kept = true;  
  27.     final ActivityStack mainStack = mStackSupervisor.getFocusedStack();  
  28.     // mainStack is null during startup.
  29.     if (mainStack != null) {  
  30.         if (changes != 0 && starting == null) {  
  31.             // If the configuration changed, and the caller is not already
  32.             // in the process of starting an activity, then find the top
  33.             // activity to check if its configuration needs to change.
  34.             starting = mainStack.topRunningActivityLocked(null);  
  35.         }  
  36.         if (starting != null) {  
  37.             kept = mainStack.ensureActivityConfigurationLocked(starting, changes);  
  38.             // And we need to make sure at this point that all other activities
  39.             // are made visible with the correct configuration.
  40.             mStackSupervisor.ensureActivitiesVisibleLocked(starting, changes);  
  41.         }  
  42.     }  
  43.     if (values != null && mWindowManager != null) {  
  44.         mWindowManager.setNewConfiguration(mConfiguration);  
  45.     }  
  46.     return kept;  
  47. }  
    /**
     * Do either or both things: (1) change the current configuration, and (2)
     * make sure the given activity is running with the (now) current
     * configuration.  Returns true if the activity has been left running, or
     * false if <var>starting</var> is being destroyed to match the new
     * configuration.
     * @param persistent TODO
     */
    boolean updateConfigurationLocked(Configuration values,
            ctivityRecord starting, boolean persistent, boolean initLocale) {
        int changes = 0;

        if (values != null) {
            Configuration newConfig = new Configuration(mConfiguration);
            changes = newConfig.updateFrom(values);

                for (int i=mLruProcesses.size()-1; i>=0; i--) {
                    ProcessRecord app = mLruProcesses.get(i);
                    try {
                        if (app.thread != null) {
                            if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending to proc "
                                    + app.processName + " new config " + mConfiguration);
                            app.thread.scheduleConfigurationChanged(configCopy);
                        }
                    } catch (Exception e) {
                    }
                }

        boolean kept = true;
        final ActivityStack mainStack = mStackSupervisor.getFocusedStack();
        // mainStack is null during startup.
        if (mainStack != null) {
            if (changes != 0 && starting == null) {
                // If the configuration changed, and the caller is not already
                // in the process of starting an activity, then find the top
                // activity to check if its configuration needs to change.
                starting = mainStack.topRunningActivityLocked(null);
            }

            if (starting != null) {
                kept = mainStack.ensureActivityConfigurationLocked(starting, changes);
                // And we need to make sure at this point that all other activities
                // are made visible with the correct configuration.
                mStackSupervisor.ensureActivitiesVisibleLocked(starting, changes);
            }
        }

        if (values != null && mWindowManager != null) {
            mWindowManager.setNewConfiguration(mConfiguration);
        }

        return kept;
    }
通常當發生橫豎屏切換的時候,Activity的生命週期通常是:onConfigureationChanged –> onDestroy –> onCreate –> onStart –> onResume。一看這裡就分為兩步驟,一:通知Configuration已經改變;二:獲取棧頂的Activity,重新執行該Activity,以適配新的Configuration。 IApplicationThread是一個aidl檔案。這裡最終呼叫的是ApplicationThread物件,而ApplicationThread類是ActivityThread的一個內部類。 ActivityThread.java (android-6.0\frameworks\base\core\java\android\app) [java] view plain copy print?
  1. publicvoid scheduleConfigurationChanged(Configuration config) {  
  2.     updatePendingConfiguration(config);  
  3.     sendMessage(H.CONFIGURATION_CHANGED, config);  
  4. }  
        public void scheduleConfigurationChanged(Configuration config) {
            updatePendingConfiguration(config);
            sendMessage(H.CONFIGURATION_CHANGED, config);
        }
直接通過Handler機制與ActivityThread進行通訊。 [java] view plain copy print?
  1. publicvoid handleMessage(Message msg) {  
  2.         case CONFIGURATION_CHANGED:  
  3.             Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ”configChanged”);  
  4.             mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;  
  5.             handleConfigurationChanged((Configuration)msg.obj, null);  
  6.             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);  
  7.             break;  
  8.     }  
  9.     if (DEBUG_MESSAGES) Slog.v(TAG, “<<< done: ” + codeToString(msg.what));  
  10. }  
        public void handleMessage(Message msg) {

                case CONFIGURATION_CHANGED:
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
                    mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
                    handleConfigurationChanged((Configuration)msg.obj, null);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
            }
            if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
        }
[java] view plain copy print?
  1. finalvoid handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {  
  2.     ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);  
  3.     freeTextLayoutCachesIfNeeded(configDiff);  
  4.     if (callbacks != null) {  
  5.         finalint N = callbacks.size();  
  6.         for (int i=0; i<N; i++) {  
  7.             performConfigurationChanged(callbacks.get(i), config);  
  8.         }  
  9.     }  
  10. }  
    final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {

        ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);

        freeTextLayoutCachesIfNeeded(configDiff);

        if (callbacks != null) {
            final int N = callbacks.size();
            for (int i=0; i<N; i++) {
                performConfigurationChanged(callbacks.get(i), config);
            }
        }
    }

[java] view plain copy print?
  1. privatestaticvoid performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {  
  2.     // Only for Activity objects, check that they actually call up to their
  3.     // superclass implementation.  ComponentCallbacks2 is an interface, so
  4.     // we check the runtime type and act accordingly.
  5.     Activity activity = (cb instanceof Activity) ? (Activity) cb : null;  
  6.     if (shouldChangeConfig) {  
  7.         cb.onConfigurationChanged(config);  
  8. }  
    private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
        // Only for Activity objects, check that they actually call up to their
        // superclass implementation.  ComponentCallbacks2 is an interface, so
        // we check the runtime type and act accordingly.
        Activity activity = (cb instanceof Activity) ? (Activity) cb : null;

        if (shouldChangeConfig) {
            cb.onConfigurationChanged(config);

    }
很明顯,在這裡呼叫了onConfigurationChanged方法。也就是常說在橫豎屏切換的時候先呼叫Activity的onConfigurationChanged,通常會重寫這個方法,做一些儲存引數之類的操作。 在呼叫完onConfigurationChanged後,Activity會重新建立。所以就回到AMS的updateConfigurationLocked方法中。 kept = mainStack.ensureActivityConfigurationLocked(starting, changes); mainStack:ActivityStack。 ActivityStack.java (android-6.0\frameworks\base\services\core\java\com\android\server\am) [java] view plain copy print?
  1. /** 
  2.  * Make sure the given activity matches the current configuration.  Returns 
  3.  * false if the activity had to be destroyed.  Returns true if the 
  4.  * configuration is the same, or the activity will remain running as-is 
  5.  * for whatever reason.  Ensures the HistoryRecord is updated with the 
  6.  * correct configuration and all other bookkeeping is handled. 
  7.  */
  8. finalboolean ensureActivityConfigurationLocked(ActivityRecord r,  
  9.         int globalChanges) {  
  10.     if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {  
  11.         // Aha, the activity isn’t handling the change, so DIE DIE DIE.
  12.         r.configChangeFlags |= changes;  
  13.         r.startFreezingScreenLocked(r.app, globalChanges);  
  14.         r.forceNewConfig = false;  
  15.         if (r.app == null || r.app.thread == null) {  
  16.             if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,  
  17.                     ”Config is destroying non-running ” + r);  
  18.             destroyActivityLocked(r, true“config”);  
  19.         } elseif (r.state == ActivityState.PAUSING) {  
  20.             // A little annoying: we are waiting for this activity to
  21.             // finish pausing.  Let’s not do anything now, but just
  22.             // flag that it needs to be restarted when done pausing.
  23.             if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,  
  24.                     ”Config is skipping already pausing ” + r);  
  25.             r.configDestroy = true;  
  26.             returntrue;  
  27.         } elseif (r.state == ActivityState.RESUMED) {  
  28.             // Try to optimize this case: the configuration is changing
  29.             // and we need to restart the top, resumed activity.
  30.             // Instead of doing the normal handshaking, just say
  31.             // “restart!”.
  32.             if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,  
  33.                     ”Config is relaunching resumed ” + r);  
  34.             relaunchActivityLocked(r, r.configChangeFlags, true);  
  35.             r.configChangeFlags = 0;  
  36.         } else {  
  37.             if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,  
  38.                     ”Config is relaunching non-resumed ” + r);  
  39.             relaunchActivityLocked(r, r.configChangeFlags, false);  
  40.             r.configChangeFlags = 0;  
  41.         }  
  42.         // All done…  tell the caller we weren’t able to keep this
  43.         // activity around.
  44.         returnfalse;  
  45.     }  
  46.     returntrue;  
  47. }  
    /**
     * Make sure the given activity matches the current configuration.  Returns
     * false if the activity had to be destroyed.  Returns true if the
     * configuration is the same, or the activity will remain running as-is
     * for whatever reason.  Ensures the HistoryRecord is updated with the
     * correct configuration and all other bookkeeping is handled.
     */
    final boolean ensureActivityConfigurationLocked(ActivityRecord r,
            int globalChanges) {

        if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
            // Aha, the activity isn't handling the change, so DIE DIE DIE.
            r.configChangeFlags |= changes;
            r.startFreezingScreenLocked(r.app, globalChanges);
            r.forceNewConfig = false;
            if (r.app == null || r.app.thread == null) {
                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                        "Config is destroying non-running " + r);
                destroyActivityLocked(r, true, "config");
            } else if (r.state == ActivityState.PAUSING) {
                // A little annoying: we are waiting for this activity to
                // finish pausing.  Let's not do anything now, but just
                // flag that it needs to be restarted when done pausing.
                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                        "Config is skipping already pausing " + r);
                r.configDestroy = true;
                return true;
            } else if (r.state == ActivityState.RESUMED) {
                // Try to optimize this case: the configuration is changing
                // and we need to restart the top, resumed activity.
                // Instead of doing the normal handshaking, just say
                // "restart!".
                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                        "Config is relaunching resumed " + r);
                relaunchActivityLocked(r, r.configChangeFlags, true);
                r.configChangeFlags = 0;
            } else {
                if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                        "Config is relaunching non-resumed " + r);
                relaunchActivityLocked(r, r.configChangeFlags, false);
                r.configChangeFlags = 0;
            }

            // All done...  tell the caller we weren't able to keep this
            // activity around.
            return false;
        }

        return true;
    }
[java] view plain copy print?
  1. privateboolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {  
  2.     try {  
  3.         if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,  
  4.                 ”Moving to ” + (andResume ? “RESUMED” : “PAUSED”) + “ Relaunching ” + r);  
  5.         r.forceNewConfig = false;  
  6.         r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,  
  7.                 !andResume, new Configuration(mService.mConfiguration),  
  8.                 new Configuration(mOverrideConfig));  
  9.         // Note: don’t need to call pauseIfSleepingLocked() here, because
  10.         // the caller will only pass in ‘andResume’ if this activity is
  11.         // currently resumed, which implies we aren’t sleeping.
  12.     } catch (RemoteException e) {  
  13.         if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, “Relaunch failed”, e);  
  14.     }  
  15.     returntrue;  
  16. }  
    private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {

        try {
            if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
                    "Moving to " + (andResume ? "RESUMED" : "PAUSED") + "