π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden Android wallpaper preview
Neeraj Gupta764ce7ec5618
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/AndroidManifest.xml b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/AndroidManifest.xmlindex 70cbe0543f..0b6544c54c 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/AndroidManifest.xml+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/AndroidManifest.xml@@ -3,17 +3,12 @@<application android:supportsRtl="true"><activityandroid:name="io.ente.photos.WallpaperActivity"- android:exported="true"+ android:exported="false"android:enableOnBackInvokedCallback="true"android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"android:hardwareAccelerated="true"android:label="@string/wallpaper"android:theme="@style/WallpaperTheme">- <intent-filter>- <action android:name="android.intent.action.ATTACH_DATA" />- <category android:name="android.intent.category.DEFAULT" />- <data android:mimeType="image/*" />- </intent-filter></activity></application></manifest>diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperService.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperService.ktindex 8bc3403791..40392601d1 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperService.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperService.kt@@ -16,6 +16,7 @@ import androidx.exifinterface.media.ExifInterfaceimport java.io.IOExceptionimport kotlin.math.maximport kotlin.math.min+import kotlin.math.sqrtinternal enum class Destination(val flags: Int) {HOME(WallpaperManager.FLAG_SYSTEM),@@ -25,6 +26,10 @@ internal enum class Destination(val flags: Int) {@android.annotation.TargetApi(24)internal class WallpaperService(context: Context) {+ private companion object {+ const val maxDecodedPixels = 4_000_000L+ }+private val resolver = context.contentResolverprivate val manager = WallpaperManager.getInstance(context)@@ -45,7 +50,7 @@ internal class WallpaperService(context: Context) {requireNotNull(BitmapRegionDecoder.newInstance(it, false))}} catch (_: IOException) {- val image = decode(uri, crop.size)+ val image = decode(uri, 2 * max(crop.size.x, crop.size.y), Long.MAX_VALUE)return try { crop.render(image) } finally { image.recycle() }}try {@@ -91,28 +96,39 @@ internal class WallpaperService(context: Context) {}}- fun decode(uri: Uri, displaySize: Point): Bitmap {- val maxSize = 2 * max(displaySize.x, displaySize.y)+ fun preview(uri: Uri, displaySize: Point): Bitmap {+ val maxSize = max(displaySize.x, displaySize.y)+ return decode(uri, maxSize, maxDecodedPixels)+ }++ private fun decode(uri: Uri, maxSize: Int, maxPixels: Long): Bitmap {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {return ImageDecoder.decodeBitmap(ImageDecoder.createSource(resolver, uri)) { decoder, info, _ ->decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE- val scale = minOf(1.0, maxSize.toDouble() / max(info.size.width, info.size.height))+ val scale = minOf(+ 1.0,+ maxSize.toDouble() / max(info.size.width, info.size.height),+ sqrt(maxPixels.toDouble() / (info.size.width.toLong() * info.size.height)),+ )decoder.setTargetSize(max(1, (info.size.width * scale).toInt()),max(1, (info.size.height * scale).toInt()),)}}- return decodeSampled(uri, maxSize)+ return decodeSampled(uri, maxSize, maxPixels)}- fun decodeSampled(uri: Uri, maxSize: Int): Bitmap {+ private fun decodeSampled(uri: Uri, maxSize: Int, maxPixels: Long): Bitmap {val options = BitmapFactory.Options().apply {inJustDecodeBounds = trueinSampleSize = 1}resolver.openInputStream(uri)!!.use { BitmapFactory.decodeStream(it, null, options) }- while (max(options.outWidth, options.outHeight) / options.inSampleSize > maxSize) {+ while (+ max(options.outWidth, options.outHeight) / options.inSampleSize > maxSize ||+ options.outWidth.toLong() * options.outHeight / (options.inSampleSize.toLong() * options.inSampleSize) > maxPixels+ ) {options.inSampleSize *= 2}options.inJustDecodeBounds = falsediff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperSession.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperSession.ktindex 26ce1649fe..3cf4597548 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperSession.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/wallpaper/WallpaperSession.kt@@ -12,15 +12,19 @@ import java.util.concurrent.Executors@android.annotation.TargetApi(24)internal class WallpaperSession(context: Context, private val uri: Uri?, private val size: Point) : AutoCloseable {+ private companion object {+ const val maxPreviewBytes = 24 * 1024 * 1024+ }+private val service = WallpaperService(context.applicationContext)private val executor = Executors.newSingleThreadExecutor()fun preview(): CompletableFuture<ByteArray> = CompletableFuture.supplyAsync({require(uri?.scheme == ContentResolver.SCHEME_CONTENT) { "Expected an image content URI" }if (!service.isAvailable) throw UnsupportedOperationException("Wallpaper changes are not allowed")- val bitmap = service.decode(requireNotNull(uri), size)+ val bitmap = service.preview(requireNotNull(uri), size)try {- ByteArrayOutputStream().use {+ BoundedByteArrayOutputStream(maxPreviewBytes).use {bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)it.toByteArray()}@@ -40,3 +44,19 @@ internal class WallpaperSession(context: Context, private val uri: Uri?, privateoverride fun close() { executor.shutdown() }}++private class BoundedByteArrayOutputStream(private val limit: Int) : ByteArrayOutputStream() {+ override fun write(value: Int) {+ checkSize(1)+ super.write(value)+ }++ override fun write(bytes: ByteArray, offset: Int, length: Int) {+ checkSize(length)+ super.write(bytes, offset, length)+ }++ private fun checkSize(length: Int) {+ check(count.toLong() + length <= limit) { "Wallpaper preview is too large" }+ }+}diff --git a/mobile/apps/photos/plugins/media_extension/android/src/main/kotlin/io/ente/photos/media_extension/MediaExtensionPlugin.kt b/mobile/apps/photos/plugins/media_extension/android/src/main/kotlin/io/ente/photos/media_extension/MediaExtensionPlugin.ktindex c8267a8e6d..9072f3ae0f 100644--- a/mobile/apps/photos/plugins/media_extension/android/src/main/kotlin/io/ente/photos/media_extension/MediaExtensionPlugin.kt+++ b/mobile/apps/photos/plugins/media_extension/android/src/main/kotlin/io/ente/photos/media_extension/MediaExtensionPlugin.kt@@ -2,6 +2,7 @@ package io.ente.photos.media_extensionimport android.app.Activityimport android.content.ClipData+import android.content.ComponentNameimport android.content.ContentResolverimport android.content.Contextimport android.content.Intent@@ -420,7 +421,9 @@ class MediaExtensionPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).putExtra("mimeType", mimeType).setDataAndType(getShareableUri(activity!!.applicationContext, uri), mimeType)- val started = safeStartActivityChooser(title, intent)+ val wallpaperIntent = Intent(intent)+ .setComponent(ComponentName(context, "io.ente.photos.WallpaperActivity"))+ val started = safeStartActivityChooser(title, intent, arrayOf(wallpaperIntent))result.success(started)}@@ -476,13 +479,22 @@ class MediaExtensionPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,}}- private fun safeStartActivityChooser(title: String?, intent: Intent): Boolean {- if (activity?.let { intent.resolveActivity(it.packageManager) } == null) {+ private fun safeStartActivityChooser(+ title: String?,+ intent: Intent,+ initialIntents: Array<Intent> = emptyArray(),+ ): Boolean {+ val currentActivity = activity ?: return false+ if (intent.resolveActivity(currentActivity.packageManager) == null) {Log.i(logTag, " intent=$intent resolved activity return null")//return false}try {- activity?.startActivity(Intent.createChooser(intent, title))+ val chooser = Intent.createChooser(intent, title)+ if (initialIntents.isNotEmpty()) {+ chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, initialIntents)+ }+ currentActivity.startActivity(chooser)return true} catch (e: SecurityException) {if (intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION != 0) {@@ -491,7 +503,7 @@ class MediaExtensionPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,// so we retry without itLog.i(logTag, "retry intent=$intent without FLAG_GRANT_WRITE_URI_PERMISSION")intent.flags = intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION.inv()- return safeStartActivityChooser(title, intent)+ return safeStartActivityChooser(title, intent, initialIntents)} else {Log.w(logTag, "failed to start activity chooser for intent=$intent", e)}