@@ -249,14 +249,6 @@ public void onUserActivityTimeoutChanged() {
249249 mViewMediatorCallback .onUserActivityTimeoutChanged ();
250250 }
251251 }
252-
253- @ Override
254- public void onPageSwitch (int newPageIndex ) {
255- if (!isCameraOrAdd (newPageIndex )) {
256- if (DEBUG ) Log .d (TAG , "Setting sticky widget index: " + newPageIndex );
257- mLockPatternUtils .setStickyWidgetIndex (newPageIndex );
258- }
259- }
260252 };
261253
262254 @ Override
@@ -737,6 +729,7 @@ public void onScreenTurnedOn() {
737729 @ Override
738730 public void onScreenTurnedOff () {
739731 if (DEBUG ) Log .d (TAG , "screen off, instance " + Integer .toHexString (hashCode ()));
732+ saveStickyWidgetIndex ();
740733 showPrimarySecurityScreen (true );
741734 getSecurityView (mCurrentSecuritySelection ).onPause ();
742735 }
@@ -859,8 +852,7 @@ public void onCameraLaunched() {
859852 slider .showHandle (true );
860853 slider .showChallenge (true );
861854 }
862- View v = mAppWidgetContainer .getChildAt (mAppWidgetContainer .getCurrentPage ());
863- if (v instanceof CameraWidgetFrame ) {
855+ if (isCameraPage (mAppWidgetContainer .getCurrentPage ())) {
864856 mAppWidgetContainer .scrollLeft ();
865857 }
866858 }
@@ -1050,6 +1042,8 @@ public SavedState[] newArray(int size) {
10501042
10511043 @ Override
10521044 public Parcelable onSaveInstanceState () {
1045+ if (DEBUG ) Log .d (TAG , "onSaveInstanceState" );
1046+ saveStickyWidgetIndex ();
10531047 Parcelable superState = super .onSaveInstanceState ();
10541048 SavedState ss = new SavedState (superState );
10551049 ss .transportState = mTransportState ;
@@ -1058,6 +1052,7 @@ public Parcelable onSaveInstanceState() {
10581052
10591053 @ Override
10601054 public void onRestoreInstanceState (Parcelable state ) {
1055+ if (DEBUG ) Log .d (TAG , "onRestoreInstanceState" );
10611056 if (!(state instanceof SavedState )) {
10621057 super .onRestoreInstanceState (state );
10631058 return ;
@@ -1068,70 +1063,82 @@ public void onRestoreInstanceState(Parcelable state) {
10681063 post (mSwitchPageRunnable );
10691064 }
10701065
1066+ @ Override
1067+ public void onWindowFocusChanged (boolean hasWindowFocus ) {
1068+ super .onWindowFocusChanged (hasWindowFocus );
1069+ if (DEBUG ) Log .d (TAG , "Window is " + (hasWindowFocus ? "focused" : "unfocused" ));
1070+ if (!hasWindowFocus ) {
1071+ saveStickyWidgetIndex ();
1072+ }
1073+ }
1074+
10711075 private void showAppropriateWidgetPage () {
1072- boolean music = mTransportControl .isMusicPlaying () || mTransportState == TRANSPORT_VISIBLE ;
1073- if (music ) {
1076+ boolean isMusicPlaying =
1077+ mTransportControl .isMusicPlaying () || mTransportState == TRANSPORT_VISIBLE ;
1078+ if (isMusicPlaying ) {
10741079 mTransportState = TRANSPORT_VISIBLE ;
10751080 } else if (mTransportState == TRANSPORT_VISIBLE ) {
10761081 mTransportState = TRANSPORT_INVISIBLE ;
10771082 }
1078- int pageToShow = getAppropriateWidgetPage ();
1083+ int pageToShow = getAppropriateWidgetPage (isMusicPlaying );
10791084 mAppWidgetContainer .setCurrentPage (pageToShow );
10801085 }
10811086
1082- private boolean isCameraOrAdd (int pageIndex ) {
1087+ private boolean isCameraPage (int pageIndex ) {
1088+ View v = mAppWidgetContainer .getChildAt (pageIndex );
1089+ return v != null && v instanceof CameraWidgetFrame ;
1090+ }
1091+
1092+ private boolean isAddPage (int pageIndex ) {
10831093 View v = mAppWidgetContainer .getChildAt (pageIndex );
1084- return v .getId () == R .id .keyguard_add_widget || v instanceof CameraWidgetFrame ;
1094+ return v != null && v .getId () == R .id .keyguard_add_widget ;
10851095 }
10861096
1087- private int getAppropriateWidgetPage () {
1097+ private int getAppropriateWidgetPage (boolean isMusicPlaying ) {
10881098 // assumes at least one widget (besides camera + add)
10891099
1090- boolean music = mTransportControl .isMusicPlaying () || mTransportState == TRANSPORT_VISIBLE ;
10911100 // if music playing, show transport
1092- if (music ) {
1101+ if (isMusicPlaying ) {
10931102 if (DEBUG ) Log .d (TAG , "Music playing, show transport" );
10941103 return mAppWidgetContainer .indexOfChild (mTransportControl );
10951104 }
10961105
1097- // if multi-user applicable, show it
1098- UserManager userManager = (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
1099- View multiUserView = findViewById (R .id .keyguard_multi_user_selector );
1100- int multiUserPosition = mAppWidgetContainer .indexOfChild (multiUserView );
1101- if (multiUserPosition != -1 && userManager .getUsers (true ).size () > 1 ) {
1102- if (DEBUG ) Log .d (TAG , "Multi-user applicable, show it" );
1103- return multiUserPosition ;
1104- }
1105-
1106- // if we have a sticky widget, show it
1107- int stickyWidgetIndex = mLockPatternUtils .getStickyWidgetIndex ();
1106+ // if we have a valid sticky widget, show it
1107+ int stickyWidgetIndex = mLockPatternUtils .getStickyAppWidgetIndex ();
11081108 if (stickyWidgetIndex > -1
11091109 && stickyWidgetIndex < mAppWidgetContainer .getChildCount ()
1110- && !isCameraOrAdd (stickyWidgetIndex )) {
1111- if (DEBUG ) Log .d (TAG , "Sticky widget found, show it" );
1110+ && !isAddPage (stickyWidgetIndex )
1111+ && !isCameraPage (stickyWidgetIndex )) {
1112+ if (DEBUG ) Log .d (TAG , "Valid sticky widget found, show page " + stickyWidgetIndex );
11121113 return stickyWidgetIndex ;
11131114 }
11141115
1115- // if we have a status view, show it
1116- View statusView = findViewById (R .id .keyguard_status_view );
1117- int statusViewIndex = mAppWidgetContainer .indexOfChild (statusView );
1118- if (statusViewIndex > -1 ) {
1119- if (DEBUG ) Log .d (TAG , "Status widget found, show it" );
1120- return mAppWidgetContainer .indexOfChild (statusView );
1121- }
1122-
1123- // else the right-most (except for camera)
1116+ // else show the right-most widget (except for camera)
11241117 int rightMost = mAppWidgetContainer .getChildCount () - 1 ;
1125- if (mAppWidgetContainer . getChildAt (rightMost ) instanceof CameraWidgetFrame ) {
1118+ if (isCameraPage (rightMost )) {
11261119 rightMost --;
11271120 }
1128- if (DEBUG ) Log .d (TAG , "Show right-most" );
1121+ if (DEBUG ) Log .d (TAG , "Show right-most page " + rightMost );
11291122 return rightMost ;
11301123 }
11311124
1125+ private void saveStickyWidgetIndex () {
1126+ int stickyWidgetIndex = mAppWidgetContainer .getCurrentPage ();
1127+ if (isAddPage (stickyWidgetIndex )) {
1128+ stickyWidgetIndex ++;
1129+ }
1130+ if (isCameraPage (stickyWidgetIndex )) {
1131+ stickyWidgetIndex --;
1132+ }
1133+ if (stickyWidgetIndex < 0 || stickyWidgetIndex >= mAppWidgetContainer .getChildCount ()) {
1134+ stickyWidgetIndex = -1 ;
1135+ }
1136+ if (DEBUG ) Log .d (TAG , "saveStickyWidgetIndex: " + stickyWidgetIndex );
1137+ mLockPatternUtils .setStickyAppWidgetIndex (stickyWidgetIndex );
1138+ }
1139+
11321140 private void enableUserSelectorIfNecessary () {
1133- // if there are multiple users, we need to add the multi-user switcher widget to the
1134- // keyguard.
1141+ // if there are multiple users, we need to enable to multi-user switcher
11351142 UserManager mUm = (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
11361143 List <UserInfo > users = mUm .getUsers (true );
11371144
0 commit comments