diff --git a/README.md b/README.md index 82d2c53..dbfaae9 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,13 @@ -[![Release](https://jitpack.io/v/stanwood/framework-network-android.svg?style=flat-square)](https://jitpack.io/#stanwood/framework-network-android) -[![Build Status](https://app.bitrise.io/app/983e6342cc5e0e24/status.svg?token=QtXUf2lbVhJrANROaTkluQ&branch=develop)](https://app.bitrise.io/app/983e6342cc5e0e24) +[![Release](https://jitpack.io/v/whisp-internet/framework-network-android.svg?style=flat-square)](https://jitpack.io/#whisp-internet/framework-network-android) [![API](https://img.shields.io/badge/API-16%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=16) -# stanwood Network Utilities (Android) +# whisp Internet Network Utilities (Android) A set of hopefully useful classes for common networking use cases. ## Import -The stanwood Network Utilities are hosted on JitPack. Therefore you can simply -import them by adding +The whisp Internet Network Utilities (based on the [stanwood Network Utilities](https://github.com/stanwood/framework-network-android/)) are hosted on JitPack. Therefore you can simply import them by adding ```groovy allprojects { @@ -26,9 +24,9 @@ Then add this to you app's `build.gradle`: ```groovy dependencies { - implementation 'com.github.stanwood.framework-network-android:arch:' // aar version available as well - implementation 'com.github.stanwood.framework-network-android:cache:' // aar version available as well - implementation 'com.github.stanwood.framework-network-android:core:' // aar version available as well, automatically included when pulling in one of the other libraries + implementation 'com.github.whisp-internet.framework-network-android:auth:' // aar version available as well + implementation 'com.github.whisp-internet.framework-network-android:cache:' // aar version available as well + implementation 'com.github.whisp-internet.framework-network-android:core:' // aar version available as well, automatically included when pulling in one of the other libraries } ``` @@ -258,4 +256,4 @@ Make sure to check the _Run Git hooks_ checkbox if you commit via Android Studio Last thing to do is setting line length to 140 (Preferences -> Editor -> Code Style) - unfortunately `ktlintApplyToIdea` can't do that for you. -The CI will check formatting as well and won't allow non style conform commits to be merged. \ No newline at end of file +The CI will check formatting as well and won't allow non style conform commits to be merged. diff --git a/auth/build.gradle b/auth/build.gradle index f879ab6..1413032 100644 --- a/auth/build.gradle +++ b/auth/build.gradle @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 stanwood GmbH + * Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,15 +24,15 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -52,17 +52,17 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) api project(":core") - implementation 'com.android.support:support-annotations:28.0.0' - testImplementation 'junit:junit:4.12' + implementation 'androidx.annotation:annotation:1.1.0' + testImplementation 'junit:junit:4.13.1' - compileOnly 'com.squareup.retrofit2:retrofit:2.5.0' + compileOnly 'com.squareup.retrofit2:retrofit:2.9.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - def retrofit_version = '2.5.0' + def retrofit_version = '2.9.0' testImplementation "com.squareup.retrofit2:retrofit:$retrofit_version" testImplementation "com.squareup.retrofit2:converter-gson:$retrofit_version" - testImplementation 'com.google.code.gson:gson:2.8.5' + testImplementation 'com.google.code.gson:gson:2.8.6' testImplementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version" diff --git a/auth/src/main/java/io/stanwood/framework/network/auth/Authenticator.kt b/auth/src/main/java/io/stanwood/framework/network/auth/Authenticator.kt index b6f0ed4..6e0f425 100644 --- a/auth/src/main/java/io/stanwood/framework/network/auth/Authenticator.kt +++ b/auth/src/main/java/io/stanwood/framework/network/auth/Authenticator.kt @@ -38,7 +38,7 @@ open class Authenticator( private val tokenReaderWriter: TokenReaderWriter, private val onAuthenticationFailedListener: OnAuthenticationFailedListener? ) : okhttp3.Authenticator { - override fun authenticate(route: Route?, response: Response): Request? = response.request().let { request -> + override fun authenticate(route: Route?, response: Response): Request? = response.request.let { request -> tokenReaderWriter.read(request)?.let { oldToken -> synchronized(authenticationProvider.lock) { if (request.header(AuthHeaderKeys.RETRY_WITH_REFRESH_HEADER_KEY) != null) { @@ -98,7 +98,7 @@ open class Authenticator( all tokens */ response.newBuilder().request( - response.request().newBuilder() + response.request.newBuilder() .header(AuthHeaderKeys.RETRY_WITH_REFRESH_HEADER_KEY, "true") .build() ).build() diff --git a/build.gradle b/build.gradle index 8400d0f..9adfc3b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 stanwood GmbH + * Copyright (c) 2018 stanwood GmbH, 2020 whisp Internet GmbH * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,8 +23,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.31' - ext.okhttp_version = '3.13.1' + ext.kotlin_version = '1.4.20' + ext.okhttp_version = '4.9.0' repositories { google() jcenter() @@ -33,9 +33,9 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:3.4.0-rc03' + classpath 'com.android.tools.build:gradle:4.2.0-alpha15' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath "org.jlleitschuh.gradle:ktlint-gradle:7.3.0" + classpath "org.jlleitschuh.gradle:ktlint-gradle:9.3.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/cache/build.gradle b/cache/build.gradle index c51800b..a2f9a3d 100644 --- a/cache/build.gradle +++ b/cache/build.gradle @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 stanwood GmbH + * Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,15 +24,15 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -52,6 +52,6 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) api project(":core") - implementation 'com.android.support:support-annotations:28.0.0' + implementation 'androidx.annotation:annotation:1.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" } diff --git a/cache/src/main/java/io/stanwood/framework/network/cache/CacheInterceptor.java b/cache/src/main/java/io/stanwood/framework/network/cache/CacheInterceptor.java index f986eba..c77c824 100644 --- a/cache/src/main/java/io/stanwood/framework/network/cache/CacheInterceptor.java +++ b/cache/src/main/java/io/stanwood/framework/network/cache/CacheInterceptor.java @@ -23,8 +23,10 @@ package io.stanwood.framework.network.cache; import android.content.Context; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -78,11 +80,12 @@ public CacheInterceptor( this.errorCallback = errorCallback; } + @NotNull @Override public Response intercept(@NonNull Chain chain) throws IOException { Request request = chain.request(); String offlineCacheHeader = request.header(CacheHeaderKeys.APPLY_OFFLINE_CACHE); - if (Boolean.valueOf(offlineCacheHeader) && !connectionState.isConnected()) { + if (Boolean.parseBoolean(offlineCacheHeader) && !connectionState.isConnected()) { Request.Builder builder = request.newBuilder(); if (queryAuthParameterKey != null) { builder.url(request @@ -100,7 +103,7 @@ public Response intercept(@NonNull Chain chain) throws IOException { } String responseCacheHeader = request.header(CacheHeaderKeys.REFRESH); - if (Boolean.valueOf(responseCacheHeader)) { + if (Boolean.parseBoolean(responseCacheHeader)) { try { return chain.proceed(request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build()); } catch (IOException e) { diff --git a/cache/src/main/java/io/stanwood/framework/network/cache/CacheNetworkInterceptor.java b/cache/src/main/java/io/stanwood/framework/network/cache/CacheNetworkInterceptor.java index 982b8ab..307619e 100644 --- a/cache/src/main/java/io/stanwood/framework/network/cache/CacheNetworkInterceptor.java +++ b/cache/src/main/java/io/stanwood/framework/network/cache/CacheNetworkInterceptor.java @@ -22,8 +22,10 @@ package io.stanwood.framework.network.cache; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -53,13 +55,22 @@ * this library. In the future this might be added to the provided interceptors by means of * accepting a list of parameter keys instead of just the one of the auth parameter. * - *

Configuration of the interceptor classes is mainly done by means of request headers. Check - * out {@link CacheHeaderKeys} for more details on what the different headers do. + *

Configuration of the interceptor classes is mainly done via request headers. Check + * out {@link CacheHeaderKeys} for more details on what the different headers do. Add them to your + * requests like so: + *

+ * {@code @Headers({ CacheHeaderKeys.APPLY_RESPONSE_CACHE + ": true" })}
+ * 
* - *

Add it as a network interceptor to your OkHttpClient Builder like so: + *

Add instances of this interceptor as a network interceptor to your OkHttpClient Builder like so: *

  * {@code okHttpClientBuilder.addNetworkInterceptor(new CacheNetworkInterceptor("auth", 3600))}
  * 
+ * + * Don't forget to add a cache to the builder as well: + *
+ * {@code okHttpClientBuilder.cache(new Cache(new File(app.cacheDir, "okhttp-storyly-rest-api-cache"), 10 * 1024))}
+ * 
*/ public class CacheNetworkInterceptor implements Interceptor { @@ -80,13 +91,14 @@ public CacheNetworkInterceptor(@Nullable String queryAuthParameterKey, long cach this.cacheForSeconds = cacheForSeconds; } + @NotNull @Override public Response intercept(@NonNull Chain chain) throws IOException { Request request = chain.request(); String responseCacheHeader = request.header(CacheHeaderKeys.APPLY_RESPONSE_CACHE); String offlineCacheHeader = request.header(CacheHeaderKeys.APPLY_OFFLINE_CACHE); - boolean isGeneralCache = Boolean.valueOf(responseCacheHeader); - boolean isOfflineCache = Boolean.valueOf(offlineCacheHeader); + boolean isGeneralCache = Boolean.parseBoolean(responseCacheHeader); + boolean isOfflineCache = Boolean.parseBoolean(offlineCacheHeader); if (isGeneralCache || isOfflineCache) { Response originalResponse = chain.proceed(request); Response.Builder builder = originalResponse.newBuilder(); diff --git a/core/build.gradle b/core/build.gradle index 1bce78e..e6d48b5 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 stanwood GmbH + * Copyright (c) 2019 stanwood GmbH, 2020 whisp Internet GmbH * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,15 +24,15 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -51,13 +51,13 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:support-annotations:28.0.0' - testImplementation 'junit:junit:4.12' + implementation 'androidx.annotation:annotation:1.1.0' + testImplementation 'junit:junit:4.13.1' api "com.squareup.okhttp3:okhttp:$okhttp_version" - compileOnly 'com.squareup.retrofit2:retrofit:2.5.0' + compileOnly 'com.squareup.retrofit2:retrofit:2.9.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.0" + api "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1" api 'com.nytimes.android:store3:3.1.1' api 'com.nytimes.android:filesystem3:3.1.1' implementation 'io.reactivex.rxjava2:rxjava:2.2.3' // same version as defined within store diff --git a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationParserFactory.kt b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationParserFactory.kt index 9c6f937..32d0492 100644 --- a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationParserFactory.kt +++ b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationParserFactory.kt @@ -22,14 +22,15 @@ package io.stanwood.framework.network.core.store import com.nytimes.android.external.store3.base.Parser -import kotlinx.io.Reader import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonConfiguration import okio.BufferedSource +import java.io.Reader object SerializationParserFactory { - private val stable = Json(JsonConfiguration.Stable) + private val stable = Json { + allowStructuredMapKeys = true + } fun createReaderParser( deserializer: KSerializer, json: Json = stable diff --git a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationReaderParser.kt b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationReaderParser.kt index 7465153..5a3a7ea 100644 --- a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationReaderParser.kt +++ b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationReaderParser.kt @@ -23,15 +23,15 @@ package io.stanwood.framework.network.core.store import com.nytimes.android.external.store3.base.Parser import com.nytimes.android.external.store3.util.ParserException -import kotlinx.io.Reader import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json +import java.io.Reader class SerializationReaderParser( private val deserializer: KSerializer, - val json: Json + private val json: Json ) : Parser { @Throws(ParserException::class) override fun apply(reader: Reader): Parsed = - json.parse(deserializer, reader.use { it.readText() }) + json.decodeFromString(deserializer, reader.use { it.readText() }) } \ No newline at end of file diff --git a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationSourceParser.kt b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationSourceParser.kt index d7e72e1..e14f521 100644 --- a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationSourceParser.kt +++ b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationSourceParser.kt @@ -28,10 +28,10 @@ import kotlinx.serialization.json.Json import okio.BufferedSource class SerializationSourceParser( - private val deserializer: KSerializer, - val json: Json + private val deserializer: KSerializer, + private val json: Json ) : Parser { @Throws(ParserException::class) override fun apply(bufferedSource: BufferedSource): Parsed = - json.parse(deserializer, bufferedSource.use { it.readUtf8() }) + json.decodeFromString(deserializer, bufferedSource.use { it.readUtf8() }) } \ No newline at end of file diff --git a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationStringParser.kt b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationStringParser.kt index 0db4408..269625e 100644 --- a/core/src/main/java/io/stanwood/framework/network/core/store/SerializationStringParser.kt +++ b/core/src/main/java/io/stanwood/framework/network/core/store/SerializationStringParser.kt @@ -32,5 +32,5 @@ class SerializationStringParser( ) : Parser { @Throws(ParserException::class) override fun apply(string: String): Parsed = - json.parse(deserializer, string) + json.decodeFromString(deserializer, string) } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 4549be6..ec5f641 100644 --- a/gradle.properties +++ b/gradle.properties @@ -31,6 +31,8 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. +android.enableJetifier=true +android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5a82d00..944c284 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip +distributionUrl=https://services.gradle.org/distributions/gradle-6.7-bin.zip diff --git a/sample/build.gradle b/sample/build.gradle index 56d3182..9734b01 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -24,18 +24,18 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { - compileSdkVersion 27 + compileSdkVersion 29 defaultConfig { applicationId "com.github.ubuntudroid.sample" minSdkVersion 21 - targetSdkVersion 27 + targetSdkVersion 29 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -57,10 +57,10 @@ dependencies { implementation project(':auth') implementation project(':cache') - implementation 'com.android.support:appcompat-v7:27.1.1' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + testImplementation 'junit:junit:4.13.1' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" } diff --git a/sample/src/androidTest/java/com/github/ubuntudroid/sample/ExampleInstrumentedTest.java b/sample/src/androidTest/java/com/github/ubuntudroid/sample/ExampleInstrumentedTest.java index 3667be8..fe0153e 100644 --- a/sample/src/androidTest/java/com/github/ubuntudroid/sample/ExampleInstrumentedTest.java +++ b/sample/src/androidTest/java/com/github/ubuntudroid/sample/ExampleInstrumentedTest.java @@ -1,8 +1,8 @@ package com.github.ubuntudroid.sample; import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -19,7 +19,7 @@ public class ExampleInstrumentedTest { @Test public void useAppContext() { // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); assertEquals("com.github.ubuntudroid.sample", appContext.getPackageName()); } diff --git a/sample/src/main/java/com/github/ubuntudroid/sample/MainActivity.java b/sample/src/main/java/com/github/ubuntudroid/sample/MainActivity.java index a0dc67e..6a270ff 100644 --- a/sample/src/main/java/com/github/ubuntudroid/sample/MainActivity.java +++ b/sample/src/main/java/com/github/ubuntudroid/sample/MainActivity.java @@ -22,7 +22,7 @@ package com.github.ubuntudroid.sample; -import android.support.v7.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; /** diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 803ad8e..90d0bdf 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -21,7 +21,7 @@ ~ SOFTWARE. --> - - \ No newline at end of file + \ No newline at end of file