Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0bfc28ae authored by Pinyao Ting's avatar Pinyao Ting Committed by Android (Google) Code Review
Browse files

Merge "Add comments to AppWidgetServiceImpl [Part 1]" into main

parents 81f30aa7 9b122bf8
Loading
Loading
Loading
Loading
+41 −9
Original line number Diff line number Diff line
@@ -173,27 +173,35 @@ import java.util.function.LongSupplier;

class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBackupProvider,
        OnCrossProfileWidgetProvidersChangeListener {
    // Name of the tag associated with the system logs generated by this service.
    private static final String TAG = "AppWidgetServiceImpl";

    // Simple flag to enable/disable debug logging.
    private static final boolean DEBUG = Build.IS_DEBUGGABLE;

    // String constants for XML schema migration related to changes in keyguard package.
    private static final String OLD_KEYGUARD_HOST_PACKAGE = "android";
    private static final String NEW_KEYGUARD_HOST_PACKAGE = "com.android.keyguard";
    private static final int KEYGUARD_HOST_ID = 0x4b455947;

    // Filename for app widgets state persisted on disk.
    private static final String STATE_FILENAME = "appwidgets.xml";

    // XML tag for widget size options of each individual widget when persisted on disk.
    private static final String KEY_SIZES = "sizes";

    // Minimum amount of time in millieconds before a widget is updated.
    private static final int MIN_UPDATE_PERIOD = DEBUG ? 0 : 30 * 60 * 1000; // 30 minutes

    // Default value of {@link Provider#tag} and {@link Host#tag}.
    private static final int TAG_UNDEFINED = -1;

    // Default uid of {@link ProviderId} when corresponding app haven't been installed yet.
    private static final int UNKNOWN_UID = -1;

    // Default return value when we can't find the parent of a given profileId.
    private static final int UNKNOWN_USER_ID = -10;

    // Bump if the stored widgets need to be upgraded.
    // Version of XML schema for app widgets. Bump if the stored widgets need to be upgraded.
    private static final int CURRENT_VERSION = 1;

    // Every widget update request is associated which an increasing sequence number. This is
@@ -205,9 +213,12 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
            Duration.ofHours(1).toMillis();
    // Default max API calls per reset interval for generated preview API rate limiting.
    private static final int DEFAULT_GENERATED_PREVIEW_MAX_CALLS_PER_INTERVAL = 2;

    // XML attribute for widget ids that are pending deletion.
    // See {@link Provider#pendingDeletedWidgetIds}.
    private static final String PENDING_DELETED_IDS_ATTR = "pending_deleted_ids";

    // Handles user and package related broadcasts.
    // See {@link #registerBroadcastReceiver}
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -249,18 +260,27 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
    private final HashMap<Pair<Integer, FilterComparison>, HashSet<Integer>>
            mRemoteViewsServicesAppWidgets = new HashMap<>();

    // Synchronization lock for internal states in this service.
    // TODO: Add GuardedBy annotation on states that need to be guarded.
    private final Object mLock = new Object();

    // Instances of actual widgets currently bound to each host.
    private final ArrayList<Widget> mWidgets = new ArrayList<>();
    // Information about the host apps that has one or more widgets bound to it.
    private final ArrayList<Host> mHosts = new ArrayList<>();
    // Information about the provider apps who indicates that they provide App Widgets
    // in their manifest.
    private final ArrayList<Provider> mProviders = new ArrayList<>();

    // Pairs of (userId, packageName) which has explicit consent from user to
    // hold the MODIFY_APPWIDGET_BIND_PERMISSIONS permission.
    // See {@link AppWidgetManager#setBindAppWidgetPermission}
    private final ArraySet<Pair<Integer, String>> mPackagesWithBindWidgetPermission =
            new ArraySet<>();

    // Ids of users whose widgets/provider/hosts have been loaded from disk.
    private final SparseBooleanArray mLoadedUserIds = new SparseBooleanArray();

    // Synchronization lock dedicated to {@link #mWidgetPackages}.
    private final Object mWidgetPackagesLock = new Object();
    // Set of packages that has at least one widget bounded by a host, keyed on userId.
    private final SparseArray<ArraySet<String>> mWidgetPackages = new SparseArray<>();

    private BackupRestoreController mBackupRestoreController;
@@ -280,18 +300,30 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku

    private SecurityPolicy mSecurityPolicy;

    // Handler to the background thread that saves states to disk.
    private Handler mSaveStateHandler;
    // Handler to the foreground thread that handles broadcasts related to user
    // and package events, as well as various internal events within
    // AppWidgetService.
    private Handler mCallbackHandler;

    // Map of user id to the next app widget id (monotonically increasing integer)
    // that can be allocated for a new app widget.
    // See {@link AppWidgetHost#allocateAppWidgetId}.
    private final SparseIntArray mNextAppWidgetIds = new SparseIntArray();

    // Indicates whether the device is running in safe mode.
    private boolean mSafeMode;
    // Load time validation of maximum memory can be used in widget bitmaps.
    private int mMaxWidgetBitmapMemory;
    // Feature flag that indicates whether
    // {@link AppWidgetManager#ACTION_APPWIDGET_ENABLED} and
    // {@linkAppWidgetManager#ACTION_APPWIDGET_UPDATE} are combined into a
    // single broadcast.
    private boolean mIsCombinedBroadcastEnabled;

    // Mark widget lifecycle broadcasts as 'interactive'
    private Bundle mInteractiveBroadcast;

    // Counter that keeps track of how many times generated preview API are
    // being called to ensure they are subject to rate limiting.
    private ApiCounter mGeneratedPreviewsApiCounter;

    AppWidgetServiceImpl(Context context) {