Skip to content

Commit 41773de

Browse files
Merge branch 'refactor' into Video-description-compose
2 parents cbc69a0 + 1d94fd1 commit 41773de

File tree

12 files changed

+128
-18
lines changed

12 files changed

+128
-18
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ android {
101101
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
102102
}
103103

104+
androidResources {
105+
generateLocaleConfig = true
106+
}
107+
104108
buildFeatures {
105109
viewBinding true
106110
compose true

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ protected void onCreate(final Bundle savedInstanceState) {
186186
&& ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
187187
UpdateSettingsFragment.askForConsentToUpdateChecks(this);
188188
}
189+
190+
Localization.migrateAppLanguageSettingIfNecessary(getApplicationContext());
189191
}
190192

191193
@Override

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.schabi.newpipe.settings;
22

33
import android.content.Context;
4+
import android.content.Intent;
5+
import android.net.Uri;
6+
import android.os.Build;
47
import android.os.Bundle;
8+
import android.provider.Settings;
59
import android.util.Log;
610
import android.widget.Toast;
711

12+
import androidx.appcompat.app.AppCompatDelegate;
813
import androidx.preference.Preference;
914

1015
import org.schabi.newpipe.DownloaderImpl;
@@ -15,13 +20,13 @@
1520
import org.schabi.newpipe.util.image.ImageStrategy;
1621
import org.schabi.newpipe.util.image.PreferredImageQuality;
1722

23+
import java.util.Locale;
24+
1825
import coil3.SingletonImageLoader;
1926

