summaryrefslogtreecommitdiff
path: root/SafetyCenter
diff options
context:
space:
mode:
authorElliot Sisteron <elliotsisteron@google.com>2023-05-05 12:52:04 +0000
committerElliot Sisteron <elliotsisteron@google.com>2023-05-05 16:39:30 +0000
commit8bde8ea5b389157c82c38818732e2d87dabce541 (patch)
treea4a21ce30f27a0ea149c92ec86b90b95682d5c94 /SafetyCenter
parent378fd7762dff020d7128dd68c2f91cd51dd0f617 (diff)
downloadPermission-8bde8ea5b389157c82c38818732e2d87dabce541.tar.gz
Fix some nullable warnings.
The `@NonNullByDefault` annotation wasn't working as expected, using a fork from another team instead. Also added it to the notifications package where it was missing. Running null analysis on the code spawned a few warnings, I tried to fix most / all of them here. Some are benign (the checker has to understand that a certain variable cannot be null), but some were genuine issues. Bug: 267476663 Test: atest CtsSafetyCenterTestCases Change-Id: I992ed765d02c1a7b0a39997ea3718e1de09d4986
Diffstat (limited to 'SafetyCenter')
-rw-r--r--SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java25
-rw-r--r--SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java7
2 files changed, 21 insertions, 11 deletions
diff --git a/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java b/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java
index d19110652..34b6ce798 100644
--- a/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java
+++ b/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java
@@ -15,19 +15,28 @@
*/
package com.android.safetycenter.annotations;
-import androidx.annotation.NonNull;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PACKAGE;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.CLASS;
-import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
/**
- * Annotation to declare all type usages in the annotated instance as {@link NonNull}, unless
- * explicitly marked with a nullable annotation.
+ * Specifies that all type uses are {@link Nonnull} within the annotated package, unless tagged with
+ * {@code @Nullable}. This helps IDEs flag all potential nullability issues without having to use
+ * {@code @NonNull} annotations.
+ *
+ * <p>This is similar to {@code @ParametersAreNonnullByDefault}, but is also applied more widely
+ * (e.g. to methods return types and fields).
*/
-@NonNull
-@TypeQualifierDefault(ElementType.TYPE_USE)
-@Retention(RetentionPolicy.CLASS)
+@Retention(CLASS)
+@Target(PACKAGE)
+@TypeQualifierDefault({PARAMETER, FIELD, METHOD})
+@Nonnull // Android variant cannot be applied as a type qualifier.
public @interface NonNullByDefault {}
diff --git a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
index bc56d18ee..5417e7b89 100644
--- a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
+++ b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java
@@ -290,6 +290,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
/** Retrieve assets held in the Safety Center resources APK. */
@Override
+ @Nullable
public AssetManager getAssets() {
if (mAssetsFromApk == null) {
Context resourcesApkContext = getResourcesApkContext();
@@ -302,6 +303,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
/** Retrieve resources held in the Safety Center resources APK. */
@Override
+ @Nullable
public Resources getResources() {
if (mResourcesFromApk == null) {
Context resourcesApkContext = getResourcesApkContext();
@@ -314,6 +316,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
/** Retrieve theme held in the Safety Center resources APK. */
@Override
+ @Nullable
public Resources.Theme getTheme() {
if (mThemeFromApk == null) {
Context resourcesApkContext = getResourcesApkContext();
@@ -366,9 +369,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper {
return null;
}
- /**
- * Gets a color by resource name
- */
+ /** Gets a color by resource name */
@ColorInt
@Nullable
public Integer getColorByName(String name) {