add custom useragent (fixes #39)

This commit is contained in:
Felix 2019-11-09 20:06:41 +01:00
parent 13a916fc0c
commit bfadf6a2c2
6 changed files with 45 additions and 27 deletions

View file

@ -2,6 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
String versionString = new File("versionfile").text.trim()
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
@ -10,7 +12,7 @@ android {
minSdkVersion rootProject.ext.minSDK
targetSdkVersion rootProject.ext.targetSDK
versionCode 1
versionName '2.0.0'
versionName versionString
javaCompileOptions {
annotationProcessorOptions {

View file

@ -2,9 +2,11 @@ package de.nicidienase.chaosflix.common
import android.arch.persistence.db.SupportSQLiteDatabase
import android.arch.persistence.room.Database
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.arch.persistence.room.TypeConverters
import android.arch.persistence.room.migration.Migration
import android.content.Context
import de.nicidienase.chaosflix.common.mediadata.entities.Converters
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceDao
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceGroup
@ -47,7 +49,16 @@ abstract class ChaosflixDatabase : RoomDatabase() {
abstract fun watchlistItemDao(): WatchlistItemDao
abstract fun offlineEventDao(): OfflineEventDao
companion object {
companion object : SingletonHolder<ChaosflixDatabase, Context>({
Room.databaseBuilder(
it.applicationContext,
ChaosflixDatabase::class.java, "mediaccc.de")
.addMigrations(
ChaosflixDatabase.migration_5_6)
.fallbackToDestructiveMigrationFrom(1, 2, 3, 4)
.build()
}) {
val migration_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE `offline_event` (" +

View file

@ -1,17 +0,0 @@
package de.nicidienase.chaosflix.common
import android.arch.persistence.room.Room
import android.content.Context
class DatabaseFactory private constructor() {
companion object : SingletonHolder<ChaosflixDatabase, Context>({
Room.databaseBuilder(
it.applicationContext,
ChaosflixDatabase::class.java, "mediaccc.de")
.addMigrations(
ChaosflixDatabase.migration_5_6)
.fallbackToDestructiveMigrationFrom(1, 2, 3, 4)
.build()
})
}

View file

@ -1,22 +1,27 @@
package de.nicidienase.chaosflix.common.mediadata.network
import android.content.res.Resources
import android.os.Build
import com.google.gson.Gson
import de.nicidienase.chaosflix.BuildConfig
import de.nicidienase.chaosflix.R
import de.nicidienase.chaosflix.common.SingletonHolder
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
class ApiFactory(val res: Resources) {
class ApiFactory private constructor(val res: Resources) {
val gsonConverterFactory by lazy { GsonConverterFactory.create(Gson()) }
private val chaosflixUserAgent: String by lazy { buildUserAgent() }
private val gsonConverterFactory: GsonConverterFactory by lazy { GsonConverterFactory.create(Gson()) }
val client: OkHttpClient by lazy {
OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(useragentInterceptor)
.build()
}
@ -35,4 +40,21 @@ class ApiFactory(val res: Resources) {
.addConverterFactory(gsonConverterFactory)
.build()
.create(StreamingService::class.java) }
private val useragentInterceptor: Interceptor = Interceptor {chain ->
val requestWithUseragent = chain.request().newBuilder()
.header("User-Agent", chaosflixUserAgent)
.build()
return@Interceptor chain.proceed(requestWithUseragent)
}
private fun buildUserAgent(): String {
val versionName = BuildConfig.VERSION_NAME
val device = "${Build.BRAND} ${Build.MODEL}"
val osVersion = "Android/${Build.VERSION.RELEASE}"
return "chaosflix/$versionName $osVersion ($device)"
}
companion object: SingletonHolder<ApiFactory, Resources>(::ApiFactory)
}

View file

@ -2,7 +2,7 @@ package de.nicidienase.chaosflix.common.mediadata.sync
import android.content.Intent
import android.support.v4.app.JobIntentService
import de.nicidienase.chaosflix.common.DatabaseFactory
import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Conference
import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Event
import de.nicidienase.chaosflix.common.mediadata.network.ApiFactory
@ -11,8 +11,8 @@ class DownloadJobService : JobIntentService() {
override fun onHandleWork(intent: Intent) {
val downloader = Downloader(
ApiFactory(resources).recordingApi,
DatabaseFactory.getInstance(applicationContext))
ApiFactory.getInstance(resources).recordingApi,
ChaosflixDatabase.getInstance(applicationContext))
val entityType: String? = intent.getStringExtra(ENTITY_KEY)
if (entityType != null) {

View file

@ -5,7 +5,7 @@ import android.arch.lifecycle.ViewModelProvider
import android.content.Context
import android.os.Environment
import android.preference.PreferenceManager
import de.nicidienase.chaosflix.common.DatabaseFactory
import de.nicidienase.chaosflix.common.ChaosflixDatabase
import de.nicidienase.chaosflix.common.OfflineItemManager
import de.nicidienase.chaosflix.common.PreferencesManager
import de.nicidienase.chaosflix.common.ResourcesFacade
@ -14,9 +14,9 @@ import de.nicidienase.chaosflix.common.mediadata.sync.Downloader
class ViewModelFactory(context: Context) : ViewModelProvider.Factory {
private val apiFactory = ApiFactory(context.resources)
private val apiFactory = ApiFactory.getInstance(context.resources)
private val database by lazy { DatabaseFactory.getInstance(context) }
private val database by lazy { ChaosflixDatabase.getInstance(context) }
private val recordingApi = apiFactory.recordingApi
private val streamingApi = apiFactory.streamingApi
private val preferencesManager =