Skip to content

Commit

Permalink
Rewrite session validation code to not use callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Dec 6, 2022
1 parent 4777840 commit a6729a7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jellyfin.androidtv.auth.ui

import android.content.Intent
import androidx.fragment.app.FragmentActivity
import org.jellyfin.androidtv.auth.repository.SessionRepository
import org.jellyfin.androidtv.ui.startup.StartupActivity
import org.koin.android.ext.android.inject
import timber.log.Timber

/**
* Extension function to check authentication. Should be called in [FragmentActivity.onCreate] and
* [FragmentActivity.onResume]. It validates the current session and opens the authentication screen
* when no session is found.
*
* @return whether to proceed creating the activity or not. When `false` is returned the
* [FragmentActivity.finish] function is automatically called.
*/
fun FragmentActivity.validateAuthentication(): Boolean {
val sessionRepository by inject<SessionRepository>()

if (sessionRepository.currentSession.value == null) {
Timber.w("Activity ${this::class.qualifiedName} started without a session, bouncing to StartupActivity")
startActivity(Intent(this, StartupActivity::class.java))
finish()
return false
}

return true
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class KoinInitializer : Initializer<KoinApplication> {
androidContext(context)

modules(
activityLifecycleCallbacksModule,
androidModule,
appModule,
authModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.auth.ui.validateAuthentication
import org.jellyfin.androidtv.data.service.BackgroundService
import org.jellyfin.androidtv.ui.navigation.NavigationAction
import org.jellyfin.androidtv.ui.navigation.NavigationRepository
Expand All @@ -36,8 +37,11 @@ class MainActivity : FragmentActivity(R.layout.fragment_content_view) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (!validateAuthentication()) return

applyTheme()


backgroundService.attach(this)
onBackPressedDispatcher.addCallback(this, backPressedCallback)

Expand All @@ -61,6 +65,8 @@ class MainActivity : FragmentActivity(R.layout.fragment_content_view) {
override fun onResume() {
super.onResume()

if (!validateAuthentication()) return

applyTheme()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.jellyfin.androidtv.R;
import org.jellyfin.androidtv.auth.repository.UserRepository;
import org.jellyfin.androidtv.auth.ui.ActivityAuthenticationExtensionsKt;
import org.jellyfin.androidtv.data.compat.PlaybackException;
import org.jellyfin.androidtv.data.compat.StreamInfo;
import org.jellyfin.androidtv.data.compat.SubtitleStreamInfo;
Expand Down Expand Up @@ -106,6 +107,8 @@ public class ExternalPlayerActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (!ActivityAuthenticationExtensionsKt.validateAuthentication(this)) return;

backgroundService.getValue().attach(this);

mItemsToPlay = mediaManager.getValue().getCurrentVideoQueue();
Expand All @@ -126,6 +129,8 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (!ActivityAuthenticationExtensionsKt.validateAuthentication(this)) return;

long playerFinishedTime = System.currentTimeMillis();
Timber.d("Returned from player, result <%d>, extra data <%s>", resultCode, data);
org.jellyfin.sdk.model.api.BaseItemDto item = mItemsToPlay.get(mCurrentNdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.add
import androidx.fragment.app.commit
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.auth.ui.validateAuthentication
import org.jellyfin.androidtv.util.applyTheme
import org.koin.android.ext.android.inject

Expand All @@ -25,6 +26,8 @@ class PlaybackOverlayActivity : FragmentActivity(R.layout.fragment_content_view)
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (!validateAuthentication()) return

applyTheme()

// Workaround for Sony Bravia devices that show a "grey" background on HDR videos
Expand All @@ -43,6 +46,12 @@ class PlaybackOverlayActivity : FragmentActivity(R.layout.fragment_content_view)
}
}

override fun onResume() {
super.onResume()

if (!validateAuthentication()) return
}

override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
// Try listener first
if (keyListener?.onKey(currentFocus, keyCode, event) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.auth.ui.validateAuthentication
import org.jellyfin.androidtv.data.service.BackgroundService
import org.jellyfin.androidtv.ui.navigation.Destinations
import org.jellyfin.androidtv.ui.navigation.NavigationRepository
Expand All @@ -28,6 +29,8 @@ class NextUpActivity : FragmentActivity(R.layout.fragment_content_view) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (!validateAuthentication()) return

applyTheme()

val useExternalPlayer = intent.getBooleanExtra(EXTRA_USE_EXTERNAL_PLAYER, false)
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit a6729a7

Please sign in to comment.