diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 62f2d378..763ce074 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + + + { @@ -93,47 +90,17 @@ public abstract class ARepository { return database.feedDao().getFeedCount(accountId); } - protected void setFaviconUtils(List feeds) { - Observable.create(emitter -> { - for (Feed feed : feeds) { - setFaviconUtils(feed); - emitter.onNext(feed); - } - }).subscribeOn(Schedulers.io()) - .observeOn(Schedulers.io()) - .doOnNext(feed1 -> database.feedDao().updateColors(feed1.getId(), - feed1.getTextColor(), feed1.getBackgroundColor())) - .subscribe(); + protected void setFeedColors(Feed feed) { + FeedColorsKt.setFeedColors(feed); + database.feedDao().updateColors(feed.getId(), + feed.getTextColor(), feed.getBackgroundColor()); } - protected void setFaviconUtils(Feed feed) throws IOException { - String favUrl; + protected void setFeedsColors(List feeds) { + Intent intent = new Intent(application, FeedsColorsIntentService.class); + intent.putParcelableArrayListExtra(FeedsColorsIntentService.FEEDS, new ArrayList<>(feeds)); - if (feed.getIconUrl() != null) - favUrl = feed.getIconUrl(); - else - favUrl = HtmlParser.getFaviconLink(feed.getSiteUrl()); - - if (favUrl != null && Patterns.WEB_URL.matcher(favUrl).matches()) { - feed.setIconUrl(favUrl); - setFeedColors(favUrl, feed); - } - } - - protected void setFeedColors(String favUrl, Feed feed) { - Bitmap favicon = Utils.getImageFromUrl(favUrl); - - if (favicon != null) { - Palette palette = Palette.from(favicon).generate(); - - if (palette.getDominantSwatch() != null) { - feed.setTextColor(palette.getDominantSwatch().getRgb()); - } - - if (palette.getMutedSwatch() != null) { - feed.setBackgroundColor(palette.getMutedSwatch().getRgb()); - } - } + application.startService(intent); } public static ARepository repositoryFactory(Account account, AccountType accountType, Application application) throws Exception { diff --git a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java index 8617b966..73adec09 100644 --- a/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/FreshRSSRepository.java @@ -183,10 +183,8 @@ public class FreshRSSRepository extends ARepository { List insertedFeedsIds = database.feedDao().feedsUpsert(feeds, account); - List insertedFeeds = new ArrayList<>(); if (!insertedFeedsIds.isEmpty()) { - insertedFeeds.addAll(database.feedDao().selectFromIdList(insertedFeedsIds)); - setFaviconUtils(insertedFeeds); + setFeedsColors(database.feedDao().selectFromIdList(insertedFeedsIds)); } } diff --git a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java index 9b26277e..3c16339e 100644 --- a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java @@ -195,7 +195,7 @@ public class LocalFeedRepository extends ARepository { if (database.feedDao().feedExists(dbFeed.getUrl(), account.getId())) return null; // feed already inserted - setFaviconUtils(dbFeed); + setFeedColors(dbFeed); dbFeed.setAccountId(account.getId()); // we need empty headers to query the feed just after, without any 304 result diff --git a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java index 4e89a581..987e53e7 100644 --- a/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/NextNewsRepository.java @@ -274,7 +274,7 @@ public class NextNewsRepository extends ARepository { List insertedFeeds = new ArrayList<>(); if (!insertedFeedsIds.isEmpty()) { insertedFeeds.addAll(database.feedDao().selectFromIdList(insertedFeedsIds)); - setFaviconUtils(insertedFeeds); + setFeedsColors(insertedFeeds); } return insertedFeeds; diff --git a/app/src/main/java/com/readrops/app/utils/ReadropsApp.java b/app/src/main/java/com/readrops/app/utils/ReadropsApp.java index b6e93c3b..689d41d1 100644 --- a/app/src/main/java/com/readrops/app/utils/ReadropsApp.java +++ b/app/src/main/java/com/readrops/app/utils/ReadropsApp.java @@ -1,18 +1,41 @@ package com.readrops.app.utils; import android.app.Application; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.os.Build; import com.facebook.stetho.Stetho; +import com.readrops.app.BuildConfig; +import com.readrops.app.R; import io.reactivex.plugins.RxJavaPlugins; public class ReadropsApp extends Application { + public static final String FEEDS_COLORS_CHANNEL_ID = "feedsColorsChannel"; + @Override public void onCreate() { super.onCreate(); RxJavaPlugins.setErrorHandler(e -> { }); - Stetho.initializeWithDefaults(this); + + if (BuildConfig.DEBUG) { + Stetho.initializeWithDefaults(this); + } + + createNotificationChannel(); + } + + private void createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel feedsColorsChannel = new NotificationChannel(FEEDS_COLORS_CHANNEL_ID, + getString(R.string.feeds_colors), NotificationManager.IMPORTANCE_DEFAULT); + feedsColorsChannel.setDescription(getString(R.string.get_feeds_colors)); + + NotificationManager manager = getSystemService(NotificationManager.class); + manager.createNotificationChannel(feedsColorsChannel); + } } } diff --git a/app/src/main/java/com/readrops/app/utils/feedscolors/FeedColors.kt b/app/src/main/java/com/readrops/app/utils/feedscolors/FeedColors.kt new file mode 100644 index 00000000..c9bd4861 --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/feedscolors/FeedColors.kt @@ -0,0 +1,33 @@ +package com.readrops.app.utils.feedscolors + +import androidx.palette.graphics.Palette +import com.readrops.app.database.entities.Feed +import com.readrops.app.utils.HtmlParser +import com.readrops.app.utils.Utils + +fun setFeedColors(feed: Feed) { + getFaviconLink(feed) + + if (feed.iconUrl != null) { + val bitmap = Utils.getImageFromUrl(feed.iconUrl) + val palette = Palette.from(bitmap).generate() + + val dominantSwatch = palette.dominantSwatch + if (dominantSwatch != null) + feed.textColor = dominantSwatch.rgb + + val mutedSwatch = palette.mutedSwatch + if (mutedSwatch != null) + feed.backgroundColor = mutedSwatch.rgb + } +} + +fun getFaviconLink(feed: Feed) { + feed.iconUrl = if (feed.iconUrl != null) + feed.iconUrl + else + HtmlParser.getFaviconLink(feed.siteUrl) +} + + + diff --git a/app/src/main/java/com/readrops/app/utils/feedscolors/FeedsColorsIntentService.kt b/app/src/main/java/com/readrops/app/utils/feedscolors/FeedsColorsIntentService.kt new file mode 100644 index 00000000..a6a73bf7 --- /dev/null +++ b/app/src/main/java/com/readrops/app/utils/feedscolors/FeedsColorsIntentService.kt @@ -0,0 +1,48 @@ +package com.readrops.app.utils.feedscolors + +import android.app.IntentService +import android.content.Intent +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import androidx.core.content.ContextCompat +import com.readrops.app.R +import com.readrops.app.database.Database +import com.readrops.app.database.entities.Feed +import com.readrops.app.utils.ReadropsApp + +class FeedsColorsIntentService : IntentService("FeedsColorsIntentService") { + + override fun onHandleIntent(intent: Intent?) { + val feeds: List = intent!!.getParcelableArrayListExtra(FEEDS) + val database = Database.getInstance(this) + + val notificationBuilder = NotificationCompat.Builder(this, ReadropsApp.FEEDS_COLORS_CHANNEL_ID) + .setContentTitle(getString(R.string.get_feeds_colors)) + .setProgress(feeds.size, 0, false) + .setSmallIcon(R.drawable.ic_readrops) + .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) + .setOnlyAlertOnce(true) + + startForeground(NOTIFICATION_ID, notificationBuilder.build()) + val notificationManager = NotificationManagerCompat.from(this) + + var feedsNb = 0 + feeds.forEach { + notificationBuilder.setContentText(it.name) + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) + setFeedColors(it) + + database.feedDao().updateColors(it.id, it.textColor, it.backgroundColor) + notificationBuilder.setProgress(feeds.size, ++feedsNb, false) + notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()) + } + + stopForeground(true) + } + + companion object { + private const val NOTIFICATION_ID = 1 + const val FEEDS = "feeds" + } + +} \ No newline at end of file diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 523dbf7a..17199013 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -84,5 +84,7 @@ Aucun item Aucun flux trouvé Erreur pour le flux %1$s + Récupération des couleurs des flux + Couleurs des flux \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 448d75b7..3640c122 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -92,4 +92,6 @@ No item No feed found Error for feed %1$s + Get feeds colors + Feeds Colors