forked from upstream-mirrors/OpenTracks
Settings: language chooser is now available for all Android versions.
Part of #1974
This commit is contained in:
@@ -401,6 +401,16 @@ limitations under the License.
|
|||||||
android:name=".services.TrackDeleteService"
|
android:name=".services.TrackDeleteService"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:permission="android.permission.BIND_JOB_SERVICE"/>
|
android:permission="android.permission.BIND_JOB_SERVICE"/>
|
||||||
|
|
||||||
|
<!--Necessary for SDK < 33 to (re-)store per app locale information-->
|
||||||
|
<service
|
||||||
|
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
|
||||||
|
android:enabled="false"
|
||||||
|
android:exported="false">
|
||||||
|
<meta-data
|
||||||
|
android:name="autoStoreLocales"
|
||||||
|
android:value="true" />
|
||||||
|
</service>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
<queries>
|
<queries>
|
||||||
|
|||||||
@@ -1,25 +1,36 @@
|
|||||||
package de.dennisguse.opentracks.settings;
|
package de.dennisguse.opentracks.settings;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.app.LocaleConfig;
|
import android.app.LocaleConfig;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.LocaleList;
|
import android.os.LocaleList;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AppCompatDelegate;
|
import androidx.appcompat.app.AppCompatDelegate;
|
||||||
|
import androidx.core.app.LocaleManagerCompat;
|
||||||
import androidx.core.os.LocaleListCompat;
|
import androidx.core.os.LocaleListCompat;
|
||||||
import androidx.preference.ListPreference;
|
import androidx.preference.ListPreference;
|
||||||
|
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
|
|
||||||
public class LocalePreference extends ListPreference {
|
public class LocalePreference extends ListPreference {
|
||||||
|
|
||||||
|
private String TAG = LocalePreference.class.getSimpleName();
|
||||||
|
|
||||||
public LocalePreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
public LocalePreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||||
super(context, attrs, defStyleAttr, defStyleRes);
|
super(context, attrs, defStyleAttr, defStyleRes);
|
||||||
init(context);
|
init(context);
|
||||||
@@ -40,18 +51,17 @@ public class LocalePreference extends ListPreference {
|
|||||||
init(context);
|
init(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /data/user/0/de.dennisguse.opentracks.debug/files/androidx.appcompat.app.AppCompatDelegate.application_locales_record_file: open failed: ENOENT (No such file or directory)
|
||||||
private void init(Context context) {
|
private void init(Context context) {
|
||||||
setPersistent(false);
|
setPersistent(false);
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
|
||||||
setEnabled(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LocaleItem systemDefaultLocale = new LocaleItem("", context.getString(R.string.settings_locale_system_default));
|
LocaleItem systemDefaultLocale = new LocaleItem("", context.getString(R.string.settings_locale_system_default));
|
||||||
|
|
||||||
LocaleItem currentLocale = new LocaleItem(Locale.getDefault().toLanguageTag(), Locale.getDefault().getDisplayName());
|
LocaleItem currentLocale = new LocaleItem(Locale.getDefault().toLanguageTag(), Locale.getDefault().getDisplayName());
|
||||||
|
|
||||||
// All available options
|
// All available options
|
||||||
LocaleList supportedLocales = LocaleConfig.fromContextIgnoringOverride(context).getSupportedLocales();
|
LocaleList supportedLocales = getLocaleListCompat();
|
||||||
|
|
||||||
ArrayList<LocaleItem> localeItemsSorting = new ArrayList<>();
|
ArrayList<LocaleItem> localeItemsSorting = new ArrayList<>();
|
||||||
for (int i = 0; i < supportedLocales.size(); i++) {
|
for (int i = 0; i < supportedLocales.size(); i++) {
|
||||||
Locale current = supportedLocales.get(i);
|
Locale current = supportedLocales.get(i);
|
||||||
@@ -104,4 +114,36 @@ public class LocalePreference extends ListPreference {
|
|||||||
String displayName
|
String displayName
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO Get this functionality from any Androidx compat library: should be in LocaleListCompat or LocaleManagerCompat
|
||||||
|
// 2024-12-12: on Android 14-: LocaleManagerCompat.getApplicationLocales(getContext()) returned "[]"
|
||||||
|
// See: https://stackoverflow.com/questions/78116375/per-app-language-preferences-get-list-of-apps-available-language-programmatic
|
||||||
|
@Deprecated
|
||||||
|
private LocaleList getLocaleListCompat() {
|
||||||
|
var a = LocaleManagerCompat.getApplicationLocales(getContext());
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||||
|
return LocaleConfig.fromContextIgnoringOverride(getContext()).getSupportedLocales();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("DiscouragedApi") int localesConfigId = getContext().getResources().getIdentifier("_generated_res_locale_config", "xml", getContext().getPackageName());
|
||||||
|
|
||||||
|
List<String> localeList = new ArrayList<>();
|
||||||
|
XmlPullParser xpp = getContext().getResources().getXml(localesConfigId);
|
||||||
|
try {
|
||||||
|
while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
|
||||||
|
if (xpp.getEventType() == XmlPullParser.START_TAG) {
|
||||||
|
if ("locale".equals(xpp.getName()) && xpp.getAttributeCount() > 0 && xpp.getAttributeName(0).equals("name")) {
|
||||||
|
localeList.add(xpp.getAttributeValue(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xpp.next();
|
||||||
|
}
|
||||||
|
} catch (IOException | XmlPullParserException e) {
|
||||||
|
Log.e(TAG, "Could not load locales: " + e.getMessage());
|
||||||
|
}
|
||||||
|
Optional<String> locales = localeList.stream().reduce((s1, s2) -> s1 + "," + s2);
|
||||||
|
return locales
|
||||||
|
.map(LocaleList::forLanguageTags)
|
||||||
|
.orElseGet(LocaleList::getEmptyLocaleList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user