Skip to content

Commit

Permalink
Prevent showing notification megaphone if not translated.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal committed Feb 25, 2021
1 parent 8f6ff21 commit 3f6c8cb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
Expand Up @@ -10,6 +10,7 @@

import com.annimon.stream.Stream;

import org.signal.core.util.TranslationDetection;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.R;
Expand All @@ -31,9 +32,11 @@
import org.thoughtcrime.securesms.util.PopulationFeatureFlags;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.VersionTracker;
import org.thoughtcrime.securesms.util.dynamiclanguage.DynamicLanguageContextWrapper;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -325,9 +328,21 @@ private static boolean shouldShowOnboardingMegaphone(@NonNull Context context) {
}

private static boolean shouldShowNotificationsMegaphone(@NonNull Context context) {
return !TextSecurePreferences.isNotificationsEnabled(context) ||
!NotificationChannels.isMessageChannelEnabled(context) ||
!NotificationChannels.areNotificationsEnabled(context);
boolean shouldShow = !TextSecurePreferences.isNotificationsEnabled(context) ||
!NotificationChannels.isMessageChannelEnabled(context) ||
!NotificationChannels.areNotificationsEnabled(context);
if (shouldShow) {
Locale locale = DynamicLanguageContextWrapper.getUsersSelectedLocale(context);
if (!new TranslationDetection(context, locale)
.textExistsInUsersLanguage(R.string.NotificationsMegaphone_turn_on_notifications,
R.string.NotificationsMegaphone_never_miss_a_message,
R.string.NotificationsMegaphone_turn_on,
R.string.NotificationsMegaphone_not_now)) {
Log.i(TAG, "Would show NotificationsMegaphone but is not yet translated in " + locale);
return false;
}
}
return shouldShow;
}

public enum Event {
Expand Down
Expand Up @@ -8,7 +8,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import org.signal.core.util.EnglishResourceUtil;
import org.signal.core.util.ResourceUtil;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
Expand Down Expand Up @@ -37,7 +37,7 @@ private SupportEmailUtil() { }
}

private static @NonNull String buildSystemInfo(@NonNull Context context, @StringRes int subject) {
Resources englishResources = EnglishResourceUtil.getEnglishResources(context);
Resources englishResources = ResourceUtil.getEnglishResources(context);

return "--- " + context.getString(R.string.HelpFragment__support_info) + " ---" +
"\n" +
Expand Down
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.res.Configuration;

import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.util.TextSecurePreferences;

import java.util.Locale;
Expand All @@ -13,15 +15,19 @@
public final class DynamicLanguageContextWrapper {
private DynamicLanguageContextWrapper() {}

public static void prepareOverrideConfiguration(Context context, Configuration base) {
String language = TextSecurePreferences.getLanguage(context);
Locale newLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);
public static void prepareOverrideConfiguration(@NonNull Context context, @NonNull Configuration base) {
Locale newLocale = getUsersSelectedLocale(context);

Locale.setDefault(newLocale);
base.setLocale(newLocale);
}

public static void updateContext(Context base) {
public static @NonNull Locale getUsersSelectedLocale(@NonNull Context context) {
String language = TextSecurePreferences.getLanguage(context);
return LocaleParser.findBestMatchingLocaleForLanguage(language);
}

public static void updateContext(@NonNull Context base) {
Configuration config = base.getResources().getConfiguration();

prepareOverrideConfiguration(base, config);
Expand Down
Expand Up @@ -3,6 +3,7 @@
import android.content.res.Configuration;
import android.content.res.Resources;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.os.ConfigurationCompat;

Expand All @@ -20,7 +21,7 @@ private LocaleParser() {
* Given a language, gets the best choice from the apps list of supported languages and the
* Systems set of languages.
*/
static Locale findBestMatchingLocaleForLanguage(@Nullable String language) {
static @NonNull Locale findBestMatchingLocaleForLanguage(@Nullable String language) {
final Locale locale = LanguageString.parseLocale(language);
if (appSupportsTheExactLocale(locale)) {
return locale;
Expand All @@ -39,7 +40,7 @@ private static boolean appSupportsTheExactLocale(@Nullable Locale locale) {
/**
* Get the first preferred language the app supports.
*/
private static Locale findBestSystemLocale() {
private static @NonNull Locale findBestSystemLocale() {
final Configuration config = Resources.getSystem().getConfiguration();

final Locale firstMatch = ConfigurationCompat.getLocales(config)
Expand Down
Expand Up @@ -12,16 +12,20 @@
/**
* Gives access to English strings.
*/
public final class EnglishResourceUtil {
public final class ResourceUtil {

private EnglishResourceUtil() {
private ResourceUtil() {
}

public static Resources getEnglishResources(@NonNull Context context) {
return getResources(context, Locale.ENGLISH);
}

public static Resources getResources(@NonNull Context context, @NonNull Locale locale) {
Configuration configurationLocal = context.getResources().getConfiguration();

Configuration configurationEn = new Configuration(configurationLocal);
configurationEn.setLocale(Locale.ENGLISH);
configurationEn.setLocale(locale);

return context.createConfigurationContext(configurationEn)
.getResources();
Expand Down
Expand Up @@ -24,7 +24,17 @@ public final class TranslationDetection {
public TranslationDetection(@NonNull Context context) {
this.resourcesLocal = context.getResources();
this.configurationLocal = resourcesLocal.getConfiguration();
this.resourcesEn = EnglishResourceUtil.getEnglishResources(context);
this.resourcesEn = ResourceUtil.getEnglishResources(context);
}

/**
* @param context Can be Application context.
* @param usersLocale Locale of user.
*/
public TranslationDetection(@NonNull Context context, @NonNull Locale usersLocale) {
this.resourcesLocal = ResourceUtil.getResources(context.getApplicationContext(), usersLocale);
this.configurationLocal = resourcesLocal.getConfiguration();
this.resourcesEn = ResourceUtil.getEnglishResources(context);
}

/**
Expand All @@ -44,6 +54,15 @@ public boolean textExistsInUsersLanguage(@StringRes int resId) {
return !stringEn.equals(stringLocal);
}

public boolean textExistsInUsersLanguage(@StringRes int... resIds) {
for (int resId : resIds) {
if (!textExistsInUsersLanguage(resId)) {
return false;
}
}
return true;
}

protected boolean configSupportsEnglish() {
if (configurationLocal.locale.getLanguage().equals("en")) {
return true;
Expand Down

1 comment on commit 3f6c8cb

@Meteor0id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice detail to add to Signal. Shows someone is giving the UI good thought.

Please sign in to comment.