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

Commit c445b83b authored by Yanting Yang's avatar Yanting Yang
Browse files

Fix NPE of ApplicationsState

Check userId before getting the entry from mEntriesMap to avoid NPE in
getEntryLocked(), since the caller from RecentAppStatsMixin may query
the entry of uncached userId.

Bug: 278034297
Test: manual && robotests
Change-Id: Ia2ff912403a33b2b94cae5138406154e9fee6adf
parent 11562e97
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -734,7 +734,10 @@ public class ApplicationsState {

    private AppEntry getEntryLocked(ApplicationInfo info) {
        int userId = UserHandle.getUserId(info.uid);
        AppEntry entry = mEntriesMap.get(userId).get(info.packageName);
        AppEntry entry = null;
        if (mEntriesMap.contains(userId)) {
            entry = mEntriesMap.get(userId).get(info.packageName);
        }
        if (DEBUG) {
            Log.i(TAG, "Looking up entry of pkg " + info.packageName + ": " + entry);
        }
+18 −0
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ import org.robolectric.util.ReflectionHelpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@@ -801,4 +802,21 @@ public class ApplicationsStateRoboTest {
        assertThat(nonPrimaryUserApp1.shouldShowInPersonalTab(um, appInfo1.uid)).isTrue();
        assertThat(nonPrimaryUserApp2.shouldShowInPersonalTab(um, appInfo2.uid)).isFalse();
    }

    @Test
    public void getEntry_validUserId_shouldReturnEntry() {
        mApplicationsState.mEntriesMap.put(/* userId= */ 0, new HashMap<>());
        addApp(PKG_1, /* id= */ 1);

        assertThat(mApplicationsState.getEntry(PKG_1, /* userId= */ 0).info.packageName)
                .isEqualTo(PKG_1);
    }

    @Test
    public void getEntry_invalidUserId_shouldReturnNull() {
        mApplicationsState.mEntriesMap.put(/* userId= */ 0, new HashMap<>());
        addApp(PKG_1, /* id= */ 1);

        assertThat(mApplicationsState.getEntry(PKG_1, /* userId= */ -1)).isNull();
    }
}