From 79b1db70c6febd55d2703a0cc757333c3738d3db Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Thu, 18 Jun 2020 18:18:49 +0200 Subject: [PATCH 1/6] Reintroduce "fixes #466. Setting the background image provides for a image that is sufficiently large to fit portrait and landscape orientations. When displayed the shorter side is scaled to fit, the larger is then centered and cropped." This reverts commit e8853a4329f7d85ec11d0424e14c2c3217f42e92. --- res/layout/conversation_activity.xml | 12 +++++++++--- .../thoughtcrime/securesms/ConversationActivity.java | 8 +++++--- .../preferences/ChatBackgroundActivity.java | 6 ++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/res/layout/conversation_activity.xml b/res/layout/conversation_activity.xml index 44a0e9089..106b79d17 100644 --- a/res/layout/conversation_activity.xml +++ b/res/layout/conversation_activity.xml @@ -1,10 +1,16 @@ + + + diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index cca33b176..1554853bc 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -175,6 +175,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity private InputAwareLayout container; private View composePanel; protected Stub reminderView; + private ImageView backgroundView; private AttachmentTypeSelector attachmentTypeSelector; private AttachmentManager attachmentManager; @@ -823,6 +824,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity quickAttachmentDrawer = ViewUtil.findById(this, R.id.quick_attachment_drawer); quickAttachmentToggle = ViewUtil.findById(this, R.id.quick_attachment_toggle); inputPanel = ViewUtil.findById(this, R.id.bottom_panel); + backgroundView = ViewUtil.findById(this, R.id.conversation_background); ImageButton quickCameraToggle = ViewUtil.findById(this, R.id.quick_camera_toggle); @@ -871,13 +873,13 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity String backgroundImagePath = Prefs.getBackgroundImagePath(this); if(!backgroundImagePath.isEmpty()) { Drawable image = Drawable.createFromPath(backgroundImagePath); - getWindow().setBackgroundDrawable(image); + backgroundView.setImageDrawable(image); } else if(dynamicTheme.isDarkTheme(this)) { - getWindow().setBackgroundDrawableResource(R.drawable.background_hd_dark); + backgroundView.setImageResource(R.drawable.background_hd_dark); } else { - getWindow().setBackgroundDrawableResource(R.drawable.background_hd); + backgroundView.setImageResource(R.drawable.background_hd); } } diff --git a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java index 29fc5b06f..b0aa6fa50 100644 --- a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java +++ b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java @@ -136,13 +136,15 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity Display display = ServiceUtil.getWindowManager(context).getDefaultDisplay(); Point size = new Point(); display.getSize(size); + // resize so that the larger side fits the screen accurately + int largerSide = (size.x > size.y ? size.x : size.y); Bitmap scaledBitmap = GlideApp.with(context) .asBitmap() .load(imageUri) - .centerCrop() + .fitCenter() .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) - .submit(size.x, size.y) + .submit(largerSide, largerSide) .get(); FileOutputStream outStream = new FileOutputStream(destinationPath); scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, outStream); From 6eb1d248583564c47dccc77f8941e2c83ff3ca21 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Thu, 18 Jun 2020 18:38:28 +0200 Subject: [PATCH 2/6] #466 fixes jumping for images that are big enough. Smaller pictures look bad atm. --- res/layout/conversation_activity.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/layout/conversation_activity.xml b/res/layout/conversation_activity.xml index 106b79d17..5d118be94 100644 --- a/res/layout/conversation_activity.xml +++ b/res/layout/conversation_activity.xml @@ -8,7 +8,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="ContentDescription" - android:scaleType="centerCrop" /> + android:scaleType="matrix" /> Date: Wed, 24 Jun 2020 15:08:56 +0200 Subject: [PATCH 3/6] #466 manually scaling and cropping of background. --- .../activity_select_chat_background.xml | 2 +- res/layout/conversation_activity.xml | 4 +- .../securesms/ConversationActivity.java | 16 +- .../components/ScaleStableImageView.java | 250 ++++++++++++++++++ 4 files changed, 263 insertions(+), 9 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/components/ScaleStableImageView.java diff --git a/res/layout/activity_select_chat_background.xml b/res/layout/activity_select_chat_background.xml index 9a7b0ada7..8175e2b72 100644 --- a/res/layout/activity_select_chat_background.xml +++ b/res/layout/activity_select_chat_background.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - + android:scaleType="centerCrop" /> reminderView; - private ImageView backgroundView; + private ScaleStableImageView backgroundView; private AttachmentTypeSelector attachmentTypeSelector; private AttachmentManager attachmentManager; @@ -829,6 +830,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity ImageButton quickCameraToggle = ViewUtil.findById(this, R.id.quick_camera_toggle); container.addOnKeyboardShownListener(this); + container.addOnKeyboardHiddenListener(backgroundView); + container.addOnKeyboardShownListener(backgroundView); inputPanel.setListener(this); inputPanel.setMediaListener(this); @@ -871,16 +874,17 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity private void initializeBackground() { String backgroundImagePath = Prefs.getBackgroundImagePath(this); + Drawable background; if(!backgroundImagePath.isEmpty()) { - Drawable image = Drawable.createFromPath(backgroundImagePath); - backgroundView.setImageDrawable(image); + background = Drawable.createFromPath(backgroundImagePath); } - else if(dynamicTheme.isDarkTheme(this)) { - backgroundView.setImageResource(R.drawable.background_hd_dark); + else if(DynamicTheme.isDarkTheme(this)) { + background = getResources().getDrawable(R.drawable.background_hd_dark); } else { - backgroundView.setImageResource(R.drawable.background_hd); + background = getResources().getDrawable(R.drawable.background_hd); } + backgroundView.setImageDrawable(background); } protected void initializeActionBar() { diff --git a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java new file mode 100644 index 000000000..2d3f6910e --- /dev/null +++ b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java @@ -0,0 +1,250 @@ +package org.thoughtcrime.securesms.components; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.Log; + +import com.bumptech.glide.load.engine.DiskCacheStrategy; + +import org.thoughtcrime.securesms.mms.GlideApp; +import org.thoughtcrime.securesms.util.Util; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatImageView; + +import static android.content.res.Configuration.ORIENTATION_PORTRAIT; +import static android.content.res.Configuration.ORIENTATION_LANDSCAPE; + +/** + * An image view that maintains the size of the drawable provided. + * That means: when you set an image drawable it will be scaled to fit the screen once. + * If you rotate the screen it will be rescaled to fit. + * If you crop the screen (e.g. because the soft keyboard is displayed) the image is cropped instead. + * + * @author Angelo Fuchs + */ +public class ScaleStableImageView + extends AppCompatImageView + implements KeyboardAwareLinearLayout.OnKeyboardShownListener, KeyboardAwareLinearLayout.OnKeyboardHiddenListener +{ + + private static final String TAG = ScaleStableImageView.class.getSimpleName(); + + private Drawable defaultDrawable; + private Drawable currentDrawable; + private Map storedSizes = new HashMap<>(); + + public ScaleStableImageView(Context context) { + this(context, null); + } + + public ScaleStableImageView(Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public ScaleStableImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public void setImageDrawable(@Nullable Drawable drawable) { + defaultDrawable = drawable; + overrideDrawable(defaultDrawable); + } + + private void overrideDrawable(Drawable newDrawable) { + if(currentDrawable == newDrawable) return; + currentDrawable = newDrawable; + super.setImageDrawable(newDrawable); + } + + private int landscapeWidth = 0; + private int landscapeHeight = 0; + private int portraitWidth = 0; + private int portraitHeight = 0; + private boolean keyboardShown = false; + + @Override + protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { + if(width == 0 || height == 0) return; + final String newKey = width + "x" + height; + int orientation = getResources().getConfiguration().orientation; + boolean portrait; + if(orientation == ORIENTATION_PORTRAIT) { + portrait = true; + } else if (orientation == ORIENTATION_LANDSCAPE) { + portrait = false; + } else { + Log.i(TAG, "orientation was: " + orientation); + return; // something fishy happened. + } + + measureViewSize(width, height, oldWidth, oldHeight, portrait); + if (!(defaultDrawable instanceof BitmapDrawable)) { + return; // need Bitmap for scaling and cropping. + } + + // if the image is already fit for the screen, just show it. + if (defaultDrawable.getIntrinsicWidth() == width && + defaultDrawable.getIntrinsicHeight() == height) { + overrideDrawable(defaultDrawable); + } + + // check if we have the new one already + if (storedSizes.containsKey(newKey)) { + super.setImageDrawable(storedSizes.get(newKey)); + return; + } + + if(keyboardShown) { + // don't scale; Crop. + Drawable large; + if(portrait) + large = storedSizes.get(portraitWidth+"x"+portraitHeight); + else + large = storedSizes.get(landscapeWidth+"x"+landscapeHeight); + if(large == null) return; // no baseline. can't work. + Bitmap original = ((BitmapDrawable) large).getBitmap(); + Bitmap cropped = Bitmap.createBitmap(original, 0, 0, width, height); + Drawable croppedDrawable = new BitmapDrawable(getResources(), cropped); + overrideDrawable(croppedDrawable); + } else { + Util.runOnBackground(() -> { + Bitmap bitmap = ((BitmapDrawable) defaultDrawable).getBitmap(); + Context context = getContext(); + try { + Bitmap scaledBitmap = GlideApp.with(context) + .asBitmap() + .load(bitmap) + .centerCrop() + .skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .submit(width, height) + .get(); + Drawable rescaled = new BitmapDrawable(getResources(), scaledBitmap); + storedSizes.put(newKey, rescaled); + Util.runOnMain(() -> overrideDrawable(rescaled)); + } catch (ExecutionException | InterruptedException ex) { + Log.e(TAG, "could not rescale background", ex); + // No background set. + } + }); + } + super.onSizeChanged(width, height, oldWidth, oldHeight); + } + + private void measureViewSize(int width, int height, int oldWidth, int oldHeight, boolean portrait) { + if(portraitWidth != 0 && portraitHeight != 0 && landscapeWidth != 0 && landscapeHeight != 0) return; + + if(oldWidth == 0 && oldHeight == 0) { // screen just opened from inside the app + if(portrait) { // portrait + portraitHeight = height; + portraitWidth = width; + } else { // landscape + landscapeHeight = height; + landscapeWidth = width; + } + } else { + if(oldWidth == portraitWidth) { // was in portrait + if(!portrait) { // rotate to landscape + landscapeHeight = height; + landscapeWidth = width; + } + } else if (oldHeight == landscapeHeight) { + if(portrait) { + portraitHeight = height; + portraitWidth = width; + } + } + } + } + + // opening in portrait + // Keyboard hidden + // 0x0 to 1080x1704 + + // opening in landscape + // Keyboard hidden + // 0x0 to 1776x1008 + + // opening keyboard in portrait + // Keyboard shown + // 1080x1704 to 1080x914 + // closing keyboard in portrait + // Keyboard hidden + // 1080x914 to 1080x1704 + + // opening keyboard in landscape + // (nothing) + // closing keyboard in landscape + // (nothing) + + // going from landscape to portrait + // Keyboard hidden + // 1776x1008 to 1080x1704 + + // going from portrait to landscape + // Keyboard hidden + // 1080x1704 to 1776x1008 + + // going with open keyboard from landscape to portrait + // Keyboard hidden + // 1776x1008 to 1080x1704 + // Keyboard shown + // 1080x1704 to 1080x914 + + // going with open keyboard from portrait to landscape + // Keyboard hidden + // Keyboard shown + // Keyboard hidden + // 1080x914 to 1776x1008 + + // locking screen in portrait + // (--) + // locking screen in portrait with keyboard open + // Keyboard hidden + // 1080x914 to 1080x1704 + + // unlocking screen in portrait + // -- + // unlocking screen in portrait (keyboard was open before lock) + // Keyboard shown + // 1080x1704 to 1080x914 + + // locking screen in landscape + // Keyboard hidden + // 1776x1008 to 1080x1704 + + // locking screen in landscape with keyboard open + // Keyboard hidden + // 1776x1008 to 1080x1704 + + // unlocking screen in landscape + // Keyboard hidden + // 1080x1704 to 1776x1008 + + // unlocking screen in landscape (keyboard was open before lock) + // Keyboard hidden + // 1776x1008 to 1080x1704 + // Keyboard hidden + // 1080x1704 to 1776x1008 + + @Override + public void onKeyboardHidden() { + keyboardShown = false; + Log.i(TAG, "Keyboard hidden"); + } + + @Override + public void onKeyboardShown() { + keyboardShown = true; + Log.i(TAG, "Keyboard shown"); + } +} From 24e530d3840ceab0b060c0aa6e480a4a40b989ee Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Wed, 24 Jun 2020 15:15:12 +0200 Subject: [PATCH 4/6] cleanup --- .../components/ScaleStableImageView.java | 102 +++--------------- 1 file changed, 16 insertions(+), 86 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java index 2d3f6910e..be687f0a3 100644 --- a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java +++ b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java @@ -32,8 +32,7 @@ import static android.content.res.Configuration.ORIENTATION_LANDSCAPE; */ public class ScaleStableImageView extends AppCompatImageView - implements KeyboardAwareLinearLayout.OnKeyboardShownListener, KeyboardAwareLinearLayout.OnKeyboardHiddenListener -{ + implements KeyboardAwareLinearLayout.OnKeyboardShownListener, KeyboardAwareLinearLayout.OnKeyboardHiddenListener { private static final String TAG = ScaleStableImageView.class.getSimpleName(); @@ -60,7 +59,7 @@ public class ScaleStableImageView } private void overrideDrawable(Drawable newDrawable) { - if(currentDrawable == newDrawable) return; + if (currentDrawable == newDrawable) return; currentDrawable = newDrawable; super.setImageDrawable(newDrawable); } @@ -73,11 +72,11 @@ public class ScaleStableImageView @Override protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { - if(width == 0 || height == 0) return; + if (width == 0 || height == 0) return; final String newKey = width + "x" + height; int orientation = getResources().getConfiguration().orientation; boolean portrait; - if(orientation == ORIENTATION_PORTRAIT) { + if (orientation == ORIENTATION_PORTRAIT) { portrait = true; } else if (orientation == ORIENTATION_LANDSCAPE) { portrait = false; @@ -103,14 +102,14 @@ public class ScaleStableImageView return; } - if(keyboardShown) { + if (keyboardShown) { // don't scale; Crop. Drawable large; - if(portrait) - large = storedSizes.get(portraitWidth+"x"+portraitHeight); + if (portrait) + large = storedSizes.get(portraitWidth + "x" + portraitHeight); else - large = storedSizes.get(landscapeWidth+"x"+landscapeHeight); - if(large == null) return; // no baseline. can't work. + large = storedSizes.get(landscapeWidth + "x" + landscapeHeight); + if (large == null) return; // no baseline. can't work. Bitmap original = ((BitmapDrawable) large).getBitmap(); Bitmap cropped = Bitmap.createBitmap(original, 0, 0, width, height); Drawable croppedDrawable = new BitmapDrawable(getResources(), cropped); @@ -141,10 +140,11 @@ public class ScaleStableImageView } private void measureViewSize(int width, int height, int oldWidth, int oldHeight, boolean portrait) { - if(portraitWidth != 0 && portraitHeight != 0 && landscapeWidth != 0 && landscapeHeight != 0) return; + if (portraitWidth != 0 && portraitHeight != 0 && landscapeWidth != 0 && landscapeHeight != 0) + return; - if(oldWidth == 0 && oldHeight == 0) { // screen just opened from inside the app - if(portrait) { // portrait + if (oldWidth == 0 && oldHeight == 0) { // screen just opened from inside the app + if (portrait) { // portrait portraitHeight = height; portraitWidth = width; } else { // landscape @@ -152,13 +152,13 @@ public class ScaleStableImageView landscapeWidth = width; } } else { - if(oldWidth == portraitWidth) { // was in portrait - if(!portrait) { // rotate to landscape + if (oldWidth == portraitWidth) { // was in portrait + if (!portrait) { // rotate to landscape landscapeHeight = height; landscapeWidth = width; } } else if (oldHeight == landscapeHeight) { - if(portrait) { + if (portrait) { portraitHeight = height; portraitWidth = width; } @@ -166,76 +166,6 @@ public class ScaleStableImageView } } - // opening in portrait - // Keyboard hidden - // 0x0 to 1080x1704 - - // opening in landscape - // Keyboard hidden - // 0x0 to 1776x1008 - - // opening keyboard in portrait - // Keyboard shown - // 1080x1704 to 1080x914 - // closing keyboard in portrait - // Keyboard hidden - // 1080x914 to 1080x1704 - - // opening keyboard in landscape - // (nothing) - // closing keyboard in landscape - // (nothing) - - // going from landscape to portrait - // Keyboard hidden - // 1776x1008 to 1080x1704 - - // going from portrait to landscape - // Keyboard hidden - // 1080x1704 to 1776x1008 - - // going with open keyboard from landscape to portrait - // Keyboard hidden - // 1776x1008 to 1080x1704 - // Keyboard shown - // 1080x1704 to 1080x914 - - // going with open keyboard from portrait to landscape - // Keyboard hidden - // Keyboard shown - // Keyboard hidden - // 1080x914 to 1776x1008 - - // locking screen in portrait - // (--) - // locking screen in portrait with keyboard open - // Keyboard hidden - // 1080x914 to 1080x1704 - - // unlocking screen in portrait - // -- - // unlocking screen in portrait (keyboard was open before lock) - // Keyboard shown - // 1080x1704 to 1080x914 - - // locking screen in landscape - // Keyboard hidden - // 1776x1008 to 1080x1704 - - // locking screen in landscape with keyboard open - // Keyboard hidden - // 1776x1008 to 1080x1704 - - // unlocking screen in landscape - // Keyboard hidden - // 1080x1704 to 1776x1008 - - // unlocking screen in landscape (keyboard was open before lock) - // Keyboard hidden - // 1776x1008 to 1080x1704 - // Keyboard hidden - // 1080x1704 to 1776x1008 - @Override public void onKeyboardHidden() { keyboardShown = false; From 83f77da502bf728cdc4ac7c8aaf145a10c830687 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Wed, 24 Jun 2020 15:25:33 +0200 Subject: [PATCH 5/6] reset stored sizes after setting a new one. --- .../thoughtcrime/securesms/components/ScaleStableImageView.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java index be687f0a3..c34e14ff8 100644 --- a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java +++ b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java @@ -55,6 +55,7 @@ public class ScaleStableImageView @Override public void setImageDrawable(@Nullable Drawable drawable) { defaultDrawable = drawable; + storedSizes.clear(); overrideDrawable(defaultDrawable); } From 87beb90b13533f111636da9a413d33f8e77210a0 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Mon, 13 Jul 2020 11:18:37 +0200 Subject: [PATCH 6/6] #466 only measure screen if background is compatible at all. --- .../thoughtcrime/securesms/components/ScaleStableImageView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java index c34e14ff8..b817fae1a 100644 --- a/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java +++ b/src/org/thoughtcrime/securesms/components/ScaleStableImageView.java @@ -86,11 +86,11 @@ public class ScaleStableImageView return; // something fishy happened. } - measureViewSize(width, height, oldWidth, oldHeight, portrait); if (!(defaultDrawable instanceof BitmapDrawable)) { return; // need Bitmap for scaling and cropping. } + measureViewSize(width, height, oldWidth, oldHeight, portrait); // if the image is already fit for the screen, just show it. if (defaultDrawable.getIntrinsicWidth() == width && defaultDrawable.getIntrinsicHeight() == height) {