mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 23:12:55 +00:00
Refactoring
This commit is contained in:
parent
12feae53a2
commit
3670c3789b
2 changed files with 51 additions and 47 deletions
|
@ -21,11 +21,8 @@ package eu.faircode.email;
|
||||||
|
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.LocaleList;
|
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.text.method.LinkMovementMethod;
|
import android.text.method.LinkMovementMethod;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -36,10 +33,6 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import io.noties.markwon.Markwon;
|
import io.noties.markwon.Markwon;
|
||||||
|
|
||||||
|
@ -76,47 +69,8 @@ public class FragmentDialogMarkdown extends FragmentDialogBase {
|
||||||
@Override
|
@Override
|
||||||
protected Spanned onExecute(Context context, Bundle args) throws Throwable {
|
protected Spanned onExecute(Context context, Bundle args) throws Throwable {
|
||||||
String name = args.getString("name");
|
String name = args.getString("name");
|
||||||
if (name == null || !name.contains("."))
|
|
||||||
throw new IllegalArgumentException(name);
|
|
||||||
|
|
||||||
List<String> names = new ArrayList<>();
|
String asset = Helper.getLocalizedAsset(context, name);
|
||||||
String[] c = name.split("\\.");
|
|
||||||
List<String> assets = Arrays.asList(getResources().getAssets().list(""));
|
|
||||||
|
|
||||||
List<Locale> locales = new ArrayList<>();
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
|
||||||
locales.add(Locale.getDefault());
|
|
||||||
else {
|
|
||||||
LocaleList ll = context.getResources().getConfiguration().getLocales();
|
|
||||||
for (int i = 0; i < ll.size(); i++)
|
|
||||||
locales.add(ll.get(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Locale locale : locales) {
|
|
||||||
String language = locale.getLanguage();
|
|
||||||
String country = locale.getCountry();
|
|
||||||
if ("en".equals(language) && "US".equals(country))
|
|
||||||
names.add(name);
|
|
||||||
else {
|
|
||||||
String localized = c[0] + "-" + language + "-r" + country + "." + c[1];
|
|
||||||
if (assets.contains(localized))
|
|
||||||
names.add(localized);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Locale locale : locales) {
|
|
||||||
String prefix = c[0] + "-" + locale.getLanguage();
|
|
||||||
for (String asset : assets)
|
|
||||||
if (asset.startsWith(prefix))
|
|
||||||
names.add(asset);
|
|
||||||
}
|
|
||||||
|
|
||||||
names.add(name);
|
|
||||||
String asset = names.get(0);
|
|
||||||
|
|
||||||
Log.i("Using " + asset +
|
|
||||||
" of " + TextUtils.join(",", names) +
|
|
||||||
" (" + TextUtils.join(",", locales) + ")");
|
|
||||||
try (InputStream is = context.getAssets().open(asset)) {
|
try (InputStream is = context.getAssets().open(asset)) {
|
||||||
byte[] buffer = new byte[is.available()];
|
byte[] buffer = new byte[is.available()];
|
||||||
is.read(buffer);
|
is.read(buffer);
|
||||||
|
|
|
@ -111,6 +111,7 @@ import java.text.DateFormat;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -841,6 +842,55 @@ public class Helper {
|
||||||
return result.toArray(new String[0]);
|
return result.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String getLocalizedAsset(Context context, String name) throws IOException {
|
||||||
|
if (name == null || !name.contains("."))
|
||||||
|
throw new IllegalArgumentException(name);
|
||||||
|
|
||||||
|
String[] list = context.getResources().getAssets().list("");
|
||||||
|
if (list == null)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
|
List<String> names = new ArrayList<>();
|
||||||
|
String[] c = name.split("\\.");
|
||||||
|
List<String> assets = Arrays.asList(list);
|
||||||
|
|
||||||
|
List<Locale> locales = new ArrayList<>();
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
||||||
|
locales.add(Locale.getDefault());
|
||||||
|
else {
|
||||||
|
LocaleList ll = context.getResources().getConfiguration().getLocales();
|
||||||
|
for (int i = 0; i < ll.size(); i++)
|
||||||
|
locales.add(ll.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Locale locale : locales) {
|
||||||
|
String language = locale.getLanguage();
|
||||||
|
String country = locale.getCountry();
|
||||||
|
if ("en".equals(language) && "US".equals(country))
|
||||||
|
names.add(name);
|
||||||
|
else {
|
||||||
|
String localized = c[0] + "-" + language + "-r" + country + "." + c[1];
|
||||||
|
if (assets.contains(localized))
|
||||||
|
names.add(localized);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Locale locale : locales) {
|
||||||
|
String prefix = c[0] + "-" + locale.getLanguage();
|
||||||
|
for (String asset : assets)
|
||||||
|
if (asset.startsWith(prefix))
|
||||||
|
names.add(asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
names.add(name);
|
||||||
|
|
||||||
|
String asset = names.get(0);
|
||||||
|
Log.i("Using " + asset +
|
||||||
|
" of " + TextUtils.join(",", names) +
|
||||||
|
" (" + TextUtils.join(",", locales) + ")");
|
||||||
|
return asset;
|
||||||
|
}
|
||||||
|
|
||||||
static boolean containsWhiteSpace(String text) {
|
static boolean containsWhiteSpace(String text) {
|
||||||
return text.matches(".*\\s+.*");
|
return text.matches(".*\\s+.*");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue