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