Skip to content

Commit 7b0c877

Browse files
mikejurkaAndroid (Google) Code Review
authored andcommitted
Merge "Delete appWidgetId when removing lockscreen widgets" into jb-mr1.1-dev
2 parents a9c0bb4 + 75b5cfb commit 7b0c877

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed

core/java/android/appwidget/AppWidgetHost.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,22 @@ public static int allocateAppWidgetIdForSystem(int hostId) {
224224
}
225225
}
226226

227+
/**
228+
* Gets a list of all the appWidgetIds that are bound to the current host
229+
*
230+
* @hide
231+
*/
232+
public int[] getAppWidgetIds() {
233+
try {
234+
if (sService == null) {
235+
bindService();
236+
}
237+
return sService.getAppWidgetIdsForHost(mHostId);
238+
} catch (RemoteException e) {
239+
throw new RuntimeException("system server dead?", e);
240+
}
241+
}
242+
227243
private static void checkCallerIsSystem() {
228244
int uid = Process.myUid();
229245
if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {

core/java/com/android/internal/appwidget/IAppWidgetService.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ interface IAppWidgetService {
3838
void deleteHost(int hostId);
3939
void deleteAllHosts();
4040
RemoteViews getAppWidgetViews(int appWidgetId);
41+
int[] getAppWidgetIdsForHost(int hostId);
4142

4243
//
4344
// for AppWidgetManager

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import android.appwidget.AppWidgetHostView;
2727
import android.appwidget.AppWidgetManager;
2828
import android.appwidget.AppWidgetProviderInfo;
29-
import android.content.ActivityNotFoundException;
3029
import android.content.ComponentName;
3130
import android.content.Context;
3231
import 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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,18 @@ public interface Callbacks {
237237
public void userActivity();
238238
public void onUserActivityTimeoutChanged();
239239
public void onAddView(View v);
240-
public void onRemoveView(View v);
240+
public void onRemoveView(View v, boolean deletePermanently);
241241
}
242242

243243
public void addWidget(View widget) {
244244
addWidget(widget, -1);
245245
}
246246

247247

248-
public void onRemoveView(View v) {
248+
public void onRemoveView(View v, final boolean deletePermanently) {
249249
final int appWidgetId = ((KeyguardWidgetFrame) v).getContentAppWidgetId();
250250
if (mCallbacks != null) {
251-
mCallbacks.onRemoveView(v);
251+
mCallbacks.onRemoveView(v, deletePermanently);
252252
}
253253
mBackgroundWorkerHandler.post(new Runnable() {
254254
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ public void run() {
14571457
}
14581458

14591459
removeView(mDragView);
1460-
onRemoveView(mDragView);
1460+
onRemoveView(mDragView, false);
14611461
addView(mDragView, pageUnderPointIndex);
14621462
onAddView(mDragView, pageUnderPointIndex);
14631463
mSidePageHoverIndex = -1;
@@ -1587,7 +1587,7 @@ mCurrentPage < getChildCount() - 1) {
15871587
}
15881588

15891589
//public abstract void onFlingToDelete(View v);
1590-
public abstract void onRemoveView(View v);
1590+
public abstract void onRemoveView(View v, boolean deletePermanently);
15911591
public abstract void onAddView(View v, int index);
15921592

15931593
private void resetTouchState() {
@@ -2391,7 +2391,7 @@ public void run() {
23912391
slideAnimations.start();
23922392

23932393
removeView(dragView);
2394-
onRemoveView(dragView);
2394+
onRemoveView(dragView, true);
23952395
}
23962396
};
23972397
}

services/java/com/android/server/AppWidgetService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public int allocateAppWidgetId(String packageName, int hostId) throws RemoteExce
146146
return getImplForUser(getCallingOrCurrentUserId()).allocateAppWidgetId(
147147
packageName, hostId);
148148
}
149+
150+
@Override
151+
public int[] getAppWidgetIdsForHost(int hostId) throws RemoteException {
152+
return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetIdsForHost(hostId);
153+
}
149154

150155
@Override
151156
public void deleteAppWidgetId(int appWidgetId) throws RemoteException {

services/java/com/android/server/AppWidgetServiceImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,28 @@ public int[] getAppWidgetIds(ComponentName provider) {
13581358
}
13591359
}
13601360

1361+
static int[] getAppWidgetIds(Host h) {
1362+
int instancesSize = h.instances.size();
1363+
int appWidgetIds[] = new int[instancesSize];
1364+
for (int i = 0; i < instancesSize; i++) {
1365+
appWidgetIds[i] = h.instances.get(i).appWidgetId;
1366+
}
1367+
return appWidgetIds;
1368+
}
1369+
1370+
public int[] getAppWidgetIdsForHost(int hostId) {
1371+
synchronized (mAppWidgetIds) {
1372+
ensureStateLoadedLocked();
1373+
int callingUid = Binder.getCallingUid();
1374+
Host host = lookupHostLocked(callingUid, hostId);
1375+
if (host != null) {
1376+
return getAppWidgetIds(host);
1377+
} else {
1378+
return new int[0];
1379+
}
1380+
}
1381+
}
1382+
13611383
private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
13621384
Provider p = null;
13631385

0 commit comments

Comments
 (0)