app/src/main/java/com/geekorum/ttrss/accounts/di.kt
author Da Risk <da_risk@geekorum.com>
Thu, 11 Jun 2020 02:30:30 -0400
changeset 723 43c8fb8d1528
parent 611 91b8d76c03cd
child 728 c7885cda3244
permissions -rw-r--r--
accounts: use hilt AndroidEntryPoint for AuthenticatorService
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
137
5464f07a306c Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents: 104
diff changeset
     1
/*
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * Geekttrss is a RSS feed reader application on the Android Platform.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 *
611
91b8d76c03cd Update copyrights headers
Da Risk <da_risk@geekorum.com>
parents: 560
diff changeset
     4
 * Copyright (C) 2017-2020 by Frederic-Charles Barthelery.
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 * This file is part of Geekttrss.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 * Geekttrss is free software: you can redistribute it and/or modify
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * it under the terms of the GNU General Public License as published by
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * the Free Software Foundation, either version 3 of the License, or
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * (at your option) any later version.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 * Geekttrss is distributed in the hope that it will be useful,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * GNU General Public License for more details.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
package com.geekorum.ttrss.accounts
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
import android.accounts.Account
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import android.accounts.AccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import android.app.Activity
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import android.content.Context
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import androidx.lifecycle.ViewModel
164
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    28
import com.geekorum.geekdroid.dagger.ViewModelKey
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
import com.geekorum.geekdroid.security.SecretEncryption
521
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    30
import com.geekorum.ttrss.core.CoroutineDispatchersProvider
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
import com.geekorum.ttrss.network.TinyrssApiModule
312
acd822d279e3 app: use Webapi and remove network/impl based api
Da Risk <da_risk@geekorum.com>
parents: 244
diff changeset
    32
import com.geekorum.ttrss.webapi.LoggedRequestInterceptorFactory
acd822d279e3 app: use Webapi and remove network/impl based api
Da Risk <da_risk@geekorum.com>
parents: 244
diff changeset
    33
import com.geekorum.ttrss.webapi.TinyRssApi
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
import dagger.Binds
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    35
import dagger.Module
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    36
import dagger.Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
import dagger.Subcomponent
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    38
import dagger.android.ContributesAndroidInjector
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
import dagger.multibindings.IntoMap
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
import javax.inject.Scope
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
import kotlin.annotation.AnnotationRetention.RUNTIME
417
8ba896069f2f Account: Use webapi TokenRetriever
Da Risk <da_risk@geekorum.com>
parents: 312
diff changeset
    42
import com.geekorum.ttrss.webapi.TokenRetriever
723
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    43
import dagger.hilt.InstallIn
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    44
import dagger.hilt.android.components.ServiceComponent
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    45
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
 * Dependency injection pieces for the account authenticator functionality.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    48
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    49
 * AuthenticatorService has a SubComponent of the ApplicationComponent.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    50
 * The AuthenticatorNetworkComponent is a  SubSubComponent that allows to do network
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
 * request with the Account backend
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
@Retention(RUNTIME)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
@MustBeDocumented
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
@Scope
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
annotation class PerAccount
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
@Module
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    61
object AndroidTinyrssAccountManagerModule {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    63
    private const val KEY_ALIAS = "com.geekorum.geekttrss.accounts.AccountManagerKey"
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
    fun providesAndroidTinyrssAccountManager(accountManager: AccountManager, secretEncryption: SecretEncryption): AndroidTinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
        val secretCipher = secretEncryption.getSecretCipher(KEY_ALIAS)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    68
        return AndroidTinyrssAccountManager(accountManager, secretCipher)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
    // may be replaced by a @Bind but it complicates the syntax in kotlin
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
    fun providesTinyrssAccountManager(androidTinyrssAccountManager: AndroidTinyrssAccountManager): TinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
        return androidTinyrssAccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    77
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    78
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    79
 * Provides the Services injectors subcomponents.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    80
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    81
@Module
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    82
abstract class ServicesInjectorModule {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    83
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
    @ContributesAndroidInjector(modules = [AuthenticatorActivityModule::class, ViewModelsModule::class])
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
    internal abstract fun contributesLoginActivityInjector(): LoginActivity
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    88
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
@Module
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    91
object NetworkLoginModule {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    93
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    94
    @PerAccount
521
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    95
    fun providesTokenRetriever(dispatchers: CoroutineDispatchersProvider,
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    96
                               accountManager: AccountManager, account: Account): TokenRetriever {
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    97
        return TinyrssAccountTokenRetriever(dispatchers, accountManager, account)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    98
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
    @PerAccount
417
8ba896069f2f Account: Use webapi TokenRetriever
Da Risk <da_risk@geekorum.com>
parents: 312
diff changeset
   102
    fun providesLoggedRequestInterceptorFactory(tokenRetriever: TokenRetriever): LoggedRequestInterceptorFactory {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
        return LoggedRequestInterceptorFactory(tokenRetriever)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
    @Provides
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   107
    fun providesServerInformation(accountManager: AndroidTinyrssAccountManager, account: Account): ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   108
        return with(accountManager) {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   109
            val ttRssAccount = fromAndroidAccount(account)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   110
            getServerInformation(ttRssAccount)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   111
        }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   114
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   115
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   117
@Module(subcomponents = [AuthenticatorNetworkComponent::class])
723
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
   118
@InstallIn(ServiceComponent::class)
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
   119
internal object AuthenticatorServiceModule
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   120
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   121
@Module(subcomponents = [AuthenticatorNetworkComponent::class])
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
   122
internal object AuthenticatorActivityModule {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   123
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   124
    fun providesContext(activity: Activity): Context {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   125
        return activity
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   126
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   127
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   128
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   129
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   130
@Subcomponent(modules = [TinyRssServerInformationModule::class, TinyrssApiModule::class])
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   131
internal interface AuthenticatorNetworkComponent {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   132
    fun getTinyRssApi(): TinyRssApi
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   133
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   134
    @Subcomponent.Builder
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   135
    interface Builder {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   136
        fun build(): AuthenticatorNetworkComponent
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   137
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   138
        fun tinyRssServerInformationModule(module: TinyRssServerInformationModule): Builder
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   139
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   140
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   141
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   142
@Module
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   143
internal class TinyRssServerInformationModule(val serverInformation: ServerInformation) {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   144
    @Provides
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   145
    fun providesServerInformation(): ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   146
        return serverInformation
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   147
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   148
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   149
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   150
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   151
@Module
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   152
private abstract class ViewModelsModule {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   153
    @Binds
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   154
    @IntoMap
164
2d6e9f2063cd Replace ViewModelsFactory by DaggerDelegateViewModelFactory
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
   155
    @ViewModelKey(LoginViewModel::class)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   156
    abstract fun getLoginViewModel(loginViewModel: LoginViewModel): ViewModel
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   157
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   158
}