Skip to content

Commit

Permalink
Fix getParcelableExtra not working on Android SDK <33
Browse files Browse the repository at this point in the history
  • Loading branch information
KasemJaffer committed Jan 29, 2024
1 parent 1190be7 commit 8b8932a
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.media.MediaMetadataRetriever
import android.media.MediaMetadataRetriever.METADATA_KEY_DURATION
import android.media.MediaMetadataRetriever.OPTION_CLOSEST_SYNC
import android.net.Uri
import android.os.Parcelable
import android.os.Build
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
Expand Down Expand Up @@ -116,13 +118,13 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
}

Intent.ACTION_SEND -> {
val uri = intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
val uri = intent.parcelable<Uri>(Intent.EXTRA_STREAM)
val text = intent.getStringExtra(Intent.EXTRA_TEXT)
toJsonObject(uri, text, intent.type)?.let { JSONArray(listOf(it)) }
}

Intent.ACTION_SEND_MULTIPLE -> {
val uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java)
val uris = intent.parcelableArrayList<Uri>(Intent.EXTRA_STREAM)
val mimeTypes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES)

uris?.mapIndexedNotNull { index, uri ->
Expand Down Expand Up @@ -204,4 +206,14 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
handleIntent(intent, false)
return false
}

inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}

inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
Build.VERSION.SDK_INT >= 33 -> getParcelableArrayListExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}
}

0 comments on commit 8b8932a

Please sign in to comment.