diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-04-07 08:11:32 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-04-07 08:11:32 -0700 |
commit | afcf016a1f08576318a5af5c086efae03e0f62f8 (patch) | |
tree | 510c41c4b631470dcc4c0f62d638079b5afb6383 | |
parent | b0f611451e627f520711a0f50da571385a6689d3 (diff) | |
download | dsub-afcf016a1f08576318a5af5c086efae03e0f62f8.tar.gz dsub-afcf016a1f08576318a5af5c086efae03e0f62f8.tar.bz2 dsub-afcf016a1f08576318a5af5c086efae03e0f62f8.zip |
#477 Add option to auto play when plugging in headphones
-rw-r--r-- | AndroidManifest.xml | 13 | ||||
-rw-r--r-- | res/values/strings.xml | 2 | ||||
-rw-r--r-- | res/xml/settings.xml | 6 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/activity/SubsonicActivity.java | 7 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/fragments/SettingsFragment.java | 10 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/receiver/BootReceiver.java | 34 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java | 42 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/service/HeadphoneListenerService.java | 66 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/Constants.java | 1 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/Util.java | 5 |
10 files changed, 185 insertions, 1 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d44eb8ec..bf2262cb 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -24,6 +24,7 @@ <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
@@ -86,7 +87,7 @@ </activity>
<service android:name=".service.DownloadService"
- android:label="Subsonic Download Service"/>
+ android:label="DSub Playback Service"/>
<service android:name="org.fourthline.cling.android.AndroidUpnpServiceImpl"/>
<service android:name="github.daneren2005.dsub.service.sync.AuthenticatorService">
<intent-filter>
@@ -137,6 +138,16 @@ android:resource="@xml/mostrecent_syncadapter" />
</service>
+ <service android:name="github.daneren2005.dsub.service.HeadphoneListenerService"
+ android:label="DSub Headphone Listener"/>
+ <receiver
+ android:name="github.daneren2005.dsub.receiver.BootReceiver">
+ <intent-filter>
+ <action
+ android:name="android.intent.action.BOOT_COMPLETED" />
+ </intent-filter>
+ </receiver>
+
<receiver android:name="github.daneren2005.dsub.receiver.MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
diff --git a/res/values/strings.xml b/res/values/strings.xml index 054d5047..7b6b3c5c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -455,6 +455,8 @@ <string name="settings.casting_proxy_summary">Stream everything through the device as a proxy. This gets around issues such as using self-signed certificates.</string>
<string name="settings.rename_duplicates">Rename duplicate tracks</string>
<string name="settings.rename_duplicates_summary">Rename duplicate tracks to the original filename so you can tell them apart.</string>
+ <string name="settings.start_on_headphones">Start on headphones</string>
+ <string name="settings.start_on_headphones_summary">Start when headphones are plugged in. This requires the use of a service which starts on boot up to check for the headphone plug event even when DSub is not running.</string>
<string name="shuffle.title">Shuffle By</string>
<string name="shuffle.startYear">Start Year:</string>
diff --git a/res/xml/settings.xml b/res/xml/settings.xml index f6b654cf..b1cbdd8c 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -439,6 +439,12 @@ android:summary="@string/settings.gapless_playback_summary" android:key="gaplessPlayback" android:defaultValue="true"/> + + <CheckBoxPreference + android:title="@string/settings.start_on_headphones" + android:summary="@string/settings.start_on_headphones_summary" + android:key="startOnHeadphones" + android:defaultValue="false"/> </PreferenceCategory> </PreferenceScreen> </PreferenceScreen> diff --git a/src/github/daneren2005/dsub/activity/SubsonicActivity.java b/src/github/daneren2005/dsub/activity/SubsonicActivity.java index 90e7dda5..f28d023a 100644 --- a/src/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/src/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -67,6 +67,7 @@ import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.ServerInfo;
import github.daneren2005.dsub.fragments.SubsonicFragment;
import github.daneren2005.dsub.service.DownloadService;
+import github.daneren2005.dsub.service.HeadphoneListenerService;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.ImageLoader;
import github.daneren2005.dsub.util.Util;
@@ -143,6 +144,12 @@ public class SubsonicActivity extends ActionBarActivity implements OnItemSelecte if(drawerToggle != null) {
drawerToggle.syncState();
}
+
+ if(Util.shouldStartOnHeadphones(this)) {
+ Intent serviceIntent = new Intent();
+ serviceIntent.setClassName(this.getPackageName(), HeadphoneListenerService.class.getName());
+ this.startService(serviceIntent);
+ }
}
@Override
diff --git a/src/github/daneren2005/dsub/fragments/SettingsFragment.java b/src/github/daneren2005/dsub/fragments/SettingsFragment.java index 8402e29a..3be21a67 100644 --- a/src/github/daneren2005/dsub/fragments/SettingsFragment.java +++ b/src/github/daneren2005/dsub/fragments/SettingsFragment.java @@ -48,6 +48,7 @@ import java.util.Map; import github.daneren2005.dsub.R; import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.HeadphoneListenerService; import github.daneren2005.dsub.service.MusicService; import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.util.Constants; @@ -146,6 +147,15 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared if(downloadService != null) { downloadService.reapplyVolume(); } + } else if(Constants.PREFERENCES_KEY_START_ON_HEADPHONES.equals(key)) { + Intent serviceIntent = new Intent(); + serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName()); + + if(sharedPreferences.getBoolean(key, false)) { + context.startService(serviceIntent); + } else { + context.stopService(serviceIntent); + } } scheduleBackup(); diff --git a/src/github/daneren2005/dsub/receiver/BootReceiver.java b/src/github/daneren2005/dsub/receiver/BootReceiver.java new file mode 100644 index 00000000..634aeeee --- /dev/null +++ b/src/github/daneren2005/dsub/receiver/BootReceiver.java @@ -0,0 +1,34 @@ +/* + This file is part of Subsonic. + Subsonic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Subsonic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Subsonic. If not, see <http://www.gnu.org/licenses/>. + Copyright 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import github.daneren2005.dsub.service.HeadphoneListenerService; +import github.daneren2005.dsub.util.Util; + +public class BootReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if(Util.shouldStartOnHeadphones(context)) { + Intent serviceIntent = new Intent(); + serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName()); + context.startService(serviceIntent); + } + } +} diff --git a/src/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java b/src/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java new file mode 100644 index 00000000..77948c41 --- /dev/null +++ b/src/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java @@ -0,0 +1,42 @@ +/* + This file is part of Subsonic. + Subsonic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Subsonic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Subsonic. If not, see <http://www.gnu.org/licenses/>. + Copyright 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.util.Util; + +public class HeadphonePlugReceiver extends BroadcastReceiver { + private static final String TAG = HeadphonePlugReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + if(Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) { + int headphoneState = intent.getIntExtra("state", -1); + Log.d(TAG, "State: " + headphoneState); + if(headphoneState == 1 && Util.shouldStartOnHeadphones(context)) { + Log.d(TAG, "Fired play event"); + Intent start = new Intent(context, DownloadService.class); + start.setAction(DownloadService.START_PLAY); + context.startService(start); + } + } + } +} diff --git a/src/github/daneren2005/dsub/service/HeadphoneListenerService.java b/src/github/daneren2005/dsub/service/HeadphoneListenerService.java new file mode 100644 index 00000000..f4375c50 --- /dev/null +++ b/src/github/daneren2005/dsub/service/HeadphoneListenerService.java @@ -0,0 +1,66 @@ +/* + This file is part of Subsonic. + Subsonic is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + Subsonic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with Subsonic. If not, see <http://www.gnu.org/licenses/>. + Copyright 2015 (C) Scott Jackson +*/ + +package github.daneren2005.dsub.service; + +import android.app.Service; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.IBinder; + +import github.daneren2005.dsub.receiver.HeadphonePlugReceiver; +import github.daneren2005.dsub.util.Util; + +/** + * Created by Scott on 4/6/2015. + */ +public class HeadphoneListenerService extends Service { + private HeadphonePlugReceiver receiver; + + @Override + public void onCreate() { + super.onCreate(); + + receiver = new HeadphonePlugReceiver(); + registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if(!Util.shouldStartOnHeadphones(this)) { + stopSelf(); + } + + return Service.START_STICKY; + } + + @Override + public void onDestroy() { + super.onDestroy(); + + try { + if(receiver != null) { + unregisterReceiver(receiver); + } + } catch(Exception e) { + // Don't care + } + } +} diff --git a/src/github/daneren2005/dsub/util/Constants.java b/src/github/daneren2005/dsub/util/Constants.java index 7ab2b736..31c5bef2 100644 --- a/src/github/daneren2005/dsub/util/Constants.java +++ b/src/github/daneren2005/dsub/util/Constants.java @@ -156,6 +156,7 @@ public final class Constants { public static final String PREFERENCES_KEY_DISABLE_EXIT_PROMPT = "disableExitPrompt"; public static final String PREFERENCES_KEY_RENAME_DUPLICATES = "renameDuplicates"; public static final String PREFERENCES_KEY_FIRST_LEVEL_ARTIST = "firstLevelArtist"; + public static final String PREFERENCES_KEY_START_ON_HEADPHONES = "startOnHeadphones"; public static final String OFFLINE_SCROBBLE_COUNT = "scrobbleCount"; public static final String OFFLINE_SCROBBLE_ID = "scrobbleID"; diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index 20cca368..75d8d5dd 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -592,6 +592,11 @@ public final class Util { editor.commit(); } + public static boolean shouldStartOnHeadphones(Context context) { + SharedPreferences prefs = getPreferences(context); + return prefs.getBoolean(Constants.PREFERENCES_KEY_START_ON_HEADPHONES, false); + } + /** * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>. * <p/> |