2022-02-01 21:22:06 +05:30
|
|
|
package com.github.libretube
|
2021-12-09 18:01:40 +05:30
|
|
|
|
2022-03-15 14:21:31 +05:30
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Context
|
2022-05-06 00:21:34 +05:30
|
|
|
import android.content.Intent
|
2022-02-10 13:52:05 +05:30
|
|
|
import android.content.pm.ActivityInfo
|
2021-12-16 03:54:40 +05:30
|
|
|
import android.content.res.Configuration
|
2022-05-09 23:24:58 +05:30
|
|
|
import android.net.ConnectivityManager
|
2022-03-14 19:40:14 +05:30
|
|
|
import android.net.Uri
|
2022-02-10 13:45:09 +05:30
|
|
|
import android.os.Build
|
2021-12-09 18:01:40 +05:30
|
|
|
import android.os.Bundle
|
2022-03-15 10:08:33 +05:30
|
|
|
import android.os.Handler
|
2022-02-02 21:40:37 +05:30
|
|
|
import android.util.Log
|
2022-05-18 21:35:54 +05:30
|
|
|
import android.util.TypedValue
|
2022-02-10 13:45:09 +05:30
|
|
|
import android.view.*
|
2022-03-15 14:21:31 +05:30
|
|
|
import android.view.inputmethod.InputMethodManager
|
2022-05-09 23:24:58 +05:30
|
|
|
import android.widget.Button
|
2022-02-10 16:39:34 +05:30
|
|
|
import android.widget.LinearLayout
|
2022-05-17 15:38:14 +05:30
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2022-01-28 18:31:41 +05:30
|
|
|
import androidx.appcompat.widget.Toolbar
|
2021-12-16 03:54:40 +05:30
|
|
|
import androidx.constraintlayout.motion.widget.MotionLayout
|
2022-02-10 16:39:34 +05:30
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout
|
2022-03-14 23:58:44 +05:30
|
|
|
import androidx.core.os.bundleOf
|
2022-02-02 18:08:57 +05:30
|
|
|
import androidx.core.text.HtmlCompat
|
2021-12-12 17:38:23 +05:30
|
|
|
import androidx.fragment.app.Fragment
|
2022-02-02 21:40:37 +05:30
|
|
|
import androidx.navigation.NavController
|
2021-12-09 18:25:32 +05:30
|
|
|
import androidx.navigation.findNavController
|
|
|
|
import androidx.navigation.ui.setupWithNavController
|
2022-02-08 19:57:13 +05:30
|
|
|
import androidx.preference.PreferenceManager
|
2022-05-17 15:38:14 +05:30
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
2022-02-02 18:08:57 +05:30
|
|
|
import com.google.android.material.color.DynamicColors
|
2021-12-09 18:01:40 +05:30
|
|
|
|
|
|
|
class MainActivity : AppCompatActivity() {
|
2022-03-14 19:40:14 +05:30
|
|
|
val TAG = "MainActivity"
|
2021-12-28 01:37:07 +05:30
|
|
|
lateinit var bottomNavigationView: BottomNavigationView
|
2022-01-28 18:31:41 +05:30
|
|
|
lateinit var toolbar: Toolbar
|
2022-05-20 03:23:55 +05:30
|
|
|
lateinit var navController: NavController
|
2022-03-27 11:49:17 +05:30
|
|
|
|
2021-12-09 18:01:40 +05:30
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
2022-05-20 03:43:02 +05:30
|
|
|
DynamicColors.applyToActivityIfAvailable(this)
|
2021-12-09 18:01:40 +05:30
|
|
|
super.onCreate(savedInstanceState)
|
2022-02-08 19:57:13 +05:30
|
|
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
2022-03-27 11:49:17 +05:30
|
|
|
RetrofitInstance.url = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!!
|
2022-05-16 15:41:22 +05:30
|
|
|
SponsorBlockSettings.sponsorBlockEnabled = sharedPreferences.getBoolean("sponsorblock_enabled_key", false)
|
|
|
|
SponsorBlockSettings.introEnabled = sharedPreferences.getBoolean("intro_category_key", false)
|
|
|
|
SponsorBlockSettings.selfPromoEnabled = sharedPreferences.getBoolean("selfpromo_category_key", false)
|
|
|
|
SponsorBlockSettings.interactionEnabled = sharedPreferences.getBoolean("interaction_category_key", false)
|
|
|
|
SponsorBlockSettings.sponsorsEnabled = sharedPreferences.getBoolean("sponsors_category_key", false)
|
|
|
|
SponsorBlockSettings.outroEnabled = sharedPreferences.getBoolean("outro_category_key", false)
|
|
|
|
|
2022-05-19 21:52:19 +05:30
|
|
|
updateAccentColor(this)
|
2022-05-19 22:35:57 +05:30
|
|
|
updateThemeMode(this)
|
2022-05-20 03:23:55 +05:30
|
|
|
updateLanguage(this)
|
2022-05-09 23:24:58 +05:30
|
|
|
|
|
|
|
val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
2022-05-20 03:23:55 +05:30
|
|
|
val networkInfo = connectivityManager.activeNetworkInfo
|
2022-05-09 23:24:58 +05:30
|
|
|
val isConnected = networkInfo != null && networkInfo.isConnected
|
2021-12-28 01:37:07 +05:30
|
|
|
|
2022-05-19 22:35:57 +05:30
|
|
|
if (!isConnected) {
|
2022-05-09 23:24:58 +05:30
|
|
|
setContentView(R.layout.activity_nointernet)
|
|
|
|
findViewById<Button>(R.id.retry_button).setOnClickListener() {
|
|
|
|
recreate()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setContentView(R.layout.activity_main)
|
|
|
|
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
|
|
|
|
|
|
|
bottomNavigationView = findViewById(R.id.bottomNav)
|
|
|
|
navController = findNavController(R.id.fragment)
|
|
|
|
bottomNavigationView.setupWithNavController(navController)
|
|
|
|
|
2022-05-14 03:39:33 +05:30
|
|
|
when (sharedPreferences.getString("default_tab", "home")!!) {
|
|
|
|
"home" -> navController.navigate(R.id.home2)
|
|
|
|
"subscriptions" -> navController.navigate(R.id.subscriptions)
|
|
|
|
"library" -> navController.navigate(R.id.library)
|
|
|
|
}
|
|
|
|
|
2022-05-19 22:35:57 +05:30
|
|
|
bottomNavigationView.setBackgroundColor(0) // otherwise Navbar Theme doesn't change
|
2022-05-09 23:24:58 +05:30
|
|
|
bottomNavigationView.setOnItemSelectedListener {
|
|
|
|
when (it.itemId) {
|
|
|
|
R.id.home2 -> {
|
|
|
|
navController.backQueue.clear()
|
|
|
|
navController.navigate(R.id.home2)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.subscriptions -> {
|
2022-05-20 03:23:55 +05:30
|
|
|
// navController.backQueue.clear()
|
2022-05-09 23:24:58 +05:30
|
|
|
navController.navigate(R.id.subscriptions)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.library -> {
|
2022-05-20 03:23:55 +05:30
|
|
|
// navController.backQueue.clear()
|
2022-05-09 23:24:58 +05:30
|
|
|
navController.navigate(R.id.library)
|
|
|
|
true
|
|
|
|
}
|
2022-02-08 20:47:49 +05:30
|
|
|
}
|
2022-05-09 23:24:58 +05:30
|
|
|
false
|
2022-02-08 20:47:49 +05:30
|
|
|
}
|
2022-01-28 22:40:32 +05:30
|
|
|
|
2022-05-09 23:24:58 +05:30
|
|
|
toolbar = findViewById(R.id.toolbar)
|
2022-05-18 21:35:54 +05:30
|
|
|
val typedValue = TypedValue()
|
2022-05-20 01:32:22 +05:30
|
|
|
this.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
|
2022-05-18 21:35:54 +05:30
|
|
|
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
|
2022-05-09 23:24:58 +05:30
|
|
|
val appName = HtmlCompat.fromHtml(
|
|
|
|
"Libre<span style='color:$hexColor';>Tube</span>",
|
|
|
|
HtmlCompat.FROM_HTML_MODE_COMPACT
|
|
|
|
)
|
|
|
|
toolbar.title = appName
|
2022-02-02 18:08:57 +05:30
|
|
|
|
2022-05-20 03:23:55 +05:30
|
|
|
toolbar.setNavigationOnClickListener {
|
|
|
|
// settings activity stuff
|
|
|
|
val intent = Intent(this, SettingsActivity::class.java)
|
|
|
|
startActivity(intent)
|
|
|
|
true
|
|
|
|
}
|
2022-01-28 22:40:32 +05:30
|
|
|
|
2022-05-09 23:24:58 +05:30
|
|
|
toolbar.setOnMenuItemClickListener {
|
|
|
|
when (it.itemId) {
|
|
|
|
R.id.action_search -> {
|
|
|
|
navController.navigate(R.id.searchFragment)
|
|
|
|
true
|
|
|
|
}
|
2022-01-28 18:31:41 +05:30
|
|
|
}
|
2022-05-09 23:24:58 +05:30
|
|
|
false
|
2022-01-28 18:31:41 +05:30
|
|
|
}
|
2021-12-28 01:37:07 +05:30
|
|
|
}
|
2022-03-15 10:08:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
2022-03-14 19:40:14 +05:30
|
|
|
val action: String? = intent?.action
|
|
|
|
val data: Uri? = intent?.data
|
2022-05-20 03:23:55 +05:30
|
|
|
Log.d(TAG, "dafaq" + data.toString())
|
2022-03-14 19:40:14 +05:30
|
|
|
|
|
|
|
if (data != null) {
|
2022-05-20 03:23:55 +05:30
|
|
|
Log.d("dafaq", data.host + " ${data.path} ")
|
|
|
|
if (data.host != null) {
|
|
|
|
if (data.path != null) {
|
|
|
|
// channel
|
|
|
|
if (data.path!!.contains("/channel/") || data.path!!.contains("/c/") || data.path!!.contains("/user/")) {
|
|
|
|
var channel = data.path
|
|
|
|
channel = channel!!.replace("/c/", "")
|
|
|
|
channel = channel!!.replace("/user/", "")
|
|
|
|
val bundle = bundleOf("channel_id" to channel)
|
|
|
|
navController.navigate(R.id.channel, bundle)
|
|
|
|
} else if (data.path!!.contains("/playlist")) {
|
|
|
|
var playlist = data.query!!
|
|
|
|
if (playlist.contains("&")) {
|
|
|
|
var playlists = playlist.split("&")
|
|
|
|
for (v in playlists) {
|
|
|
|
if (v.contains("list=")) {
|
|
|
|
playlist = v
|
|
|
|
break
|
2022-03-15 10:08:33 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
}
|
|
|
|
playlist = playlist.replace("list=", "")
|
|
|
|
val bundle = bundleOf("playlist_id" to playlist)
|
|
|
|
navController.navigate(R.id.playlistFragment, bundle)
|
|
|
|
} else if (data.path!!.contains("/shorts/") || data.path!!.contains("/embed/") || data.path!!.contains("/v/")) {
|
|
|
|
var watch = data.path!!.replace("/shorts/", "").replace("/v/", "").replace("/embed/", "")
|
|
|
|
var bundle = Bundle()
|
|
|
|
bundle.putString("videoId", watch)
|
|
|
|
var frag = PlayerFragment()
|
|
|
|
frag.arguments = bundle
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.replace(R.id.container, frag)
|
|
|
|
.commitNow()
|
|
|
|
Handler().postDelayed({
|
|
|
|
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
|
|
|
|
motionLayout.transitionToEnd()
|
|
|
|
motionLayout.transitionToStart()
|
|
|
|
}, 100)
|
|
|
|
} else if (data.path!!.contains("/watch") && data.query != null) {
|
|
|
|
Log.d("dafaq", data.query!!)
|
|
|
|
var watch = data.query!!
|
|
|
|
if (watch.contains("&")) {
|
|
|
|
var watches = watch.split("&")
|
|
|
|
for (v in watches) {
|
|
|
|
if (v.contains("v=")) {
|
|
|
|
watch = v
|
|
|
|
break
|
2022-03-14 23:58:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
var bundle = Bundle()
|
|
|
|
bundle.putString("videoId", watch.replace("v=", ""))
|
|
|
|
var frag = PlayerFragment()
|
|
|
|
frag.arguments = bundle
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.replace(R.id.container, frag)
|
|
|
|
.commitNow()
|
|
|
|
Handler().postDelayed({
|
|
|
|
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
|
|
|
|
motionLayout.transitionToEnd()
|
|
|
|
motionLayout.transitionToStart()
|
|
|
|
}, 100)
|
|
|
|
} else {
|
|
|
|
var watch = data.path!!.replace("/", "")
|
|
|
|
var bundle = Bundle()
|
|
|
|
bundle.putString("videoId", watch)
|
|
|
|
var frag = PlayerFragment()
|
|
|
|
frag.arguments = bundle
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
|
.replace(R.id.container, frag)
|
|
|
|
.commitNow()
|
|
|
|
Handler().postDelayed({
|
|
|
|
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
|
|
|
|
motionLayout.transitionToEnd()
|
|
|
|
motionLayout.transitionToStart()
|
|
|
|
}, 100)
|
2022-03-14 23:58:44 +05:30
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
}
|
2022-03-14 23:58:44 +05:30
|
|
|
}
|
2022-03-14 19:40:14 +05:30
|
|
|
}
|
2021-12-09 18:01:40 +05:30
|
|
|
}
|
2021-12-16 03:54:40 +05:30
|
|
|
|
2022-01-28 23:16:11 +05:30
|
|
|
override fun onBackPressed() {
|
2022-05-20 03:23:55 +05:30
|
|
|
try {
|
2022-02-02 23:58:24 +05:30
|
|
|
val mainMotionLayout = findViewById<MotionLayout>(R.id.mainMotionLayout)
|
2022-05-20 03:23:55 +05:30
|
|
|
if (mainMotionLayout.progress == 0.toFloat()) {
|
2022-02-02 23:58:24 +05:30
|
|
|
mainMotionLayout.transitionToEnd()
|
2022-05-20 03:23:55 +05:30
|
|
|
findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
|
2022-02-10 16:39:34 +05:30
|
|
|
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
|
|
|
|
motionLayout.transitionToEnd()
|
|
|
|
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
|
|
|
with(motionLayout) {
|
|
|
|
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
|
2022-05-20 03:23:55 +05:30
|
|
|
enableTransition(R.id.yt_transition, true)
|
2022-02-10 16:39:34 +05:30
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
|
|
|
|
isFullScreen = false
|
|
|
|
} else {
|
2022-02-02 23:58:24 +05:30
|
|
|
navController.popBackStack()
|
2022-05-20 03:23:55 +05:30
|
|
|
if (navController.currentBackStackEntry == null && (parent as View).id != R.id.settings) {
|
2022-05-17 15:38:14 +05:30
|
|
|
super.onBackPressed()
|
2022-02-10 16:39:34 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
} catch (e: Exception) {
|
2022-02-02 23:58:24 +05:30
|
|
|
navController.popBackStack()
|
2022-05-14 02:43:50 +05:30
|
|
|
moveTaskToBack(true)
|
2022-02-02 23:58:24 +05:30
|
|
|
}
|
2022-01-28 23:16:11 +05:30
|
|
|
}
|
2021-12-16 03:54:40 +05:30
|
|
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
|
|
|
super.onConfigurationChanged(newConfig)
|
|
|
|
val orientation = newConfig.orientation
|
2022-01-28 22:40:32 +05:30
|
|
|
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
|
2021-12-16 03:54:40 +05:30
|
|
|
println("Portrait")
|
2022-02-13 17:23:04 +05:30
|
|
|
unsetFullscreen()
|
2021-12-16 03:54:40 +05:30
|
|
|
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
|
|
println("Landscape")
|
2022-02-13 17:23:04 +05:30
|
|
|
setFullscreen()
|
2022-02-10 13:45:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
private fun setFullscreen() {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
|
|
window.attributes.layoutInDisplayCutoutMode =
|
|
|
|
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
|
|
window.setDecorFitsSystemWindows(false)
|
|
|
|
window.insetsController?.apply {
|
|
|
|
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
|
|
|
|
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
2022-01-29 18:41:00 +05:30
|
|
|
}
|
2022-02-10 13:45:09 +05:30
|
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
@Suppress("DEPRECATION")
|
2022-05-20 03:23:55 +05:30
|
|
|
window.decorView.systemUiVisibility = (
|
|
|
|
View.SYSTEM_UI_FLAG_FULLSCREEN
|
2022-02-10 13:45:09 +05:30
|
|
|
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
|
|
|
or View.SYSTEM_UI_FLAG_IMMERSIVE
|
|
|
|
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
|
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
2022-05-20 03:23:55 +05:30
|
|
|
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
|
)
|
2022-02-10 13:45:09 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-20 03:23:55 +05:30
|
|
|
private fun unsetFullscreen() {
|
2022-02-10 13:45:09 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
|
|
window.attributes.layoutInDisplayCutoutMode =
|
|
|
|
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
|
|
|
|
}
|
2021-12-16 03:54:40 +05:30
|
|
|
|
2022-02-10 13:45:09 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
2022-03-05 12:05:15 +05:30
|
|
|
window.setDecorFitsSystemWindows(true)
|
2022-02-10 13:45:09 +05:30
|
|
|
window.insetsController?.apply {
|
|
|
|
show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
|
|
|
|
systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
|
|
|
|
}
|
|
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
|
2021-12-16 03:54:40 +05:30
|
|
|
}
|
|
|
|
}
|
2022-03-15 14:21:31 +05:30
|
|
|
|
2022-05-14 16:08:28 +05:30
|
|
|
override fun onUserLeaveHint() {
|
|
|
|
super.onUserLeaveHint()
|
2022-05-14 17:01:33 +05:30
|
|
|
supportFragmentManager.fragments.forEach { fragment ->
|
|
|
|
(fragment as? PlayerFragment)?.onUserLeaveHint()
|
2022-05-14 16:08:28 +05:30
|
|
|
}
|
|
|
|
}
|
2022-03-15 14:21:31 +05:30
|
|
|
}
|
|
|
|
fun Fragment.hideKeyboard() {
|
|
|
|
view?.let { activity?.hideKeyboard(it) }
|
2022-02-02 18:08:57 +05:30
|
|
|
}
|
2022-03-15 14:21:31 +05:30
|
|
|
|
|
|
|
fun Activity.hideKeyboard() {
|
|
|
|
hideKeyboard(currentFocus ?: View(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Context.hideKeyboard(view: View) {
|
|
|
|
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
|
|
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
|
2022-05-18 20:02:33 +05:30
|
|
|
}
|