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

Commit a66a5319 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add null checks to removing AppTimeLimit observers"

parents dc7073dd ae9811d4
Loading
Loading
Loading
Loading
+41 −3
Original line number Diff line number Diff line
@@ -19,12 +19,13 @@ package com.android.server.usage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.app.PendingIntent;
import android.os.HandlerThread;
import android.os.Looper;

import androidx.test.filters.MediumTest;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.After;
@@ -32,11 +33,13 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RunWith(AndroidJUnit4.class)
@MediumTest
@LargeTest
public class AppTimeLimitControllerTests {

    private static final String PKG_SOC1 = "package.soc1";
@@ -159,7 +162,6 @@ public class AppTimeLimitControllerTests {
        assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
        addUsageSessionObserver(OBS_ID2, GROUP_GAME, TIME_30_MIN, TIME_1_MIN);
        assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID2));
        assertTrue("Observer wasn't added", hasUsageSessionObserver(UID, OBS_ID1));
    }

    /** Verify app usage observer is removed */
@@ -180,6 +182,42 @@ public class AppTimeLimitControllerTests {
        assertFalse("Observer wasn't removed", hasUsageSessionObserver(UID, OBS_ID1));
    }

    /** Verify nothing happens when a nonexistent app usage observer is removed */
    @Test
    public void testAppUsageObserver_RemoveMissingObserver() {
        assertFalse("Observer should not exist", hasAppUsageObserver(UID, OBS_ID1));
        try {
            mController.removeAppUsageObserver(UID, OBS_ID1, USER_ID);
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            sw.write("Hit exception trying to remove nonexistent observer:\n");
            sw.write(e.toString());
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            sw.write("\nTest Failed!");
            fail(sw.toString());
        }
        assertFalse("Observer should not exist", hasAppUsageObserver(UID, OBS_ID1));
    }

    /** Verify nothing happens when a nonexistent usage session observer is removed */
    @Test
    public void testUsageSessionObserver_RemoveMissingObserver() {
        assertFalse("Observer should not exist", hasUsageSessionObserver(UID, OBS_ID1));
        try {
            mController.removeUsageSessionObserver(UID, OBS_ID1, USER_ID);
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            sw.write("Hit exception trying to remove nonexistent observer:");
            sw.write(e.toString());
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            sw.write("\nTest Failed!");
            fail(sw.toString());
        }
        assertFalse("Observer should not exist", hasUsageSessionObserver(UID, OBS_ID1));
    }

    /** Re-adding an observer should result in only one copy */
    @Test
    public void testAppUsageObserver_ObserverReAdd() {
+23 −10
Original line number Diff line number Diff line
@@ -118,9 +118,14 @@ public class AppTimeLimitController {
        void removeUsageGroup(UsageGroup group) {
            final int size = group.mObserved.length;
            for (int i = 0; i < size; i++) {
                final ArrayList<UsageGroup> list = observedMap.get(group.mObserved[i]);
                final String observed = group.mObserved[i];
                final ArrayList<UsageGroup> list = observedMap.get(observed);
                if (list != null) {
                    list.remove(group);
                    if (list.isEmpty()) {
                        // No more observers for this observed entity, remove from map
                        observedMap.remove(observed);
                    }
                }
            }
        }
@@ -137,7 +142,7 @@ public class AppTimeLimitController {
            }
            pw.println();
            pw.print(" Observed Entities:");
            final int nEntities = currentlyActive.size();
            final int nEntities = observedMap.size();
            for (int i = 0; i < nEntities; i++) {
                pw.print(observedMap.keyAt(i));
                pw.print(", ");
@@ -183,7 +188,7 @@ public class AppTimeLimitController {
                pw.println();
            }
            pw.println("    Session Usage Groups:");
            final int nSessionUsageGroups = appUsageGroups.size();
            final int nSessionUsageGroups = sessionUsageGroups.size();
            for (int i = 0; i < nSessionUsageGroups; i++) {
                sessionUsageGroups.valueAt(i).dump(pw);
                pw.println();
@@ -616,7 +621,7 @@ public class AppTimeLimitController {
            AppUsageGroup group = observerApp.appUsageGroups.get(observerId);
            if (group != null) {
                // Remove previous app usage group associated with observerId
                observerApp.appUsageGroups.get(observerId).remove();
                group.remove();
            }

            final int observerIdCount = observerApp.appUsageGroups.size();
@@ -646,8 +651,12 @@ public class AppTimeLimitController {
     */
    public void removeAppUsageObserver(int requestingUid, int observerId, @UserIdInt int userId) {
        synchronized (mLock) {
            ObserverAppData observerApp = getOrCreateObserverAppDataLocked(requestingUid);
            observerApp.appUsageGroups.get(observerId).remove();
            final ObserverAppData observerApp = getOrCreateObserverAppDataLocked(requestingUid);
            final AppUsageGroup group = observerApp.appUsageGroups.get(observerId);
            if (group != null) {
                // Remove previous app usage group associated with observerId
                group.remove();
            }
        }
    }

@@ -668,8 +677,8 @@ public class AppTimeLimitController {
            ObserverAppData observerApp = getOrCreateObserverAppDataLocked(requestingUid);
            SessionUsageGroup group = observerApp.sessionUsageGroups.get(observerId);
            if (group != null) {
                // Remove previous app usage group associated with observerId
                observerApp.sessionUsageGroups.get(observerId).remove();
                // Remove previous session usage group associated with observerId
                group.remove();
            }

            final int observerIdCount = observerApp.sessionUsageGroups.size();
@@ -696,8 +705,12 @@ public class AppTimeLimitController {
    public void removeUsageSessionObserver(int requestingUid, int observerId,
            @UserIdInt int userId) {
        synchronized (mLock) {
            ObserverAppData observerApp = getOrCreateObserverAppDataLocked(requestingUid);
            observerApp.sessionUsageGroups.get(observerId).remove();
            final ObserverAppData observerApp = getOrCreateObserverAppDataLocked(requestingUid);
            final SessionUsageGroup group = observerApp.sessionUsageGroups.get(observerId);
            if (group != null) {
                // Remove previous app usage group associated with observerId
                group.remove();
            }
        }
    }