diff --git a/app/src/main/java/com/cappielloantonio/tempo/database/dao/DownloadDao.java b/app/src/main/java/com/cappielloantonio/tempo/database/dao/DownloadDao.java index 48fed8d4..242dd51a 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/database/dao/DownloadDao.java +++ b/app/src/main/java/com/cappielloantonio/tempo/database/dao/DownloadDao.java @@ -15,6 +15,9 @@ public interface DownloadDao { @Query("SELECT * FROM download WHERE download_state = 1 ORDER BY artist, album, track ASC") LiveData> getAll(); + @Query("SELECT * FROM download WHERE id = :id") + Download getOne(String id); + @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Download download); diff --git a/app/src/main/java/com/cappielloantonio/tempo/repository/DownloadRepository.java b/app/src/main/java/com/cappielloantonio/tempo/repository/DownloadRepository.java index b8245a62..c71d2f8c 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/repository/DownloadRepository.java +++ b/app/src/main/java/com/cappielloantonio/tempo/repository/DownloadRepository.java @@ -4,8 +4,11 @@ import androidx.lifecycle.LiveData; import com.cappielloantonio.tempo.database.AppDatabase; import com.cappielloantonio.tempo.database.dao.DownloadDao; +import com.cappielloantonio.tempo.database.dao.FavoriteDao; import com.cappielloantonio.tempo.model.Download; +import com.cappielloantonio.tempo.model.Favorite; +import java.util.ArrayList; import java.util.List; public class DownloadRepository { @@ -15,6 +18,43 @@ public class DownloadRepository { return downloadDao.getAll(); } + public Download getDownload(String id) { + Download download = null; + + GetDownloadThreadSafe getDownloadThreadSafe = new GetDownloadThreadSafe(downloadDao, id); + Thread thread = new Thread(getDownloadThreadSafe); + thread.start(); + + try { + thread.join(); + download = getDownloadThreadSafe.getDownload(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + return download; + } + + private static class GetDownloadThreadSafe implements Runnable { + private final DownloadDao downloadDao; + private final String id; + private Download download; + + public GetDownloadThreadSafe(DownloadDao downloadDao, String id) { + this.downloadDao = downloadDao; + this.id = id; + } + + @Override + public void run() { + download = downloadDao.getOne(id); + } + + public Download getDownload() { + return download; + } + } + public void insert(Download download) { InsertThreadSafe insert = new InsertThreadSafe(downloadDao, download); Thread thread = new Thread(insert); diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java index 2c4d05e5..a55640b8 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderManager.java @@ -108,6 +108,11 @@ public class DownloaderManager { } } + public static String getDownloadNotificationMessage(String id) { + com.cappielloantonio.tempo.model.Download download = getDownloadRepository().getDownload(id); + return download != null ? download.getTitle() : null; + } + private static DownloadRepository getDownloadRepository() { return new DownloadRepository(); } diff --git a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java index e5af9bd1..b975326f 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java +++ b/app/src/main/java/com/cappielloantonio/tempo/service/DownloaderService.java @@ -7,7 +7,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.media3.common.util.NotificationUtil; import androidx.media3.common.util.UnstableApi; -import androidx.media3.common.util.Util; import androidx.media3.exoplayer.offline.Download; import androidx.media3.exoplayer.offline.DownloadManager; import androidx.media3.exoplayer.offline.DownloadNotificationHelper; @@ -52,6 +51,8 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa } private static final class TerminalStateNotificationHelper implements DownloadManager.Listener { + private static final String TAG = "TerminalStateNotificatinHelper"; + private final Context context; private final DownloadNotificationHelper notificationHelper; @@ -68,10 +69,10 @@ public class DownloaderService extends androidx.media3.exoplayer.offline.Downloa Notification notification; if (download.state == Download.STATE_COMPLETED) { - notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, Util.fromUtf8Bytes(download.request.data)); + notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_check_circle, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); DownloaderManager.updateDatabase(download.request.id); } else if (download.state == Download.STATE_FAILED) { - notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, Util.fromUtf8Bytes(download.request.data)); + notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, DownloaderManager.getDownloadNotificationMessage(download.request.id)); } else { return; }