Skip to content

Commit

Permalink
[feature|fix|optimize|build] Support background playback; fix an issu…
Browse files Browse the repository at this point in the history
…e that PiP control button can't be updated; optimize player implementation code; update dependencies
  • Loading branch information
SkyD666 committed Dec 10, 2024
1 parent 9881815 commit 5a15e27
Show file tree
Hide file tree
Showing 40 changed files with 1,376 additions and 671 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = 24
versionName = "2.1-beta15"
versionName = "2.1-beta16"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -190,6 +190,7 @@ dependencies {
implementation(libs.androidx.paging.compose)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.androidx.profileinstaller)
implementation(libs.androidx.media)

implementation(libs.material)
implementation(libs.material.kolor)
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
Expand Down Expand Up @@ -148,9 +150,10 @@
</intent-filter>
</activity>
<activity
android:name=".ui.activity.PlayActivity"
android:name=".ui.activity.player.PlayActivity"
android:alwaysRetainTaskState="true"
android:autoRemoveFromRecents="true"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|uiMode|screenSize|screenLayout|smallestScreenSize"
android:exported="true"
android:label="@string/play_activity_label"
android:launchMode="singleTask"
Expand All @@ -171,6 +174,15 @@
</activity>
<activity android:name=".ui.activity.CrashActivity" />

<service
android:name=".ui.mpv.service.PlayerService"
android:exported="false"
android:foregroundServiceType="mediaPlayback"/>

<receiver
android:name=".ui.mpv.service.PlayerNotificationReceiver"
android:exported="false" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/java/com/skyd/anivu/ext/ContextExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import android.content.ContextWrapper
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Point
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.view.Window
import androidx.core.content.ContextCompat
import androidx.core.content.pm.PackageInfoCompat
import coil3.ImageLoader
import coil3.gif.AnimatedImageDecoder
Expand Down Expand Up @@ -56,24 +54,6 @@ val Context.tryWindow: Window?
val Context.screenIsLand: Boolean
get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE

fun Context.screenHeight(includeVirtualKey: Boolean): Int {
val display = ContextCompat.getDisplayOrDefault(this)
val outPoint = Point()
// 可能有虚拟按键的情况
if (includeVirtualKey) display.getRealSize(outPoint)
else display.getSize(outPoint)
return outPoint.y
}

fun Context.screenWidth(includeVirtualKey: Boolean): Int {
val display = ContextCompat.getDisplayOrDefault(this)
val outPoint = Point()
// 可能有虚拟按键的情况
if (includeVirtualKey) display.getRealSize(outPoint)
else display.getSize(outPoint)
return outPoint.x
}

