keep screen on during audio playback. fixes #2733

This commit is contained in:
Daniel Gultsch 2018-01-21 16:04:31 +01:00
parent f2bbb6087c
commit 3c4ba7e0ce
2 changed files with 15 additions and 1 deletions

View File

@ -13,7 +13,6 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.ColorInt;
import android.support.text.emoji.EmojiCompat;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
@ -33,6 +32,7 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
@ -147,6 +147,14 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
updatePreferences();
}
public void flagScreenOn() {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
public void flagScreenOff() {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
public void setOnContactPictureClicked(OnContactPictureClicked listener) {
this.mOnContactPictureClickedListener = listener;
}

View File

@ -108,10 +108,12 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
if (player.isPlaying()) {
viewHolder.progress.setEnabled(false);
player.pause();
messageAdapter.flagScreenOff();
viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_play_arrow_white_36dp : R.drawable.ic_play_arrow_black_36dp);
} else {
viewHolder.progress.setEnabled(true);
player.start();
messageAdapter.flagScreenOn();
this.stopRefresher(true);
viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_pause_white_36dp : R.drawable.ic_pause_black_36dp);
}
@ -126,10 +128,12 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
AudioPlayer.player.setOnCompletionListener(this);
AudioPlayer.player.prepare();
AudioPlayer.player.start();
messageAdapter.flagScreenOn();
viewHolder.progress.setEnabled(true);
viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_pause_white_36dp : R.drawable.ic_pause_black_36dp);
return true;
} catch (Exception e) {
messageAdapter.flagScreenOff();
AudioPlayer.currentlyPlayingMessage = null;
return false;
}
@ -150,6 +154,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
AudioPlayer.player.stop();
}
AudioPlayer.player.release();
messageAdapter.flagScreenOff();
AudioPlayer.player = null;
resetPlayerUi();
}
@ -183,6 +188,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
AudioPlayer.player = null;
}
mediaPlayer.release();
messageAdapter.flagScreenOff();
resetPlayerUi();
}
}