From 08290dec2518ccae73acc13b6bf8f59869fd5ce5 Mon Sep 17 00:00:00 2001 From: jsaura Date: Mon, 16 Nov 2020 15:03:41 -0500 Subject: [PATCH] Added TrustManagers to Picasso downloader. --- .idea/compiler.xml | 6 +++ .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- CHANGELOG.md | 7 ++- app/build.gradle | 4 +- .../jesusd0897/pictish/sample/MainActivity.kt | 2 +- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- pictish/build.gradle | 4 +- .../jesusd0897/pictish/ImageViewDownloader.kt | 33 ++++++------ .../jesusd0897/pictish/util/CustomOkHttp3.kt | 50 +++++++++++++++++++ 11 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 pictish/src/main/java/com/jesusd0897/pictish/util/CustomOkHttp3.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index b81ea7f..f135547 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -17,6 +17,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 7bfef59..d5d35ec 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/CHANGELOG.md b/CHANGELOG.md index 82348b1..d0de093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Change Log ========== -Version 0.1.0 *(2020-09-08)* +Version 0.1.1 *(2020-11-16)* ---------------------------- -Initial release. +Added custom Picasso downloader to access files without security certificates. +Version 0.1.0 *(2020-09-08)* +---------------------------- +Initial release. \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 21fb778..991f245 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,9 +66,9 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.core:core-ktx:1.3.1' + implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' - implementation 'androidx.constraintlayout:constraintlayout:2.0.1' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.google.android.material:material:1.2.1' implementation "com.squareup.picasso:picasso:2.71828" //Picasso implementation 'com.nihaskalam.android:progress-button:1.0.1' diff --git a/app/src/main/java/com/jesusd0897/pictish/sample/MainActivity.kt b/app/src/main/java/com/jesusd0897/pictish/sample/MainActivity.kt index cab9123..f81dcc0 100644 --- a/app/src/main/java/com/jesusd0897/pictish/sample/MainActivity.kt +++ b/app/src/main/java/com/jesusd0897/pictish/sample/MainActivity.kt @@ -30,7 +30,7 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) setSupportActionBar(toolbar) - val id = Random.nextInt(1, 500)//101 + val id = Random.nextInt(1, 500) val imageDownloadView: ImageViewDownloader = findViewById(R.id.image_view_downloader) imageDownloadView.preLoad( diff --git a/build.gradle b/build.gradle index dbeb95e..2136b12 100644 --- a/build.gradle +++ b/build.gradle @@ -16,14 +16,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.4.0" + ext.kotlin_version = '1.4.10' repositories { google() jcenter() maven { url 'https://jitpack.io' } } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:4.1.1' 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 3b3bb8d..5a67f8b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -14,9 +14,9 @@ # limitations under the License. # -#Mon Sep 07 09:40:06 CDT 2020 +#Mon Nov 16 14:41:23 CST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip diff --git a/pictish/build.gradle b/pictish/build.gradle index 22efd3c..d5645bb 100644 --- a/pictish/build.gradle +++ b/pictish/build.gradle @@ -27,7 +27,7 @@ android { minSdkVersion 17 targetSdkVersion 30 versionCode 1 - versionName "0.1.0" + versionName "0.1.1" multiDexEnabled = true vectorDrawables.useSupportLibrary = true consumerProguardFiles "consumer-rules.pro" @@ -53,7 +53,7 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.core:core-ktx:1.3.1' + implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation "com.squareup.picasso:picasso:2.71828" //Picasso diff --git a/pictish/src/main/java/com/jesusd0897/pictish/ImageViewDownloader.kt b/pictish/src/main/java/com/jesusd0897/pictish/ImageViewDownloader.kt index d3ae01c..56b2756 100644 --- a/pictish/src/main/java/com/jesusd0897/pictish/ImageViewDownloader.kt +++ b/pictish/src/main/java/com/jesusd0897/pictish/ImageViewDownloader.kt @@ -32,12 +32,10 @@ import androidx.annotation.RequiresApi import com.google.android.material.imageview.ShapeableImageView import com.google.android.material.shape.CornerFamily import com.jesusd0897.pictish.util.fastblur +import com.jesusd0897.pictish.util.provideOkHttpClient import com.jesusd0897.pictish.util.setSafeOnClickListener import com.nihaskalam.progressbuttonlibrary.CircularProgressButton -import com.squareup.picasso.Callback -import com.squareup.picasso.NetworkPolicy -import com.squareup.picasso.Picasso -import com.squareup.picasso.Transformation +import com.squareup.picasso.* class ImageViewDownloader : FrameLayout { @@ -47,15 +45,6 @@ class ImageViewDownloader : FrameLayout { var thumbUrl: String? = null var fullUrl: String? = null - /*@ColorRes - var progressColor = R.color.color_primary - - @ColorRes - var indicatorColor = R.color.color_secondary - - @ColorRes - var indicatorBackgroundColor = R.color.color_secondary_light*/ - @DimenRes var imageBorderRadius = R.dimen.dim_card_corner_radius @@ -193,9 +182,12 @@ class ImageViewDownloader : FrameLayout { transformation: Transformation? = null, callback: Callback? = null ) { - val picasso = Picasso.get() - .load(url) - .placeholder(placeholder) + val picasso = + Picasso.Builder(imageView.context) + .downloader(OkHttp3Downloader(provideOkHttpClient())) + .build() + .load(url) + .placeholder(placeholder) if (useCache) picasso.networkPolicy(NetworkPolicy.OFFLINE) if (transformation != null) picasso.transform(transformation) picasso.into(imageView, callback) @@ -209,9 +201,12 @@ class ImageViewDownloader : FrameLayout { transformation: Transformation? = null, callback: Callback? = null ) { - val picasso = Picasso.get() - .load(url) - .placeholder(placeholder) + val picasso = + Picasso.Builder(imageView.context) + .downloader(OkHttp3Downloader(provideOkHttpClient())) + .build() + .load(url) + .placeholder(placeholder) if (useCache) picasso.networkPolicy(NetworkPolicy.OFFLINE) if (transformation != null) picasso.transform(blurTransformation) picasso.into(imageView, callback) diff --git a/pictish/src/main/java/com/jesusd0897/pictish/util/CustomOkHttp3.kt b/pictish/src/main/java/com/jesusd0897/pictish/util/CustomOkHttp3.kt new file mode 100644 index 0000000..95c2857 --- /dev/null +++ b/pictish/src/main/java/com/jesusd0897/pictish/util/CustomOkHttp3.kt @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020 jesusd0897. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jesusd0897.pictish.util + +import okhttp3.OkHttpClient +import java.security.SecureRandom +import java.security.cert.X509Certificate +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager + +private fun provideTrustManagers(): Array = + arrayOf( + object : X509TrustManager { + override fun checkClientTrusted(chain: Array, authType: String) = Unit + override fun checkServerTrusted(chain: Array, authType: String) = Unit + override fun getAcceptedIssuers(): Array = arrayOf() + } + ) + +fun provideOkHttpClient(): OkHttpClient { + + // Create a trust manager that does not validate certificate chains + val trustManagers = provideTrustManagers() + + // Install the all-trusting trust manager + val sslContext = SSLContext.getInstance("SSL") + sslContext.init(null, trustManagers, SecureRandom()) + + // Create an ssl socket factory with our all-trusting manager + val sslSocketFactory = sslContext.socketFactory + val builder = OkHttpClient.Builder() + .hostnameVerifier { _, _ -> true } + .sslSocketFactory(sslSocketFactory, trustManagers[0] as X509TrustManager) + return builder.build() +} \ No newline at end of file