Skip to content

Commit

Permalink
Releases/v0.2.0 (#28)
Browse files Browse the repository at this point in the history
## Improvements

* fix: Hide some constructors to prevent unintended use by java callers (#23)
* feat: Add Performance Metrics Tracking (#24)



Co-authored-by: Emily Dixon <[email protected]>
  • Loading branch information
daytime-em authored Apr 12, 2023
1 parent 7323173 commit bbc5dbe
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.example.demo

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.mux.video.upload.api.MuxUpload
import com.mux.video.vod.demo.R
import com.mux.video.vod.demo.databinding.ActivityVideoListBinding
import com.mux.video.vod.demo.mediastore.model.UploadingVideo

class MediaStoreVideosActivity : AppCompatActivity() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@ import android.os.Build
import android.provider.MediaStore
import android.provider.MediaStore.Video.VideoColumns
import android.util.Log
import android.widget.Toast
import androidx.core.text.htmlEncode
import androidx.core.util.Consumer
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.mux.video.upload.api.MuxUpload
import com.mux.video.vod.demo.R
import com.mux.video.vod.demo.mediastore.model.UploadingVideo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
import kotlin.collections.ArrayList

/**
* Queries the device's content provider for saved videos to upload
Expand Down Expand Up @@ -73,6 +66,8 @@ class MediaStoreVideosViewModel(private val app: Application) : AndroidViewModel
*/
@Throws
private suspend fun copyIntoTempFile(contentUri: Uri): File {
// Create a unique name for our temp file. There are a ton of ways to do this, but this one is
// pretty easy to implement and protects from unsafe characters
val basename = android.util.Base64.encode(
contentUri.pathSegments.joinToString(separator = "-").encodeToByteArray(),
0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import java.io.File
* Represents a video saved on the device, accessed via a ContentResolver. To use with the SDK, we
* need to copy the file to our local storage
*/
data class UploadingVideo (
data class UploadingVideo(
val title: String,
val file: File,
val fromApp: String,
val date: String,
val upload: MuxUpload? = null
)
)
18 changes: 6 additions & 12 deletions app/src/main/res/layout/list_item_uploading_video.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
android:contentDescription="@string/pause_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
app:layout_constraintBottom_toBottomOf="parent" />

<!-- TODO: A thumbnail from the content provider would be pretty easy-->
<TextView
Expand All @@ -25,8 +24,7 @@
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Video Title"
/>
tools:text="Video Title" />

<TextView
android:id="@+id/mediastore_video_filename"
Expand All @@ -35,8 +33,7 @@
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
app:layout_constraintStart_toStartOf="@id/mediastore_video_title"
app:layout_constraintTop_toBottomOf="@id/mediastore_video_title"
tools:text="Video filename"
/>
tools:text="Video filename" />

<TextView
android:id="@+id/mediastore_video_filesize"
Expand All @@ -45,8 +42,7 @@
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
app:layout_constraintStart_toStartOf="@id/mediastore_video_filename"
app:layout_constraintTop_toBottomOf="@id/mediastore_video_filename"
tools:text="1000 bytes"
/>
tools:text="1000 bytes" />

<TextView
android:id="@+id/mediastore_video_date"
Expand All @@ -55,8 +51,7 @@
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
app:layout_constraintStart_toStartOf="@id/mediastore_video_filesize"
app:layout_constraintTop_toBottomOf="@id/mediastore_video_filesize"
tools:text="on 2/2/2023"
/>
tools:text="on 2/2/2023" />

<ProgressBar
android:id="@+id/mediastore_video_progress"
Expand All @@ -66,7 +61,6 @@
android:layout_marginEnd="12dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
app:layout_constraintStart_toStartOf="@id/mediastore_video_date"
app:layout_constraintTop_toBottomOf="@id/mediastore_video_date"
/>
app:layout_constraintTop_toBottomOf="@id/mediastore_video_date" />

</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 1 addition & 2 deletions app/src/test/java/com/example/demo/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.example.demo

import org.junit.Assert.assertEquals
import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
Expand Down
1 change: 1 addition & 0 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
6 changes: 2 additions & 4 deletions library/src/main/java/com/mux/video/upload/MuxUploadSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mux.video.upload
import android.content.Context
import android.util.Log
import com.mux.video.upload.internal.UploadJobFactory
import com.mux.video.upload.internal.UploadMetrics
import com.mux.video.upload.internal.initializeUploadPersistence
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand Down Expand Up @@ -56,11 +57,8 @@ object MuxUploadSdk {

@Suppress("unused")
fun initialize(appContext: Context) {
// TODO: Collect caller app metrics, get the cache directory, etc.
// Also, try not to save the appContext. Context.applicationContext is safe to hold statically
// but it makes compiler warnings

initializeUploadPersistence(appContext)
UploadMetrics.initialize(appContext)
}

/**
Expand Down
26 changes: 16 additions & 10 deletions library/src/main/java/com/mux/video/upload/api/MuxUpload.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.mux.video.upload.api

import android.net.Uri
import androidx.annotation.MainThread
import androidx.core.util.Consumer
import com.mux.video.upload.MuxUploadSdk
import com.mux.video.upload.api.MuxUpload.Builder
import com.mux.video.upload.internal.UploadInfo
import com.mux.video.upload.internal.update
import kotlinx.coroutines.*
Expand Down Expand Up @@ -111,9 +111,9 @@ class MuxUpload private constructor(
uploadInfo.uploadJob?.cancel()
uploadInfo.update(
uploadJob = null,
successChannel = null,
errorChannel = null,
progressChannel = null,
successFlow = null,
errorFlow = null,
progressFlow = null,
)
}
lastKnownState?.let { state -> progressListeners.forEach { it.onEvent(state) } }
Expand Down Expand Up @@ -172,18 +172,18 @@ class MuxUpload private constructor(
private fun newObserveProgressJob(upload: UploadInfo): Job {
// This job has up to three children, one for each of the state flows on UploadInfo
return callbackScope.launch {
upload.errorChannel?.let { flow ->
upload.errorFlow?.let { flow ->
launch { flow.collect { error -> resultListeners.forEach { it.onEvent(Result.failure(error)) } } }
}
upload.successChannel?.let { flow ->
upload.successFlow?.let { flow ->
launch {
flow.collect { state ->
lastKnownState = state
resultListeners.forEach { it.onEvent(Result.success(state)) }
}
}
}
upload.progressChannel?.let { flow ->
upload.progressFlow?.let { flow ->
launch {
flow.collect { state ->
lastKnownState = state
Expand Down Expand Up @@ -235,10 +235,11 @@ class MuxUpload private constructor(
chunkSize = 8 * 1024 * 1024, // GCP recommends at least 8M chunk size
retriesPerChunk = 3,
retryBaseTimeMs = 500,
optOut = false,
uploadJob = null,
successChannel = null,
progressChannel = null,
errorChannel = null
successFlow = null,
progressFlow = null,
errorFlow = null
)

@Suppress("unused")
Expand All @@ -253,6 +254,11 @@ class MuxUpload private constructor(
return this
}

@Suppress("unused")
fun optOutOfEventTracking(optOut: Boolean) {
uploadInfo.update(optOut = optOut)
}

@Suppress("unused")
fun retriesPerChunk(retries: Int): Builder {
uploadInfo.update(retriesPerChunk = retries)
Expand Down
18 changes: 6 additions & 12 deletions library/src/main/java/com/mux/video/upload/api/MuxUploadManager.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.mux.video.upload.api

import android.util.Log
import androidx.annotation.MainThread
import com.mux.video.upload.MuxUploadSdk
import com.mux.video.upload.internal.*
import com.mux.video.upload.internal.UploadInfo
import com.mux.video.upload.internal.assertMainThread
import com.mux.video.upload.internal.startUploadJob
import com.mux.video.upload.internal.forgetUploadState
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.FlowCollector
import java.io.File

object MuxUploadManager {
Expand Down Expand Up @@ -48,9 +42,9 @@ object MuxUploadManager {
cancelJobInner(it)
val pausedUpload = upload.update(
uploadJob = null,
progressChannel = null,
errorChannel = null,
successChannel = null,
progressFlow = null,
errorFlow = null,
successFlow = null,
)
uploadsByFilename[pausedUpload.file.absolutePath] = pausedUpload
return pausedUpload
Expand Down Expand Up @@ -106,22 +100,22 @@ object MuxUploadManager {
private fun newObserveProgressJob(upload: UploadInfo): Job {
// This job has up to three children, one for each of the state flows on UploadInfo
return mainScope.launch {
upload.progressChannel?.let { flow ->
upload.progressFlow?.let { flow ->
launch {
flow.collect { state ->
launch(Dispatchers.IO) { writeUploadState(upload, state) }
}
}
}

upload.errorChannel?.let { flow ->
upload.errorFlow?.let { flow ->
launch {
flow.collect {
jobFinished(upload, it !is CancellationException)
}
}
}
upload.successChannel?.let { flow -> launch { flow.collect { jobFinished(upload) } } }
upload.successFlow?.let { flow -> launch { flow.collect { jobFinished(upload) } } }
}
}

Expand Down
Loading

0 comments on commit bbc5dbe

Please sign in to comment.