Skip to content

Commit

Permalink
#6: Made app localizable via settings menu. Bugfix: allow utf8 in abo…
Browse files Browse the repository at this point in the history
…utbox
  • Loading branch information
k3b committed Jul 18, 2017
1 parent cf6ce14 commit a93b472
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/de/k3b/android/AndroidCompressJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import de.k3b.android.toGoZip.Global;
import de.k3b.android.toGoZip.R;
import de.k3b.android.toGoZip.SettingsImpl;
import de.k3b.android.widgets.Clipboard;
import de.k3b.android.widget.Clipboard;
import de.k3b.zip.CompressItem;
import de.k3b.zip.CompressJob;
import de.k3b.zip.ZipLog;
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/de/k3b/android/ToGoZip/Add2ZipActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,13 @@
*/
package de.k3b.android.toGoZip;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.k3b.android.AndroidCompressJob;
import de.k3b.android.MediaUtil;
import de.k3b.android.widget.LocalizedActivity;
import de.k3b.zip.CompressItem;
import de.k3b.zip.ZipLog;
import de.k3b.zip.ZipLogImpl;
Expand All @@ -41,7 +33,7 @@
* This pseudo activity has no gui. It starts add2zip from intent-data
* or starts the settings-activity if the zip-output-dir is write-protected
*/
public class Add2ZipActivity extends Activity {
public class Add2ZipActivity extends LocalizedActivity {
/**
* caption for logging
*/
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/de/k3b/android/ToGoZip/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@
*/
package de.k3b.android.toGoZip;

import java.util.Locale;

/**
* Global settings
*/
public class Global {
/** local settings: which language should the gui use */
public static final String PREF_KEY_USER_LOCALE = "user_locale";

public static final String LOG_CONTEXT = "toGoZip";
/**
* true: addToCompressQue several Log.d(...) to show what is going on.
* debugEnabled is updated by the SettingsActivity
*/
public static boolean debugEnabled = false;

/** Remember ininial language settings. This allows setting "switch back to device language" after changing app locale */
public static Locale systemLocale = Locale.getDefault();
}
55 changes: 54 additions & 1 deletion app/src/main/java/de/k3b/android/ToGoZip/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 k3b
* Copyright (C) 2014-2017 k3b
*
* This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/) .
*
Expand All @@ -22,14 +22,19 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import java.io.File;

import de.k3b.android.AndroidCompressJob;
import de.k3b.android.widget.LocalizedActivity;
import de.k3b.zip.CompressItem;
import de.k3b.zip.ZipLog;
import de.k3b.zip.ZipLogImpl;
Expand All @@ -46,6 +51,9 @@ public class SettingsActivity extends PreferenceActivity {
private static String textToBeAdded = null;
private AndroidCompressJob job = null;

private SharedPreferences prefsInstance = null;
private ListPreference defaultLocalePreference; // #6: Support to change locale at runtime

/**
* public api to start settings-activity
*/
Expand All @@ -65,6 +73,7 @@ public static void show(Context context, CompressItem[] filesToBeAdded, String t

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixLocale(this); // #6: Support to change locale at runtime
super.onCreate(savedInstanceState);
SettingsImpl.init(this);
ZipLog zipLog = new ZipLogImpl(Global.debugEnabled);
Expand All @@ -73,6 +82,23 @@ protected void onCreate(final Bundle savedInstanceState) {

this.addPreferencesFromResource(R.xml.preferences);

prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
// #6: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

// #6: Support to change locale at runtime
updateSummary();

showAlertOnError();
}

Expand Down Expand Up @@ -188,4 +214,31 @@ private void cancel() {

finishWithoutCheck();
}

// #6: Support to change locale at runtime
// This is used to show the status of some preference in the description
private void updateSummary() {
final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, "");
setLanguage(languageKey);
}

// #6: Support to change locale at runtime
private void setLanguage(String languageKey) {
setPref(languageKey, defaultLocalePreference, R.array.pref_locale_names);
}

private void setPref(String key, ListPreference listPreference, int arrayResourceId) {
int index = listPreference.findIndexOfValue(key);
String summary = "";

if (index >= 0) {
String[] names = this.getResources().getStringArray(arrayResourceId);
if (index < names.length) {
summary = names[index];
}
}
listPreference.setSummary(summary);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.android.widgets;
package de.k3b.android.widget;

import android.content.Context;
import android.preference.DialogPreference;
Expand Down Expand Up @@ -50,24 +50,37 @@ public AboutDialogPreference(Context context, AttributeSet attrs) {
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
WebView wv = (WebView) view.findViewById(R.id.content);

String html = this.context.getResources().getString(R.string.about_content); // "<html><body>some <b>html</b> here</body></html>";
setAboutText(this.context, (WebView) view.findViewById(R.id.content));
}

private static WebView setAboutText(Context context, WebView wv) {
final WebSettings settings = wv.getSettings();

// Fix for "Wrong charset in serbian translations" https://github.com/k3b/LocationMapViewer/issues/5
// (for android 2.2) see http://stackoverflow.com/questions/4933069/android-webview-with-garbled-utf-8-characters
settings.setDefaultTextEncodingName("utf-8");
settings.setBuiltInZoomControls(true);

String html = context.getResources().getString(R.string.about_content); // "<html><body>some <b>html</b> here</body></html>";

final String versionName = GuiUtil.getAppVersionName(context);
if (versionName != null) {
html = html.replace("$versionName$", versionName);
}

html = html.replace("$about$",
this.context.getText(R.string.about_content_about));
context.getText(R.string.about_content_about));

wv.loadData(html, "text/html", "UTF-8");
// Fix for "Wrong charset in serbian translations" https://github.com/k3b/LocationMapViewer/issues/5
// (for android 4.x) see http://stackoverflow.com/questions/4933069/android-webview-with-garbled-utf-8-characters
wv.loadData(html, "text/html; charset=utf-8", "UTF-8");
wv.setVerticalScrollBarEnabled(true);

final WebSettings mWebSettings = wv.getSettings();
mWebSettings.setBuiltInZoomControls(true);
wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wv.setScrollbarFadingEnabled(false);
return wv;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.android.widgets;
package de.k3b.android.widget;

import android.content.Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.k3b.android.widgets;
package de.k3b.android.widget;

import android.content.Context;
import android.content.SharedPreferences;
Expand Down
107 changes: 107 additions & 0 deletions app/src/main/java/de/k3b/android/widget/LocalizedActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2015-2017 by k3b.
*
* This file is part of AndroFotoFinder and of ToGoZip.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/

package de.k3b.android.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;

import java.util.Locale;

import de.k3b.android.toGoZip.Global;

/**
* An activity that can change the locale (language) of its content.
*
* Inspired by http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime
*
* Created by k3b on 07.01.2016.
*/
public abstract class LocalizedActivity extends Activity {
/** if myLocale != Locale.Default : activity must be recreated in on resume */
private Locale myLocale = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
fixLocale(this);
super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
super.onResume();

// Locale has changed by other Activity ?
if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) {
myLocale = null;
recreate(LocalizedActivity.this);
}
}

/**
* Set Activity-s locale to SharedPreferences-setting.
* Must be called before
*/
public static void fixLocale(Context context)
{
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
Locale locale = Global.systemLocale; // in case that setting=="use android-locale"
if ((language != null) && (language.length() > 0)) {
locale = new Locale(language); // overwrite "use android-locale"
}

if (locale != null) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = context.getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
// recreate();

if (context instanceof LocalizedActivity) {
((LocalizedActivity) context).myLocale = locale;
}
}
}

/** force all open activity to recreate */
public static void recreate(Activity child) {
Activity context = child;
while (context != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
context.recreate();
} else {
// https://stackoverflow.com/questions/11495130/android-recreate-functions-in-api-7
context.startActivity(new Intent(context, context.getClass()));
context.finish();
}
context = context.getParent();
}
}

}
4 changes: 3 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 k3b
Copyright (C) 2014-2017 k3b
This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/)
Expand Down Expand Up @@ -65,4 +65,6 @@ Möchten Sie statt dessen den Standardwert \'%2$s\' verwenden? </string>
<string name="pref_short_text_file_title" >Dateiname kurzer Text.</string>
<string name="pref_zipfile_title">Zip-Datei</string>
<string name="pref_settings_title">Einstellungen</string>
<string name="settings_locale_title">Sprache</string>
<string name="settings_locale_os_language">(Sprache des Gerätes)</string>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 k3b
Copyright (C) 2014-2017 k3b
French translation 2017 by Poussinou
This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/)
Expand Down Expand Up @@ -65,4 +66,6 @@ Voulez-vous utiliser l\'emplacement par défaut \'%2$s\' à la place? </string>
<string name="pref_short_text_file_title">Short Text File in zip</string>
<string name="pref_zipfile_title">Output Zip File</string>
<string name="pref_settings_title">Paramètres</string>
<string name="settings_locale_title">Langue</string>
<string name="settings_locale_os_language">(langue de l\'appareil)</string>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 k3b
Copyright (C) 2014-2017 k3b
Japanese translation 2017 by naofum
This file is part of de.k3b.android.toGoZip (https://github.com/k3b/ToGoZip/)
Expand Down Expand Up @@ -65,4 +66,6 @@ this program. If not, see <http://www.gnu.org/licenses/>
<string name="pref_short_text_file_title">zip に短いテキストファイル</string>
<string name="pref_zipfile_title">出力 Zip ファイル</string>
<string name="pref_settings_title">設定</string>
<string name="settings_locale_title">言語</string>
<string name="settings_locale_os_language">(デバイスの言語)</string>
</resources>

0 comments on commit a93b472

Please sign in to comment.