diff options
| author | 2023-06-01 16:07:13 +0800 | |
|---|---|---|
| committer | 2023-06-01 18:07:09 +0800 | |
| commit | 986857c9e66be751e22db8b56cf87a74bbd2a41f (patch) | |
| tree | 56b0d0343e3afd29cccb3a719be1591d12c1f925 | |
| parent | 3d137a068cde4978e9daab63492ead0179a3cfcd (diff) | |
Fix NPE when loading recently opened apps on the Apps page
From Android U, Settings start to support showing work profile apps on
recently opened apps list on the Apps page. It will query the app entry
from the cache of ApplicationsState by different user id before cache
initialization. We should check mEntriesMap by user id in getEntry() and
getEntryLocked() to avoid NPE since the cache might be empty before
reading.
Bug: 284252546
Test: robotests
Change-Id: Ie4dc0821c4fe29a389f491eb4706abe352c05c5a
2 files changed, 19 insertions, 11 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java index fe8988385453..6eb2f3834858 100644 --- a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java +++ b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java @@ -484,8 +484,9 @@ public class ApplicationsState { if (DEBUG_LOCKING) Log.v(TAG, "getEntry about to acquire lock..."); synchronized (mEntriesMap) { AppEntry entry = null; - if (mEntriesMap.contains(userId)) { - entry = mEntriesMap.get(userId).get(packageName); + HashMap<String, AppEntry> userEntriesMap = mEntriesMap.get(userId); + if (userEntriesMap != null) { + entry = userEntriesMap.get(packageName); } if (entry == null) { ApplicationInfo info = getAppInfoLocked(packageName, userId); @@ -735,8 +736,9 @@ public class ApplicationsState { private AppEntry getEntryLocked(ApplicationInfo info) { int userId = UserHandle.getUserId(info.uid); AppEntry entry = null; - if (mEntriesMap.contains(userId)) { - entry = mEntriesMap.get(userId).get(info.packageName); + HashMap<String, AppEntry> userEntriesMap = mEntriesMap.get(userId); + if (userEntriesMap != null) { + entry = userEntriesMap.get(info.packageName); } if (DEBUG) { Log.i(TAG, "Looking up entry of pkg " + info.packageName + ": " + entry); @@ -752,8 +754,11 @@ public class ApplicationsState { Log.i(TAG, "Creating AppEntry for " + info.packageName); } entry = new AppEntry(mContext, info, mCurId++); - mEntriesMap.get(userId).put(info.packageName, entry); - mAppEntries.add(entry); + userEntriesMap = mEntriesMap.get(userId); + if (userEntriesMap != null) { + userEntriesMap.put(info.packageName, entry); + mAppEntries.add(entry); + } } else if (entry.info != info) { entry.info = info; } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ApplicationsStateRoboTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ApplicationsStateRoboTest.java index 1d081d7214cc..34d8148f418f 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ApplicationsStateRoboTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/applications/ApplicationsStateRoboTest.java @@ -804,7 +804,7 @@ public class ApplicationsStateRoboTest { } @Test - public void getEntry_validUserId_shouldReturnEntry() { + public void getEntry_hasCache_shouldReturnCacheEntry() { mApplicationsState.mEntriesMap.put(/* userId= */ 0, new HashMap<>()); addApp(PKG_1, /* id= */ 1); @@ -813,10 +813,13 @@ public class ApplicationsStateRoboTest { } @Test - public void getEntry_invalidUserId_shouldReturnNull() { - mApplicationsState.mEntriesMap.put(/* userId= */ 0, new HashMap<>()); - addApp(PKG_1, /* id= */ 1); + public void getEntry_hasNoCache_shouldReturnEntry() { + mApplicationsState.mEntriesMap.clear(); + ApplicationInfo appInfo = createApplicationInfo(PKG_1, /* uid= */ 0); + mApplicationsState.mApplications.add(appInfo); + mApplicationsState.mSystemModules.put(PKG_1, /* value= */ false); - assertThat(mApplicationsState.getEntry(PKG_1, /* userId= */ -1)).isNull(); + assertThat(mApplicationsState.getEntry(PKG_1, /* userId= */ 0).info.packageName) + .isEqualTo(PKG_1); } } |