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 Android App Restart not Triggering Initial Data #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -29,7 +29,7 @@ private const val EVENTS_CHANNEL_MEDIA = "receive_sharing_intent/events-media"
private const val EVENTS_CHANNEL_TEXT = "receive_sharing_intent/events-text"

class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandler,
EventChannel.StreamHandler, NewIntentListener {
EventChannel.StreamHandler, NewIntentListener {

private var initialMedia: JSONArray? = null
private var latestMedia: JSONArray? = null
Expand All @@ -43,6 +43,8 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
private var binding: ActivityPluginBinding? = null
private lateinit var applicationContext: Context

private var initialOverride: Boolean = false

private fun setupCallbackChannels(binaryMessenger: BinaryMessenger) {
val mChannel = MethodChannel(binaryMessenger, MESSAGES_CHANNEL)
mChannel.setMethodCallHandler(this)
Expand Down Expand Up @@ -104,6 +106,7 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
latestMedia = null
initialText = null
latestText = null
initialOverride = false
result.success(null)
}
else -> result.notImplemented()
Expand Down Expand Up @@ -149,27 +152,27 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
val thumbnail = getThumbnail(path, type)
val duration = getDuration(path, type)
JSONArray().put(
JSONObject()
.put("path", path)
.put("type", type.ordinal)
.put("thumbnail", thumbnail)
.put("duration", duration)
JSONObject()
.put("path", path)
.put("type", type.ordinal)
.put("thumbnail", thumbnail)
.put("duration", duration)
)
} else null
}
Intent.ACTION_SEND_MULTIPLE -> {
val uris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
val value = uris?.mapNotNull { uri ->
val path = FileDirectory.getAbsolutePath(applicationContext, uri)
?: return@mapNotNull null
?: return@mapNotNull null
val type = getMediaType(path)
val thumbnail = getThumbnail(path, type)
val duration = getDuration(path, type)
return@mapNotNull JSONObject()
.put("path", path)
.put("type", type.ordinal)
.put("thumbnail", thumbnail)
.put("duration", duration)
.put("path", path)
.put("type", type.ordinal)
.put("thumbnail", thumbnail)
.put("duration", duration)
}?.toList()
if (value != null) JSONArray(value) else null
}
Expand All @@ -192,7 +195,7 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
val videoFile = File(path)
val targetFile = File(applicationContext.cacheDir, "${videoFile.name}.png")
val bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND)
?: return null
?: return null
FileOutputStream(targetFile).use { out ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
}
Expand All @@ -214,6 +217,7 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
initialOverride = true
this.binding = binding
binding.addOnNewIntentListener(this)
handleIntent(binding.activity.intent, true)
Expand All @@ -233,7 +237,8 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
}

override fun onNewIntent(intent: Intent): Boolean {
handleIntent(intent, false)
handleIntent(intent, false || initialOverride)
initialOverride = false
return false
}
}