From 65a461a0db31a4cee96e630e08e407f4f7bfcd48 Mon Sep 17 00:00:00 2001 From: goapunk Date: Tue, 12 Jun 2018 12:15:47 +0200 Subject: [PATCH 1/5] Reset Localizer on account deletion --- .../java/org/briarproject/briar/android/Localizer.java | 9 ++++++++- .../briar/android/login/PasswordActivity.java | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index 3f305c3f9..1a419043d 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -20,10 +20,12 @@ public class Localizer { // Locking: class @Nullable private static Localizer INSTANCE; + private final Locale systemLocale; @Nullable - private final Locale locale; + private volatile Locale locale; private Localizer(SharedPreferences sharedPreferences) { + systemLocale = Locale.getDefault(); locale = getLocaleFromTag( sharedPreferences.getString(LANGUAGE, "default")); } @@ -39,6 +41,11 @@ public class Localizer { return INSTANCE; } + // Reset to the system locale + public synchronized void reset() { + locale = systemLocale; + } + // Get Locale from BCP-47 tag @Nullable public static Locale getLocaleFromTag(String tag) { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java index c381beb36..d7a2a8801 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java @@ -13,6 +13,7 @@ import android.widget.EditText; import android.widget.ProgressBar; import org.briarproject.briar.R; +import org.briarproject.briar.android.Localizer; import org.briarproject.briar.android.activity.ActivityComponent; import org.briarproject.briar.android.activity.BaseActivity; import org.briarproject.briar.android.controller.BriarController; @@ -105,6 +106,7 @@ public class PasswordActivity extends BaseActivity { private void deleteAccount() { passwordController.deleteAccount(this); + Localizer.getInstance().reset(); setResult(RESULT_CANCELED); Intent i = new Intent(this, SetupActivity.class); i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); From fc99dedb5390e1291b063d9f558087d73ee99387 Mon Sep 17 00:00:00 2001 From: goapunk Date: Tue, 12 Jun 2018 16:23:32 +0200 Subject: [PATCH 2/5] Detect if system language changed --- .../org/briarproject/briar/android/Localizer.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index 1a419043d..d2295409b 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -21,8 +21,9 @@ public class Localizer { @Nullable private static Localizer INSTANCE; private final Locale systemLocale; + // Locking: this @Nullable - private volatile Locale locale; + private Locale locale; private Localizer(SharedPreferences sharedPreferences) { systemLocale = Locale.getDefault(); @@ -61,9 +62,8 @@ public class Localizer { return new Locale(tag); } + // Returns the localized version of context public Context setLocale(Context context) { - if (locale == null) - return context; Resources res = context.getResources(); Configuration conf = res.getConfiguration(); Locale currentLocale; @@ -71,6 +71,14 @@ public class Localizer { currentLocale = conf.getLocales().get(0); } else currentLocale = conf.locale; + synchronized (this) { + if (locale == null) { + // Detect if the user changed the system language + if (systemLocale.equals(currentLocale)) + return context; + locale = systemLocale; + } + } if (locale.equals(currentLocale)) return context; Locale.setDefault(locale); From 9743255ce958725814503a07dc03419d708f1063 Mon Sep 17 00:00:00 2001 From: goapunk Date: Wed, 13 Jun 2018 12:32:38 +0200 Subject: [PATCH 3/5] immutable version --- .../briarproject/briar/android/Localizer.java | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index d2295409b..68ea8c942 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -21,21 +21,32 @@ public class Localizer { @Nullable private static Localizer INSTANCE; private final Locale systemLocale; - // Locking: this @Nullable - private Locale locale; + private final Locale userLocale; private Localizer(SharedPreferences sharedPreferences) { systemLocale = Locale.getDefault(); - locale = getLocaleFromTag( + userLocale = getLocaleFromTag( sharedPreferences.getString(LANGUAGE, "default")); } + private Localizer(Locale systemLocale, @Nullable Locale userLocale) { + this.systemLocale = systemLocale; + this.userLocale = userLocale; + } + + // Instantiate the Localizer. public static synchronized void initialize(SharedPreferences prefs) { if (INSTANCE == null) INSTANCE = new Localizer(prefs); } + // Reinstantiate the Localizer with the system locale + private static synchronized void reinitialize(Locale systemLocale) { + INSTANCE = new Localizer(systemLocale, null); + } + + // Get the current instance. public static synchronized Localizer getInstance() { if (INSTANCE == null) throw new IllegalStateException("Localizer not initialized"); @@ -44,7 +55,7 @@ public class Localizer { // Reset to the system locale public synchronized void reset() { - locale = systemLocale; + reinitialize(systemLocale); } // Get Locale from BCP-47 tag @@ -66,19 +77,18 @@ public class Localizer { public Context setLocale(Context context) { Resources res = context.getResources(); Configuration conf = res.getConfiguration(); - Locale currentLocale; + Locale locale, currentLocale; if (SDK_INT >= 24) { currentLocale = conf.getLocales().get(0); } else currentLocale = conf.locale; - synchronized (this) { - if (locale == null) { - // Detect if the user changed the system language - if (systemLocale.equals(currentLocale)) - return context; - locale = systemLocale; - } - } + if (userLocale == null) { + // Detect if the user changed the system language + if (systemLocale.equals(currentLocale)) + return context; + locale = systemLocale; + } else + locale = userLocale; if (locale.equals(currentLocale)) return context; Locale.setDefault(locale); From 0c65ff478358d956ebae6255c7208e3475f2b2a5 Mon Sep 17 00:00:00 2001 From: goapunk Date: Wed, 13 Jun 2018 12:42:03 +0200 Subject: [PATCH 4/5] remove the unncessary synchronization from reset --- .../src/main/java/org/briarproject/briar/android/Localizer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index 68ea8c942..93b27fa19 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -54,7 +54,7 @@ public class Localizer { } // Reset to the system locale - public synchronized void reset() { + public void reset() { reinitialize(systemLocale); } From 7a2df3d6cb9f80f079447583e77758624d51a6df Mon Sep 17 00:00:00 2001 From: goapunk Date: Wed, 13 Jun 2018 13:38:15 +0200 Subject: [PATCH 5/5] simplify --- .../briarproject/briar/android/Localizer.java | 30 ++++++------------- .../briar/android/login/PasswordActivity.java | 2 +- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java index 93b27fa19..2b51659bb 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/Localizer.java @@ -21,18 +21,17 @@ public class Localizer { @Nullable private static Localizer INSTANCE; private final Locale systemLocale; - @Nullable - private final Locale userLocale; + private final Locale locale; private Localizer(SharedPreferences sharedPreferences) { - systemLocale = Locale.getDefault(); - userLocale = getLocaleFromTag( - sharedPreferences.getString(LANGUAGE, "default")); + this(Locale.getDefault(), getLocaleFromTag( + sharedPreferences.getString(LANGUAGE, "default"))); } private Localizer(Locale systemLocale, @Nullable Locale userLocale) { this.systemLocale = systemLocale; - this.userLocale = userLocale; + if (userLocale == null) locale = systemLocale; + else locale = userLocale; } // Instantiate the Localizer. @@ -42,8 +41,9 @@ public class Localizer { } // Reinstantiate the Localizer with the system locale - private static synchronized void reinitialize(Locale systemLocale) { - INSTANCE = new Localizer(systemLocale, null); + public static synchronized void reinitialize() { + if (INSTANCE != null) + INSTANCE = new Localizer(INSTANCE.systemLocale, null); } // Get the current instance. @@ -53,11 +53,6 @@ public class Localizer { return INSTANCE; } - // Reset to the system locale - public void reset() { - reinitialize(systemLocale); - } - // Get Locale from BCP-47 tag @Nullable public static Locale getLocaleFromTag(String tag) { @@ -77,18 +72,11 @@ public class Localizer { public Context setLocale(Context context) { Resources res = context.getResources(); Configuration conf = res.getConfiguration(); - Locale locale, currentLocale; + Locale currentLocale; if (SDK_INT >= 24) { currentLocale = conf.getLocales().get(0); } else currentLocale = conf.locale; - if (userLocale == null) { - // Detect if the user changed the system language - if (systemLocale.equals(currentLocale)) - return context; - locale = systemLocale; - } else - locale = userLocale; if (locale.equals(currentLocale)) return context; Locale.setDefault(locale); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java index d7a2a8801..55f08b204 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/login/PasswordActivity.java @@ -106,7 +106,7 @@ public class PasswordActivity extends BaseActivity { private void deleteAccount() { passwordController.deleteAccount(this); - Localizer.getInstance().reset(); + Localizer.reinitialize(); setResult(RESULT_CANCELED); Intent i = new Intent(this, SetupActivity.class); i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK);