2018-03-27 19:47:00 +02:00
|
|
|
/* Copyright 2018 charlag
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.di
|
|
|
|
|
2018-04-22 17:20:01 +02:00
|
|
|
import android.content.Context
|
2021-01-31 19:35:39 +01:00
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.os.Build
|
2022-05-30 20:03:40 +02:00
|
|
|
import at.connyduck.calladapter.networkresult.NetworkResultCallAdapterFactory
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.google.gson.Gson
|
2022-05-30 18:15:17 +02:00
|
|
|
import com.google.gson.GsonBuilder
|
2018-04-22 17:20:01 +02:00
|
|
|
import com.keylesspalace.tusky.BuildConfig
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
2022-06-16 18:51:35 +02:00
|
|
|
import com.keylesspalace.tusky.json.Rfc3339DateJsonAdapter
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2022-05-01 17:16:22 +02:00
|
|
|
import com.keylesspalace.tusky.network.MediaUploadApi
|
2019-01-17 16:29:33 +01:00
|
|
|
import com.keylesspalace.tusky.network.NotestockApi
|
2021-01-31 19:35:39 +01:00
|
|
|
import com.keylesspalace.tusky.util.getNonNullString
|
2018-03-27 19:47:00 +02:00
|
|
|
import dagger.Module
|
|
|
|
import dagger.Provides
|
2019-06-23 09:57:29 +02:00
|
|
|
import net.accelf.yuito.HttpToastInterceptor
|
2021-01-31 19:35:39 +01:00
|
|
|
import okhttp3.Cache
|
|
|
|
import okhttp3.OkHttp
|
2018-03-27 19:47:00 +02:00
|
|
|
import okhttp3.OkHttpClient
|
2018-04-22 17:20:01 +02:00
|
|
|
import okhttp3.logging.HttpLoggingInterceptor
|
2018-03-27 19:47:00 +02:00
|
|
|
import retrofit2.Retrofit
|
2021-05-16 19:53:27 +02:00
|
|
|
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
|
2018-03-27 19:47:00 +02:00
|
|
|
import retrofit2.converter.gson.GsonConverterFactory
|
2020-12-13 16:31:12 +01:00
|
|
|
import retrofit2.create
|
2021-01-31 19:35:39 +01:00
|
|
|
import java.net.InetSocketAddress
|
|
|
|
import java.net.Proxy
|
2022-05-30 18:15:17 +02:00
|
|
|
import java.util.Date
|
2021-01-31 19:35:39 +01:00
|
|
|
import java.util.concurrent.TimeUnit
|
2018-03-27 19:47:00 +02:00
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 3/24/18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Module
|
|
|
|
class NetworkModule {
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
@Provides
|
|
|
|
@Singleton
|
2022-05-30 18:15:17 +02:00
|
|
|
fun providesGson(): Gson = GsonBuilder()
|
2022-06-16 18:51:35 +02:00
|
|
|
.registerTypeAdapter(Date::class.java, Rfc3339DateJsonAdapter())
|
2022-05-30 18:15:17 +02:00
|
|
|
.create()
|
2018-03-27 19:47:00 +02:00
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
2020-02-25 19:49:15 +01:00
|
|
|
fun providesHttpClient(
|
2021-06-28 21:13:24 +02:00
|
|
|
accountManager: AccountManager,
|
|
|
|
context: Context,
|
|
|
|
preferences: SharedPreferences
|
2020-02-25 19:49:15 +01:00
|
|
|
): OkHttpClient {
|
2021-01-31 19:35:39 +01:00
|
|
|
val httpProxyEnabled = preferences.getBoolean("httpProxyEnabled", false)
|
|
|
|
val httpServer = preferences.getNonNullString("httpProxyServer", "")
|
|
|
|
val httpPort = preferences.getNonNullString("httpProxyPort", "-1").toIntOrNull() ?: -1
|
|
|
|
val cacheSize = 25 * 1024 * 1024L // 25 MiB
|
|
|
|
val builder = OkHttpClient.Builder()
|
|
|
|
.addInterceptor { chain ->
|
|
|
|
/**
|
|
|
|
* Add a custom User-Agent that contains Tusky, Android and OkHttp Version to all requests
|
|
|
|
* Example:
|
|
|
|
* User-Agent: Tusky/1.1.2 Android/5.0.2 OkHttp/4.9.0
|
|
|
|
* */
|
|
|
|
val requestWithUserAgent = chain.request().newBuilder()
|
|
|
|
.header(
|
|
|
|
"User-Agent",
|
|
|
|
"Tusky/${BuildConfig.VERSION_NAME} Android/${Build.VERSION.RELEASE} OkHttp/${OkHttp.VERSION}"
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
chain.proceed(requestWithUserAgent)
|
|
|
|
}
|
|
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.cache(Cache(context.cacheDir, cacheSize))
|
|
|
|
|
|
|
|
if (httpProxyEnabled && httpServer.isNotEmpty() && httpPort > 0 && httpPort < 65535) {
|
|
|
|
val address = InetSocketAddress.createUnresolved(httpServer, httpPort)
|
|
|
|
builder.proxy(Proxy(Proxy.Type.HTTP, address))
|
|
|
|
}
|
|
|
|
return builder
|
2021-06-28 21:13:24 +02:00
|
|
|
.apply {
|
|
|
|
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC })
|
2021-07-03 16:59:57 +02:00
|
|
|
addInterceptor(HttpToastInterceptor(context))
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|
|
|
|
.build()
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
2020-02-25 19:49:15 +01:00
|
|
|
fun providesRetrofit(
|
2021-06-28 21:13:24 +02:00
|
|
|
httpClient: OkHttpClient,
|
|
|
|
gson: Gson
|
2020-02-25 19:49:15 +01:00
|
|
|
): Retrofit {
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
return Retrofit.Builder().baseUrl("https://" + MastodonApi.PLACEHOLDER_DOMAIN)
|
2021-06-28 21:13:24 +02:00
|
|
|
.client(httpClient)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
|
|
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
|
2022-05-30 20:03:40 +02:00
|
|
|
.addCallAdapterFactory(NetworkResultCallAdapterFactory.create())
|
2021-06-28 21:13:24 +02:00
|
|
|
.build()
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
2020-12-13 16:31:12 +01:00
|
|
|
fun providesApi(retrofit: Retrofit): MastodonApi = retrofit.create()
|
2019-01-17 16:29:33 +01:00
|
|
|
|
2022-05-01 17:16:22 +02:00
|
|
|
@Provides
|
|
|
|
@Singleton
|
|
|
|
fun providesMediaUploadApi(retrofit: Retrofit, okHttpClient: OkHttpClient): MediaUploadApi {
|
|
|
|
val longTimeOutOkHttpClient = okHttpClient.newBuilder()
|
|
|
|
.readTimeout(100, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(100, TimeUnit.SECONDS)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
return retrofit.newBuilder()
|
|
|
|
.client(longTimeOutOkHttpClient)
|
|
|
|
.build()
|
|
|
|
.create()
|
|
|
|
}
|
2022-05-18 17:15:17 +02:00
|
|
|
|
2019-01-17 16:29:33 +01:00
|
|
|
@Provides
|
|
|
|
@Singleton
|
2021-07-03 16:59:57 +02:00
|
|
|
fun providesNotestockApi(
|
|
|
|
okHttpClient: OkHttpClient,
|
|
|
|
gson: Gson
|
|
|
|
): NotestockApi {
|
2019-01-17 16:29:33 +01:00
|
|
|
val retrofit = Retrofit.Builder().baseUrl("https://notestock.osa-p.net")
|
2021-07-03 16:59:57 +02:00
|
|
|
.client(okHttpClient)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
|
|
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
|
|
|
|
.build()
|
2019-01-17 16:29:33 +01:00
|
|
|
return retrofit.create(NotestockApi::class.java)
|
|
|
|
}
|
2020-12-16 15:47:26 +01:00
|
|
|
}
|