Merge "Fix a race condition in SeekBarVolumizer." into nyc-dev
am: 54f4a63
* commit '54f4a630dcb66f0b6d5ae5b1028dc88d7971c99e':
Fix a race condition in SeekBarVolumizer.
This commit is contained in:
@@ -38,6 +38,8 @@ import android.util.Log;
|
|||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
import android.widget.SeekBar.OnSeekBarChangeListener;
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.GuardedBy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns a {@link SeekBar} into a volume control.
|
* Turns a {@link SeekBar} into a volume control.
|
||||||
* @hide
|
* @hide
|
||||||
@@ -67,6 +69,10 @@ public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callba
|
|||||||
private Observer mVolumeObserver;
|
private Observer mVolumeObserver;
|
||||||
private int mOriginalStreamVolume;
|
private int mOriginalStreamVolume;
|
||||||
private int mLastAudibleStreamVolume;
|
private int mLastAudibleStreamVolume;
|
||||||
|
// When the old handler is destroyed and a new one is created, there could be a situation where
|
||||||
|
// this is accessed at the same time in different handlers. So, access to this field needs to be
|
||||||
|
// synchronized.
|
||||||
|
@GuardedBy("this")
|
||||||
private Ringtone mRingtone;
|
private Ringtone mRingtone;
|
||||||
private int mLastProgress = -1;
|
private int mLastProgress = -1;
|
||||||
private boolean mMuted;
|
private boolean mMuted;
|
||||||
@@ -174,9 +180,11 @@ public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callba
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onInitSample() {
|
private void onInitSample() {
|
||||||
mRingtone = RingtoneManager.getRingtone(mContext, mDefaultUri);
|
synchronized (this) {
|
||||||
if (mRingtone != null) {
|
mRingtone = RingtoneManager.getRingtone(mContext, mDefaultUri);
|
||||||
mRingtone.setStreamType(mStreamType);
|
if (mRingtone != null) {
|
||||||
|
mRingtone.setStreamType(mStreamType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,16 +200,19 @@ public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callba
|
|||||||
if (mCallback != null) {
|
if (mCallback != null) {
|
||||||
mCallback.onSampleStarting(this);
|
mCallback.onSampleStarting(this);
|
||||||
}
|
}
|
||||||
if (mRingtone != null) {
|
|
||||||
try {
|
synchronized (this) {
|
||||||
mRingtone.setAudioAttributes(new AudioAttributes.Builder(mRingtone
|
if (mRingtone != null) {
|
||||||
.getAudioAttributes())
|
try {
|
||||||
.setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY |
|
mRingtone.setAudioAttributes(new AudioAttributes.Builder(mRingtone
|
||||||
AudioAttributes.FLAG_BYPASS_MUTE)
|
.getAudioAttributes())
|
||||||
.build());
|
.setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY |
|
||||||
mRingtone.play();
|
AudioAttributes.FLAG_BYPASS_MUTE)
|
||||||
} catch (Throwable e) {
|
.build());
|
||||||
Log.w(TAG, "Error playing ringtone, stream " + mStreamType, e);
|
mRingtone.play();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
Log.w(TAG, "Error playing ringtone, stream " + mStreamType, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -216,8 +227,10 @@ public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callba
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onStopSample() {
|
private void onStopSample() {
|
||||||
if (mRingtone != null) {
|
synchronized (this) {
|
||||||
mRingtone.stop();
|
if (mRingtone != null) {
|
||||||
|
mRingtone.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,7 +287,9 @@ public class SeekBarVolumizer implements OnSeekBarChangeListener, Handler.Callba
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSamplePlaying() {
|
public boolean isSamplePlaying() {
|
||||||
return mRingtone != null && mRingtone.isPlaying();
|
synchronized (this) {
|
||||||
|
return mRingtone != null && mRingtone.isPlaying();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startSample() {
|
public void startSample() {
|
||||||
|
|||||||
Reference in New Issue
Block a user