Merge pull request #115 from ultrasonic/resume-playback-headphones

Add option to resume playback on headphones insertion.
This commit is contained in:
Yahor Berdnikau 2017-12-28 22:03:56 +01:00 committed by GitHub
commit deaa75938b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 26 deletions

View File

@ -18,17 +18,22 @@
*/
package org.moire.ultrasonic.service;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import org.moire.ultrasonic.R;
import org.moire.ultrasonic.domain.MusicDirectory;
import org.moire.ultrasonic.domain.PlayerState;
import org.moire.ultrasonic.util.CacheCleaner;
@ -128,30 +133,7 @@ public class DownloadServiceLifecycleSupport
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(downloadChecker, 5, 5, TimeUnit.SECONDS);
// Pause when headset is unplugged.
headsetEventReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras == null)
{
return;
}
Log.i(TAG, String.format("Headset event for: %s", extras.get("name")));
if (extras.getInt("state") == 0)
{
if (!downloadService.isJukeboxEnabled())
{
downloadService.pause();
}
}
}
};
downloadService.registerReceiver(headsetEventReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
registerHeadsetReceiver();
// Stop when SD card is ejected.
ejectEventReceiver = new BroadcastReceiver()
@ -202,6 +184,43 @@ public class DownloadServiceLifecycleSupport
new CacheCleaner(downloadService, downloadService).clean();
}
private void registerHeadsetReceiver() {
// Pause when headset is unplugged.
final SharedPreferences sp = Util.getPreferences(downloadService);
final String spKey = downloadService
.getString(R.string.settings_playback_resume_play_on_headphones_plug);
headsetEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
Log.i(TAG, String.format("Headset event for: %s", extras.get("name")));
final int state = extras.getInt("state");
if (state == 0) {
if (!downloadService.isJukeboxEnabled()) {
downloadService.pause();
}
} else if (state == 1) {
if (!downloadService.isJukeboxEnabled() &&
sp.getBoolean(spKey, false) &&
downloadService.getPlayerState() == PlayerState.PAUSED) {
downloadService.start();
}
}
}
};
@SuppressLint("InlinedApi")
IntentFilter headsetIntentFilter = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) ?
new IntentFilter(AudioManager.ACTION_HEADSET_PLUG) :
new IntentFilter(Intent.ACTION_HEADSET_PLUG);
downloadService.registerReceiver(headsetEventReceiver, headsetIntentFilter);
}
public void onStart(Intent intent)
{
if (intent != null && intent.getExtras() != null)

View File

@ -249,6 +249,8 @@
<string name="settings.preload_3">3 canciónes</string>
<string name="settings.preload_5">5 canciónes</string>
<string name="settings.preload_unlimited">Ilimitado</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Reanudación de la inserción de auriculares</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">La aplicación reanudará la reproducción en pausa al insertar los auriculares en el dispositivo.</string>
<string name="settings.screen_lit_summary">Mantener la pantalla encendida mientras descarga mejora la velocidad de la misma.</string>
<string name="settings.screen_lit_title">Mantener la pantalla encendida</string>
<string name="settings.scrobble_summary">Recuerda configurar tu nombre de usuario y contraseña de Last.fm en el servidor de Subsonic</string>

View File

@ -249,6 +249,8 @@
<string name="settings.preload_3">3 morceaux</string>
<string name="settings.preload_5">5 morceaux</string>
<string name="settings.preload_unlimited">Illimité</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Reprise de l\'insertion des écouteurs</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">L\'application reprendra la lecture en pause lors de l\'insertion du casque dans l\'appareil.</string>
<string name="settings.screen_lit_summary">Garder l\'écran allumé pendant le téléchargement permet d\'améliorer la vitesse de téléchargement.</string>
<string name="settings.screen_lit_title">Garder écran allumé</string>
<string name="settings.scrobble_summary">N\'oubliez pas de définir votre nom d\'utilisateur et mot de passe Last.fm sur le serveur Subsonic</string>

View File

@ -249,6 +249,8 @@
<string name="settings.preload_3">3 dal</string>
<string name="settings.preload_5">5 dal</string>
<string name="settings.preload_unlimited">Korlátlan</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Folytatás a fejhallgató behelyezésekor</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">Az alkalmazás folytatja a szüneteltetett lejátszást a fejhallgató behelyezésekor a készülékbe.</string>
<string name="settings.screen_lit_summary">Képernyő ébrentartása a letöltés alatt, a magasabb letöltési sebesség érdekében.</string>
<string name="settings.screen_lit_title">Képernyő ébrentartása</string>
<string name="settings.scrobble_summary">A Last.fm felhasználónevet és jelszót be kell állítani a Subsonic kiszolgálón!</string>

View File

@ -252,6 +252,8 @@
<string name="settings.preload_3">3 músicas</string>
<string name="settings.preload_5">5 músicas</string>
<string name="settings.preload_unlimited">Ilimitado</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Currículo na inserção de fone de ouvido</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">O aplicativo retomará a reprodução em pausa na inserção dos fones de ouvido no dispositivo.</string>
<string name="settings.screen_lit_summary">Manter a tela ligada enquanto baixando aumenta a velocidade de download.</string>
<string name="settings.screen_lit_title">Manter a Tela Ligada</string>
<string name="settings.scrobble_summary">Lembre-se de definir seu usuário e senha do Last.fm no servidor Subsonic</string>

View File

@ -252,6 +252,8 @@
<string name="settings.preload_3">3 músicas</string>
<string name="settings.preload_5">5 músicas</string>
<string name="settings.preload_unlimited">Ilimitado</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Currículo na inserção de fone de ouvido</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">O aplicativo retomará a reprodução em pausa na inserção dos fones de ouvido no dispositivo.</string>
<string name="settings.screen_lit_summary">Manter o ecrã ligado enquanto descarrega aumenta a velocidade de download.</string>
<string name="settings.screen_lit_title">Manter o Ecrã Ligado</string>
<string name="settings.scrobble_summary">Lembre-se de definir seu usuário e senha do Last.fm no servidor Subsonic</string>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="settings.playback.resume_play_on_headphones_plug" translatable="false">playback.resume_play_on_headphones_plug</string>
</resources>

View File

@ -253,6 +253,8 @@
<string name="settings.preload_3">3 songs</string>
<string name="settings.preload_5">5 songs</string>
<string name="settings.preload_unlimited">Unlimited</string>
<string name="settings.playback.resume_play_on_headphones_plug.title">Resume on headphones insertion</string>
<string name="settings.playback.resume_play_on_headphones_plug.summary">App will resume paused playback on headphones insertion into device.</string>
<string name="settings.screen_lit_summary">Keeping the screen on while downloading improves download speed.</string>
<string name="settings.screen_lit_title">Keep Screen On</string>
<string name="settings.scrobble_summary">Remember to set up your Last.fm user and password on the Subsonic server</string>

View File

@ -100,6 +100,12 @@
a:entryValues="@array/incrementTimeValues"
a:key="incrementTime"
a:title="@string/settings.increment_time"/>
<CheckBoxPreference
a:defaultValue="false"
a:key="@string/settings.playback.resume_play_on_headphones_plug"
a:title="@string/settings.playback.resume_play_on_headphones_plug.title"
a:summary="@string/settings.playback.resume_play_on_headphones_plug.summary"
/>
</PreferenceCategory>
<PreferenceCategory a:title="@string/settings.notifications_title">
<CheckBoxPreference