Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/parent/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
<activity
android:name=".features.webview.HtmlContentActivity"
android:theme="@style/CanvasMaterialTheme_Default" />

<activity
android:name=".features.media.ViewMediaActivity"
android:theme="@style/CanvasMaterialTheme_Default" />
<!-- End of Activities -->

<!-- Services -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import com.instructure.pandautils.features.inbox.list.InboxRouter
import com.instructure.pandautils.features.inbox.utils.InboxComposeOptions
import com.instructure.pandautils.utils.FileDownloader
import com.instructure.pandautils.utils.setupAsBackButton
import com.instructure.pandautils.utils.shouldOpenMediaInternally
import com.instructure.parentapp.features.inbox.coursepicker.ParentInboxCoursePickerBottomSheetDialog
import com.instructure.parentapp.features.media.ViewMediaActivity
import com.instructure.parentapp.util.navigation.Navigation


Expand Down Expand Up @@ -61,11 +63,17 @@ class ParentInboxRouter(
}

override fun routeToAttachment(attachment: Attachment) {
fileDownloader.downloadFileToDevice(attachment)
val url = attachment.url ?: return fileDownloader.downloadFileToDevice(attachment)
if (shouldOpenMediaInternally(url, attachment.contentType, attachment.mimeClass)) {
activity.startActivity(ViewMediaActivity.createIntent(activity, url, attachment.contentType.orEmpty(), attachment.displayName))
} else {
fileDownloader.downloadFileToDevice(attachment)
}
}

override fun routeToMediaAttachment(mediaComment: MediaComment) {
fileDownloader.downloadFileToDevice(mediaComment.url, mediaComment.displayName, mediaComment.contentType)
val url = mediaComment.url ?: return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We skip error handling in this case, since it is handled in the downloadFileToDevice.

activity.startActivity(ViewMediaActivity.createIntent(activity, url, mediaComment.contentType.orEmpty(), mediaComment.displayName))
}

