| 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.IdRes; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.SuppressLint; |
| 24 |
import android.app.PendingIntent; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.ComponentName; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.pm.ActivityInfo; |
| 29 |
import android.content.pm.PackageManager; |
| 30 |
import android.content.res.ResourceId; |
| 31 |
import android.content.res.Resources; |
| 32 |
import android.graphics.drawable.Drawable; |
| 33 |
import android.os.Bundle; |
| 34 |
import android.os.Parcel; |
| 35 |
import android.os.Parcelable; |
| 36 |
import android.os.UserHandle; |
| 37 |
import android.util.DisplayMetrics; |
| 38 |
import android.util.TypedValue; |
| 39 |
|
| 40 |
import java.lang.annotation.Retention; |
| 41 |
import java.lang.annotation.RetentionPolicy; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class AppWidgetProviderInfo implements Parcelable { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static final int RESIZE_NONE = 0; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final int RESIZE_HORIZONTAL = 1; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final int RESIZE_VERTICAL = 2; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final int RESIZE_BOTH = RESIZE_HORIZONTAL | RESIZE_VERTICAL; |
| 65 |
|
| 66 |
|
| 67 |
@IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 68 |
RESIZE_HORIZONTAL, |
| 69 |
RESIZE_VERTICAL, |
| 70 |
}) |
| 71 |
@Retention(RetentionPolicy.SOURCE) |
| 72 |
public @interface ResizeModeFlags {} |
| 73 |
|
| 74 |
|
| 75 |
public static final int WIDGET_CATEGORY_UNKNOWN = -1; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static final int WIDGET_CATEGORY_HOME_SCREEN = 1; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static final int WIDGET_CATEGORY_KEYGUARD = 2; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final int WIDGET_CATEGORY_SEARCHBOX = 4; |
| 91 |
|
| 92 |
|
| 93 |
@IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 94 |
WIDGET_CATEGORY_HOME_SCREEN, |
| 95 |
WIDGET_CATEGORY_KEYGUARD, |
| 96 |
WIDGET_CATEGORY_SEARCHBOX, |
| 97 |
}) |
| 98 |
@Retention(RetentionPolicy.SOURCE) |
| 99 |
public @interface CategoryFlags {} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final int WIDGET_FEATURE_RECONFIGURABLE = 1; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final int WIDGET_FEATURE_HIDE_FROM_PICKER = 2; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static final int WIDGET_FEATURE_CONFIGURATION_OPTIONAL = 4; |
| 125 |
|
| 126 |
|
| 127 |
@IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 128 |
WIDGET_FEATURE_RECONFIGURABLE, |
| 129 |
WIDGET_FEATURE_HIDE_FROM_PICKER, |
| 130 |
WIDGET_FEATURE_CONFIGURATION_OPTIONAL |
| 131 |
}) |
| 132 |
@Retention(RetentionPolicy.SOURCE) |
| 133 |
public @interface FeatureFlags {} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public ComponentName provider; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public int minWidth; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public int minHeight; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public int minResizeWidth; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public int minResizeHeight; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
@SuppressLint("MutableBareField") |
| 189 |
public int maxResizeWidth; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@SuppressLint("MutableBareField") |
| 199 |
public int maxResizeHeight; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@SuppressLint("MutableBareField") |
| 208 |
public int targetCellWidth; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@SuppressLint("MutableBareField") |
| 217 |
public int targetCellHeight; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public int updatePeriodMillis; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public int initialLayout; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public int initialKeyguardLayout; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public ComponentName configure; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
@Deprecated |
| 265 |
public String label; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public int icon; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public int autoAdvanceViewId; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public int previewImage; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
@SuppressLint("MutableBareField") |
| 307 |
@IdRes |
| 308 |
public int previewLayout; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
@ResizeModeFlags |
| 319 |
public int resizeMode; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
@CategoryFlags |
| 331 |
public int widgetCategory; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
@SuppressLint("MutableBareField") |
| 340 |
@IdRes |
| 341 |
public int descriptionRes; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
@FeatureFlags |
| 352 |
public int widgetFeatures; |
| 353 |
|
| 354 |
|
| 355 |
@UnsupportedAppUsage |
| 356 |
public ActivityInfo providerInfo; |
| 357 |
|
| 358 |
|
| 359 |
public boolean isExtendedFromAppWidgetProvider; |
| 360 |
|
| 361 |
public AppWidgetProviderInfo() { |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
@SuppressWarnings("deprecation") |
| 369 |
public AppWidgetProviderInfo(Parcel in) { |
| 370 |
this.provider = in.readTypedObject(ComponentName.CREATOR); |
| 371 |
this.minWidth = in.readInt(); |
| 372 |
this.minHeight = in.readInt(); |
| 373 |
this.minResizeWidth = in.readInt(); |
| 374 |
this.minResizeHeight = in.readInt(); |
| 375 |
this.maxResizeWidth = in.readInt(); |
| 376 |
this.maxResizeHeight = in.readInt(); |
| 377 |
this.targetCellWidth = in.readInt(); |
| 378 |
this.targetCellHeight = in.readInt(); |
| 379 |
this.updatePeriodMillis = in.readInt(); |
| 380 |
this.initialLayout = in.readInt(); |
| 381 |
this.initialKeyguardLayout = in.readInt(); |
| 382 |
this.configure = in.readTypedObject(ComponentName.CREATOR); |
| 383 |
this.label = in.readString(); |
| 384 |
this.icon = in.readInt(); |
| 385 |
this.previewImage = in.readInt(); |
| 386 |
this.previewLayout = in.readInt(); |
| 387 |
this.autoAdvanceViewId = in.readInt(); |
| 388 |
this.resizeMode = in.readInt(); |
| 389 |
this.widgetCategory = in.readInt(); |
| 390 |
this.providerInfo = in.readTypedObject(ActivityInfo.CREATOR); |
| 391 |
this.widgetFeatures = in.readInt(); |
| 392 |
this.descriptionRes = in.readInt(); |
| 393 |
this.isExtendedFromAppWidgetProvider = in.readBoolean(); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public final String loadLabel(PackageManager packageManager) { |
| 403 |
CharSequence label = providerInfo.loadLabel(packageManager); |
| 404 |
if (label != null) { |
| 405 |
return label.toString().trim(); |
| 406 |
} |
| 407 |
return null; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public final Drawable loadIcon(@NonNull Context context, int density) { |
| 427 |
return loadDrawable(context, density, providerInfo.getIconResource(), true); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public final Drawable loadPreviewImage(@NonNull Context context, int density) { |
| 447 |
return loadDrawable(context, density, previewImage, false); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
@Nullable |
| 460 |
public final CharSequence loadDescription(@NonNull Context context) { |
| 461 |
if (ResourceId.isValid(descriptionRes)) { |
| 462 |
CharSequence description = |
| 463 |
context.getPackageManager().getText( |
| 464 |
providerInfo.packageName, |
| 465 |
descriptionRes, |
| 466 |
providerInfo.applicationInfo); |
| 467 |
if (description != null) { |
| 468 |
return description.toString().trim(); |
| 469 |
} |
| 470 |
} |
| 471 |
return null; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public final UserHandle getProfile() { |
| 480 |
return new UserHandle(UserHandle.getUserId(providerInfo.applicationInfo.uid)); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
@NonNull |
| 487 |
public ActivityInfo getActivityInfo() { |
| 488 |
return providerInfo; |
| 489 |
} |
| 490 |
|
| 491 |
@Override |
| 492 |
@SuppressWarnings("deprecation") |
| 493 |
public void writeToParcel(Parcel out, int flags) { |
| 494 |
out.writeTypedObject(this.provider, flags); |
| 495 |
out.writeInt(this.minWidth); |
| 496 |
out.writeInt(this.minHeight); |
| 497 |
out.writeInt(this.minResizeWidth); |
| 498 |
out.writeInt(this.minResizeHeight); |
| 499 |
out.writeInt(this.maxResizeWidth); |
| 500 |
out.writeInt(this.maxResizeHeight); |
| 501 |
out.writeInt(this.targetCellWidth); |
| 502 |
out.writeInt(this.targetCellHeight); |
| 503 |
out.writeInt(this.updatePeriodMillis); |
| 504 |
out.writeInt(this.initialLayout); |
| 505 |
out.writeInt(this.initialKeyguardLayout); |
| 506 |
out.writeTypedObject(this.configure, flags); |
| 507 |
out.writeString(this.label); |
| 508 |
out.writeInt(this.icon); |
| 509 |
out.writeInt(this.previewImage); |
| 510 |
out.writeInt(this.previewLayout); |
| 511 |
out.writeInt(this.autoAdvanceViewId); |
| 512 |
out.writeInt(this.resizeMode); |
| 513 |
out.writeInt(this.widgetCategory); |
| 514 |
out.writeTypedObject(this.providerInfo, flags); |
| 515 |
out.writeInt(this.widgetFeatures); |
| 516 |
out.writeInt(this.descriptionRes); |
| 517 |
out.writeBoolean(this.isExtendedFromAppWidgetProvider); |
| 518 |
} |
| 519 |
|
| 520 |
@Override |
| 521 |
@SuppressWarnings("deprecation") |
| 522 |
public AppWidgetProviderInfo clone() { |
| 523 |
AppWidgetProviderInfo that = new AppWidgetProviderInfo(); |
| 524 |
that.provider = this.provider == null ? null : this.provider.clone(); |
| 525 |
that.minWidth = this.minWidth; |
| 526 |
that.minHeight = this.minHeight; |
| 527 |
that.minResizeWidth = this.minResizeWidth; |
| 528 |
that.minResizeHeight = this.minResizeHeight; |
| 529 |
that.maxResizeWidth = this.maxResizeWidth; |
| 530 |
that.maxResizeHeight = this.maxResizeHeight; |
| 531 |
that.targetCellWidth = this.targetCellWidth; |
| 532 |
that.targetCellHeight = this.targetCellHeight; |
| 533 |
that.updatePeriodMillis = this.updatePeriodMillis; |
| 534 |
that.initialLayout = this.initialLayout; |
| 535 |
that.initialKeyguardLayout = this.initialKeyguardLayout; |
| 536 |
that.configure = this.configure == null ? null : this.configure.clone(); |
| 537 |
that.label = this.label; |
| 538 |
that.icon = this.icon; |
| 539 |
that.previewImage = this.previewImage; |
| 540 |
that.previewLayout = this.previewLayout; |
| 541 |
that.autoAdvanceViewId = this.autoAdvanceViewId; |
| 542 |
that.resizeMode = this.resizeMode; |
| 543 |
that.widgetCategory = this.widgetCategory; |
| 544 |
that.providerInfo = this.providerInfo; |
| 545 |
that.widgetFeatures = this.widgetFeatures; |
| 546 |
that.descriptionRes = this.descriptionRes; |
| 547 |
that.isExtendedFromAppWidgetProvider = this.isExtendedFromAppWidgetProvider; |
| 548 |
return that; |
| 549 |
} |
| 550 |
|
| 551 |
public int describeContents() { |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
private Drawable loadDrawable(Context context, int density, int resourceId, |
| 556 |
boolean loadDefaultIcon) { |
| 557 |
try { |
| 558 |
Resources resources = context.getPackageManager().getResourcesForApplication( |
| 559 |
providerInfo.applicationInfo); |
| 560 |
if (ResourceId.isValid(resourceId)) { |
| 561 |
if (density < 0) { |
| 562 |
density = 0; |
| 563 |
} |
| 564 |
return resources.getDrawableForDensity(resourceId, density, null); |
| 565 |
} |
| 566 |
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) { |
| 567 |
|
| 568 |
} |
| 569 |
return loadDefaultIcon ? providerInfo.loadIcon(context.getPackageManager()) : null; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
public void updateDimensions(DisplayMetrics displayMetrics) { |
| 576 |
|
| 577 |
minWidth = TypedValue.complexToDimensionPixelSize(minWidth, displayMetrics); |
| 578 |
minHeight = TypedValue.complexToDimensionPixelSize(minHeight, displayMetrics); |
| 579 |
minResizeWidth = TypedValue.complexToDimensionPixelSize(minResizeWidth, displayMetrics); |
| 580 |
minResizeHeight = TypedValue.complexToDimensionPixelSize(minResizeHeight, displayMetrics); |
| 581 |
maxResizeWidth = TypedValue.complexToDimensionPixelSize(maxResizeWidth, displayMetrics); |
| 582 |
maxResizeHeight = TypedValue.complexToDimensionPixelSize(maxResizeHeight, displayMetrics); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public static final @android.annotation.NonNull Parcelable.Creator<AppWidgetProviderInfo> CREATOR |
| 589 |
= new Parcelable.Creator<AppWidgetProviderInfo>() |
| 590 |
{ |
| 591 |
public AppWidgetProviderInfo createFromParcel(Parcel parcel) |
| 592 |
{ |
| 593 |
return new AppWidgetProviderInfo(parcel); |
| 594 |
} |
| 595 |
|
| 596 |
public AppWidgetProviderInfo[] newArray(int size) |
| 597 |
{ |
| 598 |
return new AppWidgetProviderInfo[size]; |
| 599 |
} |
| 600 |
}; |
| 601 |
|
| 602 |
public String toString() { |
| 603 |
return "AppWidgetProviderInfo(" + getProfile() + '/' + provider + ')'; |
| 604 |
} |
| 605 |
} |