From 797d0fd2816a73a36c8e5851191a3b8530a866ac Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 28 Jan 2015 09:08:02 -0800 Subject: Refactor widget events/lifecycle event handling so that we can start from widget as well with app closed --- .../dsub/provider/DSubWidgetProvider.java | 6 +-- .../service/DownloadServiceLifecycleSupport.java | 56 +++++++++++----------- 2 files changed, 30 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/provider/DSubWidgetProvider.java b/src/github/daneren2005/dsub/provider/DSubWidgetProvider.java index f6b28b21..f4c3656c 100644 --- a/src/github/daneren2005/dsub/provider/DSubWidgetProvider.java +++ b/src/github/daneren2005/dsub/provider/DSubWidgetProvider.java @@ -286,19 +286,19 @@ public class DSubWidgetProvider extends AppWidgetProvider { // Emulate media button clicks. intent = new Intent("DSub.PLAY_PAUSE"); intent.setComponent(new ComponentName(context, DownloadService.class)); - intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); + intent.setAction(DownloadService.CMD_TOGGLEPAUSE); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_play, pendingIntent); intent = new Intent("DSub.NEXT"); // Use a unique action name to ensure a different PendingIntent to be created. intent.setComponent(new ComponentName(context, DownloadService.class)); - intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT)); + intent.setAction(DownloadService.CMD_NEXT); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_next, pendingIntent); intent = new Intent("DSub.PREVIOUS"); // Use a unique action name to ensure a different PendingIntent to be created. intent.setComponent(new ComponentName(context, DownloadService.class)); - intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS)); + intent.setAction(DownloadService.CMD_PREVIOUS); pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.control_previous, pendingIntent); } diff --git a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java index 0a1e2cdd..e93fb88d 100644 --- a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java +++ b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java @@ -158,16 +158,17 @@ public class DownloadServiceLifecycleSupport { public void onStart(final Intent intent) { if (intent != null) { - String action = intent.getAction(); - if(DownloadService.START_PLAY.equals(action)) { - eventHandler.post(new Runnable() { - @Override - public void run() { - if(!setup.get()) { - lock.lock(); - lock.unlock(); - } - + final String action = intent.getAction(); + + eventHandler.post(new Runnable() { + @Override + public void run() { + if(!setup.get()) { + lock.lock(); + lock.unlock(); + } + + if(DownloadService.START_PLAY.equals(action)) { int offlinePref = intent.getIntExtra(Constants.PREFERENCES_KEY_OFFLINE, 0); if(offlinePref != 0) { boolean offline = (offlinePref == 2); @@ -186,41 +187,38 @@ public class DownloadServiceLifecycleSupport { if(startYear != null) { editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYear); } - + String endYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR); if(endYear != null) { editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYear); } - + String genre = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE); if(genre != null) { editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre); } editor.commit(); - + downloadService.setShufflePlayEnabled(true); } else { downloadService.start(); } - } - }); - } else if(DownloadService.CANCEL_DOWNLOADS.equals(action)) { - downloadService.clearBackground(); - } else if(intent.getExtras() != null) { - final KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); - if (event != null) { - eventHandler.post(new Runnable() { - @Override - public void run() { - if(!setup.get()) { - lock.lock(); - lock.unlock(); - } + } else if(DownloadService.CMD_TOGGLEPAUSE.equals(action)) { + downloadService.togglePlayPause(); + } else if(DownloadService.CMD_NEXT.equals(action)) { + downloadService.next(); + } else if(DownloadService.CMD_PREVIOUS.equals(action)) { + downloadService.previous(); + } else if(DownloadService.CANCEL_DOWNLOADS.equals(action)) { + downloadService.clearBackground(); + } else if(intent.getExtras() != null) { + final KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT); + if (event != null) { handleKeyEvent(event); } - }); + } } - } + }); } } -- cgit v1.2.3