Skip to content

Commit b2f21c5

Browse files
John SpurlockAndroid (Google) Code Review
authored andcommitted
Merge "Use settings to persist sticky widget." into jb-mr1-lockscreen-dev
2 parents bc676a0 + 5f050e5 commit b2f21c5

File tree

4 files changed

+76
-58
lines changed

4 files changed

+76
-58
lines changed

core/java/android/provider/Settings.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,20 +3216,27 @@ public static boolean putFloatForUser(ContentResolver cr, String name, float val
32163216

32173217

32183218
/**
3219-
* This preference contains the string that shows for owner info on LockScren.
3219+
* This preference contains the string that shows for owner info on LockScreen.
32203220
* @hide
32213221
*/
32223222
public static final String LOCK_SCREEN_OWNER_INFO = "lock_screen_owner_info";
32233223

32243224
/**
3225-
* Id of the user-selected appwidget on the lockscreen, or -1 if none
3225+
* Ids of the user-selected appwidgets on the lockscreen (comma-delimited).
32263226
* @hide
32273227
*/
32283228
public static final String LOCK_SCREEN_APPWIDGET_IDS =
32293229
"lock_screen_appwidget_ids";
32303230

32313231
/**
3232-
* This preference enables showing the owner info on LockScren.
3232+
* Index of the lockscreen appwidget to restore, -1 if none.
3233+
* @hide
3234+
*/
3235+
public static final String LOCK_SCREEN_STICKY_APPWIDGET =
3236+
"lock_screen_sticky_appwidget";
3237+
3238+
/**
3239+
* This preference enables showing the owner info on LockScreen.
32333240
* @hide
32343241
*/
32353242
public static final String LOCK_SCREEN_OWNER_INFO_ENABLED =

core/java/com/android/internal/widget/LockPatternUtils.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ public class LockPatternUtils {
150150
private final ContentResolver mContentResolver;
151151
private DevicePolicyManager mDevicePolicyManager;
152152
private ILockSettings mLockSettingsService;
153-
private int mStickyWidgetIndex = -1;
154153

155154
// The current user is set by KeyguardViewMediator and shared by all LockPatternUtils.
156155
private static volatile int sCurrentUserId = UserHandle.USER_NULL;
@@ -1162,6 +1161,21 @@ public boolean removeAppWidget(int widgetId) {
11621161
return true;
11631162
}
11641163

1164+
public int getStickyAppWidgetIndex() {
1165+
return Settings.Secure.getIntForUser(
1166+
mContentResolver,
1167+
Settings.Secure.LOCK_SCREEN_STICKY_APPWIDGET,
1168+
-1,
1169+
UserHandle.USER_CURRENT);
1170+
}
1171+
1172+
public void setStickyAppWidgetIndex(int value) {
1173+
Settings.Secure.putIntForUser(mContentResolver,
1174+
Settings.Secure.LOCK_SCREEN_STICKY_APPWIDGET,
1175+
value,
1176+
UserHandle.USER_CURRENT);
1177+
}
1178+
11651179
private long getLong(String secureSettingKey, long defaultValue) {
11661180
try {
11671181
return getLockSettings().getLong(secureSettingKey, defaultValue,
@@ -1311,12 +1325,4 @@ public boolean getPowerButtonInstantlyLocks() {
13111325
return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true);
13121326
}
13131327

1314-
public int getStickyWidgetIndex() {
1315-
return mStickyWidgetIndex;
1316-
}
1317-
1318-
public void setStickyWidgetIndex(int stickyWidgetIndex) {
1319-
mStickyWidgetIndex = stickyWidgetIndex;
1320-
}
1321-
13221328
}

policy/src/com/android/internal/policy/impl/keyguard/KeyguardHostView.java

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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

policy/src/com/android/internal/policy/impl/keyguard/KeyguardWidgetPager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public void onPageSwitch(View newPage, int newPageIndex) {
116116
if (mCallbacks != null) {
117117
mCallbacks.onUserActivityTimeoutChanged();
118118
mCallbacks.userActivity();
119-
mCallbacks.onPageSwitch(newPageIndex);
120119
}
121120
KeyguardWidgetFrame oldWidgetPage = getWidgetPageAt(oldPageIndex);
122121
if (oldWidgetPage != null) {
@@ -156,7 +155,6 @@ public void setCallbacks(Callbacks callbacks) {
156155
public interface Callbacks {
157156
public void userActivity();
158157
public void onUserActivityTimeoutChanged();
159-
public void onPageSwitch(int newPageIndex);
160158
}
161159

162160
public void addWidget(View widget) {

0 commit comments

Comments
 (0)