Merge "Sometimes the application context is null"

This commit is contained in:
Marco Nelissen
2015-04-22 19:06:36 +00:00
committed by Gerrit Code Review

View File

@@ -58,6 +58,7 @@ import java.util.Iterator;
*/ */
public class AudioManager { public class AudioManager {
private final Context mOriginalContext;
private final Context mApplicationContext; private final Context mApplicationContext;
private long mVolumeKeyUpTime; private long mVolumeKeyUpTime;
private final boolean mUseMasterVolume; private final boolean mUseMasterVolume;
@@ -641,16 +642,35 @@ public class AudioManager {
* @hide * @hide
*/ */
public AudioManager(Context context) { public AudioManager(Context context) {
mApplicationContext = context.getApplicationContext(); setContext(context);
mUseMasterVolume = mApplicationContext.getResources().getBoolean( mUseMasterVolume = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_useMasterVolume); com.android.internal.R.bool.config_useMasterVolume);
mUseVolumeKeySounds = mApplicationContext.getResources().getBoolean( mUseVolumeKeySounds = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_useVolumeKeySounds); com.android.internal.R.bool.config_useVolumeKeySounds);
mUseFixedVolume = mApplicationContext.getResources().getBoolean( mUseFixedVolume = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume); com.android.internal.R.bool.config_useFixedVolume);
sAudioPortEventHandler.init(); sAudioPortEventHandler.init();
} }
private Context getContext() {
if (mApplicationContext == null) {
setContext(mOriginalContext);
}
if (mApplicationContext != null) {
return mApplicationContext;
}
return mOriginalContext;
}
private void setContext(Context context) {
mApplicationContext = context.getApplicationContext();
if (mApplicationContext != null) {
mOriginalContext = null;
} else {
mOriginalContext = context;
}
}
private static IAudioService getService() private static IAudioService getService()
{ {
if (sService != null) { if (sService != null) {
@@ -685,7 +705,7 @@ public class AudioManager {
* or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}. * or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}.
*/ */
public void dispatchMediaKeyEvent(KeyEvent keyEvent) { public void dispatchMediaKeyEvent(KeyEvent keyEvent) {
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mApplicationContext); MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
helper.sendMediaButtonEvent(keyEvent, false); helper.sendMediaButtonEvent(keyEvent, false);
} }
@@ -746,7 +766,7 @@ public class AudioManager {
break; break;
case KeyEvent.KEYCODE_VOLUME_MUTE: case KeyEvent.KEYCODE_VOLUME_MUTE:
if (event.getRepeatCount() == 0) { if (event.getRepeatCount() == 0) {
MediaSessionLegacyHelper.getHelper(mApplicationContext) MediaSessionLegacyHelper.getHelper(getContext())
.sendVolumeKeyEvent(event, false); .sendVolumeKeyEvent(event, false);
} }
break; break;
@@ -779,7 +799,7 @@ public class AudioManager {
mVolumeKeyUpTime = SystemClock.uptimeMillis(); mVolumeKeyUpTime = SystemClock.uptimeMillis();
break; break;
case KeyEvent.KEYCODE_VOLUME_MUTE: case KeyEvent.KEYCODE_VOLUME_MUTE:
MediaSessionLegacyHelper.getHelper(mApplicationContext) MediaSessionLegacyHelper.getHelper(getContext())
.sendVolumeKeyEvent(event, false); .sendVolumeKeyEvent(event, false);
break; break;
} }
@@ -826,10 +846,10 @@ public class AudioManager {
try { try {
if (mUseMasterVolume) { if (mUseMasterVolume) {
service.adjustMasterVolume(direction, flags, service.adjustMasterVolume(direction, flags,
mApplicationContext.getOpPackageName()); getContext().getOpPackageName());
} else { } else {
service.adjustStreamVolume(streamType, direction, flags, service.adjustStreamVolume(streamType, direction, flags,
mApplicationContext.getOpPackageName()); getContext().getOpPackageName());
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in adjustStreamVolume", e); Log.e(TAG, "Dead object in adjustStreamVolume", e);
@@ -860,10 +880,10 @@ public class AudioManager {
try { try {
if (mUseMasterVolume) { if (mUseMasterVolume) {
service.adjustMasterVolume(direction, flags, service.adjustMasterVolume(direction, flags,
mApplicationContext.getOpPackageName()); getContext().getOpPackageName());
} else { } else {
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper helper =
MediaSessionLegacyHelper.getHelper(mApplicationContext); MediaSessionLegacyHelper.getHelper(getContext());
helper.sendAdjustVolumeBy(USE_DEFAULT_STREAM_TYPE, direction, flags); helper.sendAdjustVolumeBy(USE_DEFAULT_STREAM_TYPE, direction, flags);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -896,10 +916,10 @@ public class AudioManager {
try { try {
if (mUseMasterVolume) { if (mUseMasterVolume) {
service.adjustMasterVolume(direction, flags, service.adjustMasterVolume(direction, flags,
mApplicationContext.getOpPackageName()); getContext().getOpPackageName());
} else { } else {
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper helper =
MediaSessionLegacyHelper.getHelper(mApplicationContext); MediaSessionLegacyHelper.getHelper(getContext());
helper.sendAdjustVolumeBy(suggestedStreamType, direction, flags); helper.sendAdjustVolumeBy(suggestedStreamType, direction, flags);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -919,7 +939,7 @@ public class AudioManager {
public void adjustMasterVolume(int steps, int flags) { public void adjustMasterVolume(int steps, int flags) {
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.adjustMasterVolume(steps, flags, mApplicationContext.getOpPackageName()); service.adjustMasterVolume(steps, flags, getContext().getOpPackageName());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in adjustMasterVolume", e); Log.e(TAG, "Dead object in adjustMasterVolume", e);
} }
@@ -1060,7 +1080,7 @@ public class AudioManager {
} }
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.setRingerModeExternal(ringerMode, mApplicationContext.getOpPackageName()); service.setRingerModeExternal(ringerMode, getContext().getOpPackageName());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in setRingerMode", e); Log.e(TAG, "Dead object in setRingerMode", e);
} }
@@ -1082,10 +1102,10 @@ public class AudioManager {
IAudioService service = getService(); IAudioService service = getService();
try { try {
if (mUseMasterVolume) { if (mUseMasterVolume) {
service.setMasterVolume(index, flags, mApplicationContext.getOpPackageName()); service.setMasterVolume(index, flags, getContext().getOpPackageName());
} else { } else {
service.setStreamVolume(streamType, index, flags, service.setStreamVolume(streamType, index, flags,
mApplicationContext.getOpPackageName()); getContext().getOpPackageName());
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in setStreamVolume", e); Log.e(TAG, "Dead object in setStreamVolume", e);
@@ -1151,7 +1171,7 @@ public class AudioManager {
public void setMasterVolume(int index, int flags) { public void setMasterVolume(int index, int flags) {
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.setMasterVolume(index, flags, mApplicationContext.getOpPackageName()); service.setMasterVolume(index, flags, getContext().getOpPackageName());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in setMasterVolume", e); Log.e(TAG, "Dead object in setMasterVolume", e);
} }
@@ -1252,7 +1272,7 @@ public class AudioManager {
public void setMasterMute(boolean state, int flags) { public void setMasterMute(boolean state, int flags) {
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.setMasterMute(state, flags, mApplicationContext.getOpPackageName(), mICallBack); service.setMasterMute(state, flags, getContext().getOpPackageName(), mICallBack);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in setMasterMute", e); Log.e(TAG, "Dead object in setMasterMute", e);
} }
@@ -1490,7 +1510,7 @@ public class AudioManager {
* @see #startBluetoothSco() * @see #startBluetoothSco()
*/ */
public boolean isBluetoothScoAvailableOffCall() { public boolean isBluetoothScoAvailableOffCall() {
return mApplicationContext.getResources().getBoolean( return getContext().getResources().getBoolean(
com.android.internal.R.bool.config_bluetooth_sco_off_call); com.android.internal.R.bool.config_bluetooth_sco_off_call);
} }
@@ -1543,7 +1563,7 @@ public class AudioManager {
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.startBluetoothSco(mICallBack, service.startBluetoothSco(mICallBack,
mApplicationContext.getApplicationInfo().targetSdkVersion); getContext().getApplicationInfo().targetSdkVersion);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in startBluetoothSco", e); Log.e(TAG, "Dead object in startBluetoothSco", e);
} }
@@ -1691,7 +1711,7 @@ public class AudioManager {
public void setMicrophoneMute(boolean on){ public void setMicrophoneMute(boolean on){
IAudioService service = getService(); IAudioService service = getService();
try { try {
service.setMicrophoneMute(on, mApplicationContext.getOpPackageName()); service.setMicrophoneMute(on, getContext().getOpPackageName());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Dead object in setMicrophoneMute", e); Log.e(TAG, "Dead object in setMicrophoneMute", e);
} }
@@ -2122,7 +2142,7 @@ public class AudioManager {
* Settings has an in memory cache, so this is fast. * Settings has an in memory cache, so this is fast.
*/ */
private boolean querySoundEffectsEnabled(int user) { private boolean querySoundEffectsEnabled(int user) {
return Settings.System.getIntForUser(mApplicationContext.getContentResolver(), return Settings.System.getIntForUser(getContext().getContentResolver(),
Settings.System.SOUND_EFFECTS_ENABLED, 0, user) != 0; Settings.System.SOUND_EFFECTS_ENABLED, 0, user) != 0;
} }
@@ -2534,7 +2554,7 @@ public class AudioManager {
try { try {
status = service.requestAudioFocus(requestAttributes, durationHint, mICallBack, status = service.requestAudioFocus(requestAttributes, durationHint, mICallBack,
mAudioFocusDispatcher, getIdForAudioFocusListener(l), mAudioFocusDispatcher, getIdForAudioFocusListener(l),
mApplicationContext.getOpPackageName() /* package name */, flags, getContext().getOpPackageName() /* package name */, flags,
ap != null ? ap.cb() : null); ap != null ? ap.cb() : null);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Can't call requestAudioFocus() on AudioService:", e); Log.e(TAG, "Can't call requestAudioFocus() on AudioService:", e);
@@ -2559,7 +2579,7 @@ public class AudioManager {
.setInternalLegacyStreamType(streamType).build(), .setInternalLegacyStreamType(streamType).build(),
durationHint, mICallBack, null, durationHint, mICallBack, null,
MediaFocusControl.IN_VOICE_COMM_FOCUS_ID, MediaFocusControl.IN_VOICE_COMM_FOCUS_ID,
mApplicationContext.getOpPackageName(), getContext().getOpPackageName(),
AUDIOFOCUS_FLAG_LOCK, AUDIOFOCUS_FLAG_LOCK,
null /* policy token */); null /* policy token */);
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -2628,7 +2648,7 @@ public class AudioManager {
if (eventReceiver == null) { if (eventReceiver == null) {
return; return;
} }
if (!eventReceiver.getPackageName().equals(mApplicationContext.getPackageName())) { if (!eventReceiver.getPackageName().equals(getContext().getPackageName())) {
Log.e(TAG, "registerMediaButtonEventReceiver() error: " + Log.e(TAG, "registerMediaButtonEventReceiver() error: " +
"receiver and context package names don't match"); "receiver and context package names don't match");
return; return;
@@ -2637,7 +2657,7 @@ public class AudioManager {
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
// the associated intent will be handled by the component being registered // the associated intent will be handled by the component being registered
mediaButtonIntent.setComponent(eventReceiver); mediaButtonIntent.setComponent(eventReceiver);
PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, PendingIntent pi = PendingIntent.getBroadcast(getContext(),
0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
registerMediaButtonIntent(pi, eventReceiver); registerMediaButtonIntent(pi, eventReceiver);
} }
@@ -2671,8 +2691,8 @@ public class AudioManager {
Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter"); Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter");
return; return;
} }
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mApplicationContext); MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
helper.addMediaButtonListener(pi, eventReceiver, mApplicationContext); helper.addMediaButtonListener(pi, eventReceiver, getContext());
} }
/** /**
@@ -2690,7 +2710,7 @@ public class AudioManager {
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
// the associated intent will be handled by the component being registered // the associated intent will be handled by the component being registered
mediaButtonIntent.setComponent(eventReceiver); mediaButtonIntent.setComponent(eventReceiver);
PendingIntent pi = PendingIntent.getBroadcast(mApplicationContext, PendingIntent pi = PendingIntent.getBroadcast(getContext(),
0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
unregisterMediaButtonIntent(pi); unregisterMediaButtonIntent(pi);
} }
@@ -2713,7 +2733,7 @@ public class AudioManager {
* @hide * @hide
*/ */
public void unregisterMediaButtonIntent(PendingIntent pi) { public void unregisterMediaButtonIntent(PendingIntent pi) {
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mApplicationContext); MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
helper.removeMediaButtonListener(pi); helper.removeMediaButtonListener(pi);
} }
@@ -2730,7 +2750,7 @@ public class AudioManager {
if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
return; return;
} }
rcClient.registerWithSession(MediaSessionLegacyHelper.getHelper(mApplicationContext)); rcClient.registerWithSession(MediaSessionLegacyHelper.getHelper(getContext()));
} }
/** /**
@@ -2745,7 +2765,7 @@ public class AudioManager {
if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
return; return;
} }
rcClient.unregisterWithSession(MediaSessionLegacyHelper.getHelper(mApplicationContext)); rcClient.unregisterWithSession(MediaSessionLegacyHelper.getHelper(getContext()));
} }
/** /**
@@ -3406,7 +3426,7 @@ public class AudioManager {
*/ */
public void setRingerModeInternal(int ringerMode) { public void setRingerModeInternal(int ringerMode) {
try { try {
getService().setRingerModeInternal(ringerMode, mApplicationContext.getOpPackageName()); getService().setRingerModeInternal(ringerMode, getContext().getOpPackageName());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Error calling setRingerModeInternal", e); Log.w(TAG, "Error calling setRingerModeInternal", e);
} }