fully replace language pref handling with Languages class

closes #908
This commit is contained in:
Hans-Christoph Steiner 2017-04-06 12:50:59 +02:00
parent 019964387a
commit 6d0bde716e
4 changed files with 25 additions and 36 deletions

View File

@ -27,7 +27,6 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
@ -36,16 +35,15 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.StrictMode; import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache; import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator; import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import org.acra.ACRA; import org.acra.ACRA;
import org.acra.ReportingInteractionMode; import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes; import org.acra.annotation.ReportsCrashes;
@ -59,17 +57,13 @@ import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.installer.InstallHistoryService; import org.fdroid.fdroid.installer.InstallHistoryService;
import org.fdroid.fdroid.net.ImageLoaderForUIL; import org.fdroid.fdroid.net.ImageLoaderForUIL;
import org.fdroid.fdroid.net.WifiStateChangeService; import org.fdroid.fdroid.net.WifiStateChangeService;
import sun.net.www.protocol.bluetooth.Handler;
import java.net.URL; import java.net.URL;
import java.net.URLStreamHandler; import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory; import java.net.URLStreamHandlerFactory;
import java.security.Security; import java.security.Security;
import java.util.List; import java.util.List;
import java.util.Locale;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import sun.net.www.protocol.bluetooth.Handler;
@ReportsCrashes(mailTo = "reports@f-droid.org", @ReportsCrashes(mailTo = "reports@f-droid.org",
mode = ReportingInteractionMode.DIALOG, mode = ReportingInteractionMode.DIALOG,
@ -82,8 +76,6 @@ public class FDroidApp extends Application {
public static final String SYSTEM_DIR_NAME = Environment.getRootDirectory().getAbsolutePath(); public static final String SYSTEM_DIR_NAME = Environment.getRootDirectory().getAbsolutePath();
private static Locale locale;
// for the local repo on this device, all static since there is only one // for the local repo on this device, all static since there is only one
public static volatile int port; public static volatile int port;
public static volatile String ipAddressString; public static volatile String ipAddressString;
@ -181,25 +173,10 @@ public class FDroidApp extends Application {
repo = new Repo(); repo = new Repo();
} }
public void updateLanguage() {
Context ctx = getBaseContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String lang = prefs.getString(Preferences.PREF_LANGUAGE, "");
locale = Utils.getLocaleFromAndroidLangTag(lang);
applyLanguage();
}
private void applyLanguage() {
Context ctx = getBaseContext();
Configuration cfg = new Configuration();
cfg.locale = locale == null ? Locale.getDefault() : locale;
ctx.getResources().updateConfiguration(cfg, null);
}
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
applyLanguage(); Languages.setLanguage(this, Preferences.get().getLangauge(), false);
} }
@Override @Override
@ -215,7 +192,9 @@ public class FDroidApp extends Application {
.penaltyLog() .penaltyLog()
.build()); .build());
} }
updateLanguage(); Preferences.setup(this);
Languages.setup(getClass(), R.string.pref_language_default);
Languages.setLanguage(this, Preferences.get().getLangauge(), false);
ACRA.init(this); ACRA.init(this);
if (isAcraProcess()) { if (isAcraProcess()) {
@ -224,10 +203,8 @@ public class FDroidApp extends Application {
PRNGFixes.apply(); PRNGFixes.apply();
Preferences.setup(this);
curTheme = Preferences.get().getTheme(); curTheme = Preferences.get().getTheme();
Preferences.get().configureProxy(); Preferences.get().configureProxy();
Languages.setup(getClass(), R.string.pref_language_default);
InstalledAppProviderService.compareToPackageManager(this); InstalledAppProviderService.compareToPackageManager(this);
AppUpdateStatusService.scanDownloadedApks(this); AppUpdateStatusService.scanDownloadedApks(this);
@ -326,13 +303,13 @@ public class FDroidApp extends Application {
/** /**
* Asks if the current process is "org.fdroid.fdroid:acra". * Asks if the current process is "org.fdroid.fdroid:acra".
* * <p>
* This is helpful for bailing out of the {@link FDroidApp#onCreate} method early, preventing * This is helpful for bailing out of the {@link FDroidApp#onCreate} method early, preventing
* problems that arise from executing the code twice. This happens due to the `android:process` * problems that arise from executing the code twice. This happens due to the `android:process`
* statement in AndroidManifest.xml causes another process to be created to run * statement in AndroidManifest.xml causes another process to be created to run
* {@link org.fdroid.fdroid.acra.CrashReportActivity}. This was causing lots of things to be * {@link org.fdroid.fdroid.acra.CrashReportActivity}. This was causing lots of things to be
* started/run twice including {@link CleanCacheService} and {@link WifiStateChangeService}. * started/run twice including {@link CleanCacheService} and {@link WifiStateChangeService}.
* * <p>
* Note that it is not perfect, because some devices seem to not provide a list of running app * Note that it is not perfect, because some devices seem to not provide a list of running app
* processes when asked. In such situations, F-Droid may regress to the behaviour where some * processes when asked. In such situations, F-Droid may regress to the behaviour where some
* services may run twice and thus cause weirdness or slowness. However that is probably better * services may run twice and thus cause weirdness or slowness. However that is probably better

View File

@ -25,6 +25,7 @@ public final class Languages {
private static final Locale DEFAULT_LOCALE; private static final Locale DEFAULT_LOCALE;
private static final Locale TIBETAN = new Locale("bo"); private static final Locale TIBETAN = new Locale("bo");
private static final Locale CHINESE_HONG_KONG = new Locale("zh", "HK");
private static final String DEFAULT_STRING = "System Default"; private static final String DEFAULT_STRING = "System Default";
private static Locale locale; private static Locale locale;
@ -62,8 +63,10 @@ public final class Languages {
tmpMap.put(Locale.SIMPLIFIED_CHINESE.toString(), "中文 (中国)"); // Chinese (China) tmpMap.put(Locale.SIMPLIFIED_CHINESE.toString(), "中文 (中国)"); // Chinese (China)
} else if (locale.equals(Locale.TRADITIONAL_CHINESE)) { } else if (locale.equals(Locale.TRADITIONAL_CHINESE)) {
tmpMap.put(Locale.TRADITIONAL_CHINESE.toString(), "中文 (台灣)"); // Chinese (Taiwan) tmpMap.put(Locale.TRADITIONAL_CHINESE.toString(), "中文 (台灣)"); // Chinese (Taiwan)
} else if (locale.equals(CHINESE_HONG_KONG)) {
tmpMap.put(CHINESE_HONG_KONG.toString(), "中文 (香港)"); // Chinese (Hong Kong)
} else { } else {
tmpMap.put(locale.getLanguage(), locale.getDisplayLanguage(locale)); tmpMap.put(locale.getLanguage(), capitalize(locale.getDisplayLanguage(locale)));
} }
} }
@ -196,6 +199,10 @@ public final class Languages {
return keys.toArray(new String[keys.size()]); return keys.toArray(new String[keys.size()]);
} }
private String capitalize(final String line) {
return Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
private static final Locale[] LOCALES_TO_TEST = { private static final Locale[] LOCALES_TO_TEST = {
Locale.ENGLISH, Locale.ENGLISH,
Locale.FRENCH, Locale.FRENCH,
@ -205,6 +212,7 @@ public final class Languages {
Locale.KOREAN, Locale.KOREAN,
Locale.SIMPLIFIED_CHINESE, Locale.SIMPLIFIED_CHINESE,
Locale.TRADITIONAL_CHINESE, Locale.TRADITIONAL_CHINESE,
CHINESE_HONG_KONG,
TIBETAN, TIBETAN,
new Locale("af"), new Locale("af"),
new Locale("am"), new Locale("am"),

View File

@ -229,6 +229,10 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
.replaceAll(" ", "-"); .replaceAll(" ", "-");
} }
public String getLangauge() {
return preferences.getString(Preferences.PREF_LANGUAGE, "");
}
public String getLocalRepoName() { public String getLocalRepoName() {
return preferences.getString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName()); return preferences.getString(PREF_LOCAL_REPO_NAME, getDefaultLocalRepoName());
} }

View File

@ -17,7 +17,6 @@ import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper; import info.guardianproject.netcipher.proxy.OrbotHelper;
import org.fdroid.fdroid.AppDetails2; import org.fdroid.fdroid.AppDetails2;
import org.fdroid.fdroid.CleanCacheService; import org.fdroid.fdroid.CleanCacheService;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Languages; import org.fdroid.fdroid.Languages;
import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
@ -142,8 +141,9 @@ public class PreferencesFragment extends PreferenceFragment
case Preferences.PREF_LANGUAGE: case Preferences.PREF_LANGUAGE:
entrySummary(key); entrySummary(key);
if (changing) { if (changing) {
// TODO: Ask MainActivity to restart itself. Activity activity = getActivity();
((FDroidApp) getActivity().getApplication()).updateLanguage(); Languages.setLanguage(activity, Preferences.get().getLangauge(), false);
Languages.forceChangeLanguage(activity);
} }
break; break;