LibreTube/app/src/main/java/com/github/libretube/activities/MainActivity.kt

377 lines
14 KiB
Kotlin
Raw Normal View History

2022-07-01 20:24:20 +05:30
package com.github.libretube.activities
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
2021-12-16 03:54:40 +05:30
import android.content.res.Configuration
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-06-14 14:16:35 +05:30
import android.os.Looper
2022-02-02 21:40:37 +05:30
import android.util.Log
2022-05-21 13:32:04 +05:30
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
2022-03-15 14:21:31 +05:30
import android.view.inputmethod.InputMethodManager
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
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
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-07-12 20:57:56 +05:30
import com.github.libretube.Globals
2022-07-12 01:30:56 +05:30
import com.github.libretube.PIPED_API_URL
2022-07-01 20:24:20 +05:30
import com.github.libretube.R
2022-07-01 00:35:31 +05:30
import com.github.libretube.databinding.ActivityMainBinding
2022-06-03 00:40:16 +05:30
import com.github.libretube.fragments.PlayerFragment
2022-07-02 21:53:24 +05:30
import com.github.libretube.preferences.PreferenceHelper
2022-07-17 21:48:39 +05:30
import com.github.libretube.preferences.PreferenceKeys
2022-06-28 20:02:26 +05:30
import com.github.libretube.services.ClosingService
2022-07-06 19:54:24 +05:30
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.CronetHelper
2022-06-07 13:05:49 +05:30
import com.github.libretube.util.LocaleHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-06-07 13:05:49 +05:30
import com.github.libretube.util.ThemeHelper
2022-07-08 20:12:55 +05:30
import com.google.android.material.elevation.SurfaceColors
2022-07-10 21:23:17 +05:30
import com.google.android.material.navigation.NavigationBarView
2021-12-09 18:01:40 +05:30
class MainActivity : AppCompatActivity() {
2022-03-14 19:40:14 +05:30
val TAG = "MainActivity"
2022-06-18 14:00:24 +05:30
2022-07-01 00:35:31 +05:30
lateinit var binding: ActivityMainBinding
2022-05-20 03:23:55 +05:30
lateinit var navController: NavController
2022-07-05 21:24:42 +05:30
private var startFragmentId = R.id.homeFragment
2021-12-09 18:01:40 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
2022-07-12 21:09:00 +05:30
// set the app theme (e.g. Material You)
2022-07-06 20:48:09 +05:30
ThemeHelper.updateTheme(this)
2022-07-12 21:09:00 +05:30
2022-07-06 20:27:47 +05:30
// set the language
2022-07-06 20:48:09 +05:30
LocaleHelper.updateLanguage(this)
2022-07-06 20:27:47 +05:30
2021-12-09 18:01:40 +05:30
super.onCreate(savedInstanceState)
2022-06-28 20:02:26 +05:30
2022-06-28 20:23:18 +05:30
// start service that gets called on closure
2022-06-28 20:02:26 +05:30
startService(Intent(this, ClosingService::class.java))
CronetHelper.initCronet(this.applicationContext)
2022-06-26 14:25:05 +05:30
2022-05-21 13:32:04 +05:30
RetrofitInstance.url =
2022-07-17 21:48:39 +05:30
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)!!
2022-07-03 15:09:25 +05:30
// set auth instance
RetrofitInstance.authUrl =
2022-07-17 21:48:39 +05:30
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
2022-07-03 13:50:53 +05:30
PreferenceHelper.getString(
2022-07-17 21:48:39 +05:30
PreferenceKeys.AUTH_INSTANCE,
2022-07-12 01:30:56 +05:30
PIPED_API_URL
2022-07-03 13:50:53 +05:30
)!!
} else {
RetrofitInstance.url
}
2022-05-16 15:41:22 +05:30
2022-07-16 01:07:44 +05:30
// save whether the data saver mode is enabled
Globals.dataSaverModeEnabled = PreferenceHelper.getBoolean(
2022-07-17 21:48:39 +05:30
PreferenceKeys.DATA_SAVER_MODE,
2022-07-16 01:07:44 +05:30
false
)
2022-06-18 14:00:24 +05:30
// show noInternet Activity if no internet available on app startup
2022-07-06 19:54:24 +05:30
if (!ConnectionHelper.isNetworkAvailable(this)) {
2022-07-01 20:14:14 +05:30
val noInternetIntent = Intent(this, NoInternetActivity::class.java)
startActivity(noInternetIntent)
} else {
2022-07-01 00:35:31 +05:30
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
2022-07-03 17:13:54 +05:30
navController = findNavController(R.id.fragment)
2022-07-01 00:35:31 +05:30
binding.bottomNav.setupWithNavController(navController)
2022-07-08 21:49:43 +05:30
2022-07-08 20:13:32 +05:30
// gets the surface color of the bottom navigation view
2022-07-08 20:12:55 +05:30
val color = SurfaceColors.getColorForElevation(this, 10F)
// sets the navigation bar color to the previously calculated color
window.navigationBarColor = color
2022-06-18 14:00:24 +05:30
// hide the trending page if enabled
2022-07-17 21:48:39 +05:30
val hideTrendingPage = PreferenceHelper.getBoolean(PreferenceKeys.HIDE_TRENDING_PAGE, false)
2022-07-03 15:43:38 +05:30
if (hideTrendingPage) binding.bottomNav.menu.findItem(R.id.homeFragment).isVisible =
2022-07-02 01:02:26 +05:30
false
2022-06-18 14:00:24 +05:30
2022-07-03 17:13:54 +05:30
// save start tab fragment id
2022-07-17 21:48:39 +05:30
startFragmentId = when (PreferenceHelper.getString(PreferenceKeys.DEFAULT_TAB, "home")) {
2022-07-03 17:13:54 +05:30
"home" -> R.id.homeFragment
"subscriptions" -> R.id.subscriptionsFragment
"library" -> R.id.libraryFragment
else -> R.id.homeFragment
2022-05-14 03:39:33 +05:30
}
2022-07-03 17:13:54 +05:30
// set default tab as start fragment
2022-07-05 21:24:42 +05:30
navController.graph.setStartDestination(startFragmentId)
2022-07-03 17:13:54 +05:30
// navigate to the default fragment
2022-07-05 21:24:42 +05:30
navController.navigate(startFragmentId)
2022-07-03 17:13:54 +05:30
2022-07-10 21:23:17 +05:30
val labelVisibilityMode = when (
2022-07-17 21:48:39 +05:30
PreferenceHelper.getString(PreferenceKeys.LABEL_VISIBILITY, "always")
2022-07-10 21:23:17 +05:30
) {
"always" -> NavigationBarView.LABEL_VISIBILITY_LABELED
"selected" -> NavigationBarView.LABEL_VISIBILITY_SELECTED
"never" -> NavigationBarView.LABEL_VISIBILITY_UNLABELED
else -> NavigationBarView.LABEL_VISIBILITY_AUTO
}
binding.bottomNav.labelVisibilityMode = labelVisibilityMode
2022-07-01 00:35:31 +05:30
binding.bottomNav.setOnItemSelectedListener {
2022-07-05 21:24:42 +05:30
// clear backstack if it's the start fragment
if (startFragmentId == it.itemId) navController.backQueue.clear()
// set menu item on click listeners
when (it.itemId) {
2022-07-01 21:59:47 +05:30
R.id.homeFragment -> {
navController.navigate(R.id.homeFragment)
}
2022-07-01 21:59:47 +05:30
R.id.subscriptionsFragment -> {
navController.navigate(R.id.subscriptionsFragment)
}
2022-07-01 21:59:47 +05:30
R.id.libraryFragment -> {
navController.navigate(R.id.libraryFragment)
}
2022-02-08 20:47:49 +05:30
}
false
2022-02-08 20:47:49 +05:30
}
2022-07-04 12:25:52 +05:30
binding.toolbar.title = ThemeHelper.getStyledAppName(this)
2022-07-01 00:35:31 +05:30
binding.toolbar.setNavigationOnClickListener {
2022-05-20 03:23:55 +05:30
// settings activity stuff
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
2022-07-01 00:35:31 +05:30
binding.toolbar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.action_search -> {
navController.navigate(R.id.searchFragment)
}
}
false
}
2021-12-28 01:37:07 +05:30
}
2022-03-15 10:08:33 +05:30
}
override fun onStart() {
super.onStart()
2022-05-29 19:27:01 +05:30
val intentData: Uri? = intent?.data
2022-05-30 18:54:44 +05:30
// check whether an URI got submitted over the intent data
2022-05-29 19:27:01 +05:30
if (intentData != null && intentData.host != null && intentData.path != null) {
Log.d("intentData", "${intentData.host} ${intentData.path} ")
2022-05-30 18:54:44 +05:30
// load the URI of the submitted link (e.g. video)
2022-05-29 19:27:01 +05:30
loadIntentData(intentData)
}
}
2022-03-14 19:40:14 +05:30
2022-05-29 19:27:01 +05:30
private fun loadIntentData(data: Uri) {
// channel
if (data.path!!.contains("/channel/") ||
data.path!!.contains("/c/") ||
data.path!!.contains("/user/")
) {
Log.i(TAG, "URI Type: Channel")
var channel = data.path
channel = channel!!.replace("/c/", "")
channel = channel.replace("/user/", "")
val bundle = bundleOf("channel_id" to channel)
2022-07-01 21:59:47 +05:30
navController.navigate(R.id.channelFragment, bundle)
2022-05-29 19:27:01 +05:30
} else if (data.path!!.contains("/playlist")) {
Log.i(TAG, "URI Type: Playlist")
var playlist = data.query!!
if (playlist.contains("&")) {
2022-06-18 14:00:24 +05:30
val playlists = playlist.split("&")
2022-05-29 19:27:01 +05:30
for (v in playlists) {
if (v.contains("list=")) {
playlist = v
break
}
}
}
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/")
) {
Log.i(TAG, "URI Type: Video")
val watch = data.path!!
.replace("/shorts/", "")
.replace("/v/", "")
.replace("/embed/", "")
val bundle = Bundle()
bundle.putString("videoId", watch)
2022-06-14 14:16:35 +05:30
// for time stamped links
2022-06-14 18:10:16 +05:30
if (data.query != null && data.query?.contains("t=")!!) {
2022-06-14 14:16:35 +05:30
val timeStamp = data.query.toString().split("t=")[1]
bundle.putLong("timeStamp", timeStamp.toLong())
}
loadWatch(bundle)
2022-05-29 19:27:01 +05:30
} else if (data.path!!.contains("/watch") && data.query != null) {
Log.d("dafaq", data.query!!)
var watch = data.query!!
if (watch.contains("&")) {
2022-06-18 14:00:24 +05:30
val watches = watch.split("&")
2022-05-29 19:27:01 +05:30
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
}
2022-03-14 23:58:44 +05:30
}
2022-06-18 14:00:24 +05:30
val bundle = Bundle()
2022-05-29 19:27:01 +05:30
bundle.putString("videoId", watch.replace("v=", ""))
2022-06-14 14:16:35 +05:30
// for time stamped links
2022-06-14 18:10:16 +05:30
if (data.query != null && data.query?.contains("t=")!!) {
2022-06-14 14:16:35 +05:30
val timeStamp = data.query.toString().split("t=")[1]
bundle.putLong("timeStamp", timeStamp.toLong())
}
loadWatch(bundle)
2022-05-29 19:27:01 +05:30
} else {
2022-06-18 14:00:24 +05:30
val watch = data.path!!.replace("/", "")
val bundle = Bundle()
2022-05-29 19:27:01 +05:30
bundle.putString("videoId", watch)
2022-06-14 14:16:35 +05:30
// for time stamped links
2022-06-14 18:10:16 +05:30
if (data.query != null && data.query?.contains("t=")!!) {
2022-06-14 14:16:35 +05:30
val timeStamp = data.query.toString().split("t=")[1]
bundle.putLong("timeStamp", timeStamp.toLong())
}
loadWatch(bundle)
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-06-14 14:16:35 +05:30
private fun loadWatch(bundle: Bundle) {
2022-06-18 14:00:24 +05:30
val frag = PlayerFragment()
2022-06-14 14:16:35 +05:30
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler(Looper.getMainLooper()).postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
}
2022-01-28 23:16:11 +05:30
override fun onBackPressed() {
2022-07-07 20:07:26 +05:30
if (binding.mainMotionLayout.progress == 0F) {
2022-07-07 19:53:27 +05:30
try {
minimizePlayer()
} catch (e: Exception) {
2022-07-07 20:06:39 +05:30
if (navController.currentDestination?.id == startFragmentId) {
2022-07-16 01:56:22 +05:30
// close app
moveTaskToBack(true)
2022-07-07 20:06:39 +05:30
} else {
navController.popBackStack()
}
2022-07-07 19:53:27 +05:30
}
2022-07-05 21:24:42 +05:30
} else if (navController.currentDestination?.id == startFragmentId) {
super.onBackPressed()
} else {
navController.popBackStack()
}
}
private fun minimizePlayer() {
binding.mainMotionLayout.transitionToEnd()
findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
2022-07-12 20:32:21 +05:30
// set the animation duration
motionLayout.setTransitionDuration(250)
2022-07-05 21:24:42 +05:30
motionLayout.transitionToEnd()
with(motionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition, true)
2022-02-02 23:58:24 +05:30
}
2022-07-05 21:24:42 +05:30
findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
2022-07-12 20:57:56 +05:30
Globals.isFullScreen = false
2022-01-28 23:16:11 +05:30
}
2022-05-21 13:32:04 +05:30
2021-12-16 03:54:40 +05:30
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val orientation = newConfig.orientation
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
}
}
2022-05-21 13:32:04 +05:30
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-21 13:32:04 +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")
2022-05-21 13:32:04 +05:30
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
override fun onUserLeaveHint() {
super.onUserLeaveHint()
supportFragmentManager.fragments.forEach { fragment ->
(fragment as? PlayerFragment)?.onUserLeaveHint()
}
}
2022-03-15 14:21:31 +05:30
}
2022-05-21 13:32:04 +05:30
2022-03-15 14:21:31 +05:30
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
2022-03-15 14:21:31 +05:30
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
}