2027
public class ContentSettingsFragment extends BasePreferenceFragment {
2128
private String youtubeRestrictedModeEnabledKey;
2229

23-
private Localization initialSelectedLocalization;
24-
private ContentCountry initialSelectedContentCountry;
2530
private String initialLanguage;
2631

2732
@Override
@@ -30,12 +35,28 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
3035

3136
addPreferencesFromResourceRegistry();
3237

33-
initialSelectedLocalization = org.schabi.newpipe.util.Localization
34-
.getPreferredLocalization(requireContext());
35-
initialSelectedContentCountry = org.schabi.newpipe.util.Localization
36-
.getPreferredContentCountry(requireContext());
3738
initialLanguage = defaultPreferences.getString(getString(R.string.app_language_key), "en");
3839

40+
if (Build.VERSION.SDK_INT >= 33) {
41+
requirePreference(R.string.app_language_key).setVisible(false);
42+
final Preference newAppLanguagePref =
43+
requirePreference(R.string.app_language_android_13_and_up_key);
44+
newAppLanguagePref.setSummaryProvider(preference -> {
45+
final Locale customLocale = AppCompatDelegate.getApplicationLocales().get(0);
46+
if (customLocale != null) {
47+
return customLocale.getDisplayName();
48+
}
49+
return getString(R.string.systems_language);
50+
});
51+
newAppLanguagePref.setOnPreferenceClickListener(preference -> {
52+
final Intent intent = new Intent(Settings.ACTION_APP_LOCALE_SETTINGS)
53+
.setData(Uri.fromParts("package", requireContext().getPackageName(), null));
54+
startActivity(intent);
55+
return true;
56+
});
57+
newAppLanguagePref.setVisible(true);
58+
}
59+
3960
final Preference imageQualityPreference = requirePreference(R.string.image_quality_key);
4061
imageQualityPreference.setOnPreferenceChangeListener(
4162
(preference, newValue) -> {
@@ -70,19 +91,21 @@ public boolean onPreferenceTreeClick(final Preference preference) {
7091
public void onDestroy() {
7192
super.onDestroy();
7293

73-
final Localization selectedLocalization = org.schabi.newpipe.util.Localization
74-
.getPreferredLocalization(requireContext());
75-
final ContentCountry selectedContentCountry = org.schabi.newpipe.util.Localization
76-
.getPreferredContentCountry(requireContext());
7794
final String selectedLanguage =
7895
defaultPreferences.getString(getString(R.string.app_language_key), "en");
7996

80-
if (!selectedLocalization.equals(initialSelectedLocalization)
81-
|| !selectedContentCountry.equals(initialSelectedContentCountry)
82-
|| !selectedLanguage.equals(initialLanguage)) {
83-
Toast.makeText(requireContext(), R.string.localization_changes_requires_app_restart,
84-
Toast.LENGTH_LONG).show();
85-
97+
if (!selectedLanguage.equals(initialLanguage)) {
98+
if (Build.VERSION.SDK_INT < 33) {
99+
Toast.makeText(
100+
requireContext(),
101+
R.string.localization_changes_requires_app_restart,
102+
Toast.LENGTH_LONG
103+
).show();
104+
}
105+
final Localization selectedLocalization = org.schabi.newpipe.util.Localization
106+
.getPreferredLocalization(requireContext());
107+
final ContentCountry selectedContentCountry = org.schabi.newpipe.util.Localization
108+
.getPreferredContentCountry(requireContext());
86109
NewPipe.setupLocalization(selectedLocalization, selectedContentCountry);
87110
}
88111
}

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
import android.text.TextUtils;
1313
import android.text.format.DateUtils;
1414
import android.util.DisplayMetrics;
15+
import android.util.Log;
1516

1617
import androidx.annotation.NonNull;
1718
import androidx.annotation.Nullable;
1819
import androidx.annotation.PluralsRes;
1920
import androidx.annotation.StringRes;
21+
import androidx.appcompat.app.AppCompatDelegate;
2022
import androidx.core.math.MathUtils;
23+
import androidx.core.os.LocaleListCompat;
2124
import androidx.preference.PreferenceManager;
2225

2326
import org.ocpsoft.prettytime.PrettyTime;
@@ -39,6 +42,7 @@
3942
import java.util.Arrays;
4043
import java.util.List;
4144
import java.util.Locale;
45+
import java.util.Objects;
4246
import java.util.stream.Collectors;
4347

4448

@@ -63,6 +67,7 @@
6367
*/
6468

6569
public final class Localization {
70+
private static final String TAG = Localization.class.toString();
6671
public static final String DOT_SEPARATOR = " • ";
6772
private static PrettyTime prettyTime;
6873

@@ -101,6 +106,10 @@ public static Locale getPreferredLocale(@NonNull final Context context) {
101106
}
102107

103108
public static Locale getAppLocale(@NonNull final Context context) {
109+
if (Build.VERSION.SDK_INT >= 33) {
110+
final Locale customLocale = AppCompatDelegate.getApplicationLocales().get(0);
111+
return Objects.requireNonNullElseGet(customLocale, Locale::getDefault);
112+
}
104113
return getLocaleFromPrefs(context, R.string.app_language_key);
105114
}
106115

@@ -422,4 +431,32 @@ private static String getQuantity(@NonNull final Context context,
422431
final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
423432
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
424433
}
434+
435+
public static void migrateAppLanguageSettingIfNecessary(@NonNull final Context context) {
436+
// Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
437+
// public per-app language APIs to read and set the UI language for NewPipe.
438+
// If running on Android 13+, the following code will migrate any existing custom
439+
// app language in SharedPreferences to use the public per-app language APIs instead.
440+
if (Build.VERSION.SDK_INT >= 33) {
441+
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
442+
final String appLanguageKey = context.getString(R.string.app_language_key);
443+
final String appLanguageValue = sp.getString(appLanguageKey, null);
444+
if (appLanguageValue != null) {
445+
sp.edit().remove(appLanguageKey).apply();
446+
final String appLanguageDefaultValue =
447+
context.getString(R.string.default_localization_key);
448+
if (!appLanguageValue.equals(appLanguageDefaultValue)) {
449+
try {
450+
AppCompatDelegate.setApplicationLocales(
451+
LocaleListCompat.forLanguageTags(appLanguageValue)
452+
);
453+
} catch (final RuntimeException e) {
454+
Log.e(TAG, "Failed to migrate previous custom app language "
455+
+ "setting to public per-app language APIs"
456+
);
457+
}
458+
}
459+
}
460+
}
461+
}
425462
}

app/src/main/java/org/schabi/newpipe/util/potoken/JavaScriptUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fun parseChallengeData(rawChallengeData: String): String {
1717
val descrambled = descramble(scrambled.getString(1))
1818
JsonParser.array().from(descrambled)
1919
} else {
20-
scrambled.getArray(1)
20+
scrambled.getArray(0)
2121
}
2222

2323
val messageId = challengeData.getString(0)

app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
import java.util.ArrayList;
7272
import java.util.Arrays;
7373
import java.util.Iterator;
74+
import java.util.Date;
75+
import java.util.Locale;
76+
import java.text.DateFormat;
7477

7578
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
7679
import io.reactivex.rxjava3.core.Observable;
@@ -208,11 +211,17 @@ public void onBindViewHolder(@NonNull ViewHolder view, @SuppressLint("RecyclerVi
208211
h.pause.setTitle(mission.unknownLength ? R.string.stop : R.string.pause);
209212
updateProgress(h);
210213
mPendingDownloadsItems.add(h);
214+
215+
h.date.setText("");
211216
} else {
212217
h.progress.setMarquee(false);
213218
h.status.setText("100%");
214219
h.progress.setProgress(1.0f);
215220
h.size.setText(Utility.formatBytes(item.mission.length));
221+
222+
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
223+
Date date = new Date(item.mission.timestamp);
224+
h.date.setText(dateFormat.format(date));
216225
}
217226
}
218227

@@ -832,6 +841,7 @@ class ViewHolderItem extends RecyclerView.ViewHolder {
832841
ImageView icon;
833842
TextView name;
834843
TextView size;
844+
TextView date;
835845
ProgressDrawable progress;
836846

837847
PopupMenu popupMenu;
@@ -862,6 +872,7 @@ class ViewHolderItem extends RecyclerView.ViewHolder {
862872
name = itemView.findViewById(R.id.item_name);
863873
icon = itemView.findViewById(R.id.item_icon);
864874
size = itemView.findViewById(R.id.item_size);
875+
date = itemView.findViewById(R.id.item_date);
865876

866877
name.setSelected(true);
867878

app/src/main/res/layout/mission_item.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282
android:textColor="@color/white"
8383
android:textSize="12sp" />
8484

85+
<org.schabi.newpipe.views.NewPipeTextView
86+
android:id="@+id/item_date"
87+
android:layout_width="wrap_content"
88+
android:layout_height="wrap_content"
89+
android:layout_below="@id/item_name"
90+
android:layout_alignParentRight="true"
91+
android:padding="6dp"
92+
android:singleLine="true"
93+
android:text=""
94+
android:textColor="@color/white"
95+
android:textSize="12sp" />
96+
8597
</RelativeLayout>
8698

8799
</RelativeLayout>

app/src/main/res/layout/mission_item_linear.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@
6262
android:textSize="12sp"
6363
android:textStyle="bold" />
6464

65+
<org.schabi.newpipe.views.NewPipeTextView
66+
android:id="@+id/item_date"
67+
android:layout_width="wrap_content"
68+
android:layout_height="wrap_content"
69+
android:layout_below="@id/item_name"
70+
android:layout_toLeftOf="@id/item_more"
71+
android:padding="6dp"
72+
android:singleLine="true"
73+
android:text=""
74+
android:textColor="@color/white"
75+
android:textSize="12sp" />
76+
6577
<ImageView
6678
android:id="@+id/item_more"
6779
style="?attr/buttonBarButtonStyle"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unqualifiedResLocale=en-US

app/src/main/res/values/settings_keys.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
<string name="playback_skip_silence_key">playback_skip_silence_key</string>
354354

355355
<string name="app_language_key">app_language_key</string>
356+
<string name="app_language_android_13_and_up_key">app_language_android_13_and_up_key</string>
356357

357358
<string name="feed_update_threshold_key">feed_update_threshold_key</string>
358359
<string name="feed_update_threshold_default_value">300</string>

0 commit comments

Comments
 (0)