From d65c45f96ae420df321d7a5a15b65e6d42efc621 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 4 Oct 2023 16:33:23 +0200 Subject: [PATCH 1/4] allow to set chat background per-account --- .../securesms/ConversationActivity.java | 3 ++- .../AppearancePreferenceFragment.java | 4 ++-- .../preferences/ChatBackgroundActivity.java | 16 ++++++++++------ src/org/thoughtcrime/securesms/util/Prefs.java | 8 ++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 03449391b..1ef3ca8bd 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -858,7 +858,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } private void initializeBackground() { - String backgroundImagePath = Prefs.getBackgroundImagePath(this); + String backgroundImagePath = Prefs.getBackgroundImagePath(this, dcContext.getAccountId()); Drawable background; if(!backgroundImagePath.isEmpty()) { background = Drawable.createFromPath(backgroundImagePath); @@ -892,6 +892,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity AccountManager.getInstance().switchAccount(context, accountId); dcContext = context.dcContext; fragment.dcContext = context.dcContext; + initializeBackground(); } chatId = getIntent().getIntExtra(CHAT_ID_EXTRA, -1); if(chatId == DcChat.DC_CHAT_NO_CHAT) diff --git a/src/org/thoughtcrime/securesms/preferences/AppearancePreferenceFragment.java b/src/org/thoughtcrime/securesms/preferences/AppearancePreferenceFragment.java index 944a464e9..5372c10e3 100644 --- a/src/org/thoughtcrime/securesms/preferences/AppearancePreferenceFragment.java +++ b/src/org/thoughtcrime/securesms/preferences/AppearancePreferenceFragment.java @@ -46,7 +46,7 @@ public class AppearancePreferenceFragment extends ListSummaryPreferenceFragment public void onResume() { super.onResume(); ((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.pref_appearance); - String imagePath = Prefs.getBackgroundImagePath(getContext()); + String imagePath = Prefs.getBackgroundImagePath(getContext(), dcContext.getAccountId()); String backgroundString; if(imagePath.isEmpty()){ backgroundString = this.getString(R.string.def); @@ -75,7 +75,7 @@ public class AppearancePreferenceFragment extends ListSummaryPreferenceFragment int themeIndex = Arrays.asList(themeEntryValues).indexOf(Prefs.getTheme(context)); if (themeIndex == -1) themeIndex = 0; - String imagePath = Prefs.getBackgroundImagePath(context); + String imagePath = Prefs.getBackgroundImagePath(context, DcHelper.getContext(context).getAccountId()); String backgroundString; if(imagePath.isEmpty()){ backgroundString = context.getString(R.string.def); diff --git a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java index 4baf8f09f..1718ccccd 100644 --- a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java +++ b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java @@ -23,6 +23,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy; import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity; import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.mms.AttachmentManager; import org.thoughtcrime.securesms.mms.GlideApp; import org.thoughtcrime.securesms.util.DynamicTheme; @@ -44,6 +45,8 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity Uri imageUri; Boolean imageUpdate = false; + private int accountId; + @Override protected void onCreate(Bundle savedInstanceState, boolean ready) { setContentView(R.layout.activity_select_chat_background); @@ -51,11 +54,12 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity defaultButton = findViewById(R.id.set_default_button); galleryButton = findViewById(R.id.from_gallery_button); preview = findViewById(R.id.preview); + accountId = DcHelper.getContext(getApplicationContext()).getAccountId(); defaultButton.setOnClickListener(new DefaultClickListener()); galleryButton.setOnClickListener(new GalleryClickListener()); - String backgroundImagePath = Prefs.getBackgroundImagePath(this); + String backgroundImagePath = Prefs.getBackgroundImagePath(this, accountId); if(backgroundImagePath.isEmpty()){ setDefaultLayoutBackgroundImage(); }else { @@ -94,13 +98,13 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity @Override public void run() { String destination = context.getFilesDir().getAbsolutePath() + "/background"; - Prefs.setBackgroundImagePath(context, destination); + Prefs.setBackgroundImagePath(context, accountId, destination); scaleAndSaveImage(context, destination); } }; thread.start(); } else { - Prefs.setBackgroundImagePath(context, ""); + Prefs.setBackgroundImagePath(context, accountId, ""); } } finish(); @@ -133,15 +137,15 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, outStream); } catch (InterruptedException e) { e.printStackTrace(); - Prefs.setBackgroundImagePath(context, ""); + Prefs.setBackgroundImagePath(context, accountId, ""); showBackgroundSaveError(); } catch (ExecutionException e) { e.printStackTrace(); - Prefs.setBackgroundImagePath(context, ""); + Prefs.setBackgroundImagePath(context, accountId, ""); showBackgroundSaveError(); } catch (FileNotFoundException e) { e.printStackTrace(); - Prefs.setBackgroundImagePath(context, ""); + Prefs.setBackgroundImagePath(context, accountId, ""); showBackgroundSaveError(); } } diff --git a/src/org/thoughtcrime/securesms/util/Prefs.java b/src/org/thoughtcrime/securesms/util/Prefs.java index c127de055..0578de179 100644 --- a/src/org/thoughtcrime/securesms/util/Prefs.java +++ b/src/org/thoughtcrime/securesms/util/Prefs.java @@ -306,12 +306,12 @@ public class Prefs { // misc. - public static String getBackgroundImagePath(Context context) { - return getStringPreference(context, BACKGROUND_PREF, ""); + public static String getBackgroundImagePath(Context context, int accountId) { + return getStringPreference(context, BACKGROUND_PREF+accountId, ""); } - public static void setBackgroundImagePath(Context context, String path) { - setStringPreference(context, BACKGROUND_PREF, path); + public static void setBackgroundImagePath(Context context, int accountId, String path) { + setStringPreference(context, BACKGROUND_PREF+accountId, path); } public static boolean isSystemEmojiPreferred(Context context) { From e6e92f60e8e91f0f6cecc597ff6b688ef7eda6f5 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 4 Oct 2023 20:10:03 +0200 Subject: [PATCH 2/4] be backwards compatible also fix bug: save custom wallpaper in different file per account --- .../preferences/ChatBackgroundActivity.java | 13 +++++++++++-- src/org/thoughtcrime/securesms/util/Prefs.java | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java index 1718ccccd..3a66d2e54 100644 --- a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java +++ b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java @@ -56,10 +56,19 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity preview = findViewById(R.id.preview); accountId = DcHelper.getContext(getApplicationContext()).getAccountId(); + // try to be backwards compatible so users don't lose already set background + String backgroundImagePath = Prefs.getBackgroundImagePathLegacy(this); + if (!backgroundImagePath.isEmpty()) { + for (int accId : DcHelper.getAccounts(getApplicationContext()).getAll()) { + Prefs.setBackgroundImagePath(this, accId, backgroundImagePath); + } + Prefs.setBackgroundImagePathLegacy(this, ""); + } + defaultButton.setOnClickListener(new DefaultClickListener()); galleryButton.setOnClickListener(new GalleryClickListener()); - String backgroundImagePath = Prefs.getBackgroundImagePath(this, accountId); + backgroundImagePath = Prefs.getBackgroundImagePath(this, accountId); if(backgroundImagePath.isEmpty()){ setDefaultLayoutBackgroundImage(); }else { @@ -97,7 +106,7 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity Thread thread = new Thread() { @Override public void run() { - String destination = context.getFilesDir().getAbsolutePath() + "/background"; + String destination = context.getFilesDir().getAbsolutePath() + "/background."+ accountId; Prefs.setBackgroundImagePath(context, accountId, destination); scaleAndSaveImage(context, destination); } diff --git a/src/org/thoughtcrime/securesms/util/Prefs.java b/src/org/thoughtcrime/securesms/util/Prefs.java index 0578de179..c580c02ef 100644 --- a/src/org/thoughtcrime/securesms/util/Prefs.java +++ b/src/org/thoughtcrime/securesms/util/Prefs.java @@ -306,6 +306,14 @@ public class Prefs { // misc. + public static String getBackgroundImagePathLegacy(Context context) { + return getStringPreference(context, BACKGROUND_PREF, ""); + } + + public static void setBackgroundImagePathLegacy(Context context, String path) { + setStringPreference(context, BACKGROUND_PREF, path); + } + public static String getBackgroundImagePath(Context context, int accountId) { return getStringPreference(context, BACKGROUND_PREF+accountId, ""); } From 41588d0de310e013124b987230a6c345e3f8bf8a Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 4 Oct 2023 22:19:26 +0200 Subject: [PATCH 3/4] move migration to ApplicationContext --- .../thoughtcrime/securesms/ApplicationContext.java | 10 ++++++++++ .../securesms/preferences/ChatBackgroundActivity.java | 11 +---------- src/org/thoughtcrime/securesms/util/Prefs.java | 8 -------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index da89d35a4..3cd257d4e 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -43,6 +43,7 @@ import org.thoughtcrime.securesms.notifications.NotificationCenter; import org.thoughtcrime.securesms.util.AndroidSignalProtocolLogger; import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.Prefs; import org.thoughtcrime.securesms.util.SignalProtocolLoggerProvider; import java.io.File; @@ -127,6 +128,15 @@ public class ApplicationContext extends MultiDexApplication { rpc.start(); + // [Migration 04/10/23] Chat Background: migrate old custom background + String backgroundImagePath = Prefs.getStringPreference(this, "pref_chat_background", ""); + if (!backgroundImagePath.isEmpty()) { + for (int accId : dcAccounts.getAll()) { + Prefs.setBackgroundImagePath(this, accId, backgroundImagePath); + } + Prefs.setStringPreference(this, "pref_chat_background", ""); + } + // set translations before starting I/O to avoid sending untranslated MDNs (issue #2288) DcHelper.setStockTranslations(this); diff --git a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java index 3a66d2e54..295fe8c09 100644 --- a/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java +++ b/src/org/thoughtcrime/securesms/preferences/ChatBackgroundActivity.java @@ -56,19 +56,10 @@ public class ChatBackgroundActivity extends PassphraseRequiredActionBarActivity preview = findViewById(R.id.preview); accountId = DcHelper.getContext(getApplicationContext()).getAccountId(); - // try to be backwards compatible so users don't lose already set background - String backgroundImagePath = Prefs.getBackgroundImagePathLegacy(this); - if (!backgroundImagePath.isEmpty()) { - for (int accId : DcHelper.getAccounts(getApplicationContext()).getAll()) { - Prefs.setBackgroundImagePath(this, accId, backgroundImagePath); - } - Prefs.setBackgroundImagePathLegacy(this, ""); - } - defaultButton.setOnClickListener(new DefaultClickListener()); galleryButton.setOnClickListener(new GalleryClickListener()); - backgroundImagePath = Prefs.getBackgroundImagePath(this, accountId); + String backgroundImagePath = Prefs.getBackgroundImagePath(this, accountId); if(backgroundImagePath.isEmpty()){ setDefaultLayoutBackgroundImage(); }else { diff --git a/src/org/thoughtcrime/securesms/util/Prefs.java b/src/org/thoughtcrime/securesms/util/Prefs.java index c580c02ef..0578de179 100644 --- a/src/org/thoughtcrime/securesms/util/Prefs.java +++ b/src/org/thoughtcrime/securesms/util/Prefs.java @@ -306,14 +306,6 @@ public class Prefs { // misc. - public static String getBackgroundImagePathLegacy(Context context) { - return getStringPreference(context, BACKGROUND_PREF, ""); - } - - public static void setBackgroundImagePathLegacy(Context context, String path) { - setStringPreference(context, BACKGROUND_PREF, path); - } - public static String getBackgroundImagePath(Context context, int accountId) { return getStringPreference(context, BACKGROUND_PREF+accountId, ""); } From d1f44e9c82ff303b197dd8af749404e9ee7a4b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asiel=20D=C3=ADaz=20Ben=C3=ADtez?= Date: Wed, 4 Oct 2023 17:28:44 -0400 Subject: [PATCH 4/4] Update src/org/thoughtcrime/securesms/ApplicationContext.java Co-authored-by: bjoern --- src/org/thoughtcrime/securesms/ApplicationContext.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 3cd257d4e..9530a68f6 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -128,7 +128,7 @@ public class ApplicationContext extends MultiDexApplication { rpc.start(); - // [Migration 04/10/23] Chat Background: migrate old custom background + // migrating chat backgrounds, added 04/10/23, can be removed after some versions String backgroundImagePath = Prefs.getStringPreference(this, "pref_chat_background", ""); if (!backgroundImagePath.isEmpty()) { for (int accId : dcAccounts.getAll()) { @@ -136,6 +136,7 @@ public class ApplicationContext extends MultiDexApplication { } Prefs.setStringPreference(this, "pref_chat_background", ""); } + // /migrating chat backgrounds // set translations before starting I/O to avoid sending untranslated MDNs (issue #2288) DcHelper.setStockTranslations(this);