Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDexApplication;
import androidx.preference.PreferenceManager;

import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
import io.reactivex.rxjava3.exceptions.UndeliverableException;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.acra.ACRA;
import org.acra.config.ACRAConfigurationException;
import org.acra.config.CoreConfiguration;
Expand All @@ -31,21 +41,6 @@
import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.StateSaver;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
import io.reactivex.rxjava3.exceptions.UndeliverableException;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/*
* Copyright (C) Hans-Christoph Steiner 2016 <[email protected]>
* App.java is part of NewPipe.
Expand Down Expand Up @@ -93,9 +88,9 @@ public void onCreate() {
SettingsActivity.initSettings(this);

NewPipe.init(getDownloader(),
Localization.getPreferredLocalization(this),
Localization.getPreferredContentCountry(this));
Localization.init(getApplicationContext());
Localization.getPreferredLocalization(this),
Localization.getPreferredContentCountry(this));
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));

StateSaver.init(this);
initNotificationChannels();
Expand Down
16 changes: 6 additions & 10 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package org.schabi.newpipe;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -41,7 +43,6 @@
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
Expand All @@ -52,9 +53,10 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.schabi.newpipe.databinding.ActivityMainBinding;
import org.schabi.newpipe.databinding.DrawerHeaderBinding;
import org.schabi.newpipe.databinding.DrawerLayoutBinding;
Expand Down Expand Up @@ -87,12 +89,6 @@
import org.schabi.newpipe.util.ThemeHelper;
import org.schabi.newpipe.views.FocusOverlayView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release");
Expand Down Expand Up @@ -468,7 +464,7 @@ protected void onDestroy() {
protected void onResume() {
assureCorrectAppLanguage(this);
// Change the date format to match the selected language on resume
Localization.init(getApplicationContext());
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
super.onResume();

// Close drawer on return, and don't show animation,
Expand Down
27 changes: 12 additions & 15 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;

import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.ktx.OffsetDateTimeKt;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
Expand All @@ -33,6 +24,12 @@
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.ktx.OffsetDateTimeKt;


/*
Expand Down Expand Up @@ -62,10 +59,6 @@ public final class Localization {

private Localization() { }

public static void init(final Context context) {
initPrettyTime(context);
}

@NonNull
public static String concatenateStrings(final String... strings) {
return concatenateStrings(Arrays.asList(strings));
Expand Down Expand Up @@ -307,12 +300,16 @@ public static String localizeDuration(final Context context, final int durationI
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/

private static void initPrettyTime(final Context context) {
prettyTime = new PrettyTime(getAppLocale(context));
public static void initPrettyTime(final PrettyTime time) {
prettyTime = time;
// Do not use decades as YouTube doesn't either.
prettyTime.removeUnit(Decade.class);
}

public static PrettyTime resolvePrettyTime(final Context context) {
return new PrettyTime(getAppLocale(context));
}

public static String relativeTime(final OffsetDateTime offsetDateTime) {
return relativeTime(OffsetDateTimeKt.toCalendar(offsetDateTime));
}
Expand Down
40 changes: 40 additions & 0 deletions app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.schabi.newpipe.util

import org.junit.Assert.assertEquals
import org.junit.Test
import org.ocpsoft.prettytime.PrettyTime
import java.text.SimpleDateFormat
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.GregorianCalendar
import java.util.Locale

class LocalizationTest {

@Test
fun `After initializing pretty time relativeTime() with a Calendar must work`() {
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))

val actual = Localization.relativeTime(GregorianCalendar(2021, 1, 6))

// yes this assertion is true, even if it should be 5 days, it works as it is. Future research required.
assertEquals("1 month from now", actual)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am just unable to understand Kotlin, in this case please enlighten me. But this assertion will never be true because both times are just 5 days apart from each other - especially when I compare it with the last test? So shouldn't it be assertEquals("5 days from now", actual) here as well?

Copy link
Collaborator Author

@XiangRongLin XiangRongLin Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutly correct, but this is actually what pretty time returns. I would guess it's either a bug in PrettyTime or a limitation when comparing with GregorianCalendar.

For these tests i went with the assumption that the current state is correct. So i just added assertions with what is returned, so future regressions are covered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, can you add this explanation as an inline comment so that no one stumbles upon this later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the first paragraph of the second (or both)?

I would add the first with the note that further research is required

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just add anything meaningful like // yes this assertion is true, even if it should be 5 days, it works as it is. Future research required

Maybe it has to do something with timezone shift. Could it be that Locale.ENGLISH is defaulting to the global standard locale US in Android? This would explain why 2021, 1, 1 is in fact 2020,12,12 normalized to UTC and could explain "1 month from now"

}

@Test(expected = NullPointerException::class)
fun `relativeTime() must fail without initializing pretty time`() {
Localization.relativeTime(GregorianCalendar(2021, 1, 6))
}

@Test
fun `relativeTime() with a OffsetDateTime must work`() {
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1")
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH))

val offset = OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC)
val actual = Localization.relativeTime(offset)

assertEquals("5 days from now", actual)
}
}