Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Add feature of "Keep Screen On While Playing" #968

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
import com.kabouzeid.gramophone.interfaces.MusicServiceEventListener;
import com.kabouzeid.gramophone.ui.activities.base.AbsMusicServiceActivity;
import com.kabouzeid.gramophone.util.PreferenceUtil;

/**
* @author Karim Abou Zeid (kabouzeid)
Expand Down Expand Up @@ -43,6 +47,24 @@ public void onDestroyView() {
activity.removeMusicServiceEventListener(this);
}

@Override
public void onResume() {
super.onResume();
updateScreenOnState(requireActivity().getWindow());
}

private void updateScreenOnState(Window window){
if (isKeepScreenOnWhilePlaying() && MusicPlayerRemote.isPlaying()) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}

private boolean isKeepScreenOnWhilePlaying(){
return PreferenceUtil.getInstance(requireActivity()).keepScreenOnWhilePlaying();
}

@Override
public void onPlayingMetaChanged() {

Expand All @@ -65,7 +87,7 @@ public void onQueueChanged() {

@Override
public void onPlayStateChanged() {

updateScreenOnState(requireActivity().getWindow());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.kabouzeid.appthemehelper.ThemeStore;
import com.kabouzeid.appthemehelper.util.ATHUtil;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.helper.MusicPlayerRemote;
import com.kabouzeid.gramophone.helper.MusicProgressViewUpdateHelper;
import com.kabouzeid.gramophone.helper.PlayPauseButtonOnClickHandler;
import com.kabouzeid.gramophone.ui.fragments.AbsMusicServiceFragment;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.kabouzeid.gramophone.views.PlayPauseDrawable;

import butterknife.BindView;
Expand Down Expand Up @@ -102,6 +106,7 @@ public void onPlayingMetaChanged() {

@Override
public void onPlayStateChanged() {
super.onPlayStateChanged();
updatePlayPauseDrawableState(true);
}

Expand All @@ -123,6 +128,8 @@ public void onPause() {
progressViewUpdateHelper.stop();
}



private static class FlingPlayBackController implements View.OnTouchListener {

GestureDetector flingPlayBackController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void onServiceConnected() {

@Override
public void onPlayStateChanged() {
super.onPlayStateChanged();
updatePlayPauseDrawableState(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void onServiceConnected() {

@Override
public void onPlayStateChanged() {
super.onPlayStateChanged();
updatePlayPauseDrawableState(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public final class PreferenceUtil {

public static final String SYNCHRONIZED_LYRICS_SHOW = "synchronized_lyrics_show";

public static final String KEEP_SCREEN_ON = "keep_screen_on";

public static final String INITIALIZED_BLACKLIST = "initialized_blacklist";

public static final String LIBRARY_CATEGORIES = "library_categories";
Expand Down Expand Up @@ -489,6 +491,10 @@ public final boolean synchronizedLyricsShow() {
return mPreferences.getBoolean(SYNCHRONIZED_LYRICS_SHOW, true);
}

public final boolean keepScreenOnWhilePlaying(){
return mPreferences.getBoolean(KEEP_SCREEN_ON,false);
}

public void setInitializedBlacklist() {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(INITIALIZED_BLACKLIST, true);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<string name="pref_title_audio_ducking">音频焦点丢失时降低音量</string>
<string name="pref_title_last_added_interval">最近添加播放列表间隔</string>
<string name="pref_title_synchronized_lyrics_show">显示同步歌词</string>
<string name="pref_title_keep_screen_on_while_playing">播放时屏幕常亮</string>
<string name="pref_title_remember_last_tab">记住最后打开页面</string>
<string name="pref_title_remember_shuffle">记住随机播放开关</string>
<string name="no_equalizer">没有找到均衡器。</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<string name="pref_title_audio_ducking">Reduce volume on focus loss</string>
<string name="pref_title_last_added_interval">Last added playlist interval</string>
<string name="pref_title_synchronized_lyrics_show">Show synchronized lyrics</string>
<string name="pref_title_keep_screen_on_while_playing">Keep Screen On While Playing</string>
<string name="pref_title_remember_last_tab">Remember last tab</string>
<string name="pref_title_remember_shuffle">Remember shuffle</string>
<string name="no_equalizer">No equalizer found.</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/pref_now_playing_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
android:key="synchronized_lyrics_show"
android:summary="@string/pref_summary_synchronized_lyrics_show"
android:title="@string/pref_title_synchronized_lyrics_show" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference
app:iconSpaceReserved="false"
android:defaultValue="false"
android:key="keep_screen_on"
android:title="@string/pref_title_keep_screen_on_while_playing" />

</com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory>

Expand Down