Merge "Cache weak reference for static context." into sc-dev am: f7d20b351f

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15245990

Change-Id: I3a39783e62fbb96e7e5aba37f9d27b6e555b6b31
This commit is contained in:
TreeHugger Robot
2021-07-10 00:00:23 +00:00
committed by Automerger Merge Worker

View File

@@ -73,6 +73,7 @@ import com.android.internal.util.Preconditions;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@@ -103,7 +104,7 @@ public class AudioManager {
private static final AudioVolumeGroupChangeHandler sAudioAudioVolumeGroupChangedHandler = private static final AudioVolumeGroupChangeHandler sAudioAudioVolumeGroupChangedHandler =
new AudioVolumeGroupChangeHandler(); new AudioVolumeGroupChangeHandler();
private static Context sContext; private static WeakReference<Context> sContext;
/** /**
* Broadcast intent, a hint for applications that audio is about to become * Broadcast intent, a hint for applications that audio is about to become
@@ -800,7 +801,7 @@ public class AudioManager {
} else { } else {
mOriginalContext = context; mOriginalContext = context;
} }
sContext = context; sContext = new WeakReference<>(context);
} }
@UnsupportedAppUsage @UnsupportedAppUsage
@@ -7256,23 +7257,27 @@ public class AudioManager {
*/ */
public static boolean hasHapticChannels(@Nullable Context context, @NonNull Uri uri) { public static boolean hasHapticChannels(@Nullable Context context, @NonNull Uri uri) {
Objects.requireNonNull(uri); Objects.requireNonNull(uri);
if (context != null) { if (context != null) {
return hasHapticChannelsImpl(context, uri); return hasHapticChannelsImpl(context, uri);
} else if (sContext != null) { }
Context cachedContext = sContext.get();
if (cachedContext != null) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Try to use static context to query if having haptic channels"); Log.d(TAG, "Try to use static context to query if having haptic channels");
} }
return hasHapticChannelsImpl(sContext, uri); return hasHapticChannelsImpl(cachedContext, uri);
} else { }
// Try with audio service context, this may fail to get correct result.
if (DEBUG) { // Try with audio service context, this may fail to get correct result.
Log.d(TAG, "Try to use audio service context to query if having haptic channels"); if (DEBUG) {
} Log.d(TAG, "Try to use audio service context to query if having haptic channels");
try { }
return getService().hasHapticChannels(uri); try {
} catch (RemoteException e) { return getService().hasHapticChannels(uri);
throw e.rethrowFromSystemServer(); } catch (RemoteException e) {
} throw e.rethrowFromSystemServer();
} }
} }