2626import android .appwidget .AppWidgetHostView ;
2727import android .appwidget .AppWidgetManager ;
2828import android .appwidget .AppWidgetProviderInfo ;
29- import android .content .ActivityNotFoundException ;
3029import android .content .ComponentName ;
3130import android .content .Context ;
3231import android .content .Intent ;
@@ -128,6 +127,8 @@ public KeyguardHostView(Context context, AttributeSet attrs) {
128127 mLockPatternUtils = new LockPatternUtils (context );
129128 mAppWidgetHost = new AppWidgetHost (
130129 context , APPWIDGET_HOST_ID , mOnClickHandler , Looper .myLooper ());
130+ cleanupAppWidgetIds ();
131+
131132 mAppWidgetManager = AppWidgetManager .getInstance (mContext );
132133 mSecurityModel = new KeyguardSecurityModel (context );
133134
@@ -153,6 +154,33 @@ public KeyguardHostView(Context context, AttributeSet attrs) {
153154 }
154155 }
155156
157+ private void cleanupAppWidgetIds () {
158+ // Clean up appWidgetIds that are bound to lockscreen, but not actually used
159+ // This is only to clean up after another bug: we used to not call
160+ // deleteAppWidgetId when a user manually deleted a widget in keyguard. This code
161+ // shouldn't have to run more than once per user. AppWidgetProviders rely on callbacks
162+ // that are triggered by deleteAppWidgetId, which is why we're doing this
163+ int [] appWidgetIdsInKeyguardSettings = mLockPatternUtils .getAppWidgets ();
164+ int [] appWidgetIdsBoundToHost = mAppWidgetHost .getAppWidgetIds ();
165+ for (int i = 0 ; i < appWidgetIdsBoundToHost .length ; i ++) {
166+ int appWidgetId = appWidgetIdsBoundToHost [i ];
167+ if (!contains (appWidgetIdsInKeyguardSettings , appWidgetId )) {
168+ Log .d (TAG , "Found a appWidgetId that's not being used by keyguard, deleting id "
169+ + appWidgetId );
170+ mAppWidgetHost .deleteAppWidgetId (appWidgetId );
171+ }
172+ }
173+ }
174+
175+ private static boolean contains (int [] array , int target ) {
176+ for (int value : array ) {
177+ if (value == target ) {
178+ return true ;
179+ }
180+ }
181+ return false ;
182+ }
183+
156184 private KeyguardUpdateMonitorCallback mUpdateMonitorCallbacks =
157185 new KeyguardUpdateMonitorCallback () {
158186 @ Override
@@ -331,10 +359,17 @@ public void onAddView(View v) {
331359 };
332360
333361 @ Override
334- public void onRemoveView (View v ) {
362+ public void onRemoveView (View v , boolean deletePermanently ) {
335363 if (numWidgets () < MAX_WIDGETS ) {
336364 setAddWidgetEnabled (true );
337365 }
366+ if (deletePermanently ) {
367+ final int appWidgetId = ((KeyguardWidgetFrame ) v ).getContentAppWidgetId ();
368+ if (appWidgetId != AppWidgetManager .INVALID_APPWIDGET_ID &&
369+ appWidgetId != LockPatternUtils .ID_DEFAULT_STATUS_WIDGET ) {
370+ mAppWidgetHost .deleteAppWidgetId (appWidgetId );
371+ }
372+ }
338373 }
339374 };
340375
0 commit comments