mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-04-20 17:53:15 +00:00
Move app language setting migration to SettingMigrations
This commit is contained in:
parent
980a35a708
commit
70416e73f3
@ -3,14 +3,11 @@ package org.schabi.newpipe;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.core.app.NotificationChannelCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
import androidx.core.os.LocaleListCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.jakewharton.processphoenix.ProcessPhoenix;
|
||||
@ -125,29 +122,6 @@ public class App extends Application {
|
||||
configureRxJavaErrorHandler();
|
||||
|
||||
YoutubeStreamExtractor.setPoTokenProvider(PoTokenProviderImpl.INSTANCE);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
ensureAppLanguagePreferenceIsMigrated(prefs);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureAppLanguagePreferenceIsMigrated(final SharedPreferences prefs) {
|
||||
final String appLanguageDefaultValue = getString(R.string.default_localization_key);
|
||||
final String appLanguageKey = getString(R.string.app_language_key);
|
||||
final String appLanguageCurrentValue = prefs.getString(appLanguageKey, null);
|
||||
if (appLanguageCurrentValue != null) {
|
||||
// Migrate to Android per-app language settings
|
||||
prefs.edit().remove(appLanguageKey).apply();
|
||||
if (!appLanguageCurrentValue.equals(appLanguageDefaultValue)) {
|
||||
try {
|
||||
AppCompatDelegate.setApplicationLocales(
|
||||
LocaleListCompat.forLanguageTags(appLanguageCurrentValue)
|
||||
);
|
||||
} catch (final RuntimeException e) {
|
||||
Log.e(TAG, "Error migrating to Android 13+ per-app language settings");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,9 +2,12 @@ package org.schabi.newpipe.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.core.os.LocaleListCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.schabi.newpipe.App;
|
||||
@ -143,6 +146,36 @@ public final class SettingMigrations {
|
||||
}
|
||||
};
|
||||
|
||||
public static final Migration MIGRATION_6_7 = new Migration(6, 7) {
|
||||
@Override
|
||||
protected void migrate(@NonNull final Context context) {
|
||||
// Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
|
||||
// public per-app language APIs to read and set the UI language for NewPipe.
|
||||
// If running on Android 13+, the following migration will move any existing custom
|
||||
// app language in SharedPreferences to use the public per-app language APIs instead.
|
||||
if (Build.VERSION.SDK_INT >= 33) {
|
||||
final String appLanguageDefaultValue =
|
||||
context.getString(R.string.default_localization_key);
|
||||
final String appLanguageKey = context.getString(R.string.app_language_key);
|
||||
final String appLanguageCurrentValue = sp.getString(appLanguageKey, null);
|
||||
if (appLanguageCurrentValue != null) {
|
||||
sp.edit().remove(appLanguageKey).apply();
|
||||
if (!appLanguageCurrentValue.equals(appLanguageDefaultValue)) {
|
||||
try {
|
||||
AppCompatDelegate.setApplicationLocales(
|
||||
LocaleListCompat.forLanguageTags(appLanguageCurrentValue)
|
||||
);
|
||||
} catch (final RuntimeException e) {
|
||||
Log.e(TAG, "Failed to migrate previous custom app language "
|
||||
+ "setting to public per-app language APIs"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* List of all implemented migrations.
|
||||
* <p>
|
||||
@ -156,12 +189,13 @@ public final class SettingMigrations {
|
||||
MIGRATION_3_4,
|
||||
MIGRATION_4_5,
|
||||
MIGRATION_5_6,
|
||||
MIGRATION_6_7,
|
||||
};
|
||||
|
||||
/**
|
||||
* Version number for preferences. Must be incremented every time a migration is necessary.
|
||||
*/
|
||||
private static final int VERSION = 6;
|
||||
private static final int VERSION = 7;
|
||||
|
||||
|
||||
public static void runMigrationsIfNeeded(@NonNull final Context context) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user