Skip to content

Commit

Permalink
Added TrustManagers to Picasso downloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsaura committed Nov 16, 2020
1 parent 6afea1d commit 08290de
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions pictish/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
33 changes: 14 additions & 19 deletions pictish/src/main/java/com/jesusd0897/pictish/ImageViewDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions pictish/src/main/java/com/jesusd0897/pictish/util/CustomOkHttp3.kt
Original file line number Diff line number Diff line change
@@ -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<TrustManager> =
arrayOf(
object : X509TrustManager {
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) = Unit
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) = Unit
override fun getAcceptedIssuers(): Array<X509Certificate> = 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()
}

0 comments on commit 08290de

Please sign in to comment.