| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.appwidget; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.app.Activity; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.ActivityNotFoundException; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.IntentSender; |
| 26 |
import android.content.pm.PackageManager; |
| 27 |
import android.os.Binder; |
| 28 |
import android.os.Build; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.os.Handler; |
| 31 |
import android.os.IBinder; |
| 32 |
import android.os.Looper; |
| 33 |
import android.os.Message; |
| 34 |
import android.os.Process; |
| 35 |
import android.os.RemoteException; |
| 36 |
import android.os.ServiceManager; |
| 37 |
import android.util.DisplayMetrics; |
| 38 |
import android.util.SparseArray; |
| 39 |
import android.widget.RemoteViews; |
| 40 |
import android.widget.RemoteViews.InteractionHandler; |
| 41 |
|
| 42 |
import com.android.internal.R; |
| 43 |
import com.android.internal.appwidget.IAppWidgetHost; |
| 44 |
import com.android.internal.appwidget.IAppWidgetService; |
| 45 |
|
| 46 |
import java.lang.ref.WeakReference; |
| 47 |
import java.util.List; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class AppWidgetHost { |
| 54 |
|
| 55 |
static final int HANDLE_UPDATE = 1; |
| 56 |
static final int HANDLE_PROVIDER_CHANGED = 2; |
| 57 |
static final int HANDLE_PROVIDERS_CHANGED = 3; |
| 58 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 59 |
static final int HANDLE_VIEW_DATA_CHANGED = 4; |
| 60 |
static final int HANDLE_APP_WIDGET_REMOVED = 5; |
| 61 |
|
| 62 |
final static Object sServiceLock = new Object(); |
| 63 |
@UnsupportedAppUsage |
| 64 |
static IAppWidgetService sService; |
| 65 |
static boolean sServiceInitialized = false; |
| 66 |
private DisplayMetrics mDisplayMetrics; |
| 67 |
|
| 68 |
private String mContextOpPackageName; |
| 69 |
@UnsupportedAppUsage |
| 70 |
private final Handler mHandler; |
| 71 |
private final int mHostId; |
| 72 |
private final Callbacks mCallbacks; |
| 73 |
private final SparseArray<AppWidgetHostListener> mListeners = new SparseArray<>(); |
| 74 |
private InteractionHandler mInteractionHandler; |
| 75 |
|
| 76 |
static class Callbacks extends IAppWidgetHost.Stub { |
| 77 |
private final WeakReference<Handler> mWeakHandler; |
| 78 |
|
| 79 |
public Callbacks(Handler handler) { |
| 80 |
mWeakHandler = new WeakReference<>(handler); |
| 81 |
} |
| 82 |
|
| 83 |
public void updateAppWidget(int appWidgetId, RemoteViews views) { |
| 84 |
if (isLocalBinder() && views != null) { |
| 85 |
views = views.clone(); |
| 86 |
} |
| 87 |
Handler handler = mWeakHandler.get(); |
| 88 |
if (handler == null) { |
| 89 |
return; |
| 90 |
} |
| 91 |
Message msg = handler.obtainMessage(HANDLE_UPDATE, appWidgetId, 0, views); |
| 92 |
msg.sendToTarget(); |
| 93 |
} |
| 94 |
|
| 95 |
public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) { |
| 96 |
if (isLocalBinder() && info != null) { |
| 97 |
info = info.clone(); |
| 98 |
} |
| 99 |
Handler handler = mWeakHandler.get(); |
| 100 |
if (handler == null) { |
| 101 |
return; |
| 102 |
} |
| 103 |
Message msg = handler.obtainMessage(HANDLE_PROVIDER_CHANGED, |
| 104 |
appWidgetId, 0, info); |
| 105 |
msg.sendToTarget(); |
| 106 |
} |
| 107 |
|
| 108 |
public void appWidgetRemoved(int appWidgetId) { |
| 109 |
Handler handler = mWeakHandler.get(); |
| 110 |
if (handler == null) { |
| 111 |
return; |
| 112 |
} |
| 113 |
handler.obtainMessage(HANDLE_APP_WIDGET_REMOVED, appWidgetId, 0).sendToTarget(); |
| 114 |
} |
| 115 |
|
| 116 |
public void providersChanged() { |
| 117 |
Handler handler = mWeakHandler.get(); |
| 118 |
if (handler == null) { |
| 119 |
return; |
| 120 |
} |
| 121 |
handler.obtainMessage(HANDLE_PROVIDERS_CHANGED).sendToTarget(); |
| 122 |
} |
| 123 |
|
| 124 |
public void viewDataChanged(int appWidgetId, int viewId) { |
| 125 |
Handler handler = mWeakHandler.get(); |
| 126 |
if (handler == null) { |
| 127 |
return; |
| 128 |
} |
| 129 |
Message msg = handler.obtainMessage(HANDLE_VIEW_DATA_CHANGED, |
| 130 |
appWidgetId, viewId); |
| 131 |
msg.sendToTarget(); |
| 132 |
} |
| 133 |
|
| 134 |
private static boolean isLocalBinder() { |
| 135 |
return Process.myPid() == Binder.getCallingPid(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
class UpdateHandler extends Handler { |
| 140 |
public UpdateHandler(Looper looper) { |
| 141 |
super(looper); |
| 142 |
} |
| 143 |
|
| 144 |
public void handleMessage(Message msg) { |
| 145 |
switch (msg.what) { |
| 146 |
case HANDLE_UPDATE: { |
| 147 |
updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj); |
| 148 |
break; |
| 149 |
} |
| 150 |
case HANDLE_APP_WIDGET_REMOVED: { |
| 151 |
dispatchOnAppWidgetRemoved(msg.arg1); |
| 152 |
break; |
| 153 |
} |
| 154 |
case HANDLE_PROVIDER_CHANGED: { |
| 155 |
onProviderChanged(msg.arg1, (AppWidgetProviderInfo)msg.obj); |
| 156 |
break; |
| 157 |
} |
| 158 |
case HANDLE_PROVIDERS_CHANGED: { |
| 159 |
onProvidersChanged(); |
| 160 |
break; |
| 161 |
} |
| 162 |
case HANDLE_VIEW_DATA_CHANGED: { |
| 163 |
viewDataChanged(msg.arg1, msg.arg2); |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public AppWidgetHost(Context context, int hostId) { |
| 171 |
this(context, hostId, null, context.getMainLooper()); |
| 172 |
} |
| 173 |
|
| 174 |
@Nullable |
| 175 |
private AppWidgetHostListener getListener(final int appWidgetId) { |
| 176 |
AppWidgetHostListener tempListener = null; |
| 177 |
synchronized (mListeners) { |
| 178 |
tempListener = mListeners.get(appWidgetId); |
| 179 |
} |
| 180 |
return tempListener; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 187 |
public AppWidgetHost(Context context, int hostId, InteractionHandler handler, Looper looper) { |
| 188 |
mContextOpPackageName = context.getOpPackageName(); |
| 189 |
mHostId = hostId; |
| 190 |
mInteractionHandler = handler; |
| 191 |
mHandler = new UpdateHandler(looper); |
| 192 |
mCallbacks = new Callbacks(mHandler); |
| 193 |
mDisplayMetrics = context.getResources().getDisplayMetrics(); |
| 194 |
bindService(context); |
| 195 |
} |
| 196 |
|
| 197 |
private static void bindService(Context context) { |
| 198 |
synchronized (sServiceLock) { |
| 199 |
if (sServiceInitialized) { |
| 200 |
return; |
| 201 |
} |
| 202 |
sServiceInitialized = true; |
| 203 |
PackageManager packageManager = context.getPackageManager(); |
| 204 |
if (!packageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS) |
| 205 |
&& !context.getResources().getBoolean(R.bool.config_enableAppWidgetService)) { |
| 206 |
return; |
| 207 |
} |
| 208 |
IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); |
| 209 |
sService = IAppWidgetService.Stub.asInterface(b); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public void startListening() { |
| 218 |
if (sService == null) { |
| 219 |
return; |
| 220 |
} |
| 221 |
final int[] idsToUpdate; |
| 222 |
synchronized (mListeners) { |
| 223 |
int n = mListeners.size(); |
| 224 |
idsToUpdate = new int[n]; |
| 225 |
for (int i = 0; i < n; i++) { |
| 226 |
idsToUpdate[i] = mListeners.keyAt(i); |
| 227 |
} |
| 228 |
} |
| 229 |
List<PendingHostUpdate> updates; |
| 230 |
try { |
| 231 |
updates = sService.startListening( |
| 232 |
mCallbacks, mContextOpPackageName, mHostId, idsToUpdate).getList(); |
| 233 |
} |
| 234 |
catch (RemoteException e) { |
| 235 |
throw new RuntimeException("system server dead?", e); |
| 236 |
} |
| 237 |
|
| 238 |
int N = updates.size(); |
| 239 |
for (int i = 0; i < N; i++) { |
| 240 |
PendingHostUpdate update = updates.get(i); |
| 241 |
switch (update.type) { |
| 242 |
case PendingHostUpdate.TYPE_VIEWS_UPDATE: |
| 243 |
updateAppWidgetView(update.appWidgetId, update.views); |
| 244 |
break; |
| 245 |
case PendingHostUpdate.TYPE_PROVIDER_CHANGED: |
| 246 |
onProviderChanged(update.appWidgetId, update.widgetInfo); |
| 247 |
break; |
| 248 |
case PendingHostUpdate.TYPE_VIEW_DATA_CHANGED: |
| 249 |
viewDataChanged(update.appWidgetId, update.viewId); |
| 250 |
break; |
| 251 |
case PendingHostUpdate.TYPE_APP_WIDGET_REMOVED: |
| 252 |
dispatchOnAppWidgetRemoved(update.appWidgetId); |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public void stopListening() { |
| 263 |
if (sService == null) { |
| 264 |
return; |
| 265 |
} |
| 266 |
try { |
| 267 |
sService.stopListening(mContextOpPackageName, mHostId); |
| 268 |
} |
| 269 |
catch (RemoteException e) { |
| 270 |
throw new RuntimeException("system server dead?", e); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public int allocateAppWidgetId() { |
| 280 |
if (sService == null) { |
| 281 |
return -1; |
| 282 |
} |
| 283 |
try { |
| 284 |
return sService.allocateAppWidgetId(mContextOpPackageName, mHostId); |
| 285 |
} |
| 286 |
catch (RemoteException e) { |
| 287 |
throw new RuntimeException("system server dead?", e); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public final void startAppWidgetConfigureActivityForResult(@NonNull Activity activity, |
| 311 |
int appWidgetId, int intentFlags, int requestCode, @Nullable Bundle options) { |
| 312 |
if (sService == null) { |
| 313 |
return; |
| 314 |
} |
| 315 |
try { |
| 316 |
IntentSender intentSender = sService.createAppWidgetConfigIntentSender( |
| 317 |
mContextOpPackageName, appWidgetId, intentFlags); |
| 318 |
if (intentSender != null) { |
| 319 |
activity.startIntentSenderForResult(intentSender, requestCode, null, 0, 0, 0, |
| 320 |
options); |
| 321 |
} else { |
| 322 |
throw new ActivityNotFoundException(); |
| 323 |
} |
| 324 |
} catch (IntentSender.SendIntentException e) { |
| 325 |
throw new ActivityNotFoundException(); |
| 326 |
} catch (RemoteException e) { |
| 327 |
throw new RuntimeException("system server dead?", e); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public void setAppWidgetHidden() { |
| 337 |
if (sService == null) { |
| 338 |
return; |
| 339 |
} |
| 340 |
try { |
| 341 |
sService.setAppWidgetHidden(mContextOpPackageName, mHostId); |
| 342 |
} catch (RemoteException e) { |
| 343 |
throw new RuntimeException("System server dead?", e); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public void setInteractionHandler(InteractionHandler interactionHandler) { |
| 353 |
mInteractionHandler = interactionHandler; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public int[] getAppWidgetIds() { |
| 360 |
if (sService == null) { |
| 361 |
return new int[0]; |
| 362 |
} |
| 363 |
try { |
| 364 |
return sService.getAppWidgetIdsForHost(mContextOpPackageName, mHostId); |
| 365 |
} catch (RemoteException e) { |
| 366 |
throw new RuntimeException("system server dead?", e); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public void deleteAppWidgetId(int appWidgetId) { |
| 374 |
if (sService == null) { |
| 375 |
return; |
| 376 |
} |
| 377 |
removeListener(appWidgetId); |
| 378 |
try { |
| 379 |
sService.deleteAppWidgetId(mContextOpPackageName, appWidgetId); |
| 380 |
} catch (RemoteException e) { |
| 381 |
throw new RuntimeException("system server dead?", e); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public void deleteHost() { |
| 394 |
if (sService == null) { |
| 395 |
return; |
| 396 |
} |
| 397 |
try { |
| 398 |
sService.deleteHost(mContextOpPackageName, mHostId); |
| 399 |
} |
| 400 |
catch (RemoteException e) { |
| 401 |
throw new RuntimeException("system server dead?", e); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public static void deleteAllHosts() { |
| 414 |
if (sService == null) { |
| 415 |
return; |
| 416 |
} |
| 417 |
try { |
| 418 |
sService.deleteAllHosts(); |
| 419 |
} |
| 420 |
catch (RemoteException e) { |
| 421 |
throw new RuntimeException("system server dead?", e); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public final AppWidgetHostView createView(Context context, int appWidgetId, |
| 430 |
AppWidgetProviderInfo appWidget) { |
| 431 |
if (sService == null) { |
| 432 |
return null; |
| 433 |
} |
| 434 |
AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget); |
| 435 |
view.setInteractionHandler(mInteractionHandler); |
| 436 |
view.setAppWidget(appWidgetId, appWidget); |
| 437 |
setListener(appWidgetId, view); |
| 438 |
|
| 439 |
return view; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
protected AppWidgetHostView onCreateView(Context context, int appWidgetId, |
| 447 |
AppWidgetProviderInfo appWidget) { |
| 448 |
return new AppWidgetHostView(context, mInteractionHandler); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
| 455 |
AppWidgetHostListener v = getListener(appWidgetId); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
appWidget.updateDimensions(mDisplayMetrics); |
| 461 |
if (v != null) { |
| 462 |
v.onUpdateProviderInfo(appWidget); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public interface AppWidgetHostListener { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
void onUpdateProviderInfo(@Nullable AppWidgetProviderInfo appWidget); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
void updateAppWidget(@Nullable RemoteViews views); |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
void onViewDataChanged(int viewId); |
| 497 |
} |
| 498 |
|
| 499 |
void dispatchOnAppWidgetRemoved(int appWidgetId) { |
| 500 |
removeListener(appWidgetId); |
| 501 |
onAppWidgetRemoved(appWidgetId); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public void onAppWidgetRemoved(int appWidgetId) { |
| 509 |
|
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
protected void onProvidersChanged() { |
| 517 |
|
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public void setListener(int appWidgetId, @NonNull AppWidgetHostListener listener) { |
| 528 |
synchronized (mListeners) { |
| 529 |
mListeners.put(appWidgetId, listener); |
| 530 |
} |
| 531 |
RemoteViews views = null; |
| 532 |
try { |
| 533 |
views = sService.getAppWidgetViews(mContextOpPackageName, appWidgetId); |
| 534 |
} catch (RemoteException e) { |
| 535 |
throw new RuntimeException("system server dead?", e); |
| 536 |
} |
| 537 |
listener.updateAppWidget(views); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public void removeListener(int appWidgetId) { |
| 547 |
synchronized (mListeners) { |
| 548 |
mListeners.remove(appWidgetId); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
void updateAppWidgetView(int appWidgetId, RemoteViews views) { |
| 553 |
AppWidgetHostListener v = getListener(appWidgetId); |
| 554 |
if (v != null) { |
| 555 |
v.updateAppWidget(views); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
void viewDataChanged(int appWidgetId, int viewId) { |
| 560 |
AppWidgetHostListener v = getListener(appWidgetId); |
| 561 |
if (v != null) { |
| 562 |
v.onViewDataChanged(viewId); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
protected void clearViews() { |
| 570 |
synchronized (mListeners) { |
| 571 |
mListeners.clear(); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|