Skip to content

Commit 7f10312

Browse files
committed
Move migration to NewPipeSettings
1 parent c7bf498 commit 7f10312

2 files changed

Lines changed: 34 additions & 35 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import android.content.SharedPreferences;
77
import android.os.Build;
88
import android.os.Environment;
9+
import android.util.Log;
910

1011
import androidx.annotation.NonNull;
1112
import androidx.annotation.StringRes;
13+
import androidx.appcompat.app.AppCompatDelegate;
14+
import androidx.core.os.LocaleListCompat;
1215
import androidx.preference.PreferenceManager;
1316

1417
import org.schabi.newpipe.App;
@@ -42,6 +45,7 @@
4245
* Helper class for global settings.
4346
*/
4447
public 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
}

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

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

33
import android.content.Context;
44
import android.content.SharedPreferences;
5-
import android.os.Build;
65
import android.util.Log;
76

87
import androidx.annotation.NonNull;
9-
import androidx.appcompat.app.AppCompatDelegate;
10-
import androidx.core.os.LocaleListCompat;
118
import androidx.preference.PreferenceManager;
129

1310
import org.schabi.newpipe.App;
@@ -146,36 +143,6 @@ protected void migrate(@NonNull final Context context) {
146143
}
147144
};
148145

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-
179146
/**
180147
* List of all implemented migrations.
181148
* <p>
@@ -189,13 +156,12 @@ protected void migrate(@NonNull final Context context) {
189156
MIGRATION_3_4,
190157
MIGRATION_4_5,
191158
MIGRATION_5_6,
192-
MIGRATION_6_7,
193159
};
194160

195161
/**
196162
* Version number for preferences. Must be incremented every time a migration is necessary.
197163
*/
198-
private static final int VERSION = 7;
164+
private static final int VERSION = 6;
199165

200166

201167
public static void runMigrationsIfNeeded(@NonNull final Context context) {

0 commit comments

Comments
 (0)