2022-07-20 01:01:56 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
2022-09-26 22:11:41 +05:30
|
|
|
import android.app.NotificationManager
|
2022-07-20 01:01:56 +05:30
|
|
|
import android.content.Context
|
2022-11-26 22:55:07 +05:30
|
|
|
import android.content.ContextWrapper
|
2022-09-26 22:11:41 +05:30
|
|
|
import android.content.Intent
|
|
|
|
import android.content.pm.PackageManager
|
2022-07-20 01:01:56 +05:30
|
|
|
import android.os.Bundle
|
2023-01-14 21:29:21 +05:30
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
2022-07-20 01:01:56 +05:30
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.core.os.bundleOf
|
|
|
|
import com.github.libretube.R
|
2022-09-08 23:49:44 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
2023-01-14 21:29:21 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-11-20 20:24:55 +05:30
|
|
|
import com.github.libretube.enums.PlaylistType
|
2022-08-27 18:43:24 +05:30
|
|
|
import com.github.libretube.extensions.toID
|
2022-09-20 23:30:51 +05:30
|
|
|
import com.github.libretube.ui.activities.MainActivity
|
2022-09-20 23:23:34 +05:30
|
|
|
import com.github.libretube.ui.fragments.PlayerFragment
|
2022-10-06 01:17:09 +05:30
|
|
|
import com.github.libretube.ui.views.SingleViewTouchableMotionLayout
|
2022-07-20 01:01:56 +05:30
|
|
|
|
|
|
|
object NavigationHelper {
|
2023-01-14 21:29:21 +05:30
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
|
|
|
|
2022-08-08 18:52:08 +05:30
|
|
|
fun navigateChannel(
|
|
|
|
context: Context,
|
|
|
|
channelId: String?
|
|
|
|
) {
|
2022-10-06 01:17:09 +05:30
|
|
|
if (channelId == null) return
|
|
|
|
|
2022-11-26 22:55:07 +05:30
|
|
|
val activity = unwrap(context)
|
2022-10-06 01:17:09 +05:30
|
|
|
val bundle = bundleOf(IntentData.channelId to channelId)
|
|
|
|
activity.navController.navigate(R.id.channelFragment, bundle)
|
|
|
|
try {
|
|
|
|
if (activity.binding.mainMotionLayout.progress == 0.toFloat()) {
|
|
|
|
activity.binding.mainMotionLayout.transitionToEnd()
|
|
|
|
activity.findViewById<SingleViewTouchableMotionLayout>(R.id.playerMotionLayout)
|
|
|
|
.transitionToEnd()
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
2022-10-06 01:17:09 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 22:55:07 +05:30
|
|
|
private fun unwrap(context: Context): MainActivity {
|
|
|
|
var correctContext: Context? = context
|
|
|
|
while (correctContext !is MainActivity && correctContext is ContextWrapper) {
|
|
|
|
correctContext = correctContext.baseContext
|
|
|
|
}
|
|
|
|
return correctContext as MainActivity
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:29:21 +05:30
|
|
|
/**
|
|
|
|
* Navigate to the given video using the other provided parameters as well
|
|
|
|
* If the audio only mode is enabled, play it in the background, else as a normal video
|
|
|
|
*/
|
2022-08-08 18:52:08 +05:30
|
|
|
fun navigateVideo(
|
|
|
|
context: Context,
|
|
|
|
videoId: String?,
|
2022-12-02 19:15:52 +05:30
|
|
|
playlistId: String? = null,
|
2022-12-28 21:37:35 +05:30
|
|
|
channelId: String? = null,
|
2023-01-14 21:29:21 +05:30
|
|
|
keepQueue: Boolean = false,
|
2023-01-21 16:23:52 +05:30
|
|
|
timeStamp: Long? = null,
|
|
|
|
forceVideo: Boolean = false
|
2022-08-08 18:52:08 +05:30
|
|
|
) {
|
2022-10-06 01:17:09 +05:30
|
|
|
if (videoId == null) return
|
|
|
|
|
2023-01-21 16:23:52 +05:30
|
|
|
if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) {
|
2023-01-14 21:29:21 +05:30
|
|
|
BackgroundHelper.stopBackgroundPlay(context)
|
2023-01-14 21:31:44 +05:30
|
|
|
BackgroundHelper.playOnBackground(
|
|
|
|
context,
|
|
|
|
videoId.toID(),
|
|
|
|
timeStamp,
|
|
|
|
playlistId,
|
|
|
|
channelId,
|
|
|
|
keepQueue
|
|
|
|
)
|
2023-01-14 21:29:21 +05:30
|
|
|
handler.postDelayed({
|
|
|
|
startAudioPlayer(context)
|
|
|
|
}, 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-28 21:37:35 +05:30
|
|
|
val bundle = Bundle().apply {
|
|
|
|
putString(IntentData.videoId, videoId.toID())
|
|
|
|
putString(IntentData.playlistId, playlistId)
|
|
|
|
putString(IntentData.channelId, channelId)
|
|
|
|
putBoolean(IntentData.keepQueue, keepQueue)
|
2023-01-14 21:29:21 +05:30
|
|
|
timeStamp?.let { putLong(IntentData.timeStamp, it) }
|
2022-12-28 21:37:35 +05:30
|
|
|
}
|
2022-12-02 19:15:52 +05:30
|
|
|
|
2022-10-06 01:17:09 +05:30
|
|
|
val activity = context as AppCompatActivity
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
2022-12-28 21:37:35 +05:30
|
|
|
.replace(
|
|
|
|
R.id.container,
|
|
|
|
PlayerFragment().apply {
|
|
|
|
arguments = bundle
|
|
|
|
}
|
|
|
|
)
|
2022-10-06 01:17:09 +05:30
|
|
|
.commitNow()
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
|
|
|
|
2022-08-08 18:52:08 +05:30
|
|
|
fun navigatePlaylist(
|
|
|
|
context: Context,
|
|
|
|
playlistId: String?,
|
2022-11-20 20:24:55 +05:30
|
|
|
playlistType: PlaylistType
|
2022-08-08 18:52:08 +05:30
|
|
|
) {
|
2022-10-06 01:17:09 +05:30
|
|
|
if (playlistId == null) return
|
|
|
|
|
2022-11-26 22:55:07 +05:30
|
|
|
val activity = unwrap(context)
|
2022-10-06 01:17:09 +05:30
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(IntentData.playlistId, playlistId)
|
2022-11-20 20:24:55 +05:30
|
|
|
bundle.putSerializable(IntentData.playlistType, playlistType)
|
2022-10-06 01:17:09 +05:30
|
|
|
activity.navController.navigate(R.id.playlistFragment, bundle)
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
2022-09-26 22:11:41 +05:30
|
|
|
|
2023-01-14 21:29:21 +05:30
|
|
|
/**
|
|
|
|
* Start the audio player fragment
|
|
|
|
*/
|
|
|
|
fun startAudioPlayer(context: Context) {
|
|
|
|
val activity = unwrap(context)
|
|
|
|
activity.navController.navigate(R.id.audioPlayerFragment)
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:11:41 +05:30
|
|
|
/**
|
|
|
|
* Needed due to different MainActivity Aliases because of the app icons
|
|
|
|
*/
|
|
|
|
fun restartMainActivity(context: Context) {
|
|
|
|
// kill player notification
|
|
|
|
val nManager = context
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
nManager.cancelAll()
|
|
|
|
// start a new Intent of the app
|
|
|
|
val pm: PackageManager = context.packageManager
|
|
|
|
val intent = pm.getLaunchIntentForPackage(context.packageName)
|
|
|
|
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
|
context.startActivity(intent)
|
|
|
|
// kill the old application
|
|
|
|
android.os.Process.killProcess(android.os.Process.myPid())
|
|
|
|
}
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|