Skip to content

Commit 358148e

Browse files
author
John Spurlock
committed
Implement "sticky" widget logic.
(frameworks/base) Used after screen-off, and after launching camera. Bug: 7418847 Change-Id: I5179e6af13be4494fde77fde2cdb5610ab888dac
1 parent 61426f3 commit 358148e

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

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

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

154155
// The current user is set by KeyguardViewMediator and shared by all LockPatternUtils.
155156
private static volatile int sCurrentUserId = UserHandle.USER_NULL;
@@ -1310,4 +1311,12 @@ public boolean getPowerButtonInstantlyLocks() {
13101311
return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true);
13111312
}
13121313

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

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

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ 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+
}
252260
};
253261

254262
@Override
@@ -712,7 +720,7 @@ private void showSecurityScreen(SecurityMode securityMode) {
712720

713721
@Override
714722
public void onScreenTurnedOn() {
715-
if (DEBUG) Log.d(TAG, "screen on");
723+
if (DEBUG) Log.d(TAG, "screen on, instance " + Integer.toHexString(hashCode()));
716724
showPrimarySecurityScreen(false);
717725
getSecurityView(mCurrentSecuritySelection).onResume();
718726

@@ -728,7 +736,7 @@ public void onScreenTurnedOn() {
728736

729737
@Override
730738
public void onScreenTurnedOff() {
731-
if (DEBUG) Log.d(TAG, "screen off");
739+
if (DEBUG) Log.d(TAG, "screen off, instance " + Integer.toHexString(hashCode()));
732740
showPrimarySecurityScreen(true);
733741
getSecurityView(mCurrentSecuritySelection).onPause();
734742
}
@@ -849,6 +857,7 @@ public void onCameraLaunched() {
849857
SlidingChallengeLayout slider = locateSlider();
850858
if (slider != null) {
851859
slider.showHandle(true);
860+
slider.showChallenge(true);
852861
}
853862
View v = mAppWidgetContainer.getChildAt(mAppWidgetContainer.getCurrentPage());
854863
if (v instanceof CameraWidgetFrame) {
@@ -1060,41 +1069,66 @@ public void onRestoreInstanceState(Parcelable state) {
10601069
}
10611070

10621071
private void showAppropriateWidgetPage() {
1063-
1064-
// The following sets the priority for showing widgets. Transport should be shown if
1065-
// music is playing, followed by the multi-user widget if enabled, followed by the
1066-
// status widget.
1067-
final int pageToShow;
1068-
if (mTransportControl.isMusicPlaying() || mTransportState == TRANSPORT_VISIBLE) {
1072+
boolean music = mTransportControl.isMusicPlaying() || mTransportState == TRANSPORT_VISIBLE;
1073+
if (music) {
10691074
mTransportState = TRANSPORT_VISIBLE;
1070-
pageToShow = mAppWidgetContainer.indexOfChild(mTransportControl);
1071-
} else {
1072-
UserManager mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1073-
final View multiUserView = findViewById(R.id.keyguard_multi_user_selector);
1074-
final int multiUserPosition = mAppWidgetContainer.indexOfChild(multiUserView);
1075-
if (multiUserPosition != -1 && mUm.getUsers(true).size() > 1) {
1076-
pageToShow = multiUserPosition;
1077-
} else {
1078-
final View statusView = findViewById(R.id.keyguard_status_view);
1079-
int statusViewIndex = mAppWidgetContainer.indexOfChild(statusView);
1080-
if (statusViewIndex == -1) {
1081-
// TEMP code for default page
1082-
if (mAppWidgetContainer.getChildCount() > 2) {
1083-
pageToShow = mAppWidgetContainer.getChildCount() - 2;
1084-
} else {
1085-
pageToShow = 0;
1086-
}
1087-
} else {
1088-
pageToShow = mAppWidgetContainer.indexOfChild(statusView);
1089-
}
1090-
}
1091-
if (mTransportState == TRANSPORT_VISIBLE) {
1092-
mTransportState = TRANSPORT_INVISIBLE;
1093-
}
1075+
} else if (mTransportState == TRANSPORT_VISIBLE) {
1076+
mTransportState = TRANSPORT_INVISIBLE;
10941077
}
1078+
int pageToShow = getAppropriateWidgetPage();
10951079
mAppWidgetContainer.setCurrentPage(pageToShow);
10961080
}
10971081

1082+
private boolean isCameraOrAdd(int pageIndex) {
1083+
View v = mAppWidgetContainer.getChildAt(pageIndex);
1084+
return v.getId() == R.id.keyguard_add_widget || v instanceof CameraWidgetFrame;
1085+
}
1086+
1087+
private int getAppropriateWidgetPage() {
1088+
// assumes at least one widget (besides camera + add)
1089+
1090+
boolean music = mTransportControl.isMusicPlaying() || mTransportState == TRANSPORT_VISIBLE;
1091+
// if music playing, show transport
1092+
if (music) {
1093+
if (DEBUG) Log.d(TAG, "Music playing, show transport");
1094+
return mAppWidgetContainer.indexOfChild(mTransportControl);
1095+
}
1096+
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();
1108+
if (stickyWidgetIndex > -1
1109+
&& stickyWidgetIndex < mAppWidgetContainer.getChildCount()
1110+
&& !isCameraOrAdd(stickyWidgetIndex)) {
1111+
if (DEBUG) Log.d(TAG, "Sticky widget found, show it");
1112+
return stickyWidgetIndex;
1113+
}
1114+
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)
1124+
int rightMost = mAppWidgetContainer.getChildCount() - 1;
1125+
if (mAppWidgetContainer.getChildAt(rightMost) instanceof CameraWidgetFrame) {
1126+
rightMost--;
1127+
}
1128+
if (DEBUG) Log.d(TAG, "Show right-most");
1129+
return rightMost;
1130+
}
1131+
10981132
private void enableUserSelectorIfNecessary() {
10991133
// if there are multiple users, we need to add the multi-user switcher widget to the
11001134
// keyguard.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void onPageSwitch(View newPage, int newPageIndex) {
110110
if (mCallbacks != null) {
111111
mCallbacks.onUserActivityTimeoutChanged();
112112
mCallbacks.userActivity();
113+
mCallbacks.onPageSwitch(newPageIndex);
113114
}
114115
KeyguardWidgetFrame oldWidgetPage = getWidgetPageAt(oldPageIndex);
115116
if (oldWidgetPage != null) {
@@ -149,6 +150,7 @@ public void setCallbacks(Callbacks callbacks) {
149150
public interface Callbacks {
150151
public void userActivity();
151152
public void onUserActivityTimeoutChanged();
153+
public void onPageSwitch(int newPageIndex);
152154
}
153155

154156
public void addWidget(View widget) {

0 commit comments

Comments
 (0)