override fun popDetailsScreen(activity: FragmentActivity?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2026 - present Instructure, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package com.instructure.parentapp.features.media

import android.content.Context
import android.content.Intent
import com.instructure.pandautils.activities.BaseViewMediaActivity
import com.instructure.pandautils.analytics.SCREEN_VIEW_VIEW_MEDIA
import com.instructure.pandautils.analytics.ScreenView
import com.instructure.pandautils.models.EditableFile

@ScreenView(SCREEN_VIEW_VIEW_MEDIA)
class ViewMediaActivity : BaseViewMediaActivity() {
override fun allowEditing() = false
override fun allowCopyingUrl() = false
override fun handleEditing(editableFile: EditableFile) {}

companion object {
fun createIntent(context: Context, url: String, contentType: String, displayName: String?): Intent {
val bundle = makeBundle(url, null, contentType, displayName, true)
return Intent(context, ViewMediaActivity::class.java).putExtras(bundle)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.instructure.student.activity

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
Expand All @@ -34,7 +33,10 @@ import com.instructure.canvasapi2.utils.LinkHeaders
import com.instructure.canvasapi2.utils.Logger
import com.instructure.interactions.FullScreenInteractions
import com.instructure.interactions.router.Route
import com.instructure.interactions.router.RouteContext
import com.instructure.pandautils.activities.BaseViewMediaActivity
import com.instructure.pandautils.loaders.OpenMediaAsyncTaskLoader
import com.instructure.pandautils.utils.shouldOpenMediaInternally
import com.instructure.pandautils.models.PushNotification
import com.instructure.pandautils.receivers.PushExternalReceiver
import com.instructure.pandautils.utils.Const
Expand Down Expand Up @@ -258,20 +260,15 @@ abstract class BaseRouterActivity : CallbackActivity(), FullScreenInteractions {
FileFolderManager.getFileFolderFromURL("files/$fileID", true, fileFolderCanvasCallback)
}

fun openMedia(canvasContext: CanvasContext?, url: String, fileID: String?) {
fun openMedia(canvasContext: CanvasContext?, url: String, fileID: String?, mimeType: String? = null, mimeClass: String? = null) {
showLoadingIndicator()
lifecycleScope.launch {
if (shouldOpenInternally(url)) {
startActivity(
VideoViewActivity.createIntent(
this@BaseRouterActivity,
url
)
)
if (shouldOpenMediaInternally(url, mimeType, mimeClass)) {
val bundle = BaseViewMediaActivity.makeBundle(url, null, mimeType.orEmpty(), null, true)
RouteMatcher.route(this@BaseRouterActivity, Route(bundle, RouteContext.MEDIA))
} else {
openMediaBundle =
OpenMediaAsyncTaskLoader.createBundle(url, null, fileID, canvasContext)
LoaderUtils.restartLoaderWithBundle<LoaderManager.LoaderCallbacks<OpenMediaAsyncTaskLoader.LoadedMedia>>(
openMediaBundle = OpenMediaAsyncTaskLoader.createBundle(url, null, fileID, canvasContext)
LoaderUtils.restartLoaderWithBundle(
LoaderManager.getInstance(this@BaseRouterActivity),
openMediaBundle,
loaderCallbacks,
Expand All @@ -291,13 +288,9 @@ abstract class BaseRouterActivity : CallbackActivity(), FullScreenInteractions {
) {
showLoadingIndicator()
lifecycleScope.launch {
if (shouldOpenInternally(url)) {
startActivity(
VideoViewActivity.createIntent(
this@BaseRouterActivity,
url
)
)
if (shouldOpenMediaInternally(url, mime)) {
val bundle = BaseViewMediaActivity.makeBundle(url, null, mime, filename, true)
RouteMatcher.route(this@BaseRouterActivity, Route(bundle, RouteContext.MEDIA))
} else {
openMediaBundle = if (localFile) {
OpenMediaAsyncTaskLoader.createLocalBundle(
Expand Down Expand Up @@ -327,9 +320,7 @@ abstract class BaseRouterActivity : CallbackActivity(), FullScreenInteractions {
}
}

private suspend fun shouldOpenInternally(url: String): Boolean {
return (url.endsWith(".mpd") || url.endsWith(".m3u8") || url.endsWith(".mp4"))
}


override fun onDestroy() {
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class StudentInboxRouter(private val activity: FragmentActivity, private val fra
}

override fun routeToAttachment(attachment: Attachment) {
openMedia(activity, attachment.url)
openMedia(activity, attachment.url, attachment.contentType, attachment.mimeClass)
}

override fun routeToMediaAttachment(mediaComment: MediaComment) {
openMedia(activity, mediaComment.url)
openMedia(activity, mediaComment.url, mediaComment.contentType)
}

override fun popDetailsScreen(activity: FragmentActivity?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import com.instructure.canvasapi2.models.Assignment
import com.instructure.canvasapi2.models.Attachment
import com.instructure.canvasapi2.models.CanvasContext
import com.instructure.canvasapi2.models.Submission
import com.instructure.interactions.router.Route
import com.instructure.interactions.router.RouteContext
import com.instructure.pandautils.activities.BaseViewMediaActivity
import com.instructure.pandautils.room.studentdb.StudentDb
import com.instructure.pandautils.utils.ThemePrefs
import com.instructure.pandautils.utils.ViewStyler
import com.instructure.pandautils.utils.onClick
Expand All @@ -46,7 +44,6 @@ import com.instructure.student.mobius.assignmentDetails.submission.picker.ui.Pic
import com.instructure.student.mobius.assignmentDetails.submissionDetails.drawer.comments.SubmissionCommentsEvent
import com.instructure.student.mobius.assignmentDetails.submissionDetails.drawer.comments.SubmissionCommentsViewState
import com.instructure.student.mobius.common.ui.MobiusView
import com.instructure.pandautils.room.studentdb.StudentDb
import com.instructure.student.router.RouteMatcher
import com.spotify.mobius.functions.Consumer

Expand Down Expand Up @@ -173,20 +170,7 @@ class SubmissionCommentsView(
}

fun openMedia(canvasContext: CanvasContext, contentType: String, url: String, fileName: String) {
if (contentType.startsWith("video") || contentType.startsWith("audio")) {
val bundle = BaseViewMediaActivity.makeBundle(url, null, contentType, null, true, null)
(activity as? BaseRouterActivity)?.let {
RouteMatcher.route(it as FragmentActivity, Route(bundle, RouteContext.MEDIA))
}
} else {
(activity as? BaseRouterActivity)?.openMedia(
canvasContext,
contentType,
url,
fileName,
null
)
}
(activity as? BaseRouterActivity)?.openMedia(canvasContext, contentType, url, fileName, null)
}

fun showPermissionDeniedToast() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.instructure.pandautils.loaders.OpenMediaAsyncTaskLoader
import com.instructure.pandautils.room.offline.OfflineDatabase
import com.instructure.pandautils.utils.Const
import com.instructure.pandautils.utils.LoaderUtils
import com.instructure.pandautils.utils.shouldOpenMediaInternally
import com.instructure.pandautils.utils.NetworkStateProvider
import com.instructure.pandautils.utils.RouteUtils
import com.instructure.pandautils.utils.nonNullArgs
Expand Down Expand Up @@ -790,8 +791,16 @@ object RouteMatcher : BaseRouteMatcher() {
return openMediaCallbacks!!
}

fun openMedia(activity: FragmentActivity?, url: String?) {
if (activity != null) {
fun shouldOpenInternally(url: String?, mimeType: String? = null, mimeClass: String? = null): Boolean {
return shouldOpenMediaInternally(url, mimeType, mimeClass)
}

fun openMedia(activity: FragmentActivity?, url: String?, mimeType: String? = null, mimeClass: String? = null) {
if (activity == null) return
if (!url.isNullOrBlank() && shouldOpenInternally(url, mimeType, mimeClass)) {
val bundle = BaseViewMediaActivity.makeBundle(url, null, mimeType.orEmpty(), null, true)
route(activity, Route(bundle, RouteContext.MEDIA))
} else {
openMediaCallbacks = null
openMediaBundle = OpenMediaAsyncTaskLoader.createBundle(url)
LoaderUtils.restartLoaderWithBundle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ class TeacherInboxRouter(private val activity: FragmentActivity, private val fra
}

override fun routeToAttachment(attachment: Attachment) {
openMedia(activity, attachment.url)
openMedia(activity, attachment.url, attachment.displayName, mimeType = attachment.contentType, mimeClass = attachment.mimeClass)
}

override fun routeToMediaAttachment(mediaComment: MediaComment) {
openMedia(activity, mediaComment.url)
openMedia(activity, mediaComment.url, mediaComment.displayName, mimeType = mediaComment.contentType)
}

override fun popDetailsScreen(activity: FragmentActivity?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import com.instructure.pandautils.fragments.RemoteConfigParamsFragment
import com.instructure.pandautils.loaders.OpenMediaAsyncTaskLoader
import com.instructure.pandautils.utils.Const
import com.instructure.pandautils.utils.LoaderUtils
import com.instructure.pandautils.utils.shouldOpenMediaInternally
import com.instructure.pandautils.utils.RouteUtils
import com.instructure.pandautils.utils.argsWithContext
import com.instructure.pandautils.utils.nonNullArgs
Expand Down Expand Up @@ -785,11 +786,19 @@ object RouteMatcher : BaseRouteMatcher() {
return openMediaCallbacks!!
}

fun openMedia(activity: FragmentActivity?, url: String?, fileName: String? = null, fileId: String? = null) {
if (activity != null) {
fun shouldOpenInternally(url: String?, mimeType: String? = null, mimeClass: String? = null): Boolean {
return shouldOpenMediaInternally(url, mimeType, mimeClass)
}

fun openMedia(activity: FragmentActivity?, url: String?, fileName: String? = null, fileId: String? = null, mimeType: String? = null, mimeClass: String? = null) {
if (activity == null) return
if (!url.isNullOrBlank() && shouldOpenInternally(url, mimeType, mimeClass)) {
val bundle = BaseViewMediaActivity.makeBundle(url, null, mimeType.orEmpty(), fileName, true)
route(activity, Route(bundle, RouteContext.MEDIA))
} else {
openMediaCallbacks = null
openMediaBundle = OpenMediaAsyncTaskLoader.createBundle(url, fileName, fileId)
LoaderUtils.restartLoaderWithBundle<LoaderManager.LoaderCallbacks<OpenMediaAsyncTaskLoader.LoadedMedia>>(
LoaderUtils.restartLoaderWithBundle(
LoaderManager.getInstance(activity), openMediaBundle, getLoaderCallbacks(activity), R.id.openMediaLoaderID
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package com.instructure.pandautils.utils
import android.net.Uri
import android.view.SurfaceView
import androidx.annotation.OptIn
import androidx.core.net.toUri
import androidx.media3.common.C
import androidx.media3.common.C.TRACK_TYPE_VIDEO
import androidx.media3.common.MimeTypes
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.PlaybackParameters
Expand Down Expand Up @@ -84,11 +84,11 @@ class ExoAgent private constructor(val uri: Uri) {
/** Whether the media track is audio only, or false if unknown */
private var mIsAudioOnly = false

/** Whether we've already tried retrying with .mpd extension */
private var triedMpdRetry = false
/** Whether we've already tried retrying as DASH */
private var dashRetry = false

/** The current URI being used (may be modified for DASH retry) */
private var currentUri: Uri = uri
/** The current PlayerView, kept so the retry can re-attach after recreating the player */
private var mPlayerView: PlayerView? = null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not use hungarian notation.


/** The current state of this agent */
private var currentState = ExoAgentState.IDLE
Expand All @@ -98,12 +98,20 @@ class ExoAgent private constructor(val uri: Uri) {

/** Creates a media source for the given URI */
private fun createMediaSource(sourceUri: Uri) = run {
val mediaItem = MediaItem.fromUri(sourceUri)
when (Util.inferContentType(sourceUri)) {
C.CONTENT_TYPE_SS -> SsMediaSource.Factory(DefaultSsChunkSource.Factory(DATA_SOURCE_FACTORY), DATA_SOURCE_FACTORY).createMediaSource(mediaItem)
C.CONTENT_TYPE_DASH -> DashMediaSource.Factory(DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY), DATA_SOURCE_FACTORY).createMediaSource(mediaItem)
C.CONTENT_TYPE_HLS -> HlsMediaSource.Factory(DefaultHlsDataSourceFactory(DATA_SOURCE_FACTORY)).createMediaSource(mediaItem)
else -> ProgressiveMediaSource.Factory(DATA_SOURCE_FACTORY, DefaultExtractorsFactory()).createMediaSource(mediaItem)
// Direct CMAF/DASH URLs (e.g. Notorious) are single-use and can't be retried, so we
// detect them upfront by path suffix instead of falling back via dashRetry.
val useDash = dashRetry || sourceUri.path?.endsWith("/cmaf", ignoreCase = true) == true
if (useDash) {
val mediaItem = MediaItem.Builder().setUri(sourceUri).setMimeType(MimeTypes.APPLICATION_MPD).build()
DashMediaSource.Factory(DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY), DATA_SOURCE_FACTORY).createMediaSource(mediaItem)
} else {
val mediaItem = MediaItem.fromUri(sourceUri)
when (Util.inferContentType(sourceUri)) {
C.CONTENT_TYPE_SS -> SsMediaSource.Factory(DefaultSsChunkSource.Factory(DATA_SOURCE_FACTORY), DATA_SOURCE_FACTORY).createMediaSource(mediaItem)
C.CONTENT_TYPE_DASH -> DashMediaSource.Factory(DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY), DATA_SOURCE_FACTORY).createMediaSource(mediaItem)
C.CONTENT_TYPE_HLS -> HlsMediaSource.Factory(DefaultHlsDataSourceFactory(DATA_SOURCE_FACTORY)).createMediaSource(mediaItem)
else -> ProgressiveMediaSource.Factory(DATA_SOURCE_FACTORY, DefaultExtractorsFactory()).createMediaSource(mediaItem)
}
}
}

Expand All @@ -113,6 +121,7 @@ class ExoAgent private constructor(val uri: Uri) {
* NOTE: This function MUST be called before [prepare] is called by the same client.
*/
fun attach(playerView: PlayerView, listener: ExoInfoListener) {
mPlayerView = playerView
mInfoListener = listener
mInfoListener?.onStateChanged(currentState)
if (mIsAudioOnly) mInfoListener?.setAudioOnly()
Expand All @@ -125,6 +134,7 @@ class ExoAgent private constructor(val uri: Uri) {
* NOTE: The client MUST call [attach] prior to calling this function.
*/
fun prepare(playerView: PlayerView) {
mPlayerView = playerView
if (mPlayer == null) preparePlayer()
mPlayer?.switchSurface(playerView)
}
Expand Down Expand Up @@ -173,21 +183,16 @@ class ExoAgent private constructor(val uri: Uri) {
override fun onPlayerError(exception: PlaybackException) {
val cause = exception.cause

// Check if this might be a DASH video without .mpd extension
if (cause is UnrecognizedInputFormatException &&
!triedMpdRetry &&
!currentUri.toString().endsWith(".mpd")) {

// Retry with .mpd appended
triedMpdRetry = true
currentUri = "${currentUri}.mpd".toUri()

// Reset and retry with the new URI
// If the format wasn't recognized, retry as DASH using the same URL.
// Canvas media_download URLs generate a fresh JWT on each call so a second
// request is safe.
if (cause is UnrecognizedInputFormatException && !dashRetry) {
dashRetry = true
mPlayer?.release()
mPlayer = null
preparePlayer()
mPlayerView?.let { mPlayer?.switchSurface(it) }
} else {
// Not a retryable error, or already tried retry
reset()
mInfoListener?.onError(cause)
}
Expand All @@ -210,7 +215,7 @@ class ExoAgent private constructor(val uri: Uri) {
})

mPlayer?.playWhenReady = true
mPlayer?.setMediaSource(createMediaSource(currentUri))
mPlayer?.setMediaSource(createMediaSource(uri))
mPlayer?.prepare()
}

Expand Down
Loading
Loading