Skip to content

Commit 0f9aea2

Browse files
committedFeb 13, 2023
Check for null results from PackageManager.getApplicationInfo
This should never happen based on the API documention. However, Compose's preview API seems to return null anyway. Catch for the exception for now while we wait for a fix in the tooling.
1 parent c283043 commit 0f9aea2

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed
 

‎library/src/main/java/com/bumptech/glide/module/ManifestParser.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import android.content.Context;
44
import android.content.pm.ApplicationInfo;
55
import android.content.pm.PackageManager;
6+
import android.content.pm.PackageManager.NameNotFoundException;
67
import android.util.Log;
8+
import androidx.annotation.Nullable;
79
import java.lang.reflect.InvocationTargetException;
810
import java.util.ArrayList;
911
import java.util.List;
@@ -24,18 +26,24 @@ public ManifestParser(Context context) {
2426
this.context = context;
2527
}
2628

29+
// getApplicationInfo returns null in Compose previews, see #4977 and b/263613353.
30+
@SuppressWarnings("ConstantConditions")
31+
@Nullable
32+
private ApplicationInfo getOurApplicationInfo() throws NameNotFoundException {
33+
return context
34+
.getPackageManager()
35+
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
36+
}
37+
2738
@SuppressWarnings("deprecation")
2839
public List<GlideModule> parse() {
2940
if (Log.isLoggable(TAG, Log.DEBUG)) {
3041
Log.d(TAG, "Loading Glide modules");
3142
}
3243
List<GlideModule> modules = new ArrayList<>();
3344
try {
34-
ApplicationInfo appInfo =
35-
context
36-
.getPackageManager()
37-
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
38-
if (appInfo.metaData == null) {
45+
ApplicationInfo appInfo = getOurApplicationInfo();
46+
if (appInfo == null || appInfo.metaData == null) {
3947
if (Log.isLoggable(TAG, Log.DEBUG)) {
4048
Log.d(TAG, "Got null app info metadata");
4149
}

‎library/test/src/test/java/com/bumptech/glide/module/ManifestParserTest.java

+45-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.bumptech.glide.module;
22

33
import static com.google.common.truth.Truth.assertThat;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.mockito.ArgumentMatchers.anyInt;
6+
import static org.mockito.ArgumentMatchers.anyString;
47
import static org.mockito.ArgumentMatchers.eq;
8+
import static org.mockito.Mockito.doThrow;
59
import static org.mockito.Mockito.mock;
610
import static org.mockito.Mockito.when;
711

812
import android.content.Context;
913
import android.content.pm.ApplicationInfo;
1014
import android.content.pm.PackageManager;
15+
import android.content.pm.PackageManager.NameNotFoundException;
1116
import android.os.Bundle;
1217
import androidx.annotation.NonNull;
1318
import com.bumptech.glide.Glide;
@@ -16,6 +21,7 @@
1621
import java.util.List;
1722
import org.junit.Before;
1823
import org.junit.Test;
24+
import org.junit.function.ThrowingRunnable;
1925
import org.junit.runner.RunWith;
2026
import org.mockito.Mock;
2127
import org.mockito.MockitoAnnotations;
@@ -27,6 +33,7 @@
2733
@SuppressWarnings("deprecation")
2834
public class ManifestParserTest {
2935
private static final String MODULE_VALUE = "GlideModule";
36+
private static final String PACKAGE_NAME = "com.bumptech.test";
3037

3138
@Mock private Context context;
3239
private ManifestParser parser;
@@ -38,17 +45,27 @@ public void setUp() throws PackageManager.NameNotFoundException {
3845
applicationInfo = new ApplicationInfo();
3946
applicationInfo.metaData = new Bundle();
4047

41-
String packageName = "com.bumptech.test";
42-
when(context.getPackageName()).thenReturn(packageName);
48+
when(context.getPackageName()).thenReturn(PACKAGE_NAME);
4349

4450
PackageManager pm = mock(PackageManager.class);
45-
when(pm.getApplicationInfo(eq(packageName), eq(PackageManager.GET_META_DATA)))
51+
when(pm.getApplicationInfo(eq(PACKAGE_NAME), eq(PackageManager.GET_META_DATA)))
4652
.thenReturn(applicationInfo);
4753
when(context.getPackageManager()).thenReturn(pm);
4854

4955
parser = new ManifestParser(context);
5056
}
5157

58+
// TODO(#4977): Remove this after the bug in Compose's previews is fixed.
59+
@Test
60+
public void parse_withNullApplicationInfo_doesNotThrow() throws NameNotFoundException {
61+
PackageManager pm = mock(PackageManager.class);
62+
when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(null);
63+
when(context.getPackageManager()).thenReturn(pm);
64+
65+
parser = new ManifestParser(context);
66+
parser.parse();
67+
}
68+
5269
@Test
5370
public void testParse_returnsEmptyListIfNoModulesListed() {
5471
assertThat(parser.parse()).isEmpty();
@@ -78,7 +95,6 @@ public void testParse_withMultipleValidModuleNames_returnsListContainingModules(
7895
@Test
7996
public void testParse_withValidModuleName_ignoresMetadataWithoutGlideModuleValue() {
8097
applicationInfo.metaData.putString(TestModule1.class.getName(), MODULE_VALUE + "test");
81-
8298
assertThat(parser.parse()).isEmpty();
8399
}
84100

@@ -96,13 +112,35 @@ public void testThrows_whenClassInManifestIsNotAModule() {
96112
parser.parse();
97113
}
98114

99-
@Test(expected = RuntimeException.class)
100-
public void testThrows_whenPackageNameNotFound() {
101-
when(context.getPackageName()).thenReturn("fakePackageName");
115+
@Test
116+
public void parse_withNullMetadata_doesNotThrow() throws NameNotFoundException {
117+
PackageManager pm = mock(PackageManager.class);
118+
ApplicationInfo applicationInfo = new ApplicationInfo();
119+
applicationInfo.metaData = null;
120+
when(pm.getApplicationInfo(eq(PACKAGE_NAME), eq(PackageManager.GET_META_DATA)))
121+
.thenReturn(applicationInfo);
122+
when(context.getPackageManager()).thenReturn(pm);
102123

103124
parser.parse();
104125
}
105126

127+
@Test
128+
public void parse_withMissingName_throwsRuntimeException() throws NameNotFoundException {
129+
PackageManager pm = mock(PackageManager.class);
130+
doThrow(new NameNotFoundException("name")).when(pm).getApplicationInfo(anyString(), anyInt());
131+
when(context.getPackageManager()).thenReturn(pm);
132+
133+
assertThrows(
134+
"Unable to find metadata to parse GlideModules",
135+
RuntimeException.class,
136+
new ThrowingRunnable() {
137+
@Override
138+
public void run() {
139+
parser.parse();
140+
}
141+
});
142+
}
143+
106144
private void addModuleToManifest(Class<?> moduleClass) {
107145
addToManifest(moduleClass.getName());
108146
}

0 commit comments

Comments
 (0)
Please sign in to comment.