From c745b845c50437aa7728743102b69ae7a5ea7178 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 18 Oct 2020 11:46:55 +0530 Subject: [PATCH] Switch to the Java 8 Date/Time API. --- .../schabi/newpipe/database/Converters.java | 22 ++++++++------ .../newpipe/database/feed/dao/FeedDAO.kt | 14 ++++----- .../feed/model/FeedLastUpdatedEntity.kt | 5 ++-- .../history/model/SearchHistoryEntry.java | 11 +++---- .../history/model/StreamHistoryEntity.java | 13 +++++---- .../history/model/StreamHistoryEntry.kt | 6 ++-- .../database/stream/StreamStatisticsEntry.kt | 13 ++++----- .../newpipe/database/stream/dao/StreamDAO.kt | 4 +-- .../database/stream/model/StreamEntity.kt | 13 ++++----- .../fragments/detail/VideoDetailFragment.java | 2 +- .../org/schabi/newpipe/ktx/OffsetDateTime.kt | 10 +++++++ .../newpipe/local/LocalItemListAdapter.java | 11 +++---- .../newpipe/local/feed/FeedDatabaseManager.kt | 29 +++++++++---------- .../newpipe/local/feed/FeedViewModel.kt | 11 ++++--- .../local/feed/service/FeedLoadService.kt | 7 ++--- .../local/history/HistoryRecordManager.java | 7 +++-- .../newpipe/local/holder/LocalItemHolder.java | 4 +-- .../local/holder/LocalPlaylistItemHolder.java | 6 ++-- .../holder/LocalPlaylistStreamItemHolder.java | 4 +-- .../LocalStatisticStreamItemHolder.java | 10 +++---- .../local/holder/PlaylistItemHolder.java | 4 +-- .../holder/RemotePlaylistItemHolder.java | 6 ++-- .../org/schabi/newpipe/util/Localization.java | 17 +++++++---- checkstyle-suppressions.xml | 2 +- 24 files changed, 122 insertions(+), 109 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt diff --git a/app/src/main/java/org/schabi/newpipe/database/Converters.java b/app/src/main/java/org/schabi/newpipe/database/Converters.java index ca2d8d875..c46b5f427 100644 --- a/app/src/main/java/org/schabi/newpipe/database/Converters.java +++ b/app/src/main/java/org/schabi/newpipe/database/Converters.java @@ -5,31 +5,35 @@ import androidx.room.TypeConverter; import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.local.subscription.FeedGroupIcon; -import java.util.Date; +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; public final class Converters { private Converters() { } /** - * Convert a long value to a date. + * Convert a long value to a {@link OffsetDateTime}. * * @param value the long value - * @return the date + * @return the {@code OffsetDateTime} */ @TypeConverter - public static Date fromTimestamp(final Long value) { - return value == null ? null : new Date(value); + public static OffsetDateTime offsetDateTimeFromTimestamp(final Long value) { + return value == null ? null : OffsetDateTime.ofInstant(Instant.ofEpochMilli(value), + ZoneOffset.UTC); } /** - * Convert a date to a long value. + * Convert a {@link OffsetDateTime} to a long value. * - * @param date the date + * @param offsetDateTime the {@code OffsetDateTime} * @return the long value */ @TypeConverter - public static Long dateToTimestamp(final Date date) { - return date == null ? null : date.getTime(); + public static Long offsetDateTimeToTimestamp(final OffsetDateTime offsetDateTime) { + return offsetDateTime == null ? null : offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC) + .toInstant().toEpochMilli(); } @TypeConverter diff --git a/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt b/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt index 74f5b369e..d8b4f72cc 100644 --- a/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/feed/dao/FeedDAO.kt @@ -7,7 +7,7 @@ import androidx.room.Query import androidx.room.Transaction import androidx.room.Update import io.reactivex.Flowable -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.feed.model.FeedEntity import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity import org.schabi.newpipe.database.stream.model.StreamEntity @@ -58,10 +58,10 @@ abstract class FeedDAO { INNER JOIN feed f ON s.uid = f.stream_id - WHERE s.upload_date < :date + WHERE s.upload_date < :offsetDateTime ) """) - abstract fun unlinkStreamsOlderThan(date: Date) + abstract fun unlinkStreamsOlderThan(offsetDateTime: OffsetDateTime) @Query(""" DELETE FROM feed @@ -106,10 +106,10 @@ abstract class FeedDAO { INNER JOIN feed_group_subscription_join fgs ON fgs.subscription_id = lu.subscription_id AND fgs.group_id = :groupId """) - abstract fun oldestSubscriptionUpdate(groupId: Long): Flowable> + abstract fun oldestSubscriptionUpdate(groupId: Long): Flowable> @Query("SELECT MIN(last_updated) FROM feed_last_updated") - abstract fun oldestSubscriptionUpdateFromAll(): Flowable> + abstract fun oldestSubscriptionUpdateFromAll(): Flowable> @Query("SELECT COUNT(*) FROM feed_last_updated WHERE last_updated IS NULL") abstract fun notLoadedCount(): Flowable @@ -135,7 +135,7 @@ abstract class FeedDAO { WHERE lu.last_updated IS NULL OR lu.last_updated < :outdatedThreshold """) - abstract fun getAllOutdated(outdatedThreshold: Date): Flowable> + abstract fun getAllOutdated(outdatedThreshold: OffsetDateTime): Flowable> @Query(""" SELECT s.* FROM subscriptions s @@ -148,5 +148,5 @@ abstract class FeedDAO { WHERE lu.last_updated IS NULL OR lu.last_updated < :outdatedThreshold """) - abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: Date): Flowable> + abstract fun getAllOutdatedForGroup(groupId: Long, outdatedThreshold: OffsetDateTime): Flowable> } diff --git a/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedLastUpdatedEntity.kt b/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedLastUpdatedEntity.kt index 78b2550a5..069d1138f 100644 --- a/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedLastUpdatedEntity.kt +++ b/app/src/main/java/org/schabi/newpipe/database/feed/model/FeedLastUpdatedEntity.kt @@ -4,7 +4,7 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey import androidx.room.PrimaryKey -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity.Companion.FEED_LAST_UPDATED_TABLE import org.schabi.newpipe.database.feed.model.FeedLastUpdatedEntity.Companion.SUBSCRIPTION_ID import org.schabi.newpipe.database.subscription.SubscriptionEntity @@ -25,9 +25,8 @@ data class FeedLastUpdatedEntity( var subscriptionId: Long, @ColumnInfo(name = LAST_UPDATED) - var lastUpdated: Date? = null + var lastUpdated: OffsetDateTime? = null ) { - companion object { const val FEED_LAST_UPDATED_TABLE = "feed_last_updated" diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java b/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java index 752835182..fd4588700 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/SearchHistoryEntry.java @@ -6,7 +6,7 @@ import androidx.room.Ignore; import androidx.room.Index; import androidx.room.PrimaryKey; -import java.util.Date; +import java.time.OffsetDateTime; import static org.schabi.newpipe.database.history.model.SearchHistoryEntry.SEARCH; @@ -24,7 +24,7 @@ public class SearchHistoryEntry { private long id; @ColumnInfo(name = CREATION_DATE) - private Date creationDate; + private OffsetDateTime creationDate; @ColumnInfo(name = SERVICE_ID) private int serviceId; @@ -32,7 +32,8 @@ public class SearchHistoryEntry { @ColumnInfo(name = SEARCH) private String search; - public SearchHistoryEntry(final Date creationDate, final int serviceId, final String search) { + public SearchHistoryEntry(final OffsetDateTime creationDate, final int serviceId, + final String search) { this.serviceId = serviceId; this.creationDate = creationDate; this.search = search; @@ -46,11 +47,11 @@ public class SearchHistoryEntry { this.id = id; } - public Date getCreationDate() { + public OffsetDateTime getCreationDate() { return creationDate; } - public void setCreationDate(final Date creationDate) { + public void setCreationDate(final OffsetDateTime creationDate) { this.creationDate = creationDate; } diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java index bf1f7a9dd..ad1941adb 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntity.java @@ -9,7 +9,7 @@ import androidx.room.Index; import org.schabi.newpipe.database.stream.model.StreamEntity; -import java.util.Date; +import java.time.OffsetDateTime; import static androidx.room.ForeignKey.CASCADE; import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID; @@ -37,12 +37,12 @@ public class StreamHistoryEntity { @NonNull @ColumnInfo(name = STREAM_ACCESS_DATE) - private Date accessDate; + private OffsetDateTime accessDate; @ColumnInfo(name = STREAM_REPEAT_COUNT) private long repeatCount; - public StreamHistoryEntity(final long streamUid, @NonNull final Date accessDate, + public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate, final long repeatCount) { this.streamUid = streamUid; this.accessDate = accessDate; @@ -50,7 +50,7 @@ public class StreamHistoryEntity { } @Ignore - public StreamHistoryEntity(final long streamUid, @NonNull final Date accessDate) { + public StreamHistoryEntity(final long streamUid, @NonNull final OffsetDateTime accessDate) { this(streamUid, accessDate, 1); } @@ -62,11 +62,12 @@ public class StreamHistoryEntity { this.streamUid = streamUid; } - public Date getAccessDate() { + @NonNull + public OffsetDateTime getAccessDate() { return accessDate; } - public void setAccessDate(@NonNull final Date accessDate) { + public void setAccessDate(@NonNull final OffsetDateTime accessDate) { this.accessDate = accessDate; } diff --git a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.kt b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.kt index c653e6c6f..b928b00bf 100644 --- a/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.kt +++ b/app/src/main/java/org/schabi/newpipe/database/history/model/StreamHistoryEntry.kt @@ -2,7 +2,7 @@ package org.schabi.newpipe.database.history.model import androidx.room.ColumnInfo import androidx.room.Embedded -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.stream.model.StreamEntity data class StreamHistoryEntry( @@ -13,7 +13,7 @@ data class StreamHistoryEntry( val streamId: Long, @ColumnInfo(name = StreamHistoryEntity.STREAM_ACCESS_DATE) - val accessDate: Date, + val accessDate: OffsetDateTime, @ColumnInfo(name = StreamHistoryEntity.STREAM_REPEAT_COUNT) val repeatCount: Long @@ -25,6 +25,6 @@ data class StreamHistoryEntry( fun hasEqualValues(other: StreamHistoryEntry): Boolean { return this.streamEntity.uid == other.streamEntity.uid && streamId == other.streamId && - accessDate.compareTo(other.accessDate) == 0 + accessDate.isEqual(other.accessDate) } } diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.kt b/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.kt index dde1f0392..1e4c672ab 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.kt +++ b/app/src/main/java/org/schabi/newpipe/database/stream/StreamStatisticsEntry.kt @@ -2,26 +2,25 @@ package org.schabi.newpipe.database.stream import androidx.room.ColumnInfo import androidx.room.Embedded -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.LocalItem import org.schabi.newpipe.database.history.model.StreamHistoryEntity import org.schabi.newpipe.database.stream.model.StreamEntity import org.schabi.newpipe.extractor.stream.StreamInfoItem class StreamStatisticsEntry( - @Embedded + @Embedded val streamEntity: StreamEntity, - @ColumnInfo(name = StreamHistoryEntity.JOIN_STREAM_ID) + @ColumnInfo(name = StreamHistoryEntity.JOIN_STREAM_ID) val streamId: Long, - @ColumnInfo(name = STREAM_LATEST_DATE) - val latestAccessDate: Date, + @ColumnInfo(name = STREAM_LATEST_DATE) + val latestAccessDate: OffsetDateTime, - @ColumnInfo(name = STREAM_WATCH_COUNT) + @ColumnInfo(name = STREAM_WATCH_COUNT) val watchCount: Long ) : LocalItem { - fun toStreamInfoItem(): StreamInfoItem { val item = StreamInfoItem(streamEntity.serviceId, streamEntity.url, streamEntity.title, streamEntity.streamType) item.duration = streamEntity.duration diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt index 921c08b46..89757c17d 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt +++ b/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt @@ -7,7 +7,7 @@ import androidx.room.OnConflictStrategy import androidx.room.Query import androidx.room.Transaction import io.reactivex.Flowable -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.BasicDAO import org.schabi.newpipe.database.stream.model.StreamEntity import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_ID @@ -129,7 +129,7 @@ abstract class StreamDAO : BasicDAO { var textualUploadDate: String? = null, @ColumnInfo(name = StreamEntity.STREAM_UPLOAD_DATE) - var uploadDate: Date? = null, + var uploadDate: OffsetDateTime? = null, @ColumnInfo(name = StreamEntity.STREAM_IS_UPLOAD_DATE_APPROXIMATION) var isUploadDateApproximation: Boolean? = null, diff --git a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt index d13f5cc2d..defcb7acf 100644 --- a/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt +++ b/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt @@ -6,8 +6,7 @@ import androidx.room.Ignore import androidx.room.Index import androidx.room.PrimaryKey import java.io.Serializable -import java.util.Calendar -import java.util.Date +import java.time.OffsetDateTime import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_SERVICE_ID import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_TABLE import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_URL @@ -55,18 +54,17 @@ data class StreamEntity( var textualUploadDate: String? = null, @ColumnInfo(name = STREAM_UPLOAD_DATE) - var uploadDate: Date? = null, + var uploadDate: OffsetDateTime? = null, @ColumnInfo(name = STREAM_IS_UPLOAD_DATE_APPROXIMATION) var isUploadDateApproximation: Boolean? = null ) : Serializable { - @Ignore constructor(item: StreamInfoItem) : this( serviceId = item.serviceId, url = item.url, title = item.name, streamType = item.streamType, duration = item.duration, uploader = item.uploaderName, thumbnailUrl = item.thumbnailUrl, viewCount = item.viewCount, - textualUploadDate = item.textualUploadDate, uploadDate = item.uploadDate?.date()?.time, + textualUploadDate = item.textualUploadDate, uploadDate = item.uploadDate?.offsetDateTime(), isUploadDateApproximation = item.uploadDate?.isApproximation ) @@ -75,7 +73,7 @@ data class StreamEntity( serviceId = info.serviceId, url = info.url, title = info.name, streamType = info.streamType, duration = info.duration, uploader = info.uploaderName, thumbnailUrl = info.thumbnailUrl, viewCount = info.viewCount, - textualUploadDate = info.textualUploadDate, uploadDate = info.uploadDate?.date()?.time, + textualUploadDate = info.textualUploadDate, uploadDate = info.uploadDate?.offsetDateTime(), isUploadDateApproximation = info.uploadDate?.isApproximation ) @@ -95,8 +93,7 @@ data class StreamEntity( if (viewCount != null) item.viewCount = viewCount as Long item.textualUploadDate = textualUploadDate item.uploadDate = uploadDate?.let { - DateWrapper(Calendar.getInstance().apply { time = it }, isUploadDateApproximation - ?: false) + DateWrapper(it, isUploadDateApproximation ?: false) } return item diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index cd4a3a083..365804f75 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1527,7 +1527,7 @@ public class VideoDetailFragment if (info.getUploadDate() != null) { videoUploadDateView.setText(Localization - .localizeUploadDate(activity, info.getUploadDate().date().getTime())); + .localizeUploadDate(activity, info.getUploadDate().offsetDateTime())); videoUploadDateView.setVisibility(View.VISIBLE); } else { videoUploadDateView.setText(null); diff --git a/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt b/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt new file mode 100644 index 000000000..b3df83c25 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt @@ -0,0 +1,10 @@ +package org.schabi.newpipe.ktx + +import java.time.OffsetDateTime +import java.time.ZoneId +import java.util.Calendar +import java.util.GregorianCalendar + +fun OffsetDateTime.toCalendar(zoneId: ZoneId = ZoneId.systemDefault()): Calendar { + return GregorianCalendar.from(if (zoneId != offset) atZoneSameInstant(zoneId) else toZonedDateTime()) +} diff --git a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java index 5b67f51da..da8902c08 100644 --- a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java @@ -26,7 +26,8 @@ import org.schabi.newpipe.util.FallbackViewHolder; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.OnClickGesture; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.ArrayList; import java.util.List; @@ -69,7 +70,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter localItems; private final HistoryRecordManager recordManager; - private final DateFormat dateFormat; + private final DateTimeFormatter dateTimeFormatter; private boolean showFooter = false; private boolean useGridVariant = false; @@ -80,8 +81,8 @@ public class LocalItemListAdapter extends RecyclerView.Adapter(); - dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, - Localization.getPreferredLocale(context)); + dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT) + .withLocale(Localization.getPreferredLocale(context)); } public void setSelectedListener(final OnClickGesture listener) { @@ -303,7 +304,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter { return when (groupId) { @@ -64,7 +60,7 @@ class FeedDatabaseManager(context: Context) { } } - fun outdatedSubscriptionsForGroup(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, outdatedThreshold: Date) = + fun outdatedSubscriptionsForGroup(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, outdatedThreshold: OffsetDateTime) = feedTable.getAllOutdatedForGroup(groupId, outdatedThreshold) fun markAsOutdated(subscriptionId: Long) = feedTable @@ -73,7 +69,7 @@ class FeedDatabaseManager(context: Context) { fun upsertAll( subscriptionId: Long, items: List, - oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.time + oldestAllowedDate: OffsetDateTime = FEED_OLDEST_ALLOWED_DATE ) { val itemsToInsert = ArrayList() loop@ for (streamItem in items) { @@ -81,7 +77,7 @@ class FeedDatabaseManager(context: Context) { itemsToInsert += when { uploadDate == null && streamItem.streamType == StreamType.LIVE_STREAM -> streamItem - uploadDate != null && uploadDate.date().time >= oldestAllowedDate -> streamItem + uploadDate != null && uploadDate.offsetDateTime() >= oldestAllowedDate -> streamItem else -> continue@loop } } @@ -96,10 +92,11 @@ class FeedDatabaseManager(context: Context) { feedTable.insertAll(feedEntities) } - feedTable.setLastUpdatedForSubscription(FeedLastUpdatedEntity(subscriptionId, Calendar.getInstance().time)) + feedTable.setLastUpdatedForSubscription(FeedLastUpdatedEntity(subscriptionId, + OffsetDateTime.now(ZoneOffset.UTC))) } - fun removeOrphansOrOlderStreams(oldestAllowedDate: Date = FEED_OLDEST_ALLOWED_DATE.time) { + fun removeOrphansOrOlderStreams(oldestAllowedDate: OffsetDateTime = FEED_OLDEST_ALLOWED_DATE) { feedTable.unlinkStreamsOlderThan(oldestAllowedDate) streamTable.deleteOrphans() } @@ -159,7 +156,7 @@ class FeedDatabaseManager(context: Context) { .observeOn(AndroidSchedulers.mainThread()) } - fun oldestSubscriptionUpdate(groupId: Long): Flowable> { + fun oldestSubscriptionUpdate(groupId: Long): Flowable> { return when (groupId) { FeedGroupEntity.GROUP_ALL_ID -> feedTable.oldestSubscriptionUpdateFromAll() else -> feedTable.oldestSubscriptionUpdate(groupId) diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt index da2b5ffa4..13c3183da 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/FeedViewModel.kt @@ -9,11 +9,11 @@ import io.reactivex.Flowable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.functions.Function4 import io.reactivex.schedulers.Schedulers -import java.util.Calendar -import java.util.Date +import java.time.OffsetDateTime import java.util.concurrent.TimeUnit import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.extractor.stream.StreamInfoItem +import org.schabi.newpipe.ktx.toCalendar import org.schabi.newpipe.local.feed.service.FeedEventManager import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.IdleEvent @@ -41,7 +41,7 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn feedDatabaseManager.notLoadedCount(groupId), feedDatabaseManager.oldestSubscriptionUpdate(groupId), - Function4 { t1: FeedEventManager.Event, t2: List, t3: Long, t4: List -> + Function4 { t1: FeedEventManager.Event, t2: List, t3: Long, t4: List -> return@Function4 CombineResultHolder(t1, t2, t3, t4.firstOrNull()) } ) @@ -51,8 +51,7 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn .subscribe { val (event, listFromDB, notLoadedCount, oldestUpdate) = it - val oldestUpdateCalendar = - oldestUpdate?.let { Calendar.getInstance().apply { time = it } } + val oldestUpdateCalendar = oldestUpdate?.toCalendar() mutableStateLiveData.postValue(when (event) { is IdleEvent -> FeedState.LoadedState(listFromDB, oldestUpdateCalendar, notLoadedCount) @@ -71,5 +70,5 @@ class FeedViewModel(applicationContext: Context, val groupId: Long = FeedGroupEn combineDisposable.dispose() } - private data class CombineResultHolder(val t1: FeedEventManager.Event, val t2: List, val t3: Long, val t4: Date?) + private data class CombineResultHolder(val t1: FeedEventManager.Event, val t2: List, val t3: Long, val t4: OffsetDateTime?) } diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt index 556c215d9..8d3afbc7e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt @@ -41,7 +41,8 @@ import io.reactivex.functions.Function import io.reactivex.processors.PublishProcessor import io.reactivex.schedulers.Schedulers import java.io.IOException -import java.util.Calendar +import java.time.OffsetDateTime +import java.time.ZoneOffset import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger @@ -172,9 +173,7 @@ class FeedLoadService : Service() { private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedSeconds: Int) { feedResultsHolder = ResultsHolder() - val outdatedThreshold = Calendar.getInstance().apply { - add(Calendar.SECOND, -thresholdOutdatedSeconds) - }.time + val outdatedThreshold = OffsetDateTime.now(ZoneOffset.UTC).minusSeconds(thresholdOutdatedSeconds.toLong()) val subscriptions = when (groupId) { FeedGroupEntity.GROUP_ALL_ID -> feedDatabaseManager.outdatedSubscriptions(outdatedThreshold) diff --git a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java index 6af57bc94..7f5c4f7a7 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java @@ -44,9 +44,10 @@ import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.player.playqueue.PlayQueueItem; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.List; import io.reactivex.Completable; @@ -85,7 +86,7 @@ public class HistoryRecordManager { return Maybe.empty(); } - final Date currentTime = new Date(); + final OffsetDateTime currentTime = OffsetDateTime.now(ZoneOffset.UTC); return Maybe.fromCallable(() -> database.runInTransaction(() -> { final long streamId = streamTable.upsert(new StreamEntity(info)); final StreamHistoryEntity latestEntry = streamHistoryTable.getLatestEntry(streamId); @@ -161,7 +162,7 @@ public class HistoryRecordManager { return Maybe.empty(); } - final Date currentTime = new Date(); + final OffsetDateTime currentTime = OffsetDateTime.now(ZoneOffset.UTC); final SearchHistoryEntry newEntry = new SearchHistoryEntry(currentTime, serviceId, search); return Maybe.fromCallable(() -> database.runInTransaction(() -> { diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java index c4307fcde..a093d93e1 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalItemHolder.java @@ -9,7 +9,7 @@ import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.local.LocalItemBuilder; import org.schabi.newpipe.local.history.HistoryRecordManager; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; /* * Created by Christian Schabesberger on 12.02.17. @@ -41,7 +41,7 @@ public abstract class LocalItemHolder extends RecyclerView.ViewHolder { } public abstract void updateFromItem(LocalItem item, HistoryRecordManager historyRecordManager, - DateFormat dateFormat); + DateTimeFormatter dateTimeFormatter); public void updateState(final LocalItem localItem, final HistoryRecordManager historyRecordManager) { } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java index 458b3c30e..5560df3e0 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistItemHolder.java @@ -10,7 +10,7 @@ import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; public class LocalPlaylistItemHolder extends PlaylistItemHolder { public LocalPlaylistItemHolder(final LocalItemBuilder infoItemBuilder, final ViewGroup parent) { @@ -25,7 +25,7 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder { @Override public void updateFromItem(final LocalItem localItem, final HistoryRecordManager historyRecordManager, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { if (!(localItem instanceof PlaylistMetadataEntry)) { return; } @@ -39,6 +39,6 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder { itemBuilder.displayImage(item.thumbnailUrl, itemThumbnailView, ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS); - super.updateFromItem(localItem, historyRecordManager, dateFormat); + super.updateFromItem(localItem, historyRecordManager, dateTimeFormatter); } } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java index 33722e380..f7cf69708 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalPlaylistStreamItemHolder.java @@ -20,7 +20,7 @@ import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.views.AnimatedProgressBar; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @@ -52,7 +52,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder { @Override public void updateFromItem(final LocalItem localItem, final HistoryRecordManager historyRecordManager, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { if (!(localItem instanceof PlaylistStreamEntry)) { return; } diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java index 8eaef807a..f473b0277 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/LocalStatisticStreamItemHolder.java @@ -20,7 +20,7 @@ import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.views.AnimatedProgressBar; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @@ -71,10 +71,10 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { } private String getStreamInfoDetailLine(final StreamStatisticsEntry entry, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { final String watchCount = Localization .shortViewCount(itemBuilder.getContext(), entry.getWatchCount()); - final String uploadDate = dateFormat.format(entry.getLatestAccessDate()); + final String uploadDate = dateTimeFormatter.format(entry.getLatestAccessDate()); final String serviceName = NewPipe.getNameOfService(entry.getStreamEntity().getServiceId()); return Localization.concatenateStrings(watchCount, uploadDate, serviceName); } @@ -82,7 +82,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { @Override public void updateFromItem(final LocalItem localItem, final HistoryRecordManager historyRecordManager, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { if (!(localItem instanceof StreamStatisticsEntry)) { return; } @@ -116,7 +116,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder { } if (itemAdditionalDetails != null) { - itemAdditionalDetails.setText(getStreamInfoDetailLine(item, dateFormat)); + itemAdditionalDetails.setText(getStreamInfoDetailLine(item, dateTimeFormatter)); } // Default thumbnail is shown on error, while loading and if the url is empty diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java index 11e3deb67..e8c53161e 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/PlaylistItemHolder.java @@ -9,7 +9,7 @@ import org.schabi.newpipe.database.LocalItem; import org.schabi.newpipe.local.LocalItemBuilder; import org.schabi.newpipe.local.history.HistoryRecordManager; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; public abstract class PlaylistItemHolder extends LocalItemHolder { public final ImageView itemThumbnailView; @@ -34,7 +34,7 @@ public abstract class PlaylistItemHolder extends LocalItemHolder { @Override public void updateFromItem(final LocalItem localItem, final HistoryRecordManager historyRecordManager, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { itemView.setOnClickListener(view -> { if (itemBuilder.getOnItemSelectedListener() != null) { itemBuilder.getOnItemSelectedListener().selected(localItem); diff --git a/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java b/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java index a47d61d2f..a39e3cecb 100644 --- a/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/local/holder/RemotePlaylistItemHolder.java @@ -11,7 +11,7 @@ import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; -import java.text.DateFormat; +import java.time.format.DateTimeFormatter; public class RemotePlaylistItemHolder extends PlaylistItemHolder { public RemotePlaylistItemHolder(final LocalItemBuilder infoItemBuilder, @@ -27,7 +27,7 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder { @Override public void updateFromItem(final LocalItem localItem, final HistoryRecordManager historyRecordManager, - final DateFormat dateFormat) { + final DateTimeFormatter dateTimeFormatter) { if (!(localItem instanceof PlaylistRemoteEntity)) { return; } @@ -48,6 +48,6 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder { itemBuilder.displayImage(item.getThumbnailUrl(), itemThumbnailView, ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS); - super.updateFromItem(localItem, historyRecordManager, dateFormat); + super.updateFromItem(localItem, historyRecordManager, dateTimeFormatter); } } diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index 96ba2808d..9cebfa863 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -23,11 +23,13 @@ import org.schabi.newpipe.extractor.localization.ContentCountry; import java.math.BigDecimal; import java.math.RoundingMode; -import java.text.DateFormat; import java.text.NumberFormat; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.Arrays; import java.util.Calendar; -import java.util.Date; import java.util.List; import java.util.Locale; @@ -139,13 +141,16 @@ public final class Localization { return nf.format(number); } - public static String formatDate(final Date date, final Context context) { - return DateFormat.getDateInstance(DateFormat.MEDIUM, getAppLocale(context)).format(date); + public static String formatDate(final OffsetDateTime offsetDateTime, final Context context) { + return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM) + .withLocale(getAppLocale(context)).format(offsetDateTime + .atZoneSameInstant(ZoneId.systemDefault())); } @SuppressLint("StringFormatInvalid") - public static String localizeUploadDate(final Context context, final Date date) { - return context.getString(R.string.upload_date_text, formatDate(date, context)); + public static String localizeUploadDate(final Context context, + final OffsetDateTime offsetDateTime) { + return context.getString(R.string.upload_date_text, formatDate(offsetDateTime, context)); } public static String localizeViewCount(final Context context, final long viewCount) { diff --git a/checkstyle-suppressions.xml b/checkstyle-suppressions.xml index 43ee99958..73bdbf4ca 100644 --- a/checkstyle-suppressions.xml +++ b/checkstyle-suppressions.xml @@ -5,7 +5,7 @@ + lines="221,293"/>