Skip to content

Commit

Permalink
tv focus changes + gradle bump + pip crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Aug 4, 2023
1 parent 653982a commit f5c4864
Show file tree
Hide file tree
Showing 28 changed files with 583 additions and 313 deletions.
36 changes: 20 additions & 16 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import com.android.build.gradle.api.BaseVariantOutput
import org.jetbrains.dokka.gradle.DokkaTask
import java.io.ByteArrayOutputStream
import java.net.URL
Expand Down Expand Up @@ -52,7 +51,7 @@ android {
targetSdk = 33

versionCode = 59
versionName = "4.1.1"
versionName = "4.1.2"

resValue("string", "app_version", "${defaultConfig.versionName}${versionNameSuffix ?: ""}")

Expand Down Expand Up @@ -108,14 +107,19 @@ android {
versionCode = (System.currentTimeMillis() / 60000).toInt()
}
}
//toolchain {
// languageVersion.set(JavaLanguageVersion.of(17))
// }
// jvmToolchain(17)

compileOptions {
isCoreLibraryDesugaringEnabled = true

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
freeCompilerArgs = listOf("-Xjvm-default=compatibility")
}
lint {
Expand All @@ -131,22 +135,22 @@ repositories {

dependencies {
implementation("com.google.android.mediahome:video:1.0.0")
implementation("androidx.test.ext:junit-ktx:1.1.3")
implementation("androidx.test.ext:junit-ktx:1.1.5")
testImplementation("org.json:json:20180813")

implementation("androidx.core:core-ktx:1.8.0")
implementation("androidx.appcompat:appcompat:1.4.2") // need target 32 for 1.5.0
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.appcompat:appcompat:1.6.1") // need target 32 for 1.5.0

// dont change this to 1.6.0 it looks ugly af
implementation("com.google.android.material:material:1.5.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.1")
implementation("androidx.navigation:navigation-ui-ktx:2.5.1")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.5.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
implementation("androidx.navigation:navigation-fragment-ktx:2.6.0")
implementation("androidx.navigation:navigation-ui-ktx:2.6.0")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation("androidx.test:core")

//implementation("io.karn:khttp-android:0.1.2") //okhttp instead
Expand Down Expand Up @@ -201,8 +205,8 @@ dependencies {
//implementation("com.github.TorrentStream:TorrentStream-Android:2.7.0")

// Downloading
implementation("androidx.work:work-runtime:2.8.0")
implementation("androidx.work:work-runtime-ktx:2.8.0")
implementation("androidx.work:work-runtime:2.8.1")
implementation("androidx.work:work-runtime-ktx:2.8.1")

// Networking
// implementation("com.squareup.okhttp3:okhttp:4.9.2")
Expand Down
95 changes: 68 additions & 27 deletions app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.preference.PreferenceManager
import com.google.android.gms.cast.framework.CastSession
import com.google.android.material.chip.ChipGroup
import com.google.android.material.navigationrail.NavigationRailView
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey
import com.lagradost.cloudstream3.mvvm.logError
Expand All @@ -39,6 +42,13 @@ import org.schabi.newpipe.extractor.NewPipe
import java.lang.ref.WeakReference
import java.util.*

enum class FocusDirection {
Start,
End,
Up,
Down,
}

object CommonActivity {

private var _activity: WeakReference<Activity>? = null
Expand Down Expand Up @@ -318,17 +328,70 @@ object CommonActivity {
currentLook = currentLook.parent as? View ?: break
}*/

/** skips the initial stage of searching for an id using the view, see getNextFocus for specification */
fun continueGetNextFocus(
root: Any?,
view: View,
direction: FocusDirection,
nextId: Int,
depth: Int = 0
): View? {
if (nextId == NO_ID) return null

// do an initial search for the view, in case the localLook is too deep we can use this as
// an early break and backup view
var next =
when (root) {
is Activity -> root.findViewById(nextId)
is View -> root.rootView.findViewById<View?>(nextId)
else -> null
} ?: return null

next = localLook(view, nextId) ?: next

// if cant focus but visible then break and let android decide
// the exception if is the view is a parent and has children that wants focus
val hasChildrenThatWantsFocus = (next as? ViewGroup)?.let { parent ->
parent.descendantFocusability == ViewGroup.FOCUS_AFTER_DESCENDANTS && parent.childCount > 0
} ?: false
if (!next.isFocusable && next.isShown && !hasChildrenThatWantsFocus) return null

// if not shown then continue because we will "skip" over views to get to a replacement
if (!next.isShown) {
// we don't want a while true loop, so we let android decide if we find a recursive view
if (next == view) return null
return getNextFocus(root, next, direction, depth + 1)
}

(when (next) {
is ChipGroup -> {
next.children.firstOrNull { it.isFocusable && it.isShown }
}

is NavigationRailView -> {
next.findViewById(next.selectedItemId) ?: next.findViewById(R.id.navigation_home)
}

else -> null
})?.let {
return it
}

// nothing wrong with the view found, return it
return next
}

/** recursively looks for a next focus up to a depth of 10,
* this is used to override the normal shit focus system
* because this application has a lot of invisible views that messes with some tv devices*/
private fun getNextFocus(
act: Activity?,
fun getNextFocus(
root: Any?,
view: View?,
direction: FocusDirection,
depth: Int = 0
): View? {
// if input is invalid let android decide + depth test to not crash if loop is found
if (view == null || depth >= 10 || act == null) {
if (view == null || depth >= 10 || root == null) {
return null
}

Expand Down Expand Up @@ -363,31 +426,9 @@ object CommonActivity {
if (nextId == NO_ID)
return null
}

var next = act.findViewById<View?>(nextId) ?: return null

next = localLook(view, nextId) ?: next

// if cant focus but visible then break and let android decide
// the exception if is the view is a parent and has children that wants focus
val hasChildrenThatWantsFocus = (next as? ViewGroup)?.let { parent ->
parent.descendantFocusability == ViewGroup.FOCUS_AFTER_DESCENDANTS && parent.childCount > 0
} ?: false
if (!next.isFocusable && next.isShown && !hasChildrenThatWantsFocus) return null

// if not shown then continue because we will "skip" over views to get to a replacement
if (!next.isShown) return getNextFocus(act, next, direction, depth + 1)

// nothing wrong with the view found, return it
return next
return continueGetNextFocus(root, view, direction, nextId, depth)
}

private enum class FocusDirection {
Start,
End,
Up,
Down,
}

fun onKeyDown(act: Activity?, keyCode: Int, event: KeyEvent?) {
//println("Keycode: $keyCode")
Expand Down Expand Up @@ -517,7 +558,7 @@ object CommonActivity {

else -> null
}
// println("NEXT FOCUS : $nextView")
// println("NEXT FOCUS : $nextView")
if (nextView != null) {
nextView.requestFocus()
keyEventListener?.invoke(Pair(event, true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.FragmentChildDownloadsBinding
import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.getKeys
Expand Down Expand Up @@ -110,7 +111,11 @@ class DownloadChildFragment : Fragment() {
downloadDeleteEventListener?.let { VideoDownloadManager.downloadDeleteEvent += it }

binding?.downloadChildList?.adapter = adapter
binding?.downloadChildList?.layoutManager = GridLayoutManager(context, 1)
binding?.downloadChildList?.setLinearListLayout(
isHorizontal = false,
nextDown = FOCUS_SELF,
nextRight = FOCUS_SELF
)//layoutManager = GridLayoutManager(context, 1)

updateList(folder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import com.lagradost.cloudstream3.databinding.FragmentDownloadsBinding
import com.lagradost.cloudstream3.databinding.StreamInputBinding
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.ui.player.BasicLink
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
import java.net.URI

Expand Down Expand Up @@ -74,7 +76,7 @@ class DownloadFragment : Fragment() {
super.onDestroyView()
}

var binding : FragmentDownloadsBinding? = null
var binding: FragmentDownloadsBinding? = null

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -151,6 +153,7 @@ class DownloadFragment : Fragment() {
)
}
}

1 -> {
(activity as AppCompatActivity?)?.loadResult(
click.data.url,
Expand Down Expand Up @@ -187,7 +190,13 @@ class DownloadFragment : Fragment() {

binding?.downloadList?.apply {
this.adapter = adapter
layoutManager = GridLayoutManager(context, 1)
setLinearListLayout(
isHorizontal = false,
nextRight = FOCUS_SELF,
nextUp = FOCUS_SELF,
nextDown = FOCUS_SELF
)
//layoutManager = GridLayoutManager(context, 1)
}

// Should be visible in emulator layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ class HomeChildItemAdapter(
private val nextFocusUp: Int? = null,
private val nextFocusDown: Int? = null,
private val isHorizontal: Boolean = false,
private val isRtl : Boolean
private val isRtl: Boolean
) :
RecyclerView.ViewHolder(binding.root) {

fun bind(card: SearchResponse, position: Int) {

// TV focus fixing
val nextFocusBehavior = when (position) {
/*val nextFocusBehavior = when (position) {
0 -> true
itemCount - 1 -> false
else -> null
Expand All @@ -113,7 +113,7 @@ class HomeChildItemAdapter(
} else {
itemView.nextFocusRightId = -1
itemView.nextFocusLeftId = -1
}
}*/


when (binding) {
Expand Down Expand Up @@ -171,7 +171,7 @@ class HomeChildItemAdapter(
card,
position,
itemView,
nextFocusBehavior,
null, // nextFocusBehavior,
nextFocusUp,
nextFocusDown
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,35 @@ class HomeFragment : Fragment() {
selectedTypes: List<TvType>,
validTypes: List<TvType>,
callback: (List<TvType>) -> Unit
) {
bindChips(header, selectedTypes, validTypes, callback, null, null)
}

fun bindChips(
header: TvtypesChipsBinding?,
selectedTypes: List<TvType>,
validTypes: List<TvType>,
callback: (List<TvType>) -> Unit,
nextFocusDown: Int?,
nextFocusUp: Int?
) {
if (header == null) return
val pairList = getPairList(header)
for ((button, types) in pairList) {
val isValid = validTypes.any { types.contains(it) }
button?.isVisible = isValid
button?.isChecked = isValid && selectedTypes.any { types.contains(it) }
button?.isFocusable = true
if (isTrueTvSettings()) {
button?.isFocusableInTouchMode = true
}

if (nextFocusDown != null)
button?.nextFocusDownId = nextFocusDown

if (nextFocusUp != null)
button?.nextFocusUpId = nextFocusUp

button?.setOnCheckedChangeListener { _, _ ->
val list = ArrayList<TvType>()
for ((sbutton, vvalidTypes) in pairList) {
Expand Down Expand Up @@ -462,7 +484,7 @@ class HomeFragment : Fragment() {

private val apiChangeClickListener = View.OnClickListener { view ->
view.context.selectHomepage(currentApiName) { api ->
homeViewModel.loadAndCancel(api, forceReload = true,fromUI = true)
homeViewModel.loadAndCancel(api, forceReload = true, fromUI = true)
}
/*val validAPIs = view.context?.filterProviderByPreferredMedia()?.toMutableList() ?: mutableListOf()
Expand Down
Loading

0 comments on commit f5c4864

Please sign in to comment.