aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2015-04-07 08:11:32 -0700
committerScott Jackson <daneren2005@gmail.com>2015-04-07 08:11:32 -0700
commitafcf016a1f08576318a5af5c086efae03e0f62f8 (patch)
tree510c41c4b631470dcc4c0f62d638079b5afb6383 /src
parentb0f611451e627f520711a0f50da571385a6689d3 (diff)
downloaddsub-afcf016a1f08576318a5af5c086efae03e0f62f8.tar.gz
dsub-afcf016a1f08576318a5af5c086efae03e0f62f8.tar.bz2
dsub-afcf016a1f08576318a5af5c086efae03e0f62f8.zip
#477 Add option to auto play when plugging in headphones
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/activity/SubsonicActivity.java7
-rw-r--r--src/github/daneren2005/dsub/fragments/SettingsFragment.java10
-rw-r--r--src/github/daneren2005/dsub/receiver/BootReceiver.java34
-rw-r--r--src/github/daneren2005/dsub/receiver/HeadphonePlugReceiver.java42
-rw-r--r--src/github/daneren2005/dsub/service/HeadphoneListenerService.java66
-rw-r--r--src/github/daneren2005/dsub/util/Constants.java1
-rw-r--r--src/github/daneren2005/dsub/util/Util.java5
7 files changed, 165 insertions, 0 deletions
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/>