Skip to content

Commit

Permalink
changes in click handling and refresh bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
XilinJia committed Mar 3, 2024
1 parent e24c764 commit da66b32
Show file tree
Hide file tree
Showing 59 changed files with 485 additions and 413 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
// Version code schema:
// "1.2.3-beta4" -> 1020304
// "1.2.3" -> 1020395
versionCode 3020103
versionName "4.2.0"
versionCode 3020104
versionName "4.2.1"

def commit = ""
try {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<supports-screens
android:anyDensity="true"
Expand Down
31 changes: 18 additions & 13 deletions app/src/main/java/ac/mdiq/podcini/service/FeedUpdateWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import ac.mdiq.podcini.ui.gui.NotificationUtils
import ac.mdiq.podcini.storage.model.download.DownloadError
import ac.mdiq.podcini.storage.model.download.DownloadResult
import ac.mdiq.podcini.storage.model.feed.Feed
import android.os.Build
import android.widget.Toast
import java.util.*

class FeedUpdateWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
Expand Down Expand Up @@ -107,21 +109,24 @@ class FeedUpdateWorker(context: Context, params: WorkerParameters) : Worker(cont
}

@UnstableApi private fun refreshFeeds(toUpdate: MutableList<Feed>, force: Boolean) {
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(this.applicationContext,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "refreshFeeds: require POST_NOTIFICATIONS permission")
// Toast.makeText(applicationContext, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}

while (toUpdate.isNotEmpty()) {
if (isStopped) {
return
}
if (ActivityCompat.checkSelfPermission(this.applicationContext,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
notificationManager.notify(R.id.notification_updating_feeds, createNotification(toUpdate))
val feed = toUpdate[0]
try {
Expand Down Expand Up @@ -187,9 +192,9 @@ class FeedUpdateWorker(context: Context, params: WorkerParameters) : Worker(cont
newEpisodesNotification.showIfNeeded(applicationContext, feedSyncTask.savedFeed!!)
if (!request.source.isNullOrEmpty()) {
if (!downloader.permanentRedirectUrl.isNullOrEmpty()) {
DBWriter.updateFeedDownloadURL(request.source!!, downloader.permanentRedirectUrl!!)
DBWriter.updateFeedDownloadURL(request.source, downloader.permanentRedirectUrl!!)
} else if (feedSyncTask.redirectUrl.isNotEmpty() && feedSyncTask.redirectUrl != request.source) {
DBWriter.updateFeedDownloadURL(request.source!!, feedSyncTask.redirectUrl)
DBWriter.updateFeedDownloadURL(request.source, feedSyncTask.redirectUrl)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.app.PendingIntent
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.annotation.OptIn
import androidx.core.app.NotificationCompat
import androidx.media3.common.util.UnstableApi
import androidx.work.Data
Expand Down Expand Up @@ -79,8 +80,8 @@ class EpisodeDownloadWorker(context: Context, params: WorkerParameters) : Worker
e.printStackTrace()
result = Result.failure()
}
if (result == Result.failure() && downloader != null) {
FileUtils.deleteQuietly(File(downloader!!.downloadRequest.destination))
if (result == Result.failure() && downloader?.downloadRequest?.destination != null) {
FileUtils.deleteQuietly(File(downloader!!.downloadRequest.destination!!))
}
progressUpdaterThread.interrupt()
try {
Expand All @@ -102,17 +103,15 @@ class EpisodeDownloadWorker(context: Context, params: WorkerParameters) : Worker

override fun onStopped() {
super.onStopped()
if (downloader != null) {
downloader!!.cancel()
}
downloader?.cancel()
}

override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
return Futures.immediateFuture(
ForegroundInfo(R.id.notification_downloading, generateProgressNotification()))
}

private fun performDownload(media: FeedMedia, request: DownloadRequest): Result {
@OptIn(UnstableApi::class) private fun performDownload(media: FeedMedia, request: DownloadRequest): Result {
val dest = File(request.destination)
if (!dest.exists()) {
try {
Expand Down Expand Up @@ -162,7 +161,7 @@ class EpisodeDownloadWorker(context: Context, params: WorkerParameters) : Worker
if (status.reason == DownloadError.ERROR_HTTP_DATA_ERROR
&& status.reasonDetailed.toInt() == 416) {
Log.d(TAG, "Requested invalid range, restarting download from the beginning")
FileUtils.deleteQuietly(File(downloader!!.downloadRequest.destination))
if (downloader?.downloadRequest?.destination != null) FileUtils.deleteQuietly(File(downloader!!.downloadRequest.destination!!))
sendMessage(request.title?:"", false)
return retry3times()
}
Expand Down Expand Up @@ -199,7 +198,7 @@ class EpisodeDownloadWorker(context: Context, params: WorkerParameters) : Worker
EventBus.getDefault().post(MessageEvent(
applicationContext.getString(
if (retrying) R.string.download_error_retrying else R.string.download_error_not_retrying,
episodeTitle), { ctx: Context -> MainActivityStarter(ctx!!).withDownloadLogsOpen().start() },
episodeTitle), { ctx: Context -> MainActivityStarter(ctx).withDownloadLogsOpen().start() },
applicationContext.getString(R.string.download_error_details)))
}

Expand Down Expand Up @@ -251,8 +250,7 @@ class EpisodeDownloadWorker(context: Context, params: WorkerParameters) : Worker
applicationContext.resources.getQuantityString(R.plurals.downloads_left,
progressCopy.size, progressCopy.size)
}
val builder = NotificationCompat.Builder(applicationContext,
NotificationUtils.CHANNEL_ID_DOWNLOADING)
val builder = NotificationCompat.Builder(applicationContext, NotificationUtils.CHANNEL_ID_DOWNLOADING)
builder.setTicker(applicationContext.getString(R.string.download_notification_title_episodes))
.setContentTitle(applicationContext.getString(R.string.download_notification_title_episodes))
.setContentText(contentText)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ac.mdiq.podcini.service.download

import ac.mdiq.podcini.R
import ac.mdiq.podcini.service.FeedUpdateWorker
import android.Manifest
import android.app.PendingIntent
import android.content.ComponentName
Expand All @@ -19,6 +20,7 @@ import ac.mdiq.podcini.ui.gui.NotificationUtils
import ac.mdiq.podcini.storage.model.feed.Feed
import ac.mdiq.podcini.storage.model.feed.FeedCounter
import ac.mdiq.podcini.storage.database.PodDBAdapter
import android.widget.Toast

class NewEpisodesNotification {
private var countersBefore: Map<Long, Int>? = null
Expand Down Expand Up @@ -80,7 +82,7 @@ class NewEpisodesNotification {
.setAutoCancel(true)
.build()

if (ActivityCompat.checkSelfPermission(context,
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(context,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
Expand All @@ -89,6 +91,8 @@ class NewEpisodesNotification {
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "showNotification: require POST_NOTIFICATIONS permission")
Toast.makeText(context, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}
notificationManager.notify(NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS,
Expand Down Expand Up @@ -117,7 +121,7 @@ class NewEpisodesNotification {
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.build()
if (ActivityCompat.checkSelfPermission(context,
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(context,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
Expand All @@ -126,6 +130,8 @@ class NewEpisodesNotification {
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "showGroupSummaryNotification: require POST_NOTIFICATIONS permission")
Toast.makeText(context, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}
notificationManager.notify(NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS, 0, notificationGroupSummary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import ac.mdiq.podcini.preferences.UserPreferences.showNextChapterOnFullNotifica
import ac.mdiq.podcini.preferences.UserPreferences.showPlaybackSpeedOnFullNotification
import ac.mdiq.podcini.preferences.UserPreferences.showSkipOnFullNotification
import ac.mdiq.podcini.preferences.UserPreferences.videoPlaybackSpeed
import ac.mdiq.podcini.service.download.NewEpisodesNotification
import ac.mdiq.podcini.ui.appstartintent.MainActivityStarter
import ac.mdiq.podcini.ui.appstartintent.VideoPlayerActivityStarter
import android.os.Build.VERSION_CODES
Expand Down Expand Up @@ -246,7 +247,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
if (notificationBuilder.playerStatus == PlayerStatus.PLAYING) {
notificationBuilder.playerStatus = PlayerStatus.STOPPED
val notificationManager = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission(this,
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
Expand All @@ -255,6 +256,8 @@ class PlaybackService : MediaBrowserServiceCompat() {
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "onDestroy: require POST_NOTIFICATIONS permission")
Toast.makeText(applicationContext, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}
notificationManager.notify(R.id.notification_playing, notificationBuilder.build())
Expand Down Expand Up @@ -576,7 +579,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
pendingIntentAlwaysAllow)
.setAutoCancel(true)
val notificationManager = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission(this,
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
Expand All @@ -585,6 +588,8 @@ class PlaybackService : MediaBrowserServiceCompat() {
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "displayStreamingNotAllowedNotification: require POST_NOTIFICATIONS permission")
Toast.makeText(applicationContext, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}
notificationManager.notify(R.id.notification_streaming_confirmation, builder.build())
Expand Down Expand Up @@ -1303,7 +1308,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
notificationBuilder.updatePosition(currentPosition, currentPlaybackSpeed)

val notificationManager = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission(this,
if (Build.VERSION.SDK_INT >= 33 && ActivityCompat.checkSelfPermission(this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
Expand All @@ -1312,6 +1317,8 @@ class PlaybackService : MediaBrowserServiceCompat() {
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "setupNotification: require POST_NOTIFICATIONS permission")
Toast.makeText(applicationContext, R.string.notification_permission_text, Toast.LENGTH_LONG).show()
return
}
notificationManager.notify(R.id.notification_playing, notificationBuilder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ open class AutomaticDownloadAlgorithm {
@UnstableApi open fun autoDownloadUndownloadedItems(context: Context): Runnable? {
return Runnable {
// true if we should auto download based on network status
val networkShouldAutoDl = (isAutoDownloadAllowed && isEnableAutodownload)
val networkShouldAutoDl = (isAutoDownloadAllowed)
// val networkShouldAutoDl = (isAutoDownloadAllowed && isEnableAutodownload)

// true if we should auto download based on power status
val powerShouldAutoDl = (deviceCharging(context!!) || isEnableAutodownloadOnBattery)
val powerShouldAutoDl = (deviceCharging(context) || isEnableAutodownloadOnBattery)

// we should only auto download if both network AND power are happy
if (networkShouldAutoDl && powerShouldAutoDl) {
Log.d(TAG, "Performing auto-dl of undownloaded episodes")

val candidates: MutableList<FeedItem>
val queue = getQueue()
val newItems = getEpisodes(0, Int.MAX_VALUE,
FeedItemFilter(FeedItemFilter.NEW), SortOrder.DATE_NEW_OLD)
val newItems = getEpisodes(0, Int.MAX_VALUE, FeedItemFilter(FeedItemFilter.NEW), SortOrder.DATE_NEW_OLD)
candidates = ArrayList(queue.size + newItems.size)
candidates.addAll(queue)
for (newItem in newItems) {
Expand Down
Loading

0 comments on commit da66b32

Please sign in to comment.