Use ShortcutPseudoActivity for widgets.

Android 8 does not allow us to directly start a background service, use the ShortcutPseudoActivity to do it anyway.

That's how we roll now.
This commit is contained in:
Adrian Ulrich 2017-08-28 22:41:59 +02:00
parent 4b06348059
commit 9fa33f6420
7 changed files with 64 additions and 63 deletions

View File

@ -123,21 +123,19 @@ public class FourLongWidget extends AppWidgetProvider {
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LibraryActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent = new Intent(context, LibraryActivity.class).setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.cover, pendingIntent);
views.setOnClickPendingIntent(R.id.text_layout, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
manager.updateAppWidget(new ComponentName(context, FourLongWidget.class), views);

View File

@ -126,21 +126,19 @@ public class FourSquareWidget extends AppWidgetProvider {
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LibraryActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent = new Intent(context, LibraryActivity.class).setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.title, pendingIntent);
views.setOnClickPendingIntent(R.id.artist, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
manager.updateAppWidget(new ComponentName(context, FourSquareWidget.class), views);

View File

@ -1,4 +1,5 @@
/*
* Copyright (C) 2017 Adrian Ulrich <adrian@blinkenlights.ch>
* Copyright (C) 2012 Ferenc Nagy <nferenc@nferenc.com>
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
*
@ -127,27 +128,25 @@ public class FourWhiteWidget extends AppWidgetProvider {
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LibraryActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent = new Intent(context, LibraryActivity.class).setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.cover, pendingIntent);
views.setOnClickPendingIntent(R.id.text_layout, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
intent = new Intent(PlaybackService.ACTION_PREVIOUS_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_PREVIOUS_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.previous, pendingIntent);
manager.updateAppWidget(new ComponentName(context, FourWhiteWidget.class), views);
}
}

View File

@ -97,15 +97,19 @@ public class OneCellWidget extends AppWidgetProvider {
boolean playing = (state & PlaybackService.FLAG_PLAYING) != 0;
views.setImageViewResource(R.id.play_pause, playing ? R.drawable.hidden_pause : R.drawable.hidden_play);
ComponentName service = new ComponentName(context, PlaybackService.class);
Intent playPause = new Intent(doubleTap ? PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED : PlaybackService.ACTION_TOGGLE_PLAYBACK);
playPause.setComponent(service);
views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getService(context, 0, playPause, 0));
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(doubleTap ? PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED : PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(doubleTap ? PlaybackService.ACTION_NEXT_SONG_DELAYED : PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
Intent next = new Intent(doubleTap ? PlaybackService.ACTION_NEXT_SONG_DELAYED : PlaybackService.ACTION_NEXT_SONG);
next.setComponent(service);
views.setOnClickPendingIntent(R.id.next, PendingIntent.getService(context, 0, next, 0));
Bitmap cover = null;
if ((state & PlaybackService.FLAG_NO_MEDIA) != 0) {

View File

@ -12,7 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.blinkenlights.android.vanilla;
@ -32,7 +32,13 @@ public class ShortcutPseudoActivity extends Activity {
final String action = getIntent().getAction();
switch (action) {
case PlaybackService.ACTION_TOGGLE_PLAYBACK:
case PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED:
case PlaybackService.ACTION_RANDOM_MIX_AUTOPLAY:
case PlaybackService.ACTION_NEXT_SONG:
case PlaybackService.ACTION_NEXT_SONG_DELAYED:
case PlaybackService.ACTION_PREVIOUS_SONG:
case PlaybackService.ACTION_CYCLE_SHUFFLE:
case PlaybackService.ACTION_CYCLE_REPEAT:
Intent intent = new Intent(this, PlaybackService.class);
intent.setAction(action);
startService(intent);

View File

@ -121,32 +121,30 @@ public class WidgetD extends AppWidgetProvider {
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LibraryActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent = new Intent(context, LibraryActivity.class).setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.cover, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
intent = new Intent(PlaybackService.ACTION_PREVIOUS_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_PREVIOUS_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.previous, pendingIntent);
intent = new Intent(PlaybackService.ACTION_CYCLE_SHUFFLE).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_CYCLE_SHUFFLE);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.shuffle, pendingIntent);
intent = new Intent(PlaybackService.ACTION_CYCLE_REPEAT).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_CYCLE_REPEAT);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.end_action, pendingIntent);
manager.updateAppWidget(new ComponentName(context, WidgetD.class), views);

View File

@ -111,33 +111,31 @@ public class WidgetE extends AppWidgetProvider {
Intent intent;
PendingIntent pendingIntent;
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME;
ComponentName service = new ComponentName(context, PlaybackService.class);
intent = new Intent(context, LibraryActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent = new Intent(context, LibraryActivity.class).setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.title, pendingIntent);
views.setOnClickPendingIntent(R.id.artist, pendingIntent);
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_TOGGLE_PLAYBACK);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause, pendingIntent);
intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_NEXT_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);
intent = new Intent(PlaybackService.ACTION_PREVIOUS_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_PREVIOUS_SONG);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.previous, pendingIntent);
intent = new Intent(PlaybackService.ACTION_CYCLE_SHUFFLE).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_CYCLE_SHUFFLE);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.shuffle, pendingIntent);
intent = new Intent(PlaybackService.ACTION_CYCLE_REPEAT).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
intent = new Intent(context, ShortcutPseudoActivity.class).setFlags(flags).setAction(PlaybackService.ACTION_CYCLE_REPEAT);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.end_action, pendingIntent);
manager.updateAppWidget(new ComponentName(context, WidgetE.class), views);