Fixes NPE and adds @Nullable to PackageSetting.pkg

This change addresses an NPE encountered when a package setting refers
to an unavailable package and is thus null. It adds a @Nullable
annotation to the field to hopefully catch more of this type of error
going forward.

Fixes: 153007501
Test: atest AppEnumerationTests PackageManagerTest
Change-Id: I0ffe1f488d69d45893c96c755e48e19de43fcdf2
(cherry picked from commit 74860927b9cc2e847efe2097e244155f15796d6b)
diff --git a/services/core/java/com/android/server/pm/AppsFilter.java b/services/core/java/com/android/server/pm/AppsFilter.java
index 805d918..09b782d 100644
--- a/services/core/java/com/android/server/pm/AppsFilter.java
+++ b/services/core/java/com/android/server/pm/AppsFilter.java
@@ -674,7 +674,8 @@
             Trace.endSection();
 
             if (callingPkgSetting != null) {
-                if (!mFeatureConfig.packageIsEnabled(callingPkgSetting.pkg)) {
+                if (callingPkgSetting.pkg != null
+                        && !mFeatureConfig.packageIsEnabled(callingPkgSetting.pkg)) {
                     if (DEBUG_LOGGING) {
                         log(callingSetting, targetPkgSetting, "DISABLED");
                     }
@@ -682,7 +683,8 @@
                 }
             } else {
                 for (int i = callingSharedPkgSettings.size() - 1; i >= 0; i--) {
-                    if (!mFeatureConfig.packageIsEnabled(callingSharedPkgSettings.valueAt(i).pkg)) {
+                    final AndroidPackage pkg = callingSharedPkgSettings.valueAt(i).pkg;
+                    if (pkg != null && !mFeatureConfig.packageIsEnabled(pkg)) {
                         if (DEBUG_LOGGING) {
                             log(callingSetting, targetPkgSetting, "DISABLED");
                         }
diff --git a/services/core/java/com/android/server/pm/PackageSetting.java b/services/core/java/com/android/server/pm/PackageSetting.java
index 9a8692d..600518e 100644
--- a/services/core/java/com/android/server/pm/PackageSetting.java
+++ b/services/core/java/com/android/server/pm/PackageSetting.java
@@ -43,6 +43,7 @@
 public class PackageSetting extends PackageSettingBase {
     int appId;
 
+    @Nullable
     public AndroidPackage pkg;
     /**
      * WARNING. The object reference is important. We perform integer equality and NOT