app/src/main/java/com/geekorum/ttrss/accounts/di.kt
author Da Risk <da_risk@geekorum.com>
Wed, 12 Jan 2022 17:29:59 -0400
changeset 882 7a74abf66c49
parent 846 ac0863af5ef6
child 943 298742859784
permissions -rw-r--r--
update license headers
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
 *
882
7a74abf66c49 update license headers
Da Risk <da_risk@geekorum.com>
parents: 846
diff changeset
     4
 * Copyright (C) 2017-2022 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 com.geekorum.geekdroid.security.SecretEncryption
521
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    26
import com.geekorum.ttrss.core.CoroutineDispatchersProvider
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
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
    28
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
    29
import com.geekorum.ttrss.webapi.TinyRssApi
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
    30
import com.geekorum.ttrss.webapi.TokenRetriever
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    31
import dagger.Module
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
import dagger.Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    33
import dagger.Subcomponent
723
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    34
import dagger.hilt.InstallIn
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    35
import dagger.hilt.android.components.ServiceComponent
769
179666072c7c build: update dagger
Da Risk <da_risk@geekorum.com>
parents: 734
diff changeset
    36
import dagger.hilt.components.SingletonComponent
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
    37
import dagger.hilt.migration.DisableInstallInCheck
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
    38
import javax.inject.Scope
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
    39
import kotlin.annotation.AnnotationRetention.RUNTIME
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    41
/**
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
 * Dependency injection pieces for the account authenticator functionality.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    44
 * AuthenticatorService has a SubComponent of the ApplicationComponent.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    45
 * The AuthenticatorNetworkComponent is a  SubSubComponent that allows to do network
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
 * request with the Account backend
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
 *
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
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    50
@Retention(RUNTIME)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
@MustBeDocumented
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
@Scope
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
annotation class PerAccount
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
@Module
769
179666072c7c build: update dagger
Da Risk <da_risk@geekorum.com>
parents: 734
diff changeset
    56
@InstallIn(SingletonComponent::class)
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    57
object AndroidTinyrssAccountManagerModule {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    59
    private const val KEY_ALIAS = "com.geekorum.geekttrss.accounts.AccountManagerKey"
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    60
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    61
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    62
    fun providesAndroidTinyrssAccountManager(accountManager: AccountManager, secretEncryption: SecretEncryption): AndroidTinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
        val secretCipher = secretEncryption.getSecretCipher(KEY_ALIAS)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
        return AndroidTinyrssAccountManager(accountManager, secretCipher)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
    // 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
    68
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
    fun providesTinyrssAccountManager(androidTinyrssAccountManager: AndroidTinyrssAccountManager): TinyrssAccountManager {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
        return androidTinyrssAccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
@Module
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
    76
@DisableInstallInCheck
560
67bb424bb68b Update to daggger-2.25.2
Da Risk <da_risk@geekorum.com>
parents: 521
diff changeset
    77
object NetworkLoginModule {
0
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
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    80
    @PerAccount
521
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    81
    fun providesTokenRetriever(dispatchers: CoroutineDispatchersProvider,
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    82
                               accountManager: AccountManager, account: Account): TokenRetriever {
2a5be03dd6bc Use CoroutineDispatchersProvider everywhere
Da Risk <da_risk@geekorum.com>
parents: 417
diff changeset
    83
        return TinyrssAccountTokenRetriever(dispatchers, accountManager, account)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    84
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    85
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    86
    @Provides
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    87
    @PerAccount
417
8ba896069f2f Account: Use webapi TokenRetriever
Da Risk <da_risk@geekorum.com>
parents: 312
diff changeset
    88
    fun providesLoggedRequestInterceptorFactory(tokenRetriever: TokenRetriever): LoggedRequestInterceptorFactory {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    89
        return LoggedRequestInterceptorFactory(tokenRetriever)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    90
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    91
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    92
    @Provides
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    93
    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
    94
        return with(accountManager) {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    95
            val ttRssAccount = fromAndroidAccount(account)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    96
            getServerInformation(ttRssAccount)
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    97
        }
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
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
@Module(subcomponents = [AuthenticatorNetworkComponent::class])
723
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
   104
@InstallIn(ServiceComponent::class)
43c8fb8d1528 accounts: use hilt AndroidEntryPoint for AuthenticatorService
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
   105
internal object AuthenticatorServiceModule
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
@Module(subcomponents = [AuthenticatorNetworkComponent::class])
769
179666072c7c build: update dagger
Da Risk <da_risk@geekorum.com>
parents: 734
diff changeset
   108
@InstallIn(SingletonComponent::class)
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
   109
internal object AuthenticatorActivityModule
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   110
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   111
@Subcomponent(modules = [TinyRssServerInformationModule::class, TinyrssApiModule::class])
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
internal interface AuthenticatorNetworkComponent {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
    fun getTinyRssApi(): TinyRssApi
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
    @Subcomponent.Builder
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
    interface Builder {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   117
        fun build(): AuthenticatorNetworkComponent
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   118
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   119
        fun tinyRssServerInformationModule(module: TinyRssServerInformationModule): Builder
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
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   122
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   123
@Module
734
2c85f9213f22 accounts: convert to hilt
Da Risk <da_risk@geekorum.com>
parents: 728
diff changeset
   124
@DisableInstallInCheck
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   125
internal class TinyRssServerInformationModule(val serverInformation: ServerInformation) {
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   126
    @Provides
104
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   127
    fun providesServerInformation(): ServerInformation {
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   128
        return serverInformation
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   129
    }
8b4047ebec59 Accounts: store a ServerInformation in the account manager
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
   130
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   131
}
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   132