From 113a2df77549d2b96f467156b6cfcb176ff288a1 Mon Sep 17 00:00:00 2001 From: tonihei Date: Fri, 3 Jul 2020 12:59:32 +0100 Subject: [PATCH] Add some missing nullness assertions. They are all for Context.getSystemService that is allowed to return null. In most cases where we need to service, we make an assertion that it is available. PiperOrigin-RevId: 319503557 --- .../main/java/com/google/android/exoplayer2/C.java | 6 ++++-- .../com/google/android/exoplayer2/util/Util.java | 9 +++++---- .../google/android/exoplayer2/AudioFocusManager.java | 9 ++++++--- .../exoplayer2/scheduler/PlatformScheduler.java | 10 ++++++---- .../exoplayer2/scheduler/RequirementsWatcher.java | 12 ++++++------ .../android/exoplayer2/util/NotificationUtil.java | 7 +++++-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/C.java b/library/common/src/main/java/com/google/android/exoplayer2/C.java index 64123b730e..a4996879e4 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/C.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/C.java @@ -22,6 +22,7 @@ import android.media.AudioManager; import android.media.MediaCodec; import android.media.MediaFormat; import androidx.annotation.IntDef; +import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.util.Util; import java.lang.annotation.Documented; @@ -1110,8 +1111,9 @@ public final class C { */ @RequiresApi(21) public static int generateAudioSessionIdV21(Context context) { - return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)) - .generateAudioSessionId(); + @Nullable + AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)); + return audioManager == null ? AudioManager.ERROR : audioManager.generateAudioSessionId(); } } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index 0afeaffdaf..fee10c349a 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.util; import static android.content.Context.UI_MODE_SERVICE; +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import android.Manifest.permission; import android.annotation.SuppressLint; @@ -1829,8 +1830,7 @@ public final class Util { Matcher matcher = ESCAPED_CHARACTER_PATTERN.matcher(fileName); int startOfNotEscaped = 0; while (percentCharacterCount > 0 && matcher.find()) { - char unescapedCharacter = - (char) Integer.parseInt(Assertions.checkNotNull(matcher.group(1)), 16); + char unescapedCharacter = (char) Integer.parseInt(checkNotNull(matcher.group(1)), 16); builder.append(fileName, startOfNotEscaped, matcher.start()).append(unescapedCharacter); startOfNotEscaped = matcher.end(); percentCharacterCount--; @@ -2086,7 +2086,8 @@ public final class Util { * @return The size of the current mode, in pixels. */ public static Point getCurrentDisplayModeSize(Context context) { - WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + WindowManager windowManager = + checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)); return getCurrentDisplayModeSize(context, windowManager.getDefaultDisplay()); } @@ -2327,7 +2328,7 @@ public final class Util { private static boolean isTrafficRestricted(Uri uri) { return "http".equals(uri.getScheme()) && !NetworkSecurityPolicy.getInstance() - .isCleartextTrafficPermitted(Assertions.checkNotNull(uri.getHost())); + .isCleartextTrafficPermitted(checkNotNull(uri.getHost())); } private static String maybeReplaceGrandfatheredLanguageTags(String languageTag) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java b/library/core/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java index 5aeca440ff..b56e8838c5 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2; +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; + import android.content.Context; import android.media.AudioFocusRequest; import android.media.AudioManager; @@ -116,7 +118,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; */ public AudioFocusManager(Context context, Handler eventHandler, PlayerControl playerControl) { this.audioManager = - (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE); + checkNotNull( + (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE)); this.playerControl = playerControl; this.focusListener = new AudioFocusListener(eventHandler); this.audioFocusState = AUDIO_FOCUS_STATE_NO_FOCUS; @@ -212,7 +215,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; private int requestAudioFocusDefault() { return audioManager.requestAudioFocus( focusListener, - Util.getStreamTypeForAudioUsage(Assertions.checkNotNull(audioAttributes).usage), + Util.getStreamTypeForAudioUsage(checkNotNull(audioAttributes).usage), focusGain); } @@ -227,7 +230,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; boolean willPauseWhenDucked = willPauseWhenDucked(); audioFocusRequest = builder - .setAudioAttributes(Assertions.checkNotNull(audioAttributes).getAudioAttributesV21()) + .setAudioAttributes(checkNotNull(audioAttributes).getAudioAttributesV21()) .setWillPauseWhenDucked(willPauseWhenDucked) .setOnAudioFocusChangeListener(focusListener) .build(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/PlatformScheduler.java b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/PlatformScheduler.java index 11036fc77c..357fdab957 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/PlatformScheduler.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/PlatformScheduler.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.scheduler; +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; + import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobScheduler; @@ -25,7 +27,6 @@ import android.content.Intent; import android.os.PersistableBundle; import androidx.annotation.RequiresApi; import androidx.annotation.RequiresPermission; -import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; @@ -72,7 +73,8 @@ public final class PlatformScheduler implements Scheduler { context = context.getApplicationContext(); this.jobId = jobId; jobServiceComponentName = new ComponentName(context, PlatformSchedulerService.class); - jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); + jobScheduler = + checkNotNull((JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)); } @Override @@ -140,8 +142,8 @@ public final class PlatformScheduler implements Scheduler { Requirements requirements = new Requirements(extras.getInt(KEY_REQUIREMENTS)); int notMetRequirements = requirements.getNotMetRequirements(this); if (notMetRequirements == 0) { - String serviceAction = Assertions.checkNotNull(extras.getString(KEY_SERVICE_ACTION)); - String servicePackage = Assertions.checkNotNull(extras.getString(KEY_SERVICE_PACKAGE)); + String serviceAction = checkNotNull(extras.getString(KEY_SERVICE_ACTION)); + String servicePackage = checkNotNull(extras.getString(KEY_SERVICE_PACKAGE)); Intent intent = new Intent(serviceAction).setPackage(servicePackage); Util.startForegroundService(this, intent); } else { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java index f0a9ae3efc..6293cbf36d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.scheduler; +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -27,7 +29,6 @@ import android.os.Looper; import android.os.PowerManager; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; /** @@ -115,7 +116,7 @@ public final class RequirementsWatcher { /** Stops watching for changes. */ public void stop() { - context.unregisterReceiver(Assertions.checkNotNull(receiver)); + context.unregisterReceiver(checkNotNull(receiver)); receiver = null; if (Util.SDK_INT >= 24 && networkCallback != null) { unregisterNetworkCallbackV24(); @@ -130,8 +131,7 @@ public final class RequirementsWatcher { @RequiresApi(24) private void registerNetworkCallbackV24() { ConnectivityManager connectivityManager = - Assertions.checkNotNull( - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); + checkNotNull((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); networkCallback = new NetworkCallback(); connectivityManager.registerDefaultNetworkCallback(networkCallback); } @@ -139,8 +139,8 @@ public final class RequirementsWatcher { @RequiresApi(24) private void unregisterNetworkCallbackV24() { ConnectivityManager connectivityManager = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - connectivityManager.unregisterNetworkCallback(Assertions.checkNotNull(networkCallback)); + checkNotNull((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); + connectivityManager.unregisterNetworkCallback(checkNotNull(networkCallback)); networkCallback = null; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java index 756494f9d0..6c2b337344 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.util; +import static com.google.android.exoplayer2.util.Assertions.checkNotNull; + import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationChannel; @@ -99,7 +101,8 @@ public final class NotificationUtil { @Importance int importance) { if (Util.SDK_INT >= 26) { NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + checkNotNull( + (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); NotificationChannel channel = new NotificationChannel(id, context.getString(nameResourceId), importance); if (descriptionResourceId != 0) { @@ -122,7 +125,7 @@ public final class NotificationUtil { */ public static void setNotification(Context context, int id, @Nullable Notification notification) { NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + checkNotNull((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); if (notification != null) { notificationManager.notify(id, notification); } else {