add cancel action to download notification

This commit is contained in:
dkanada 2021-08-09 23:58:35 +09:00
commit cac64c53c7
5 changed files with 28 additions and 8 deletions

View file

@ -20,15 +20,17 @@ import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public class DownloadService extends Service { public class DownloadService extends Service {
public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID; public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
public static final String ACTION_START = PACKAGE_NAME + ".action.start";
public static final String ACTION_CANCEL = PACKAGE_NAME + ".action.cancel";
public static final String EXTRA_SONGS = PACKAGE_NAME + ".extra.songs"; public static final String EXTRA_SONGS = PACKAGE_NAME + ".extra.songs";
private Executor executor; private ExecutorService executor;
private DownloadNotification notification; private DownloadNotification notification;
@Override @Override
@ -41,15 +43,23 @@ public class DownloadService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) { if (intent == null || intent.getAction() == null) {
return super.onStartCommand(null, flags, startId); return super.onStartCommand(null, flags, startId);
} }
switch (intent.getAction()) {
case DownloadService.ACTION_CANCEL:
executor.shutdownNow();
notification.stop(null);
stopSelf();
break;
case DownloadService.ACTION_START:
List<Song> songs = intent.getParcelableArrayListExtra(EXTRA_SONGS); List<Song> songs = intent.getParcelableArrayListExtra(EXTRA_SONGS);
for (Song song : songs) { for (Song song : songs) {
download(song); download(song);
notification.start(song); notification.start(song);
} }
}
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
} }

View file

@ -13,6 +13,7 @@ import androidx.core.app.NotificationCompat;
import com.dkanada.gramophone.R; import com.dkanada.gramophone.R;
import com.dkanada.gramophone.activities.MainActivity; import com.dkanada.gramophone.activities.MainActivity;
import com.dkanada.gramophone.model.Song; import com.dkanada.gramophone.model.Song;
import com.dkanada.gramophone.service.DownloadService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -54,6 +55,9 @@ public class DownloadNotification {
Intent action = new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); Intent action = new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent clickIntent = PendingIntent.getActivity(context, 0, action, 0); PendingIntent clickIntent = PendingIntent.getActivity(context, 0, action, 0);
Intent cancel = new Intent(context, DownloadService.class).setAction(DownloadService.ACTION_CANCEL);
PendingIntent pendingCancel = PendingIntent.getService(context, 0, cancel, 0);
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
for (Song item : songs.stream().limit(5).collect(Collectors.toList())) { for (Song item : songs.stream().limit(5).collect(Collectors.toList())) {
style.addLine(item.title); style.addLine(item.title);
@ -65,6 +69,7 @@ public class DownloadNotification {
.setContentTitle(String.format(context.getString(R.string.downloading_x_songs), songs.size())) .setContentTitle(String.format(context.getString(R.string.downloading_x_songs), songs.size()))
.setProgress(this.maximum, this.current, false) .setProgress(this.maximum, this.current, false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(R.drawable.ic_close_white_24dp, context.getString(R.string.action_cancel), pendingCancel)
.setStyle(style) .setStyle(style)
.setShowWhen(false); .setShowWhen(false);
@ -74,6 +79,8 @@ public class DownloadNotification {
public synchronized void stop(Song song) { public synchronized void stop(Song song) {
if (song != null) { if (song != null) {
songs.remove(song); songs.remove(song);
} else {
songs.clear();
} }
if (songs.size() != 0) { if (songs.size() != 0) {

View file

@ -115,6 +115,8 @@ public class NavigationUtil {
Intent intent = new Intent(activity, DownloadService.class); Intent intent = new Intent(activity, DownloadService.class);
intent.putExtra(DownloadService.EXTRA_SONGS, new ArrayList<>(songs)); intent.putExtra(DownloadService.EXTRA_SONGS, new ArrayList<>(songs));
intent.setAction(DownloadService.ACTION_START);
activity.startService(intent); activity.startService(intent);
} }
} }

View file

@ -30,7 +30,7 @@ import com.dkanada.gramophone.util.MusicUtil;
import java.util.Arrays; import java.util.Arrays;
public abstract class BaseAppWidget extends AppWidgetProvider { public abstract class BaseAppWidget extends AppWidgetProvider {
public static final String NAME = "app_widget"; public static final String NAME = "widget.base";
public int imageSize = 0; public int imageSize = 0;
public float cardRadius = 0f; public float cardRadius = 0f;

View file

@ -18,6 +18,7 @@
<string name="action_remove_from_favorites">Remove from favorites</string> <string name="action_remove_from_favorites">Remove from favorites</string>
<string name="action_search">Search</string> <string name="action_search">Search</string>
<string name="action_download">Download</string> <string name="action_download">Download</string>
<string name="action_cancel">Cancel</string>
<string name="action_play_next">Play next</string> <string name="action_play_next">Play next</string>
<string name="action_play">Play</string> <string name="action_play">Play</string>
<string name="action_play_pause">Play/Pause</string> <string name="action_play_pause">Play/Pause</string>