check if wake lock needs reinit when pressing start/pause in audio player

This commit is contained in:
Daniel Gultsch 2018-09-23 11:47:13 +02:00
parent 2d206122a5
commit 5cf64826ac

View file

@ -39,12 +39,11 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
private static final Object LOCK = new Object();
private static MediaPlayer player = null;
private static Message currentlyPlayingMessage = null;
private static PowerManager.WakeLock wakeLock;
private final MessageAdapter messageAdapter;
private final WeakReferenceSet<RelativeLayout> audioPlayerLayouts = new WeakReferenceSet<>();
private final SensorManager sensorManager;
private final Sensor proximitySensor;
private static PowerManager.WakeLock wakeLock;
private final PendingItem<WeakReference<ImageButton>> pendingOnClickView = new PendingItem<>();
private final Handler handler = new Handler();
@ -54,17 +53,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
this.messageAdapter = adapter;
this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
this.proximitySensor = this.sensorManager == null ? null : this.sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (Build.VERSION.SDK_INT >= 21) {
synchronized (AudioPlayer.LOCK) {
if (AudioPlayer.wakeLock == null) {
final PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
AudioPlayer.wakeLock = powerManager == null ? null : powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, AudioPlayer.class.getSimpleName());
AudioPlayer.wakeLock.setReferenceCounted(false);
}
}
} else {
AudioPlayer.wakeLock = null;
}
initializeProximityWakeLock(context);
synchronized (AudioPlayer.LOCK) {
if (AudioPlayer.player != null) {
AudioPlayer.player.setOnCompletionListener(this);
@ -79,6 +68,20 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
return String.format(Locale.ENGLISH, "%d:%02d", ms / 60000, Math.min(Math.round((ms % 60000) / 1000f), 59));
}
private void initializeProximityWakeLock(Context context) {
if (Build.VERSION.SDK_INT >= 21) {
synchronized (AudioPlayer.LOCK) {
if (AudioPlayer.wakeLock == null) {
final PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
AudioPlayer.wakeLock = powerManager == null ? null : powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, AudioPlayer.class.getSimpleName());
AudioPlayer.wakeLock.setReferenceCounted(false);
}
}
} else {
AudioPlayer.wakeLock = null;
}
}
public void init(RelativeLayout audioPlayer, Message message) {
synchronized (AudioPlayer.LOCK) {
audioPlayer.setTag(message);
@ -138,6 +141,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
ActivityCompat.requestPermissions(messageAdapter.getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ConversationsActivity.REQUEST_PLAY_PAUSE);
return;
}
initializeProximityWakeLock(playPause.getContext());
final RelativeLayout audioPlayer = (RelativeLayout) playPause.getParent();
final ViewHolder viewHolder = ViewHolder.get(audioPlayer);
final Message message = (Message) audioPlayer.getTag();