feat: added an option to prevent phone from going into sleep mode if in-app

This commit is contained in:
antonio 2024-01-28 23:22:03 +01:00
parent cd44368d66
commit 634de67d74
5 changed files with 35 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
@ -37,6 +38,7 @@ public class BaseActivity extends AppCompatActivity {
initializeDownloader();
checkBatteryOptimization();
checkPermission();
checkAlwaysOnDisplay();
}
@Override
@ -66,6 +68,12 @@ public class BaseActivity extends AppCompatActivity {
}
}
private void checkAlwaysOnDisplay() {
if (Preferences.isDisplayAlwaysOn()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
private boolean detectBatteryOptimization() {
String packageName = getPackageName();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);

View file

@ -6,6 +6,7 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
@ -91,6 +92,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
actionSyncStarredTracks();
actionChangeDownloadStorage();
actionDeleteDownloadStorage();
actionKeepScreenOn();
}
@Override
@ -248,4 +250,17 @@ public class SettingsFragment extends PreferenceFragmentCompat {
}
});
}
private void actionKeepScreenOn() {
findPreference("always_on_display").setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue instanceof Boolean) {
if ((Boolean) newValue) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
return true;
});
}
}

View file

@ -46,6 +46,7 @@ object Preferences {
private const val SKIP_MIN_STAR_RATING = "skip_min_star_rating"
private const val MIN_STAR_RATING = "min_star_rating"
private const val ARTIST_ALBUM_LAYOUT = "artist_album_layout"
private const val ALWAYS_ON_DISPLAY = "always_on_display"
@JvmStatic
@ -357,4 +358,9 @@ object Preferences {
App.getInstance().preferences.edit().putBoolean(ARTIST_ALBUM_LAYOUT, isArtistAlbumLayoutHorizontal)
.apply()
}
@JvmStatic
fun isDisplayAlwaysOn(): Boolean {
return App.getInstance().preferences.getBoolean(ALWAYS_ON_DISPLAY, false)
}
}

View file

@ -224,6 +224,7 @@
<string name="server_unreachable_dialog_title">Server unreachable</string>
<string name="settings_about_summary">Tempo is an open source and lightweight music client for Subsonic, designed and built natively for Android.</string>
<string name="settings_about_title">About</string>
<string name="settings_always_on_display">Always on display</string>
<string name="settings_audio_transcode_download_format">Transcode format</string>
<string name="settings_audio_transcode_download_priority_summary">If enabled, Tempo will not force download the track with the transcode settings below.</string>
<string name="settings_audio_transcode_download_priority_title">Prioritize server settings used for streaming in downloads</string>

View file

@ -31,6 +31,11 @@
app:title="@string/settings_theme"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:title="@string/settings_always_on_display"
android:defaultValue="false"
android:key="always_on_display" />
<SwitchPreference
android:title="@string/settings_rounded_corner"
android:defaultValue="true"