Prevent showing notification megaphone if not translated.
This commit is contained in:
parent
8f6ff215aa
commit
3f6c8cb622
6 changed files with 60 additions and 15 deletions
|
@ -10,6 +10,7 @@ import androidx.annotation.Nullable;
|
|||
|
||||
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;
|
||||
|
@ -31,9 +32,11 @@ import org.thoughtcrime.securesms.util.FeatureFlags;
|
|||
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;
|
||||
|
@ -325,9 +328,21 @@ public final class Megaphones {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -8,7 +8,7 @@ import androidx.annotation.NonNull;
|
|||
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;
|
||||
|
@ -37,7 +37,7 @@ public final class 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" +
|
||||
|
|
|
@ -3,6 +3,8 @@ package org.thoughtcrime.securesms.util.dynamiclanguage;
|
|||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
|
||||
import java.util.Locale;
|
||||
|
@ -13,15 +15,19 @@ import java.util.Locale;
|
|||
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);
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util.dynamiclanguage;
|
|||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.os.ConfigurationCompat;
|
||||
|
||||
|
@ -20,7 +21,7 @@ final class 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;
|
||||
|
@ -39,7 +40,7 @@ final class LocaleParser {
|
|||
/**
|
||||
* 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)
|
||||
|
|
|
@ -12,16 +12,20 @@ import java.util.Locale;
|
|||
/**
|
||||
* 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();
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,6 +54,15 @@ public final class TranslationDetection {
|
|||
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;
|
||||
|
|
Loading…
Add table
Reference in a new issue