22
33import android .content .Context ;
44import android .content .SharedPreferences ;
5+ import android .os .Build ;
56import android .util .Log ;
67
78import androidx .annotation .NonNull ;
9+ import androidx .appcompat .app .AppCompatDelegate ;
10+ import androidx .core .os .LocaleListCompat ;
811import androidx .preference .PreferenceManager ;
912
1013import org .schabi .newpipe .App ;
@@ -143,6 +146,36 @@ protected void migrate(@NonNull final Context context) {
143146 }
144147 };
145148
149+ public static final Migration MIGRATION_6_7 = new Migration (6 , 7 ) {
150+ @ Override
151+ protected void migrate (@ NonNull final Context context ) {
152+ // Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
153+ // public per-app language APIs to read and set the UI language for NewPipe.
154+ // If running on Android 13+, the following migration will move any existing custom
155+ // app language in SharedPreferences to use the public per-app language APIs instead.
156+ if (Build .VERSION .SDK_INT >= 33 ) {
157+ final String appLanguageDefaultValue =
158+ context .getString (R .string .default_localization_key );
159+ final String appLanguageKey = context .getString (R .string .app_language_key );
160+ final String appLanguageCurrentValue = sp .getString (appLanguageKey , null );
161+ if (appLanguageCurrentValue != null ) {
162+ sp .edit ().remove (appLanguageKey ).apply ();
163+ if (!appLanguageCurrentValue .equals (appLanguageDefaultValue )) {
164+ try {
165+ AppCompatDelegate .setApplicationLocales (
166+ LocaleListCompat .forLanguageTags (appLanguageCurrentValue )
167+ );
168+ } catch (final RuntimeException e ) {
169+ Log .e (TAG , "Failed to migrate previous custom app language "
170+ + "setting to public per-app language APIs"
171+ );
172+ }
173+ }
174+ }
175+ }
176+ }
177+ };
178+
146179 /**
147180 * List of all implemented migrations.
148181 * <p>
@@ -156,12 +189,13 @@ protected void migrate(@NonNull final Context context) {
156189 MIGRATION_3_4 ,
157190 MIGRATION_4_5 ,
158191 MIGRATION_5_6 ,
192+ MIGRATION_6_7 ,
159193 };
160194
161195 /**
162196 * Version number for preferences. Must be incremented every time a migration is necessary.
163197 */
164- private static final int VERSION = 6 ;
198+ private static final int VERSION = 7 ;
165199
166200
167201 public static void runMigrationsIfNeeded (@ NonNull final Context context ) {
0 commit comments