Merge pull request #508 from Bnyro/master

small cleanup
This commit is contained in:
Bnyro 2022-06-14 19:09:34 +02:00 committed by GitHub
commit 0bc3a40d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 22 deletions

View File

@ -11,7 +11,6 @@ import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.ui.PlayerNotificationManager
import gen._base._base_java__rjava_resources.srcjar.R.id.title
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -45,14 +44,16 @@ class BackgroundMode {
*/ */
private lateinit var playerNotification: PlayerNotificationManager private lateinit var playerNotification: PlayerNotificationManager
/**
* The [AudioAttributes] handle the audio focus of the [player]
*/
private lateinit var audioAttributes: AudioAttributes
/** /**
* Initializes the [player] with the [MediaItem]. * Initializes the [player] with the [MediaItem].
*/ */
private fun initializePlayer(c: Context) { private fun initializePlayer(c: Context) {
/** audioAttributes = AudioAttributes.Builder()
* The [audioAttributes] handle the audio focus of the [player]
*/
val audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA) .setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MUSIC) .setContentType(C.CONTENT_TYPE_MUSIC)
.build() .build()
@ -68,12 +69,16 @@ class BackgroundMode {
/** /**
* Initializes the [playerNotification] attached to the [player] and shows it. * Initializes the [playerNotification] attached to the [player] and shows it.
*/ */
private fun initializePlayerNotification(c: Context, streams: Streams) { private fun initializePlayerNotification(c: Context) {
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(c, 1, "background_mode") .Builder(c, 1, "background_mode")
// set the description of the notification // set the description of the notification
.setMediaDescriptionAdapter( .setMediaDescriptionAdapter(
DescriptionAdapter(streams.title!!, streams.uploader!!, streams.thumbnailUrl!!) DescriptionAdapter(
response?.title!!,
response?.uploader!!,
response?.thumbnailUrl!!
)
) )
.build() .build()
playerNotification.apply { playerNotification.apply {
@ -113,7 +118,7 @@ class BackgroundMode {
job.join() job.join()
initializePlayer(c) initializePlayer(c)
initializePlayerNotification(c, response!!) initializePlayerNotification(c)
player?.apply { player?.apply {
playWhenReady = playWhenReadyPlayer playWhenReady = playWhenReadyPlayer

View File

@ -0,0 +1,7 @@
package com.github.libretube
const val GITHUB_API_URL = "https://api.github.com/repos/libre-tube/LibreTube/releases/latest"
const val WEBSITE_URL = "https://libre-tube.github.io/"
const val AUTHORS_URL = "https://github.com/libre-tube/LibreTube/graphs/contributors"
const val DONATE_URL = "https://libre-tube.github.io/#donate"
const val CONTRIBUTING_URL = "https://github.com/libre-tube/LibreTube#donate"

View File

@ -0,0 +1,7 @@
package com.github.libretube.obj
// data class for the update info, required to return the data
data class UpdateInfo(
val updateUrl: String,
val tagName: String
)

View File

@ -11,8 +11,12 @@ import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.github.libretube.AUTHORS_URL
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
import com.github.libretube.CONTRIBUTING_URL
import com.github.libretube.DONATE_URL
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.WEBSITE_URL
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class AboutFragment : Fragment() { class AboutFragment : Fragment() {
@ -34,19 +38,19 @@ class AboutFragment : Fragment() {
val website = view.findViewById<LinearLayout>(R.id.website) val website = view.findViewById<LinearLayout>(R.id.website)
website.setOnClickListener { website.setOnClickListener {
openLinkFromHref("https://libre-tube.github.io/") openLinkFromHref(WEBSITE_URL)
} }
val authors = view.findViewById<LinearLayout>(R.id.authors) val authors = view.findViewById<LinearLayout>(R.id.authors)
authors.setOnClickListener { authors.setOnClickListener {
openLinkFromHref("https://github.com/libre-tube/LibreTube/graphs/contributors") openLinkFromHref(AUTHORS_URL)
} }
val donate = view.findViewById<LinearLayout>(R.id.donate) val donate = view.findViewById<LinearLayout>(R.id.donate)
donate.setOnClickListener { donate.setOnClickListener {
openLinkFromHref("https://libre-tube.github.io/#donate") openLinkFromHref(DONATE_URL)
} }
val contributing = view.findViewById<LinearLayout>(R.id.contributing) val contributing = view.findViewById<LinearLayout>(R.id.contributing)
contributing.setOnClickListener { contributing.setOnClickListener {
openLinkFromHref("https://github.com/libre-tube/LibreTube") openLinkFromHref(CONTRIBUTING_URL)
} }
val license = view.findViewById<LinearLayout>(R.id.license) val license = view.findViewById<LinearLayout>(R.id.license)
license.setOnClickListener { license.setOnClickListener {

View File

@ -7,7 +7,10 @@ import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.ui.PlayerNotificationManager
import java.net.URL import java.net.URL
// used to show title and thumbnail of the video in the notification /**
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
*/
class DescriptionAdapter( class DescriptionAdapter(
private val title: String, private val title: String,
private val channelName: String, private val channelName: String,

View File

@ -3,8 +3,10 @@ package com.github.libretube.util
import android.util.Log import android.util.Log
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
import com.github.libretube.GITHUB_API_URL
import com.github.libretube.dialogs.NoUpdateAvailableDialog import com.github.libretube.dialogs.NoUpdateAvailableDialog
import com.github.libretube.dialogs.UpdateAvailableDialog import com.github.libretube.dialogs.UpdateAvailableDialog
import com.github.libretube.obj.UpdateInfo
import java.io.BufferedReader import java.io.BufferedReader
import java.io.InputStreamReader import java.io.InputStreamReader
import java.net.URL import java.net.URL
@ -40,7 +42,7 @@ fun checkUpdate(childFragmentManager: FragmentManager) {
} }
fun getUpdateInfo(): UpdateInfo? { fun getUpdateInfo(): UpdateInfo? {
val latest = URL("https://api.github.com/repos/libre-tube/LibreTube/releases/latest") val latest = URL(GITHUB_API_URL)
val json = StringBuilder() val json = StringBuilder()
val urlConnection: HttpsURLConnection? val urlConnection: HttpsURLConnection?
urlConnection = latest.openConnection() as HttpsURLConnection urlConnection = latest.openConnection() as HttpsURLConnection
@ -49,7 +51,7 @@ fun getUpdateInfo(): UpdateInfo? {
var line: String? var line: String?
while (br.readLine().also { line = it } != null) json.append(line) while (br.readLine().also { line = it } != null) json.append(line)
// Parse and return json data // Parse and return the json data
val jsonRoot = JSONObject(json.toString()) val jsonRoot = JSONObject(json.toString())
if (jsonRoot.has("tag_name") && if (jsonRoot.has("tag_name") &&
jsonRoot.has("html_url") && jsonRoot.has("html_url") &&
@ -63,7 +65,7 @@ fun getUpdateInfo(): UpdateInfo? {
val name = jsonAsset.getString("name") val name = jsonAsset.getString("name")
if (name.endsWith(".apk")) { if (name.endsWith(".apk")) {
val tagName = jsonRoot.getString("name") val tagName = jsonRoot.getString("name")
Log.i("", "Lastest version: $tagName") Log.i("", "Latest version: $tagName")
return UpdateInfo(updateUrl, tagName) return UpdateInfo(updateUrl, tagName)
} }
} }
@ -71,9 +73,3 @@ fun getUpdateInfo(): UpdateInfo? {
} }
return null return null
} }
// data class for the update info, required to return the data
data class UpdateInfo(
val updateUrl: String,
val tagName: String
)