Skip to content

Commit b5dab2e

Browse files
committed
update
1 parent 98c3700 commit b5dab2e

File tree

10 files changed

+293
-204
lines changed

10 files changed

+293
-204
lines changed

fqnews/app/build.gradle

+13-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,21 @@ android {
8080
resValue 'string', 'native_adUnitId', "ca-app-pub-2194043486084479/8959992113"
8181
resValue 'string', 'interstitial_adUnitId', "ca-app-pub-2194043486084479/3248199117"
8282
}
83+
fqnews2 {
84+
dimension "site"
85+
applicationId "jww.feed.fqnews2"
86+
resValue 'string', 'applicationId', applicationId
87+
resValue 'string', 'admob_appid', "ca-app-pub-2194043486084479~6090891748"
88+
resValue 'string', 'banner_adUnitId', "ca-app-pub-2194043486084479/2969070238"
89+
resValue 'string', 'native_adUnitId', "ca-app-pub-2194043486084479/2813991710"
90+
resValue 'string', 'interstitial_adUnitId', "ca-app-pub-2194043486084479/5707748368"
91+
}
8392
}
8493
sourceSets {
8594
fqnews.setRoot('src/fqnews')
8695
hwnews.setRoot('src/hwnews')
8796
aunews.setRoot('src/aunews')
97+
fqnews2.setRoot('src/fqnews2')
8898
}
8999
compileOptions {
90100
sourceCompatibility javaVersion
@@ -140,7 +150,7 @@ android {
140150
main.java.srcDirs += 'src/main/kotlin'
141151
}
142152

143-
if(getCurrentProductFlavor()=='fqnews'){
153+
if(getCurrentProductFlavor()=='fqnews' || getCurrentProductFlavor()=='fqnews2'){
144154
splits {
145155
abi {
146156
enable true
@@ -158,6 +168,7 @@ kapt {
158168

159169
dependencies {
160170
fqnewsImplementation project(':core')
171+
fqnews2Implementation project(':core')
161172

162173
hwnewsImplementation project(':plugin')
163174
hwnewsApi "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
@@ -206,6 +217,7 @@ dependencies {
206217
implementation 'androidx.core:core-ktx:1.3.0'
207218
implementation 'androidx.paging:paging-runtime:2.1.2'
208219
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
220+
implementation 'androidx.preference:preference-ktx:1.1.1'
209221
implementation 'org.jsoup:jsoup:1.12.1'
210222
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
211223
implementation 'com.github.Tunous:SwipeActionView:1.3.0'

fqnews/app/src/aunews/java/net/frju/flym/service/FetcherService.kt

+93-88
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import android.net.ConnectivityManager
2929
import android.net.Uri
3030
import android.os.Build
3131
import android.os.Handler
32-
import android.text.Html
3332
import android.util.Log
3433
import androidx.core.app.NotificationCompat
34+
import androidx.core.text.HtmlCompat
3535
import com.rometools.rome.io.SyndFeedInput
3636
import com.rometools.rome.io.XmlReader
3737
import net.dankito.readability4j.extended.Readability4JExtended
@@ -61,6 +61,7 @@ import java.net.CookiePolicy
6161
import java.util.concurrent.ExecutorCompletionService
6262
import java.util.concurrent.Executors
6363
import java.util.concurrent.TimeUnit
64+
import kotlin.math.max
6465

6566

6667
class FetcherService : IntentService(FetcherService::class.java.simpleName) {
@@ -73,7 +74,7 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
7374

7475
private val HTTP_CLIENT: OkHttpClient = OkHttpClient.Builder()
7576
.connectTimeout(10, TimeUnit.SECONDS)
76-
.readTimeout(10, TimeUnit.SECONDS)
77+
.readTimeout(20, TimeUnit.SECONDS)
7778
.cookieJar(JavaNetCookieJar(COOKIE_MANAGER))
7879
.build()
7980

@@ -116,14 +117,14 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
116117
return
117118
}
118119

119-
when {
120-
ACTION_MOBILIZE_FEEDS == action -> {
121-
mobilizeAllEntries()
122-
downloadAllImages()
123-
}
124-
ACTION_DOWNLOAD_IMAGES == action -> downloadAllImages()
125-
else -> { // == Constants.ACTION_REFRESH_FEEDS
126-
context.putPrefBoolean(PrefConstants.IS_REFRESHING, true)
120+
when (action) {
121+
ACTION_MOBILIZE_FEEDS -> {
122+
mobilizeAllEntries()
123+
downloadAllImages()
124+
}
125+
ACTION_DOWNLOAD_IMAGES -> downloadAllImages()
126+
else -> { // == Constants.ACTION_REFRESH_FEEDS
127+
context.putPrefBoolean(PrefConstants.IS_REFRESHING, true)
127128

128129
val readEntriesKeepTime = context.getPrefString(PrefConstants.KEEP_TIME, "4")!!.toLong() * 86400000L
129130
val readEntriesKeepDate = if (readEntriesKeepTime > 0) System.currentTimeMillis() - readEntriesKeepTime else 0
@@ -135,21 +136,21 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
135136
deleteOldEntries(unreadEntriesKeepDate, 0)
136137
COOKIE_MANAGER.cookieStore.removeAll() // Cookies are important for some sites, but we clean them each times
137138

138-
// We need to use the more recent date in order to be sure to not see old entries again
139-
val acceptMinDate = Math.max(readEntriesKeepDate, unreadEntriesKeepDate)
140-
141-
var newCount = 0
142-
if (feedId == 0L) {
143-
newCount = refreshFeeds(acceptMinDate)
144-
} else {
145-
App.db.feedDao().findById(feedId)?.let {
146-
try {
147-
newCount = refreshFeed(it, acceptMinDate)
148-
} catch (e: Exception) {
149-
error("Can't fetch feed ${it.link}", e)
150-
}
151-
}
152-
}
139+
// We need to use the more recent date in order to be sure to not see old entries again
140+
val acceptMinDate = max(readEntriesKeepDate, unreadEntriesKeepDate)
141+
142+
var newCount = 0
143+
if (feedId == 0L || App.db.feedDao().findById(feedId)?.isGroup == true) {
144+
newCount = refreshFeeds(feedId, acceptMinDate)
145+
} else {
146+
App.db.feedDao().findById(feedId)?.let {
147+
try {
148+
newCount = refreshFeed(it, acceptMinDate)
149+
} catch (e: Exception) {
150+
error("Can't fetch feed ${it.link}", e)
151+
}
152+
}
153+
}
153154

154155
showRefreshNotification(newCount)
155156
mobilizeAllEntries()
@@ -209,7 +210,7 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
209210
.setTicker(text)
210211
.setWhen(System.currentTimeMillis())
211212
.setAutoCancel(true)
212-
.setContentTitle(context.getString(R.string.app_name) +" "+ context.getString(R.string.flym_feeds))
213+
.setContentTitle(context.getString(R.string.flym_feeds))
213214
.setContentText(text)
214215

215216
context.notificationManager.notify(0, notifBuilder.build())
@@ -223,15 +224,14 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
223224
fun shouldDownloadPictures(): Boolean {
224225
val fetchPictureMode = context.getPrefString(PrefConstants.PRELOAD_IMAGE_MODE, PrefConstants.PRELOAD_IMAGE_MODE__WIFI_ONLY)
225226

226-
if (context.getPrefBoolean(PrefConstants.DISPLAY_IMAGES, true)) {
227-
if (PrefConstants.PRELOAD_IMAGE_MODE__ALWAYS == fetchPictureMode) {
228-
return true
229-
} else if (PrefConstants.PRELOAD_IMAGE_MODE__WIFI_ONLY == fetchPictureMode) {
230-
if (App.context.connectivityManager.activeNetworkInfo?.type == ConnectivityManager.TYPE_WIFI) {
231-
return true
232-
}
233-
}
234-
}
227+
if (context.getPrefBoolean(PrefConstants.DISPLAY_IMAGES, true)) {
228+
if (PrefConstants.PRELOAD_IMAGE_MODE__ALWAYS == fetchPictureMode) {
229+
return true
230+
} else if (PrefConstants.PRELOAD_IMAGE_MODE__WIFI_ONLY == fetchPictureMode
231+
&& context.connectivityManager.activeNetworkInfo?.type == ConnectivityManager.TYPE_WIFI) {
232+
return true
233+
}
234+
}
235235

236236
return false
237237
}
@@ -293,19 +293,19 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
293293
Readability4JExtended(link, Jsoup.parse(input, null, link)).parse().articleContent?.html()?.let {
294294
val mobilizedHtml = HtmlUtils.improveHtmlContent(it, getBaseUrl(link))
295295

296-
@Suppress("DEPRECATION")
297-
if (entry.description == null || Html.fromHtml(mobilizedHtml).length > Html.fromHtml(entry.description).length) { // If the retrieved text is smaller than the original one, then we certainly failed...
298-
if (downloadPictures) {
299-
val imagesList = HtmlUtils.getImageURLs(mobilizedHtml)
300-
if (imagesList.isNotEmpty()) {
301-
if (entry.imageLink == null) {
302-
entry.imageLink = HtmlUtils.getMainImageURL(imagesList)
303-
}
304-
imgUrlsToDownload[entry.id] = imagesList
305-
}
306-
} else if (entry.imageLink == null) {
307-
entry.imageLink = HtmlUtils.getMainImageURL(mobilizedHtml)
308-
}
296+
val entryDescription = entry.description
297+
if (entryDescription == null || HtmlCompat.fromHtml(mobilizedHtml, HtmlCompat.FROM_HTML_MODE_LEGACY).length > HtmlCompat.fromHtml(entryDescription, HtmlCompat.FROM_HTML_MODE_LEGACY).length) { // If the retrieved text is smaller than the original one, then we certainly failed...
298+
if (downloadPictures) {
299+
val imagesList = HtmlUtils.getImageURLs(mobilizedHtml)
300+
if (imagesList.isNotEmpty()) {
301+
if (entry.imageLink == null) {
302+
entry.imageLink = HtmlUtils.getMainImageURL(imagesList)
303+
}
304+
imgUrlsToDownload[entry.id] = imagesList
305+
}
306+
} else if (entry.imageLink == null) {
307+
entry.imageLink = HtmlUtils.getMainImageURL(mobilizedHtml)
308+
}
309309

310310
success = true
311311

@@ -359,7 +359,7 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
359359
}
360360
}
361361

362-
private fun refreshFeeds(acceptMinDate: Long): Int {
362+
private fun refreshFeeds(feedId: Long, acceptMinDate: Long): Int {
363363

364364
val executor = Executors.newFixedThreadPool(THREAD_NUMBER) { r ->
365365
Thread(r).apply {
@@ -368,29 +368,34 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
368368
}
369369
val completionService = ExecutorCompletionService<Int>(executor)
370370

371-
var globalResult = 0
372-
373-
val feeds = App.db.feedDao().allNonGroupFeeds
374-
for (feed in feeds) {
375-
completionService.submit {
376-
var result = 0
377-
try {
378-
result = refreshFeed(feed, acceptMinDate)
379-
} catch (e: Exception) {
380-
error("Can't fetch feedWithCount ${feed.link}", e)
381-
}
371+
var globalResult = 0
372+
val feeds: List<Feed>
373+
if (feedId == 0L) {
374+
feeds = App.db.feedDao().allNonGroupFeeds
375+
} else {
376+
feeds = App.db.feedDao().allFeedsInGroup(feedId)
377+
}
378+
379+
for (feed in feeds) {
380+
completionService.submit {
381+
var result = 0
382+
try {
383+
result = refreshFeed(feed, acceptMinDate)
384+
} catch (e: Exception) {
385+
error("Can't fetch feedWithCount ${feed.link}", e)
386+
}
382387

383388
result
384389
}
385390
}
386391

387-
for (i in 0 until feeds.size) {
388-
try {
389-
val f = completionService.take()
390-
globalResult += f.get()
391-
} catch (ignored: Exception) {
392-
}
393-
}
392+
for (i in feeds.indices) {
393+
try {
394+
val f = completionService.take()
395+
globalResult += f.get()
396+
} catch (ignored: Exception) {
397+
}
398+
}
394399

395400
executor.shutdownNow() // To purge observeAll threads
396401

@@ -408,7 +413,7 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
408413
createCall(feed.link).execute().use { response ->
409414
val input = SyndFeedInput()
410415
val romeFeed = input.build(XmlReader(response.body!!.byteStream()))
411-
entries.addAll(romeFeed.entries.asSequence().filter { it.publishedDate?.time ?: Long.MAX_VALUE > acceptMinDate }.map { it.toDbFormat(feed) })
416+
entries.addAll(romeFeed.entries.asSequence().filter { it.publishedDate?.time ?: Long.MAX_VALUE > acceptMinDate }.map { it.toDbFormat(context, feed) })
412417
feed.update(romeFeed)
413418
}
414419
} catch (t: Throwable) {
@@ -435,15 +440,16 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
435440
if (filterKeywordString.isNotBlank()) {
436441
val keywordLists = filterKeywordString.split(',').map { it.trim() }
437442

438-
if (keywordLists.isNotEmpty()) {
439-
entries.removeAll { entry ->
440-
keywordLists.any {
441-
entry.title?.contains(it, true) == true ||
442-
entry.description?.contains(it, true) == true
443-
}
444-
}
445-
}
446-
}
443+
if (keywordLists.isNotEmpty()) {
444+
entries.removeAll { entry ->
445+
keywordLists.any {
446+
entry.title?.contains(it, true) == true ||
447+
entry.description?.contains(it, true) == true ||
448+
entry.author?.contains(it, true) == true
449+
}
450+
}
451+
}
452+
}
447453

448454
val feedBaseUrl = getBaseUrl(feed.link)
449455
var foundExisting = false
@@ -511,9 +517,8 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
511517
if (!File(tempImgPath).exists() && !File(finalImgPath).exists()) {
512518
IMAGE_FOLDER_FILE.mkdir() // create images dir
513519

514-
// Compute the real URL (without "&eacute;", ...)
515-
@Suppress("DEPRECATION")
516-
val realUrl = Html.fromHtml(imgUrl).toString()
520+
// Compute the real URL (without "&eacute;", ...)
521+
val realUrl = HtmlCompat.fromHtml(imgUrl, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()
517522

518523
try {
519524
createCall(realUrl).execute().use { response ->
@@ -541,14 +546,14 @@ class FetcherService : IntentService(FetcherService::class.java.simpleName) {
541546
// We need to exclude favorite entries images to this cleanup
542547
val favoriteIds = App.db.entryDao().favoriteIds
543548

544-
IMAGE_FOLDER_FILE.listFiles().forEach { file ->
545-
// If old file and not part of a favorite entry
546-
if (file.lastModified() < keepDateBorderTime && !favoriteIds.any { file.name.startsWith(it + ID_SEPARATOR) }) {
547-
file.delete()
548-
}
549-
}
550-
}
551-
}
549+
IMAGE_FOLDER_FILE.listFiles()?.forEach { file ->
550+
// If old file and not part of a favorite entry
551+
if (file.lastModified() < keepDateBorderTime && !favoriteIds.any { file.name.startsWith(it + ID_SEPARATOR) }) {
552+
file.delete()
553+
}
554+
}
555+
}
556+
}
552557

553558
private fun getBaseUrl(link: String): String {
554559
var baseUrl = link

0 commit comments

Comments
 (0)