Merge "Refactor Ringtone.playFallbackRingtone" am: 2f56b3ab02

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

Change-Id: I6f1336c981a74f1ecd9c6e9f6827319a96ba8e4c
This commit is contained in:
Kevin Rocard
2022-01-19 20:18:16 +00:00
committed by Automerger Merge Worker

View File

@@ -504,49 +504,51 @@ public class Ringtone {
} }
private boolean playFallbackRingtone() { private boolean playFallbackRingtone() {
if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) int streamType = AudioAttributes.toLegacyStreamType(mAudioAttributes);
!= 0) { if (mAudioManager.getStreamVolume(streamType) == 0) {
int ringtoneType = RingtoneManager.getDefaultType(mUri); return false;
if (ringtoneType == -1 ||
RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
// Default ringtone, try fallback ringtone.
try {
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(
com.android.internal.R.raw.fallbackring);
if (afd != null) {
mLocalPlayer = new MediaPlayer();
if (afd.getDeclaredLength() < 0) {
mLocalPlayer.setDataSource(afd.getFileDescriptor());
} else {
mLocalPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getDeclaredLength());
}
mLocalPlayer.setAudioAttributes(mAudioAttributes);
synchronized (mPlaybackSettingsLock) {
applyPlaybackProperties_sync();
}
if (mVolumeShaperConfig != null) {
mVolumeShaper = mLocalPlayer.createVolumeShaper(mVolumeShaperConfig);
}
mLocalPlayer.prepare();
startLocalPlayer();
afd.close();
return true;
} else {
Log.e(TAG, "Could not load fallback ringtone");
}
} catch (IOException ioe) {
destroyLocalPlayer();
Log.e(TAG, "Failed to open fallback ringtone");
} catch (NotFoundException nfe) {
Log.e(TAG, "Fallback ringtone does not exist");
}
} else {
Log.w(TAG, "not playing fallback for " + mUri);
}
} }
return false; int ringtoneType = RingtoneManager.getDefaultType(mUri);
if (ringtoneType != -1 &&
RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) == null) {
Log.w(TAG, "not playing fallback for " + mUri);
return false;
}
// Default ringtone, try fallback ringtone.
try {
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(
com.android.internal.R.raw.fallbackring);
if (afd == null) {
Log.e(TAG, "Could not load fallback ringtone");
return false;
}
mLocalPlayer = new MediaPlayer();
if (afd.getDeclaredLength() < 0) {
mLocalPlayer.setDataSource(afd.getFileDescriptor());
} else {
mLocalPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getDeclaredLength());
}
mLocalPlayer.setAudioAttributes(mAudioAttributes);
synchronized (mPlaybackSettingsLock) {
applyPlaybackProperties_sync();
}
if (mVolumeShaperConfig != null) {
mVolumeShaper = mLocalPlayer.createVolumeShaper(mVolumeShaperConfig);
}
mLocalPlayer.prepare();
startLocalPlayer();
afd.close();
} catch (IOException ioe) {
destroyLocalPlayer();
Log.e(TAG, "Failed to open fallback ringtone");
return false;
} catch (NotFoundException nfe) {
Log.e(TAG, "Fallback ringtone does not exist");
return false;
}
return true;
} }
void setTitle(String title) { void setTitle(String title) {