Selaa lähdekoodia

Merge pull request #13350 from nextcloud/bugfix/not-attached-context-PreviewMediaFragment

Bugfix - Not attached to an activity on PreviewMediaFragment
Alper Öztürk 11 kuukautta sitten
vanhempi
commit
773f1a967d

+ 100 - 82
app/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.kt

@@ -13,6 +13,7 @@ package com.owncloud.android.ui.preview
 
 import android.annotation.SuppressLint
 import android.app.Activity
+import android.content.Context
 import android.content.Intent
 import android.content.res.Configuration
 import android.content.res.Resources
@@ -125,14 +126,8 @@ class PreviewMediaFragment : FileFragment(), OnTouchListener, Injectable {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
 
-        arguments?.let { bundle ->
-            file.logFileSize(TAG)
-            file = bundle.getParcelableArgument(FILE, OCFile::class.java)
-            user = bundle.getParcelableArgument(USER, User::class.java)
-
-            savedPlaybackPosition = bundle.getLong(PLAYBACK_POSITION)
-            autoplay = bundle.getBoolean(AUTOPLAY)
-            isLivePhoto = bundle.getBoolean(IS_LIVE_PHOTO)
+        arguments?.let {
+            initArguments(it)
         }
 
         mediaPlayerServiceConnection = PlayerServiceConnection(requireContext())
@@ -149,53 +144,58 @@ class PreviewMediaFragment : FileFragment(), OnTouchListener, Injectable {
         return binding.root
     }
 
-    private fun setLoadingView() {
-        binding.progress.visibility = View.VISIBLE
-        binding.emptyView.emptyListView.visibility = View.GONE
-    }
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        Log_OC.v(TAG, "onActivityCreated")
 
-    private fun setVideoErrorMessage(headline: String, @StringRes message: Int = R.string.stream_not_possible_message) {
-        binding.emptyView.run {
-            emptyListViewHeadline.text = headline
-            emptyListViewText.setText(message)
-            emptyListIcon.setImageResource(R.drawable.file_movie)
-            emptyListViewText.visibility = View.VISIBLE
-            emptyListIcon.visibility = View.VISIBLE
-            emptyListView.visibility = View.VISIBLE
+        checkArgumentsAfterViewCreation(savedInstanceState)
+
+        if (file != null) {
+            prepareExoPlayerView()
         }
 
-        binding.progress.visibility = View.GONE
+        toggleDrawerLockMode(containerActivity, DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
+        addMenuHost()
     }
 
-    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
-        super.onViewCreated(view, savedInstanceState)
-        Log_OC.v(TAG, "onActivityCreated")
-
-        var file = file
+    private fun checkArgumentsAfterViewCreation(savedInstanceState: Bundle?) {
         if (savedInstanceState == null) {
             checkNotNull(file) { "Instanced with a NULL OCFile" }
             checkNotNull(user) { "Instanced with a NULL ownCloud Account" }
         } else {
             file = savedInstanceState.getParcelableArgument(EXTRA_FILE, OCFile::class.java)
-            setFile(file)
             user = savedInstanceState.getParcelableArgument(EXTRA_USER, User::class.java)
             savedPlaybackPosition = savedInstanceState.getInt(EXTRA_PLAY_POSITION).toLong()
             autoplay = savedInstanceState.getBoolean(EXTRA_PLAYING)
         }
+    }
 
-        if (file != null) {
-            if (MimeTypeUtil.isVideo(file)) {
-                binding.exoplayerView.visibility = View.VISIBLE
-                binding.imagePreview.visibility = View.GONE
-            } else {
-                binding.exoplayerView.visibility = View.GONE
-                binding.imagePreview.visibility = View.VISIBLE
-                extractAndSetCoverArt(file)
-            }
+    private fun initArguments(bundle: Bundle) {
+        file.logFileSize(TAG)
+        file = bundle.getParcelableArgument(FILE, OCFile::class.java)
+        user = bundle.getParcelableArgument(USER, User::class.java)
+
+        savedPlaybackPosition = bundle.getLong(PLAYBACK_POSITION)
+        autoplay = bundle.getBoolean(AUTOPLAY)
+        isLivePhoto = bundle.getBoolean(IS_LIVE_PHOTO)
+    }
+
+    private fun setLoadingView() {
+        binding.progress.visibility = View.VISIBLE
+        binding.emptyView.emptyListView.visibility = View.GONE
+    }
+
+    private fun setVideoErrorMessage(headline: String, @StringRes message: Int = R.string.stream_not_possible_message) {
+        binding.emptyView.run {
+            emptyListViewHeadline.text = headline
+            emptyListViewText.setText(message)
+            emptyListIcon.setImageResource(R.drawable.file_movie)
+            emptyListViewText.visibility = View.VISIBLE
+            emptyListIcon.visibility = View.VISIBLE
+            emptyListView.visibility = View.VISIBLE
         }
 
-        toggleDrawerLockMode(containerActivity, DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
-        addMenuHost()
+        binding.progress.visibility = View.GONE
     }
 
     /**
@@ -275,61 +275,68 @@ class PreviewMediaFragment : FileFragment(), OnTouchListener, Injectable {
         }
     }
 
-    @Suppress("TooGenericExceptionCaught")
     override fun onStart() {
         super.onStart()
-        Log_OC.v(TAG, "onStart")
-        val context = if (context != null) {
-            requireContext()
-        } else {
-            MainApp.getAppContext()
+        prepareMedia()
+    }
+
+    private fun prepareMedia() {
+        if (file == null || !isAdded) {
+            Log_OC.d(TAG, "File is null or fragment not attached to a context.")
+            return
         }
 
-        val file = file
-        if (file != null) {
-            // bind to any existing player
-            mediaPlayerServiceConnection?.bind()
-
-            if (MimeTypeUtil.isAudio(file)) {
-                binding.mediaController.setMediaPlayer(mediaPlayerServiceConnection)
-                binding.mediaController.visibility = View.VISIBLE
-                mediaPlayerServiceConnection?.start(user!!, file, autoplay, savedPlaybackPosition)
-                binding.emptyView.emptyListView.visibility = View.GONE
-                binding.progress.visibility = View.GONE
-            } else if (MimeTypeUtil.isVideo(file)) {
-                if (mediaPlayerServiceConnection?.isConnected == true) {
-                    // always stop player
-                    stopAudio()
-                }
-                if (exoPlayer != null) {
-                    playVideo()
-                } else {
-                    val handler = Handler(Looper.getMainLooper())
-                    Executors.newSingleThreadExecutor().execute {
-                        try {
-                            nextcloudClient = clientFactory.createNextcloudClient(accountManager.user)
-                            handler.post {
-                                exoPlayer = createNextcloudExoplayer(context, nextcloudClient!!)
-                                exoPlayer?.addListener(
-                                    ExoplayerListener(
-                                        context,
-                                        binding.exoplayerView,
-                                        exoPlayer!!
-                                    ) {
-                                        goBackToLivePhoto()
-                                    }
-                                )
-                                playVideo()
-                            }
-                        } catch (e: CreationException) {
-                            handler.post { Log_OC.e(TAG, "error setting up ExoPlayer", e) }
+        mediaPlayerServiceConnection?.bind()
+
+        if (MimeTypeUtil.isAudio(file)) {
+            prepareForAudio()
+        } else if (MimeTypeUtil.isVideo(file)) {
+            prepareForVideo(context ?: MainApp.getAppContext())
+        }
+    }
+
+    @Suppress("DEPRECATION", "TooGenericExceptionCaught")
+    private fun prepareForVideo(context: Context) {
+        if (mediaPlayerServiceConnection?.isConnected == true) {
+            // always stop player
+            stopAudio()
+        }
+        if (exoPlayer != null) {
+            playVideo()
+        } else {
+            val handler = Handler(Looper.getMainLooper())
+            Executors.newSingleThreadExecutor().execute {
+                try {
+                    nextcloudClient = clientFactory.createNextcloudClient(accountManager.user)
+                    handler.post {
+                        nextcloudClient?.let { client ->
+                            createExoPlayer(context, client)
+                            playVideo()
                         }
                     }
+                } catch (e: CreationException) {
+                    handler.post { Log_OC.e(TAG, "error setting up ExoPlayer", e) }
                 }
             }
         }
     }
 
+    private fun createExoPlayer(context: Context, client: NextcloudClient) {
+        exoPlayer = createNextcloudExoplayer(context, client)
+        exoPlayer?.let {
+            val listener = ExoplayerListener(context, binding.exoplayerView, it) { goBackToLivePhoto() }
+            it.addListener(listener)
+        }
+    }
+
+    private fun prepareForAudio() {
+        binding.mediaController.setMediaPlayer(mediaPlayerServiceConnection)
+        binding.mediaController.visibility = View.VISIBLE
+        mediaPlayerServiceConnection?.start(user!!, file, autoplay, savedPlaybackPosition)
+        binding.emptyView.emptyListView.visibility = View.GONE
+        binding.progress.visibility = View.GONE
+    }
+
     private fun goBackToLivePhoto() {
         if (!isLivePhoto) {
             return
@@ -339,6 +346,17 @@ class PreviewMediaFragment : FileFragment(), OnTouchListener, Injectable {
         requireActivity().supportFragmentManager.popBackStack()
     }
 
+    private fun prepareExoPlayerView() {
+        if (MimeTypeUtil.isVideo(file)) {
+            binding.exoplayerView.visibility = View.VISIBLE
+            binding.imagePreview.visibility = View.GONE
+        } else {
+            binding.exoplayerView.visibility = View.GONE
+            binding.imagePreview.visibility = View.VISIBLE
+            extractAndSetCoverArt(file)
+        }
+    }
+
     private fun showActionBar() {
         val currentActivity: Activity = requireActivity()
         if (currentActivity is PreviewImageActivity) {