Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Fix Android widget image orientation (#12514)

Aman Rajd51a5c8ad1ab
diff --git a/mobile/apps/photos/android/app/build.gradle b/mobile/apps/photos/android/app/build.gradle
index 37b3ae6961..2d1493b4f3 100644
--- a/mobile/apps/photos/android/app/build.gradle
+++ b/mobile/apps/photos/android/app/build.gradle
@@ -194,6 +194,7 @@ flutter {
}
dependencies {
+ implementation 'androidx.exifinterface:exifinterface:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteAlbumsWidgetProvider.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteAlbumsWidgetProvider.kt
index e4976b6d2c..8a00ac23cb 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteAlbumsWidgetProvider.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteAlbumsWidgetProvider.kt
@@ -3,8 +3,6 @@ package io.ente.photos
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
-import android.graphics.Bitmap
-import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.util.Log
@@ -52,12 +50,9 @@ class EnteAlbumsWidgetProvider : HomeWidgetProvider() {
null
)
}
- var imageExists: Boolean = false
- if (imagePath != null) {
- val imageFile = File(imagePath)
- imageExists = imageFile.exists()
- }
- if (imageExists) {
+ val existingImagePath =
+ imagePath?.takeIf { File(it).exists() }
+ if (existingImagePath != null) {
val data =
widgetData.getString(
"albums_widget_${randomNumber}_data",
@@ -92,7 +87,7 @@ class EnteAlbumsWidgetProvider : HomeWidgetProvider() {
Log.d(
"EnteAlbumsWidgetProvider",
- "Image exists: $imagePath"
+ "Image exists: $existingImagePath"
)
val hideFlags =
widgetData.getInt(
@@ -137,8 +132,8 @@ class EnteAlbumsWidgetProvider : HomeWidgetProvider() {
View.GONE
)
- val bitmap: Bitmap =
- BitmapFactory.decodeFile(imagePath)
+ val bitmap =
+ decodeWidgetBitmap(existingImagePath)
setImageViewBitmap(R.id.widget_img, bitmap)
setTextViewText(R.id.widget_title, title)
setTextViewText(
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteMemoryWidgetProvider.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteMemoryWidgetProvider.kt
index 835cfa29e1..b3f8b8ee12 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteMemoryWidgetProvider.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteMemoryWidgetProvider.kt
@@ -3,8 +3,6 @@ package io.ente.photos
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
-import android.graphics.Bitmap
-import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.util.Log
@@ -46,12 +44,9 @@ class EnteMemoryWidgetProvider : HomeWidgetProvider() {
null
)
}
- var imageExists: Boolean = false
- if (imagePath != null) {
- val imageFile = File(imagePath)
- imageExists = imageFile.exists()
- }
- if (imageExists) {
+ val existingImagePath =
+ imagePath?.takeIf { File(it).exists() }
+ if (existingImagePath != null) {
val data =
widgetData.getString(
"memory_widget_${randomNumber}_data",
@@ -85,7 +80,7 @@ class EnteMemoryWidgetProvider : HomeWidgetProvider() {
Log.d(
"EnteMemoryWidgetProvider",
- "Image exists: $imagePath"
+ "Image exists: $existingImagePath"
)
val hideFlags =
widgetData.getInt(
@@ -130,8 +125,8 @@ class EnteMemoryWidgetProvider : HomeWidgetProvider() {
View.GONE
)
- val bitmap: Bitmap =
- BitmapFactory.decodeFile(imagePath)
+ val bitmap =
+ decodeWidgetBitmap(existingImagePath)
setImageViewBitmap(R.id.widget_img, bitmap)
setTextViewText(R.id.widget_title, title)
setTextViewText(
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EntePeopleWidgetProvider.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EntePeopleWidgetProvider.kt
index 78edf4ae58..8da512fb4e 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EntePeopleWidgetProvider.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EntePeopleWidgetProvider.kt
@@ -3,8 +3,6 @@ package io.ente.photos
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
-import android.graphics.Bitmap
-import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.util.Log
@@ -51,12 +49,9 @@ class EntePeopleWidgetProvider : HomeWidgetProvider() {
null
)
}
- var imageExists: Boolean = false
- if (imagePath != null) {
- val imageFile = File(imagePath)
- imageExists = imageFile.exists()
- }
- if (imageExists) {
+ val existingImagePath =
+ imagePath?.takeIf { File(it).exists() }
+ if (existingImagePath != null) {
val data =
widgetData.getString(
"people_widget_${randomNumber}_data",
@@ -91,7 +86,7 @@ class EntePeopleWidgetProvider : HomeWidgetProvider() {
Log.d(
"EntePeopleWidgetProvider",
- "Image exists: $imagePath"
+ "Image exists: $existingImagePath"
)
val hideFlags =
widgetData.getInt(
@@ -136,8 +131,8 @@ class EntePeopleWidgetProvider : HomeWidgetProvider() {
View.GONE
)
- val bitmap: Bitmap =
- BitmapFactory.decodeFile(imagePath)
+ val bitmap =
+ decodeWidgetBitmap(existingImagePath)
setImageViewBitmap(R.id.widget_img, bitmap)
setTextViewText(R.id.widget_title, title)
setTextViewText(
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/WidgetImageDecoder.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/WidgetImageDecoder.kt
new file mode 100644
index 0000000000..92ea977bb7
--- /dev/null
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/WidgetImageDecoder.kt
@@ -0,0 +1,36 @@
+package io.ente.photos
+
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.Matrix
+import androidx.exifinterface.media.ExifInterface
+
+internal fun decodeWidgetBitmap(imagePath: String): Bitmap {
+ val bitmap: Bitmap = BitmapFactory.decodeFile(imagePath)
+
+ return try {
+ val exif = ExifInterface(imagePath)
+ val rotationDegrees = exif.rotationDegrees
+ val isFlipped = exif.isFlipped
+ if (rotationDegrees == 0 && !isFlipped) return bitmap
+
+ val transform =
+ Matrix().apply {
+ if (isFlipped) preScale(-1f, 1f)
+ if (rotationDegrees != 0) postRotate(rotationDegrees.toFloat())
+ }
+ Bitmap.createBitmap(
+ bitmap,
+ 0,
+ 0,
+ bitmap.width,
+ bitmap.height,
+ transform,
+ true
+ ).also { corrected ->
+ if (corrected !== bitmap) bitmap.recycle()
+ }
+ } catch (_: Exception) {
+ bitmap
+ }
+}
diff --git a/mobile/apps/photos/changes/android-widget-image-orientation.md b/mobile/apps/photos/changes/android-widget-image-orientation.md
new file mode 100644
index 0000000000..1a937405d6
--- /dev/null
+++ b/mobile/apps/photos/changes/android-widget-image-orientation.md
@@ -0,0 +1 @@
+- Fixed incorrectly rotated photos in Android home screen widgets.