Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in addMedia on Android 28 and below #2061

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class Service(
override fun onNext(value: MaestroAndroid.AddMediaRequest) {
if (outputStream == null) {
outputStream = MediaStorage.getOutputStream(
InstrumentationRegistry.getInstrumentation()
.context,
value.mediaName,
value.mediaExt
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package dev.mobile.maestro

import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.ContentValues
import android.content.Context
import android.content.pm.PackageManager
import android.provider.MediaStore
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import java.io.OutputStream

object MediaStorage {

fun getOutputStream(mediaName: String, mediaExt: String): OutputStream? {
fun getOutputStream(context: Context, mediaName: String, mediaExt: String): OutputStream? {
if (context.checkSelfPermission(WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Log.e("MediaStorage", "Permission $WRITE_EXTERNAL_STORAGE not granted")
return null
}
val uri = when (mediaExt) {
Service.FileType.JPG.ext,
Service.FileType.PNG.ext,
Expand Down
3 changes: 3 additions & 0 deletions maestro-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
tools:ignore="ProtectedPermissions" />

<application
android:usesCleartextTraffic="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ class AndroidDriver(

override fun addMedia(mediaFiles: List<File>) {
LOGGER.info("[Start] Adding media files")
dadb.shell("pm grant dev.mobile.maestro android.permission.WRITE_EXTERNAL_STORAGE")
mediaFiles.forEach { addMediaToDevice(it) }
LOGGER.info("[Done] Adding media files")
}
Expand Down
Binary file modified maestro-client/src/main/resources/maestro-app.apk
Binary file not shown.
Binary file modified maestro-client/src/main/resources/maestro-server.apk
Binary file not shown.
Loading