Skip to content

Commit c57bcd7

Browse files
committed
Glimpse: Create a Media out of thin air if not in media provider
Change-Id: I5e7333f096a98ac06e1b3ac1e630c380f197310d
1 parent c9a90aa commit c57bcd7

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

app/src/main/java/org/lineageos/glimpse/viewmodels/IntentsViewModel.kt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.lineageos.glimpse.models.MediaType
3535
import org.lineageos.glimpse.models.RequestStatus
3636
import org.lineageos.glimpse.models.RequestStatus.Companion.map
3737
import org.lineageos.glimpse.utils.MimeUtils
38+
import java.util.Date
3839

3940
/**
4041
* A view model used by activities to handle intents.
@@ -270,8 +271,38 @@ class IntentsViewModel(application: Application) : GlimpseViewModel(application)
270271
is RequestStatus.Success -> it.data
271272

272273
is RequestStatus.Error -> {
273-
Log.e(LOG_TAG, "Cannot get media of $uri, error: ${it.error}")
274-
null
274+
// Build a `Media` object with the available data
275+
Log.i(
276+
LOG_TAG,
277+
"Cannot get media object from media provider, trying manual fallback"
278+
)
279+
when (type) {
280+
MediaType.IMAGE,
281+
MediaType.VIDEO ->
282+
Media(
283+
uri,
284+
type,
285+
applicationContext.contentResolver.getType(uri) ?: run {
286+
Log.e(LOG_TAG, "Cannot get media type of $uri")
287+
return null
288+
},
289+
uri,
290+
albumName = null,
291+
displayName = null,
292+
isFavorite = false,
293+
isTrashed = false,
294+
dateAdded = Date(),
295+
dateModified = Date(),
296+
width = 0,
297+
height = 0,
298+
orientation = 0,
299+
)
300+
301+
else -> {
302+
Log.e(LOG_TAG, "Cannot build media object for $uri")
303+
null
304+
}
305+
}
275306
}
276307
}
277308
}

0 commit comments

Comments
 (0)