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

Commit 6526fb66 authored by Pinyao Ting's avatar Pinyao Ting Committed by Automerger Merge Worker
Browse files

Merge "Enforce hard limits on hosts per package and widgets per host." into...

Merge "Enforce hard limits on hosts per package and widgets per host." into sc-dev am: 3cf46260 am: e065815c am: 3cb5c4bb am: b0b38d58

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/29024856



Change-Id: Idaaac82bea709c4ad79c2e617fb2170272ed6054
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents cdb8b4e9 b0b38d58
Loading
Loading
Loading
Loading
+49 −1
Original line number Original line Diff line number Diff line
@@ -177,6 +177,15 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
    // used to verify which request has successfully been received by the host.
    // used to verify which request has successfully been received by the host.
    private static final AtomicLong UPDATE_COUNTER = new AtomicLong();
    private static final AtomicLong UPDATE_COUNTER = new AtomicLong();


    // Hard limit of number of hosts an app can create, note that the app that hosts the widgets
    // can have multiple instances of {@link AppWidgetHost}, typically in respect to different
    // surfaces in the host app.
    // @see AppWidgetHost
    // @see AppWidgetHost#mHostId
    private static final int MAX_NUMBER_OF_HOSTS_PER_PACKAGE = 20;
    // Hard limit of number of widgets can be pinned by a host.
    private static final int MAX_NUMBER_OF_WIDGETS_PER_HOST = 200;

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
@@ -1720,7 +1729,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
        if (host != null) {
        if (host != null) {
            return host;
            return host;
        }
        }

        ensureHostCountBeforeAddLocked(id);
        host = new Host();
        host = new Host();
        host.id = id;
        host.id = id;
        mHosts.add(host);
        mHosts.add(host);
@@ -1728,6 +1737,24 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
        return host;
        return host;
    }
    }


    /**
     * Ensures that the number of hosts for a package is less than the maximum number of hosts per
     * package. If the number of hosts is greater than the maximum number of hosts per package, then
     * removes the oldest host.
     */
    private void ensureHostCountBeforeAddLocked(HostId hostId) {
        final List<Host> hosts = new ArrayList<>();
        for (Host host : mHosts) {
            if (host.id.uid == hostId.uid
                    && host.id.packageName.equals(hostId.packageName)) {
                hosts.add(host);
            }
        }
        while (hosts.size() >= MAX_NUMBER_OF_HOSTS_PER_PACKAGE) {
            deleteHostLocked(hosts.remove(0));
        }
    }

    private void deleteHostLocked(Host host) {
    private void deleteHostLocked(Host host) {
        final int N = host.widgets.size();
        final int N = host.widgets.size();
        for (int i = N - 1; i >= 0; i--) {
        for (int i = N - 1; i >= 0; i--) {
@@ -2916,11 +2943,32 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
     * Adds the widget to mWidgets and tracks the package name in mWidgetPackages.
     * Adds the widget to mWidgets and tracks the package name in mWidgetPackages.
     */
     */
    void addWidgetLocked(Widget widget) {
    void addWidgetLocked(Widget widget) {
        ensureWidgetCountBeforeAddLocked(widget);
        mWidgets.add(widget);
        mWidgets.add(widget);


        onWidgetProviderAddedOrChangedLocked(widget);
        onWidgetProviderAddedOrChangedLocked(widget);
    }
    }


    /**
     * Ensures that the widget count for the widget's host is not greater than the maximum
     * number of widgets per host. If the count is greater than the maximum, removes oldest widgets
     * from the host until the count is less than or equal to the maximum.
     */
    private void ensureWidgetCountBeforeAddLocked(Widget widget) {
        if (widget.host == null || widget.host.id == null) {
            return;
        }
        final List<Widget> widgetsInSameHost = new ArrayList<>();
        for (Widget w : mWidgets) {
            if (w.host != null && widget.host.id.equals(w.host.id)) {
                widgetsInSameHost.add(w);
            }
        }
        while (widgetsInSameHost.size() >= MAX_NUMBER_OF_WIDGETS_PER_HOST) {
            removeWidgetLocked(widgetsInSameHost.remove(0));
        }
    }

    /**
    /**
     * Checks if the provider is assigned and updates the mWidgetPackages to track packages
     * Checks if the provider is assigned and updates the mWidgetPackages to track packages
     * that have bound widgets.
     * that have bound widgets.