Commit 113a2df7 by tonihei Committed by Ian Baker

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
parent 968a0baa
......@@ -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();
}
}
......@@ -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) {
......
......@@ -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();
......
......@@ -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 {
......
......@@ -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;
}
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment