From 07e4a33cbd28286fbc82f45441e4427973d5c91a Mon Sep 17 00:00:00 2001 From: Amine Date: Sat, 5 Jan 2019 21:43:09 +0100 Subject: [PATCH] Closes #266. --- .../bou/readerforselfoss/HomeActivity.kt | 9 +++- .../apps/amine/bou/readerforselfoss/MyApp.kt | 6 ++- .../adapters/ItemCardAdapter.kt | 6 ++- .../adapters/ItemListAdapter.kt | 6 ++- .../readerforselfoss/adapters/ItemsAdapter.kt | 2 + .../adapters/SourcesListAdapter.kt | 5 ++- .../fragments/ArticleFragment.kt | 7 ++- .../utils/glide/GlideUtils.kt | 44 ++++++++++++++----- 8 files changed, 62 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index 8aa29db..87fa3bf 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -152,6 +152,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private lateinit var db: AppDatabase + private lateinit var config: Config + data class DrawerData(val tags: List?, val sources: List?) override fun onStart() { @@ -161,6 +163,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@HomeActivity) + config = Config(this@HomeActivity) super.onCreate(savedInstanceState) @@ -1144,7 +1147,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { fullHeightCards, appColors, debugReadingItems, - userIdentifier + userIdentifier, + config ) { updateItems(it) } @@ -1160,7 +1164,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { articleViewer, debugReadingItems, userIdentifier, - appColors + appColors, + config ) { updateItems(it) } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt index 15ae0c8..85b945f 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt @@ -10,6 +10,7 @@ import android.preference.PreferenceManager import androidx.multidex.MultiDexApplication import android.widget.ImageView import apps.amine.bou.readerforselfoss.utils.Config +import apps.amine.bou.readerforselfoss.utils.glide.loadMaybeBasicAuth import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions import com.ftinc.scoop.Scoop @@ -42,10 +43,11 @@ import java.util.UUID.randomUUID ReportField.USER_APP_START_DATE, ReportField.USER_COMMENT, ReportField.USER_CRASH_DATE, ReportField.USER_EMAIL, ReportField.CUSTOM_DATA], buildConfigClass = BuildConfig::class) class MyApp : MultiDexApplication() { + private lateinit var config: Config override fun onCreate() { super.onCreate() - + config = Config(baseContext) initAmplify() val prefs = getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) @@ -105,7 +107,7 @@ class MyApp : MultiDexApplication() { tag: String? ) { Glide.with(imageView?.context) - .load(uri) + .loadMaybeBasicAuth(config, uri.toString()) .apply(RequestOptions.fitCenterTransform().placeholder(placeholder)) .into(imageView) } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index 0f24582..da3322a 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -17,6 +17,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.themes.AppColors +import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.LinkOnTouchListener import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper @@ -51,6 +52,7 @@ class ItemCardAdapter( override val appColors: AppColors, override val debugReadingItems: Boolean, override val userIdentifier: String, + override val config: Config, override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val c: Context = app.baseContext @@ -86,7 +88,7 @@ class ItemCardAdapter( holder.mView.itemImage.setImageDrawable(null) } else { holder.mView.itemImage.visibility = View.VISIBLE - c.bitmapCenterCrop(itm.getThumbnail(c), holder.mView.itemImage) + c.bitmapCenterCrop(config, itm.getThumbnail(c), holder.mView.itemImage) } if (itm.getIcon(c).isEmpty()) { @@ -99,7 +101,7 @@ class ItemCardAdapter( .build(itm.sourcetitle.toTextDrawableString(c), color) holder.mView.sourceImage.setImageDrawable(drawable) } else { - c.circularBitmapDrawable(itm.getIcon(c), holder.mView.sourceImage) + c.circularBitmapDrawable(config, itm.getIcon(c), holder.mView.sourceImage) } holder.mView.favButton.isLiked = itm.starred diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index 0a99e78..fffc8a6 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -20,6 +20,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors +import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.LinkOnTouchListener import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper @@ -52,6 +53,7 @@ class ItemListAdapter( override val debugReadingItems: Boolean, override val userIdentifier: String, override val appColors: AppColors, + override val config: Config, override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val generator: ColorGenerator = ColorGenerator.MATERIAL @@ -108,10 +110,10 @@ class ItemListAdapter( holder.mView.itemImage.setImageDrawable(drawable) } else { - c.circularBitmapDrawable(itm.getIcon(c), holder.mView.itemImage) + c.circularBitmapDrawable(config, itm.getIcon(c), holder.mView.itemImage) } } else { - c.bitmapCenterCrop(itm.getThumbnail(c), holder.mView.itemImage) + c.bitmapCenterCrop(config, itm.getThumbnail(c), holder.mView.itemImage) } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt index c322eb2..ca96fae 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt @@ -13,6 +13,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.persistence.entities.ActionEntity import apps.amine.bou.readerforselfoss.themes.AppColors +import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible import apps.amine.bou.readerforselfoss.utils.persistence.toEntity @@ -31,6 +32,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte abstract val userIdentifier: String abstract val app: Activity abstract val appColors: AppColors + abstract val config: Config abstract val updateItems: (ArrayList) -> Unit fun updateAllItems(newItems: ArrayList) { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt index 18493ad..635bdd5 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt @@ -12,6 +12,7 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.Source import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse +import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.glide.circularBitmapDrawable import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible import apps.amine.bou.readerforselfoss.utils.toTextDrawableString @@ -29,6 +30,7 @@ class SourcesListAdapter( ) : RecyclerView.Adapter() { private val c: Context = app.baseContext private val generator: ColorGenerator = ColorGenerator.MATERIAL + private lateinit var config: Config override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val v = LayoutInflater.from(c).inflate( @@ -41,6 +43,7 @@ class SourcesListAdapter( override fun onBindViewHolder(holder: ViewHolder, position: Int) { val itm = items[position] + config = Config(c) if (itm.getIcon(c).isEmpty()) { val color = generator.getColor(itm.title) @@ -52,7 +55,7 @@ class SourcesListAdapter( .build(itm.title.toTextDrawableString(c), color) holder.mView.itemImage.setImageDrawable(drawable) } else { - c.circularBitmapDrawable(itm.getIcon(c), holder.mView.itemImage) + c.circularBitmapDrawable(config, itm.getIcon(c), holder.mView.itemImage) } holder.mView.sourceTitle.text = itm.title diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index d90ccf3..ea99349 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -34,6 +34,7 @@ import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.glide.loadMaybeBasicAuth import apps.amine.bou.readerforselfoss.utils.isEmptyOrNullOrNullString import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible @@ -68,6 +69,7 @@ class ArticleFragment : Fragment() { private lateinit var appColors: AppColors private lateinit var db: AppDatabase private lateinit var textAlignment: String + private lateinit var config: Config override fun onStop() { super.onStop() @@ -78,6 +80,7 @@ class ArticleFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(activity!!) + config = Config(activity!!) super.onCreate(savedInstanceState) @@ -214,7 +217,7 @@ class ArticleFragment : Fragment() { Glide .with(context!!) .asBitmap() - .load(contentImage) + .loadMaybeBasicAuth(config, contentImage) .apply(RequestOptions.fitCenterTransform()) .into(rootView!!.imageView) } else { @@ -309,7 +312,7 @@ class ArticleFragment : Fragment() { Glide .with(context!!) .asBitmap() - .load(response.body()!!.lead_image_url) + .loadMaybeBasicAuth(config, response.body()!!.lead_image_url.orEmpty()) .apply(RequestOptions.fitCenterTransform()) .into(rootView!!.imageView) } catch (e: Exception) { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt index d107bea..ea29a70 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt @@ -2,30 +2,30 @@ package apps.amine.bou.readerforselfoss.utils.glide import android.content.Context import android.graphics.Bitmap +import android.graphics.drawable.Drawable +import android.util.Base64 import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory import android.widget.ImageView +import apps.amine.bou.readerforselfoss.utils.Config import com.bumptech.glide.Glide +import com.bumptech.glide.RequestBuilder +import com.bumptech.glide.RequestManager +import com.bumptech.glide.load.model.GlideUrl +import com.bumptech.glide.load.model.LazyHeaders import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.BitmapImageViewTarget -fun Context.bitmapCenterCrop(url: String, iv: ImageView) = +fun Context.bitmapCenterCrop(config: Config, url: String, iv: ImageView) = Glide.with(this) .asBitmap() - .load(url) + .loadMaybeBasicAuth(config, url) .apply(RequestOptions.centerCropTransform()) .into(iv) -fun Context.bitmapFitCenter(url: String, iv: ImageView) = +fun Context.circularBitmapDrawable(config: Config, url: String, iv: ImageView) = Glide.with(this) .asBitmap() - .load(url) - .apply(RequestOptions.fitCenterTransform()) - .into(iv) - -fun Context.circularBitmapDrawable(url: String, iv: ImageView) = - Glide.with(this) - .asBitmap() - .load(url) + .loadMaybeBasicAuth(config, url) .apply(RequestOptions.centerCropTransform()) .into(object : BitmapImageViewTarget(iv) { override fun setResource(resource: Bitmap?) { @@ -36,4 +36,24 @@ fun Context.circularBitmapDrawable(url: String, iv: ImageView) = circularBitmapDrawable.isCircular = true iv.setImageDrawable(circularBitmapDrawable) } - }) \ No newline at end of file + }) + +fun RequestBuilder.loadMaybeBasicAuth(config: Config, url: String): RequestBuilder { + val builder: LazyHeaders.Builder = LazyHeaders.Builder() + if (config.httpUserLogin.isNotEmpty() || config.httpUserPassword.isNotEmpty()) { + val basicAuth = "Basic " + Base64.encodeToString("${config.httpUserLogin}:${config.httpUserPassword}".toByteArray(), Base64.NO_WRAP) + builder.addHeader("Authorization", basicAuth) + } + val glideUrl = GlideUrl(url, builder.build()) + return this.load(glideUrl) +} + +fun RequestManager.loadMaybeBasicAuth(config: Config, url: String): RequestBuilder { + val builder: LazyHeaders.Builder = LazyHeaders.Builder() + if (config.httpUserLogin.isNotEmpty() || config.httpUserPassword.isNotEmpty()) { + val basicAuth = "Basic " + Base64.encodeToString("${config.httpUserLogin}:${config.httpUserPassword}".toByteArray(), Base64.NO_WRAP) + builder.addHeader("Authorization", basicAuth) + } + val glideUrl = GlideUrl(url, builder.build()) + return this.load(glideUrl) +} \ No newline at end of file