66import android .content .SharedPreferences ;
77import android .os .Build ;
88import android .os .Environment ;
9+ import android .util .Log ;
910
1011import androidx .annotation .NonNull ;
1112import androidx .annotation .StringRes ;
13+ import androidx .appcompat .app .AppCompatDelegate ;
14+ import androidx .core .os .LocaleListCompat ;
1215import androidx .preference .PreferenceManager ;
1316
1417import org .schabi .newpipe .App ;
4245 * Helper class for global settings.
4346 */
4447public final class NewPipeSettings {
48+ private static final String TAG = NewPipeSettings .class .toString ();
4549 private NewPipeSettings () { }
4650
4751 public static void initSettings (final Context context ) {
@@ -64,6 +68,7 @@ public static void initSettings(final Context context) {
6468 saveDefaultAudioDownloadDirectory (context );
6569
6670 disableMediaTunnelingIfNecessary (context );
71+ migrateAppLanguageSettingIfNecessary (context );
6772 }
6873
6974 static void saveDefaultVideoDownloadDirectory (final Context context ) {
@@ -184,4 +189,32 @@ public static void setMediaTunneling(@NonNull final Context context) {
184189 DeviceUtils .MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION ).apply ();
185190 }
186191 }
192+
193+ private static void migrateAppLanguageSettingIfNecessary (@ NonNull final Context context ) {
194+ // Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
195+ // public per-app language APIs to read and set the UI language for NewPipe.
196+ // If running on Android 13+, the following migration will move any existing custom
197+ // app language in SharedPreferences to use the public per-app language APIs instead.
198+ if (Build .VERSION .SDK_INT >= 33 ) {
199+ final SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences (context );
200+ final String appLanguageKey = context .getString (R .string .app_language_key );
201+ final String appLanguageValue = sp .getString (appLanguageKey , null );
202+ if (appLanguageValue != null ) {
203+ sp .edit ().remove (appLanguageKey ).apply ();
204+ final String appLanguageDefaultValue =
205+ context .getString (R .string .default_localization_key );
206+ if (!appLanguageValue .equals (appLanguageDefaultValue )) {
207+ try {
208+ AppCompatDelegate .setApplicationLocales (
209+ LocaleListCompat .forLanguageTags (appLanguageValue )
210+ );
211+ } catch (final RuntimeException e ) {
212+ Log .e (TAG , "Failed to migrate previous custom app language "
213+ + "setting to public per-app language APIs"
214+ );
215+ }
216+ }
217+ }
218+ }
219+ }
187220}
0 commit comments