From d0000d66b2f35a774e2db0526c4f5c9c9a1e95e6 Mon Sep 17 00:00:00 2001 From: Amine Date: Sat, 2 Sep 2017 10:02:35 +0200 Subject: [PATCH] Fixes #70. Updated glide for images loading. --- CHANGELOG.md | 6 +++ app/build.gradle | 5 ++- app/proguard-rules.pro | 6 ++- app/src/main/AndroidManifest.xml | 3 ++ .../apps/amine/bou/readerforselfoss/MyApp.kt | 9 ++++- .../bou/readerforselfoss/ReaderActivity.kt | 5 ++- .../adapters/ItemCardAdapter.kt | 5 ++- .../adapters/ItemListAdapter.kt | 2 + .../adapters/SourcesListAdapter.kt | 2 +- .../api/selfoss/SelfossApi.kt | 40 +------------------ .../bou/readerforselfoss/utils/GlideUtils.kt | 24 ----------- .../bou/readerforselfoss/utils/HttpUtils.kt | 39 ++++++++++++++++++ .../utils/glide/GlideUtils.kt | 29 ++++++++++++++ .../utils/glide/SelfSignedGlideModule.kt | 32 +++++++++++++++ build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 16 files changed, 140 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/HttpUtils.kt create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/SelfSignedGlideModule.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f45d7..8548899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +**1.5.2.13** + +- Updated glide. + +- Loading images from self signed certificate now working. + **1.5.2.12** - Self signed certificates are now working for loading data. Image are not loading yet. diff --git a/app/build.gradle b/app/build.gradle index c52dee9..a0c74a0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,7 +35,7 @@ repositories { android { compileSdkVersion 26 - buildToolsVersion "26.0.0" + buildToolsVersion "26.0.1" defaultConfig { applicationId "apps.amine.bou.readerforselfoss" minSdkVersion 16 @@ -137,7 +137,8 @@ dependencies { compile 'org.sufficientlysecure:html-textview:3.3' // glide - compile 'com.github.bumptech.glide:glide:3.7.0' + compile 'com.github.bumptech.glide:glide:4.1.0' + compile 'com.github.bumptech.glide:okhttp3-integration:4.0.0' // Asking politely users to rate the app compile 'com.github.stkent:amplify:1.5.0' diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 11f314f..6de36ee 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -56,4 +56,8 @@ #Bottom bar lib --dontwarn com.roughike.bottombar.** \ No newline at end of file +-dontwarn com.roughike.bottombar.** + + +# self signed glidemodule +-keep public class * implements com.bumptech.glide.module.GlideModule \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b5cf3cf..9ecc30d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,6 +64,9 @@ + \ No newline at end of file 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 df83ce2..8b882c3 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt @@ -9,6 +9,7 @@ import android.widget.ImageView import apps.amine.bou.readerforselfoss.utils.Config import com.anupcowkur.reservoir.Reservoir import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions import com.crashlytics.android.Crashlytics import com.ftinc.scoop.Scoop import com.github.stkent.amplify.tracking.Amplify @@ -63,11 +64,15 @@ class MyApp : MultiDexApplication() { private fun initDrawerImageLoader() { DrawerImageLoader.init(object : AbstractDrawerImageLoader() { override fun set(imageView: ImageView?, uri: Uri?, placeholder: Drawable?, tag: String?) { - Glide.with(imageView?.context).load(uri).placeholder(placeholder).into(imageView) + Glide.with(imageView?.context) + .load(uri) + .apply(RequestOptions.fitCenterTransform() + .placeholder(placeholder)) + .into(imageView) } override fun cancel(imageView: ImageView?) { - Glide.clear(imageView) + Glide.with(imageView?.context).clear(imageView) } override fun placeholder(ctx: Context?, tag: String?): Drawable { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index c7e0cf4..59b62d4 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -24,6 +24,7 @@ import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper import apps.amine.bou.readerforselfoss.utils.openItemUrl import apps.amine.bou.readerforselfoss.utils.shareLink +import com.bumptech.glide.request.RequestOptions import com.ftinc.scoop.Scoop @@ -75,9 +76,9 @@ class ReaderActivity : DragDismissActivity() { if (response.body()!!.lead_image_url != null && !response.body()!!.lead_image_url.isEmpty()) Glide .with(baseContext) - .load(response.body()!!.lead_image_url) .asBitmap() - .fitCenter() + .load(response.body()!!.lead_image_url) + .apply(RequestOptions.fitCenterTransform()) .into(image) shareBtn.setOnClickListener { 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 68c4f25..44fc888 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 @@ -32,6 +32,9 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.themes.AppColors import apps.amine.bou.readerforselfoss.utils.* import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.glide.bitmapCenterCrop +import apps.amine.bou.readerforselfoss.utils.glide.bitmapFitCenter +import apps.amine.bou.readerforselfoss.utils.glide.circularBitmapDrawable import com.crashlytics.android.Crashlytics import kotlin.collections.ArrayList @@ -63,7 +66,7 @@ class ItemCardAdapter(private val app: Activity, holder.sourceTitleAndDate.text = itm.sourceAndDateText() if (itm.getThumbnail(c).isEmpty()) { - Glide.clear(holder.itemImage) + Glide.with(c).clear(holder.itemImage) holder.itemImage.setImageDrawable(null) } else { if (fullHeightCards) { 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 fff5628..f60eadc 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 @@ -29,6 +29,8 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.utils.* import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.glide.bitmapCenterCrop +import apps.amine.bou.readerforselfoss.utils.glide.circularBitmapDrawable import com.crashlytics.android.Crashlytics import kotlin.collections.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 e3c0efb..cfe6ac6 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 @@ -21,7 +21,7 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.Sources import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse -import apps.amine.bou.readerforselfoss.utils.circularBitmapDrawable +import apps.amine.bou.readerforselfoss.utils.glide.circularBitmapDrawable import apps.amine.bou.readerforselfoss.utils.toTextDrawableString diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index 83752f5..74810f0 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -2,11 +2,6 @@ package apps.amine.bou.readerforselfoss.api.selfoss import android.app.Activity import android.content.Context -import android.content.Intent -import android.support.v7.app.AlertDialog -import android.widget.Toast -import apps.amine.bou.readerforselfoss.LoginActivity -import apps.amine.bou.readerforselfoss.R import java.util.concurrent.ConcurrentHashMap import com.burgstaller.okhttp.AuthenticationCacheInterceptor @@ -23,9 +18,7 @@ import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import apps.amine.bou.readerforselfoss.utils.Config -import java.security.cert.CertificateException -import java.security.cert.X509Certificate -import javax.net.ssl.* +import apps.amine.bou.readerforselfoss.utils.getUnsafeHttpClient // codebeat:disable[ARITY,TOO_MANY_FUNCTIONS] @@ -38,36 +31,7 @@ class SelfossApi(c: Context, callingActivity: Activity, isWithSelfSignedCert: Bo fun OkHttpClient.Builder.maybeWithSelfSigned(isWithSelfSignedCert: Boolean): OkHttpClient.Builder = if (isWithSelfSignedCert) { - try { - // Create a trust manager that does not validate certificate chains - val trustAllCerts = arrayOf(object : X509TrustManager { - override fun getAcceptedIssuers(): Array = - arrayOf() - - @Throws(CertificateException::class) - override fun checkClientTrusted(chain: Array, authType: String) { - } - - @Throws(CertificateException::class) - override fun checkServerTrusted(chain: Array, authType: String) { - } - - }) - - // Install the all-trusting trust manager - val sslContext = SSLContext.getInstance("SSL") - sslContext.init(null, trustAllCerts, java.security.SecureRandom()) - - val sslSocketFactory = sslContext.socketFactory - - OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) - .hostnameVerifier { _, _ -> true } - - } catch (e: Exception) { - throw RuntimeException(e) - } - + getUnsafeHttpClient() } else { this } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt deleted file mode 100644 index 28324af..0000000 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt +++ /dev/null @@ -1,24 +0,0 @@ -package apps.amine.bou.readerforselfoss.utils - -import android.content.Context -import android.graphics.Bitmap -import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory -import android.widget.ImageView -import com.bumptech.glide.Glide -import com.bumptech.glide.request.target.BitmapImageViewTarget - - -fun Context.bitmapCenterCrop(url: String, iv: ImageView) = - Glide.with(this).load(url).asBitmap().centerCrop().into(iv) - -fun Context.bitmapFitCenter(url: String, iv: ImageView) = - Glide.with(this).load(url).asBitmap().fitCenter().into(iv) - -fun Context.circularBitmapDrawable(url: String, iv: ImageView) = - Glide.with(this).load(url).asBitmap().centerCrop().into(object : BitmapImageViewTarget(iv) { - override fun setResource(resource: Bitmap) { - val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(resources, resource) - circularBitmapDrawable.isCircular = true - iv.setImageDrawable(circularBitmapDrawable) - } - }) \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/HttpUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/HttpUtils.kt new file mode 100644 index 0000000..6a23bbe --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/HttpUtils.kt @@ -0,0 +1,39 @@ +package apps.amine.bou.readerforselfoss.utils + +import okhttp3.OkHttpClient +import java.security.cert.CertificateException +import java.security.cert.X509Certificate +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager + +fun getUnsafeHttpClient() = + try { + // Create a trust manager that does not validate certificate chains + val trustAllCerts = arrayOf(object : X509TrustManager { + override fun getAcceptedIssuers(): Array = + arrayOf() + + @Throws(CertificateException::class) + override fun checkClientTrusted(chain: Array, authType: String) { + } + + @Throws(CertificateException::class) + override fun checkServerTrusted(chain: Array, authType: String) { + } + + }) + + // Install the all-trusting trust manager + val sslContext = SSLContext.getInstance("SSL") + sslContext.init(null, trustAllCerts, java.security.SecureRandom()) + + val sslSocketFactory = sslContext.socketFactory + + OkHttpClient.Builder() + .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) + .hostnameVerifier { _, _ -> true } + + } catch (e: Exception) { + throw RuntimeException(e) + } \ No newline at end of file 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 new file mode 100644 index 0000000..95226a4 --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/GlideUtils.kt @@ -0,0 +1,29 @@ +package apps.amine.bou.readerforselfoss.utils.glide + +import android.content.Context +import android.graphics.Bitmap +import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory +import android.widget.ImageView +import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions +import com.bumptech.glide.request.target.BitmapImageViewTarget + + +fun Context.bitmapCenterCrop(url: String, iv: ImageView) = + Glide.with(this).asBitmap().load(url).apply(RequestOptions.centerCropTransform()).into(iv) + +fun Context.bitmapFitCenter(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) + .apply(RequestOptions.centerCropTransform()). + into(object : BitmapImageViewTarget(iv) { + override fun setResource(resource: Bitmap?) { + val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(resources, resource) + circularBitmapDrawable.isCircular = true + iv.setImageDrawable(circularBitmapDrawable) + } + }) \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/SelfSignedGlideModule.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/SelfSignedGlideModule.kt new file mode 100644 index 0000000..137207d --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/glide/SelfSignedGlideModule.kt @@ -0,0 +1,32 @@ +package apps.amine.bou.readerforselfoss.utils.glide + +import android.content.Context +import apps.amine.bou.readerforselfoss.utils.Config +import apps.amine.bou.readerforselfoss.utils.getUnsafeHttpClient +import com.bumptech.glide.Glide +import com.bumptech.glide.GlideBuilder +import com.bumptech.glide.Registry +import com.bumptech.glide.module.GlideModule +import com.bumptech.glide.load.model.GlideUrl +import java.io.InputStream + + +class SelfSignedGlideModule : GlideModule { + + override fun applyOptions(context: Context?, builder: GlideBuilder?) {} + + override fun registerComponents(context: Context?, glide: Glide?, registry: Registry?) { + + if (context != null) { + val pref = context?.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) + if (pref.getBoolean("isSelfSignedCert", false)) { + val client = getUnsafeHttpClient().build() + + registry?.append(GlideUrl::class.java, InputStream::class.java, + com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader.Factory(client)) + } + + } + } + +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 336a788..81a2259 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.1.3-2' + ext.kotlin_version = '1.1.4-3' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0-beta2' + classpath 'com.android.tools.build:gradle:3.0.0-beta4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cfa7b20..e94ac2b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Aug 16 19:45:22 CEST 2017 +#Sat Sep 02 07:47:16 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip