Skip to content

Commit 70416e7

Browse files
committed
Move app language setting migration to SettingMigrations
1 parent 980a35a commit 70416e7

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

app/src/main/java/org/schabi/newpipe/App.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import android.app.Application;
44
import android.content.Context;
55
import android.content.SharedPreferences;
6-
import android.os.Build;
76
import android.util.Log;
87

98
import androidx.annotation.NonNull;
10-
import androidx.appcompat.app.AppCompatDelegate;
119
import androidx.core.app.NotificationChannelCompat;
1210
import androidx.core.app.NotificationManagerCompat;
13-
import androidx.core.os.LocaleListCompat;
1411
import androidx.preference.PreferenceManager;
1512

1613
import com.jakewharton.processphoenix.ProcessPhoenix;
@@ -125,29 +122,6 @@ public void onCreate() {
125122
configureRxJavaErrorHandler();
126123

127124
YoutubeStreamExtractor.setPoTokenProvider(PoTokenProviderImpl.INSTANCE);
128-
129-
if (Build.VERSION.SDK_INT >= 33) {
130-
ensureAppLanguagePreferenceIsMigrated(prefs);
131-
}
132-
}
133-
134-
private void ensureAppLanguagePreferenceIsMigrated(final SharedPreferences prefs) {
135-
final String appLanguageDefaultValue = getString(R.string.default_localization_key);
136-
final String appLanguageKey = getString(R.string.app_language_key);
137-
final String appLanguageCurrentValue = prefs.getString(appLanguageKey, null);
138-
if (appLanguageCurrentValue != null) {
139-
// Migrate to Android per-app language settings
140-
prefs.edit().remove(appLanguageKey).apply();
141-
if (!appLanguageCurrentValue.equals(appLanguageDefaultValue)) {
142-
try {
143-
AppCompatDelegate.setApplicationLocales(
144-
LocaleListCompat.forLanguageTags(appLanguageCurrentValue)
145-
);
146-
} catch (final RuntimeException e) {
147-
Log.e(TAG, "Error migrating to Android 13+ per-app language settings");
148-
}
149-
}
150-
}
151125
}
152126

153127
@Override

app/src/main/java/org/schabi/newpipe/settings/SettingMigrations.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import android.content.Context;
44
import android.content.SharedPreferences;
5+
import android.os.Build;
56
import android.util.Log;
67

78
import androidx.annotation.NonNull;
9+
import androidx.appcompat.app.AppCompatDelegate;
10+
import androidx.core.os.LocaleListCompat;
811
import androidx.preference.PreferenceManager;
912

1013
import 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

Comments
 (0)