@@ -69,6 +69,8 @@ public class KeyguardHostView extends KeyguardViewBase {
6969 // Found in KeyguardAppWidgetPickActivity.java
7070 static final int APPWIDGET_HOST_ID = 0x4B455947 ;
7171
72+ private final int MAX_WIDGETS = 5 ;
73+
7274 private AppWidgetHost mAppWidgetHost ;
7375 private AppWidgetManager mAppWidgetManager ;
7476 private KeyguardWidgetPager mAppWidgetContainer ;
@@ -225,6 +227,9 @@ protected void onFinishInflate() {
225227 addDefaultWidgets ();
226228
227229 addWidgetsFromSettings ();
230+ if (numWidgets () >= MAX_WIDGETS ) {
231+ setAddWidgetEnabled (false );
232+ }
228233 checkAppWidgetConsistency ();
229234 mSwitchPageRunnable .run ();
230235 // This needs to be called after the pages are all added.
@@ -313,6 +318,13 @@ public void userActivity() {
313318 public void onUserActivityTimeoutChanged () {
314319 KeyguardHostView .this .onUserActivityTimeoutChanged ();
315320 }
321+
322+ @ Override
323+ public void onRemoveView (View v ) {
324+ if (numWidgets () < MAX_WIDGETS ) {
325+ setAddWidgetEnabled (true );
326+ }
327+ }
316328 };
317329
318330 public void userActivity () {
@@ -975,13 +987,33 @@ LockPatternUtils getLockPatternUtils() {
975987 }
976988 };
977989
990+ private int numWidgets () {
991+ final int childCount = mAppWidgetContainer .getChildCount ();
992+ int widgetCount = 0 ;
993+ for (int i = 0 ; i < childCount ; i ++) {
994+ if (mAppWidgetContainer .isWidgetPage (i )) {
995+ widgetCount ++;
996+ }
997+ }
998+ return widgetCount ;
999+ }
1000+
1001+
1002+ private void setAddWidgetEnabled (boolean clickable ) {
1003+ View addWidget = mAppWidgetContainer .findViewById (R .id .keyguard_add_widget );
1004+ if (addWidget != null ) {
1005+ View addWidgetButton = addWidget .findViewById (R .id .keyguard_add_widget_view );
1006+ addWidgetButton .setEnabled (clickable );
1007+ }
1008+ }
1009+
9781010 private void addDefaultWidgets () {
9791011 LayoutInflater inflater = LayoutInflater .from (mContext );
9801012 inflater .inflate (R .layout .keyguard_transport_control_view , this , true );
9811013
9821014 if (!mSafeModeEnabled && !widgetsDisabledByDpm ()) {
9831015 View addWidget = inflater .inflate (R .layout .keyguard_add_widget , this , false );
984- mAppWidgetContainer .addWidget (addWidget );
1016+ mAppWidgetContainer .addWidget (addWidget , 0 );
9851017 View addWidgetButton = addWidget .findViewById (R .id .keyguard_add_widget_view );
9861018 addWidgetButton .setOnClickListener (new OnClickListener () {
9871019 @ Override
0 commit comments