Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt

353 lines
11 KiB
Kotlin
Raw Normal View History

2016-07-02 05:54:53 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.dagger
import android.app.Application
import android.content.Context
2017-01-21 05:08:47 +01:00
import android.location.LocationManager
2017-02-02 11:18:34 +01:00
import android.net.ConnectivityManager
2017-01-05 14:08:49 +01:00
import android.os.Build
2016-07-02 05:54:53 +02:00
import android.os.Looper
2017-02-02 11:18:34 +01:00
import android.support.v4.net.ConnectivityManagerCompat
2016-07-02 05:54:53 +02:00
import android.support.v4.text.BidiFormatter
2017-03-01 04:22:22 +01:00
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory
2017-03-01 07:13:10 +01:00
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.extractor.ExtractorsFactory
2017-03-01 04:22:22 +01:00
import com.google.android.exoplayer2.upstream.DataSource
2016-07-02 05:54:53 +02:00
import com.squareup.otto.Bus
import com.squareup.otto.ThreadEnforcer
import com.twitter.Extractor
2017-02-28 08:34:00 +01:00
import com.twitter.Validator
2016-07-02 05:54:53 +02:00
import dagger.Module
import dagger.Provides
import edu.tsinghua.hotmobi.HotMobiLogger
2017-03-01 09:27:45 +01:00
import okhttp3.Cache
2016-07-02 05:54:53 +02:00
import okhttp3.ConnectionPool
2017-03-01 09:27:45 +01:00
import okhttp3.Dns
2017-03-01 04:22:22 +01:00
import okhttp3.OkHttpClient
2016-09-09 05:58:26 +02:00
import org.mariotaku.kpreferences.KPreferences
2016-07-02 05:54:53 +02:00
import org.mariotaku.mediaviewer.library.FileCache
import org.mariotaku.mediaviewer.library.MediaDownloader
import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.Constants
import org.mariotaku.twidere.constant.SharedPreferenceConstants
2017-02-04 11:42:14 +01:00
import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_CACHE_SIZE_LIMIT
import org.mariotaku.twidere.constant.autoRefreshCompatibilityModeKey
2016-09-09 05:58:26 +02:00
import org.mariotaku.twidere.model.DefaultFeatures
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.util.*
2017-03-02 14:59:57 +01:00
import org.mariotaku.twidere.util.cache.DiskLRUFileCache
2017-03-01 15:12:25 +01:00
import org.mariotaku.twidere.util.cache.JsonCache
2016-07-02 05:54:53 +02:00
import org.mariotaku.twidere.util.media.TwidereMediaDownloader
import org.mariotaku.twidere.util.net.TwidereDns
2017-01-09 06:16:23 +01:00
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
2017-01-05 14:08:49 +01:00
import org.mariotaku.twidere.util.refresh.AutoRefreshController
import org.mariotaku.twidere.util.refresh.JobSchedulerAutoRefreshController
import org.mariotaku.twidere.util.refresh.LegacyAutoRefreshController
import org.mariotaku.twidere.util.sync.JobSchedulerSyncController
import org.mariotaku.twidere.util.sync.LegacySyncController
import org.mariotaku.twidere.util.sync.SyncController
import org.mariotaku.twidere.util.sync.SyncPreferences
2017-03-01 09:27:45 +01:00
import java.io.File
2016-07-02 05:54:53 +02:00
import javax.inject.Singleton
/**
* Created by mariotaku on 15/10/5.
*/
@Module
class ApplicationModule(private val application: Application) {
init {
if (Thread.currentThread() !== Looper.getMainLooper().thread) {
throw RuntimeException("Module must be created inside main thread")
}
}
@Provides
@Singleton
fun keyboardShortcutsHandler(): KeyboardShortcutsHandler {
return KeyboardShortcutsHandler(application)
}
@Provides
@Singleton
fun externalThemeManager(preferences: SharedPreferencesWrapper): ExternalThemeManager {
return ExternalThemeManager(application, preferences)
}
@Provides
@Singleton
fun notificationManagerWrapper(): NotificationManagerWrapper {
return NotificationManagerWrapper(application)
}
@Provides
@Singleton
fun sharedPreferences(): SharedPreferencesWrapper {
return SharedPreferencesWrapper.getInstance(application, Constants.SHARED_PREFERENCES_NAME,
Context.MODE_PRIVATE, SharedPreferenceConstants::class.java)
}
2016-09-09 05:58:26 +02:00
@Provides
@Singleton
fun kPreferences(sharedPreferences: SharedPreferencesWrapper): KPreferences {
return KPreferences(sharedPreferences)
}
2016-07-02 05:54:53 +02:00
@Provides
@Singleton
fun permissionsManager(): PermissionsManager {
return PermissionsManager(application)
}
@Provides
@Singleton
fun userColorNameManager(): UserColorNameManager {
return UserColorNameManager(application)
}
@Provides
@Singleton
fun multiSelectManager(): MultiSelectManager {
return MultiSelectManager()
}
@Provides
@Singleton
2017-03-01 09:27:45 +01:00
fun restHttpClient(prefs: SharedPreferencesWrapper, dns: Dns,
connectionPool: ConnectionPool, cache: Cache): RestHttpClient {
2017-01-29 13:09:59 +01:00
val conf = HttpClientFactory.HttpClientConfiguration(prefs)
2017-03-01 09:27:45 +01:00
return HttpClientFactory.createRestHttpClient(conf, dns, connectionPool, cache)
2016-07-02 05:54:53 +02:00
}
@Provides
@Singleton
2017-03-01 04:22:22 +01:00
fun connectionPool(): ConnectionPool {
2016-07-02 05:54:53 +02:00
return ConnectionPool()
}
@Provides
@Singleton
fun bus(): Bus {
return Bus(ThreadEnforcer.MAIN)
}
@Provides
@Singleton
fun asyncTaskManager(): AsyncTaskManager {
return AsyncTaskManager()
}
@Provides
@Singleton
fun activityTracker(): ActivityTracker {
return ActivityTracker()
}
@Provides
@Singleton
fun asyncTwitterWrapper(bus: Bus, preferences: SharedPreferencesWrapper,
2017-02-16 12:07:39 +01:00
asyncTaskManager: AsyncTaskManager, notificationManagerWrapper: NotificationManagerWrapper): AsyncTwitterWrapper {
return AsyncTwitterWrapper(application, bus, preferences, asyncTaskManager, notificationManagerWrapper)
2016-07-02 05:54:53 +02:00
}
@Provides
@Singleton
fun readStateManager(): ReadStateManager {
return ReadStateManager(application)
}
2017-02-16 12:07:39 +01:00
@Provides
@Singleton
fun contentNotificationManager(activityTracker: ActivityTracker, userColorNameManager: UserColorNameManager,
notificationManagerWrapper: NotificationManagerWrapper, preferences: SharedPreferencesWrapper): ContentNotificationManager {
return ContentNotificationManager(application, activityTracker, userColorNameManager, notificationManagerWrapper, preferences)
}
2016-07-02 05:54:53 +02:00
@Provides
@Singleton
fun mediaLoaderWrapper(preferences: SharedPreferencesWrapper): MediaPreloader {
val preloader = MediaPreloader(application)
preloader.reloadOptions(preferences)
2017-02-02 11:18:34 +01:00
val cm = application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
preloader.isNetworkMetered = ConnectivityManagerCompat.isActiveNetworkMetered(cm)
return preloader
2016-07-02 05:54:53 +02:00
}
@Provides
@Singleton
2017-03-01 09:27:45 +01:00
fun dns(preferences: SharedPreferencesWrapper): Dns {
2016-07-02 05:54:53 +02:00
return TwidereDns(application, preferences)
}
@Provides
@Singleton
fun mediaDownloader(preferences: SharedPreferencesWrapper, client: RestHttpClient): MediaDownloader {
return TwidereMediaDownloader(application, preferences, client)
}
@Provides
@Singleton
2017-02-28 08:34:00 +01:00
fun validator(): Validator {
return Validator()
2016-07-02 05:54:53 +02:00
}
@Provides
@Singleton
fun extractor(): Extractor {
return Extractor()
}
@Provides
@Singleton
fun errorInfoStore(): ErrorInfoStore {
return ErrorInfoStore(application)
}
@Provides
fun provideBidiFormatter(): BidiFormatter {
return BidiFormatter.getInstance()
}
2016-12-17 16:05:02 +01:00
@Provides
@Singleton
2016-12-17 16:05:02 +01:00
fun autoRefreshController(kPreferences: KPreferences): AutoRefreshController {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !kPreferences[autoRefreshCompatibilityModeKey]) {
2016-12-17 16:05:02 +01:00
return JobSchedulerAutoRefreshController(application, kPreferences)
}
return LegacyAutoRefreshController(application, kPreferences)
}
@Provides
@Singleton
2017-01-09 06:16:23 +01:00
fun syncController(): SyncController {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return JobSchedulerSyncController(application)
}
return LegacySyncController(application)
}
@Provides
@Singleton
fun syncPreferences(): SyncPreferences {
return SyncPreferences(application)
}
@Provides
@Singleton
2017-01-09 06:16:23 +01:00
fun taskCreator(kPreferences: KPreferences, bus: Bus): TaskServiceRunner {
return TaskServiceRunner(application, kPreferences, bus)
}
2016-09-09 05:58:26 +02:00
@Provides
@Singleton
2016-11-25 14:21:51 +01:00
fun defaultFeatures(preferences: SharedPreferencesWrapper): DefaultFeatures {
val features = DefaultFeatures()
features.load(preferences)
return features
2016-09-09 05:58:26 +02:00
}
@Provides
@Singleton
fun hotMobiLogger(): HotMobiLogger {
return HotMobiLogger(application)
}
2017-01-09 06:16:23 +01:00
@Provides
@Singleton
fun extraFeaturesService(): ExtraFeaturesService {
return ExtraFeaturesService.newInstance(application)
}
2017-01-12 17:26:44 +01:00
@Provides
@Singleton
fun etagCache(): ETagCache {
return ETagCache(application)
}
2017-01-21 05:08:47 +01:00
@Provides
fun locationManager(): LocationManager {
return application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
}
@Provides
fun okHttpClient(preferences: SharedPreferencesWrapper, dns: Dns, connectionPool: ConnectionPool,
cache: Cache): OkHttpClient {
val conf = HttpClientFactory.HttpClientConfiguration(preferences)
val builder = OkHttpClient.Builder()
HttpClientFactory.initOkHttpClient(conf, builder, dns, connectionPool, cache)
return builder.build()
}
2017-03-01 04:22:22 +01:00
@Provides
@Singleton
2017-03-01 09:27:45 +01:00
fun dataSourceFactory(preferences: SharedPreferencesWrapper, dns: Dns, connectionPool: ConnectionPool,
cache: Cache): DataSource.Factory {
2017-03-01 04:22:22 +01:00
val conf = HttpClientFactory.HttpClientConfiguration(preferences)
val builder = OkHttpClient.Builder()
2017-03-01 09:27:45 +01:00
HttpClientFactory.initOkHttpClient(conf, builder, dns, connectionPool, cache)
2017-03-01 04:22:22 +01:00
val userAgent = UserAgentUtils.getDefaultUserAgentStringSafe(application)
return OkHttpDataSourceFactory(builder.build(), userAgent, null)
}
2017-03-01 09:27:45 +01:00
@Provides
@Singleton
fun cache(preferences: SharedPreferencesWrapper): Cache {
val cacheSizeMB = preferences.getInt(KEY_CACHE_SIZE_LIMIT, 300).coerceIn(100..500)
// Convert to bytes
2017-03-06 09:36:25 +01:00
return Cache(getCacheDir("network", cacheSizeMB * 1048576L), cacheSizeMB * 1048576L)
2017-03-01 09:27:45 +01:00
}
2017-03-01 07:13:10 +01:00
@Provides
@Singleton
fun extractorsFactory(): ExtractorsFactory {
return DefaultExtractorsFactory()
}
2017-03-01 15:12:25 +01:00
@Provides
@Singleton
fun jsonCache(): JsonCache {
2017-03-06 09:36:25 +01:00
return JsonCache(getCacheDir("json", 100 * 1048576L))
2017-03-01 15:12:25 +01:00
}
@Provides
@Singleton
fun fileCache(): FileCache {
2017-03-06 09:36:25 +01:00
return DiskLRUFileCache(getCacheDir("media", 100 * 1048576L))
2017-03-01 09:27:45 +01:00
}
2016-07-02 05:54:53 +02:00
2017-03-06 09:36:25 +01:00
private fun getCacheDir(dirName: String, sizeInBytes: Long): File {
return Utils.getExternalCacheDir(application, dirName, sizeInBytes) ?:
2017-03-01 09:27:45 +01:00
Utils.getInternalCacheDir(application, dirName)
2016-07-02 05:54:53 +02:00
}
companion object {
private var sApplicationModule: ApplicationModule? = null
fun get(context: Context): ApplicationModule {
if (sApplicationModule != null) return sApplicationModule!!
val application = context.applicationContext as Application
sApplicationModule = ApplicationModule(application)
return sApplicationModule!!
}
}
}
2016-12-17 16:05:02 +01:00