fun Context.getAppVersionName(): String {
var appVersionName = ""
try {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/skyd/anivu/ext/IOExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.provider.DocumentsContract
import android.provider.OpenableColumns
import android.webkit.URLUtil
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile
import com.skyd.anivu.R
import com.skyd.anivu.appContext
Expand Down Expand Up @@ -103,7 +102,7 @@ private fun Uri.openChooser(
if (context.tryActivity == null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
ContextCompat.startActivity(context, intent, null)
context.startActivity(intent, null)
} catch (e: Exception) {
e.printStackTrace()
context.getString(R.string.failed_msg, e.message).showToast(Toast.LENGTH_LONG)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/PreferenceExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.skyd.anivu.model.preference.data.autodelete.AutoDeleteArticleKeepFavo
import com.skyd.anivu.model.preference.data.autodelete.AutoDeleteArticleKeepUnreadPreference
import com.skyd.anivu.model.preference.data.autodelete.UseAutoDeletePreference
import com.skyd.anivu.model.preference.data.medialib.MediaLibLocationPreference
import com.skyd.anivu.model.preference.player.BackgroundPlayPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerAutoPipPreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
Expand Down Expand Up @@ -117,6 +118,7 @@ fun Preferences.toSettings(): Settings {
playerMaxCacheSize = PlayerMaxCacheSizePreference.fromPreferences(this),
playerMaxBackCacheSize = PlayerMaxBackCacheSizePreference.fromPreferences(this),
playerSeekOption = PlayerSeekOptionPreference.fromPreferences(this),
backgroundPlay = BackgroundPlayPreference.fromPreferences(this),

// Data
useAutoDelete = UseAutoDeletePreference.fromPreferences(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface MediaPlayHistoryDao {
WHERE ${MediaPlayHistoryBean.PATH_COLUMN} = :path
"""
)
fun getMediaPlayHistory(path: String): MediaPlayHistoryBean
fun getMediaPlayHistory(path: String): MediaPlayHistoryBean?

@Transaction
@Query("SELECT * FROM $MEDIA_PLAY_HISTORY_TABLE_NAME")
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/skyd/anivu/model/preference/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.skyd.anivu.model.preference.data.autodelete.AutoDeleteArticleKeepFavo
import com.skyd.anivu.model.preference.data.autodelete.AutoDeleteArticleKeepUnreadPreference
import com.skyd.anivu.model.preference.data.autodelete.UseAutoDeletePreference
import com.skyd.anivu.model.preference.data.medialib.MediaLibLocationPreference
import com.skyd.anivu.model.preference.player.BackgroundPlayPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerAutoPipPreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
Expand Down Expand Up @@ -77,6 +78,7 @@ import com.skyd.anivu.ui.local.LocalAutoDeleteArticleBefore
import com.skyd.anivu.ui.local.LocalAutoDeleteArticleFrequency
import com.skyd.anivu.ui.local.LocalAutoDeleteArticleKeepFavorite
import com.skyd.anivu.ui.local.LocalAutoDeleteArticleKeepUnread
import com.skyd.anivu.ui.local.LocalBackgroundPlay
import com.skyd.anivu.ui.local.LocalDarkMode
import com.skyd.anivu.ui.local.LocalDateStyle
import com.skyd.anivu.ui.local.LocalDeduplicateTitleInDesc
Expand Down Expand Up @@ -175,6 +177,7 @@ data class Settings(
val playerMaxCacheSize: Long = PlayerMaxCacheSizePreference.default,
val playerMaxBackCacheSize: Long = PlayerMaxBackCacheSizePreference.default,
val playerSeekOption: String = PlayerSeekOptionPreference.default,
val backgroundPlay: Boolean = BackgroundPlayPreference.default,
// Data
val useAutoDelete: Boolean = UseAutoDeletePreference.default,
val autoDeleteArticleFrequency: Long = AutoDeleteArticleFrequencyPreference.default,
Expand Down Expand Up @@ -251,6 +254,7 @@ fun SettingsProvider(
LocalPlayerMaxCacheSize provides settings.playerMaxCacheSize,
LocalPlayerMaxBackCacheSize provides settings.playerMaxBackCacheSize,
LocalPlayerSeekOption provides settings.playerSeekOption,
LocalBackgroundPlay provides settings.backgroundPlay,
// Data
LocalUseAutoDelete provides settings.useAutoDelete,
LocalAutoDeleteArticleFrequency provides settings.autoDeleteArticleFrequency,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skyd.anivu.model.preference.player

import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import com.skyd.anivu.base.BasePreference
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object BackgroundPlayPreference : BasePreference<Boolean> {
private const val BACKGROUND_PLAY = "backgroundPlay"
override val default = false

val key = booleanPreferencesKey(BACKGROUND_PLAY)

fun put(context: Context, scope: CoroutineScope, value: Boolean) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
}
}

override fun fromPreferences(preferences: Preferences): Boolean = preferences[key] ?: default
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PlayerRepository @Inject constructor(

fun requestLastPlayPosition(path: String): Flow<Long> {
return flow {
emit(mediaPlayHistoryDao.getMediaPlayHistory(path).lastPlayPosition)
emit(mediaPlayHistoryDao.getMediaPlayHistory(path)?.lastPlayPosition ?: 0L)
}.flowOn(Dispatchers.IO)
}
}
110 changes: 0 additions & 110 deletions app/src/main/java/com/skyd/anivu/ui/activity/PlayActivity.kt

This file was deleted.

Loading

0 comments on commit 5a15e27

Please sign in to comment.