Skip to content

Commit 0adf299

Browse files
authored
Merge pull request #390 from code-payments/chore/scope-authenticator-type-to-namespace
chore: scope authenticator type to namespace
2 parents a3ed380 + a2bd41f commit 0adf299

File tree

13 files changed

+65
-92
lines changed

13 files changed

+65
-92
lines changed

api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ dependencies {
7878

7979
implementation(Libs.grpc_okhttp)
8080
implementation(Libs.grpc_kotlin)
81+
implementation(Libs.androidx_lifecycle_runtime)
8182
implementation(Libs.androidx_room_runtime)
8283
implementation(Libs.androidx_room_ktx)
8384
implementation(Libs.androidx_room_rxjava3)

api/src/main/java/com/getcode/manager/MnemonicManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ class MnemonicManager @Inject constructor(
3030
return mnemonicPhrase.getBase64EncodedEntropy(context)
3131
}
3232

33+
fun getEncodedBase58(mnemonicPhrase: MnemonicPhrase): String {
34+
return mnemonicPhrase.getBase58EncodedEntropy(context)
35+
}
36+
3337
val mnemonicCode: MnemonicCode = context.resources.let(::MnemonicCode)
3438
}

api/src/main/java/com/getcode/network/TipController.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.getcode.network
22

3-
import androidx.lifecycle.DefaultLifecycleObserver
43
import androidx.lifecycle.Lifecycle
54
import androidx.lifecycle.LifecycleEventObserver
6-
import androidx.lifecycle.LifecycleObserver
75
import androidx.lifecycle.LifecycleOwner
86
import com.getcode.manager.SessionManager
97
import com.getcode.model.CodePayload
10-
import com.getcode.model.TipMetadata
118
import com.getcode.model.PrefsBool
129
import com.getcode.model.PrefsString
10+
import com.getcode.model.TipMetadata
1311
import com.getcode.model.TwitterUser
1412
import com.getcode.network.client.Client
1513
import com.getcode.network.client.fetchTwitterUser
Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
package com.getcode.network.client
22

3-
import android.content.Context
4-
import androidx.lifecycle.DefaultLifecycleObserver
5-
import androidx.lifecycle.Lifecycle
6-
import androidx.lifecycle.LifecycleEventObserver
7-
import androidx.lifecycle.LifecycleObserver
8-
import androidx.lifecycle.LifecycleOwner
93
import com.getcode.analytics.AnalyticsService
4+
import com.getcode.manager.MnemonicManager
105
import com.getcode.manager.SessionManager
116
import com.getcode.network.BalanceController
12-
import com.getcode.network.HistoryController
137
import com.getcode.network.exchange.Exchange
148
import com.getcode.network.repository.AccountRepository
159
import com.getcode.network.repository.IdentityRepository
1610
import com.getcode.network.repository.MessagingRepository
1711
import com.getcode.network.repository.PrefRepository
1812
import com.getcode.network.repository.TransactionRepository
19-
import com.getcode.utils.network.NetworkConnectivityListener
2013
import com.getcode.network.service.ChatService
2114
import com.getcode.network.service.DeviceService
2215
import com.getcode.utils.ErrorUtils
23-
import dagger.hilt.android.qualifiers.ApplicationContext
16+
import com.getcode.utils.network.NetworkConnectivityListener
2417
import kotlinx.coroutines.CoroutineScope
2518
import kotlinx.coroutines.Dispatchers
2619
import kotlinx.coroutines.cancel
27-
import kotlinx.coroutines.delay
20+
import kotlinx.coroutines.flow.filter
21+
import kotlinx.coroutines.flow.filterNotNull
22+
import kotlinx.coroutines.flow.launchIn
23+
import kotlinx.coroutines.flow.map
24+
import kotlinx.coroutines.flow.onEach
2825
import kotlinx.coroutines.launch
2926
import timber.log.Timber
3027
import java.util.Timer
@@ -36,8 +33,6 @@ internal const val TAG = "Client"
3633

3734
@Singleton
3835
class Client @Inject constructor(
39-
@ApplicationContext
40-
internal val context: Context,
4136
internal val identityRepository: IdentityRepository,
4237
internal val transactionRepository: TransactionRepository,
4338
internal val messagingRepository: MessagingRepository,
@@ -51,9 +46,9 @@ class Client @Inject constructor(
5146
internal val networkObserver: NetworkConnectivityListener,
5247
internal val chatService: ChatService,
5348
internal val deviceService: DeviceService,
54-
) : LifecycleEventObserver {
49+
internal val mnemonicManager: MnemonicManager,
50+
) {
5551

56-
private val TAG = "PollTimer"
5752
private val scope = CoroutineScope(Dispatchers.IO)
5853

5954
private var pollTimer: Timer? = null
@@ -62,13 +57,15 @@ class Client @Inject constructor(
6257
private fun startPollTimerWhenAuthenticated() {
6358
Timber.tag(TAG).i("Creating poll timer")
6459
scope.launch {
65-
SessionManager.authState.collect {
66-
if (it.isAuthenticated == true) {
60+
SessionManager.authState
61+
.map { it.isAuthenticated }
62+
.filterNotNull()
63+
.filter { it }
64+
.onEach {
6765
Timber.tag(TAG).i("User Authenticated - starting timer")
6866
startPollTimer()
6967
this.cancel()
70-
}
71-
}
68+
}.launchIn(this)
7269
}
7370
}
7471

@@ -101,28 +98,12 @@ class Client @Inject constructor(
10198
}
10299
}
103100

104-
private fun startTimer() {
101+
fun startTimer() {
105102
startPollTimerWhenAuthenticated()
106103
}
107104

108-
fun pollOnce(delay: Long = 2_000L) {
109-
scope.launch {
110-
delay(delay)
111-
Timber.tag(TAG).i("Poll Once")
112-
poll()
113-
}
114-
}
115-
116-
private fun stopTimer() {
105+
fun stopTimer() {
117106
Timber.tag(TAG).i("Cancelling Poller")
118107
pollTimer?.cancel()
119108
}
120-
121-
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
122-
when (event) {
123-
Lifecycle.Event.ON_RESUME -> startTimer()
124-
Lifecycle.Event.ON_STOP -> stopTimer()
125-
else -> Unit
126-
}
127-
}
128109
}

api/src/main/java/com/getcode/network/client/Client_Transaction.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fun Client.transferWithResultSingle(
9696
return getTransferPreflightAction(amount.kin)
9797
.andThen(Single.defer {
9898
transactionRepository.transfer(
99-
context, amount, fee, additionalFees, organizer, rendezvousKey, destination, isWithdrawal, tipMetadata
99+
amount, fee, additionalFees, organizer, rendezvousKey, destination, isWithdrawal, tipMetadata
100100
)
101101
})
102102
.map {
@@ -141,7 +141,6 @@ fun Client.sendRemotely(
141141
getTransferPreflightAction(truncatedAmount.kin)
142142
.andThen(
143143
sendRemotely(
144-
context = context,
145144
amount = truncatedAmount,
146145
organizer = organizer,
147146
rendezvousKey = rendezvousKey,
@@ -150,7 +149,7 @@ fun Client.sendRemotely(
150149
.doOnComplete {
151150
val giftCardItem = GiftCard(
152151
key = giftCard.cluster.vaultPublicKey.base58(),
153-
entropy = giftCard.mnemonicPhrase.getBase58EncodedEntropy(context),
152+
entropy = mnemonicManager.getEncodedBase58(giftCard.mnemonicPhrase),
154153
amount = truncatedAmount.kin.quarks,
155154
date = System.currentTimeMillis()
156155
)
@@ -367,15 +366,14 @@ private fun Client.withdraw(
367366
}
368367

369368
fun Client.sendRemotely(
370-
context: Context,
371369
amount: KinAmount,
372370
organizer: Organizer,
373371
rendezvousKey: PublicKey,
374372
giftCard: GiftCardAccount
375373
): Completable {
376374
return Completable.defer {
377375
transactionRepository.sendRemotely(
378-
context, amount, organizer, rendezvousKey, giftCard
376+
amount, organizer, rendezvousKey, giftCard
379377
)
380378
.map {
381379
if (it is IntentRemoteSend) {

api/src/main/java/com/getcode/network/repository/TransactionRepository.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ class TransactionRepository @Inject constructor(
128128
}
129129

130130
fun transfer(
131-
context: Context,
132131
amount: KinAmount,
133132
fee: Kin,
134133
additionalFees: List<Fee>,
@@ -254,7 +253,6 @@ class TransactionRepository @Inject constructor(
254253
}
255254

256255
fun sendRemotely(
257-
context: Context,
258256
amount: KinAmount,
259257
organizer: Organizer,
260258
rendezvousKey: PublicKey,

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ android {
3737
buildToolsVersion = Android.buildToolsVersion
3838
testInstrumentationRunner = Android.testInstrumentationRunner
3939

40+
resValue("string", "applicationId", Android.namespace)
41+
4042
buildConfigField("String", "MIXPANEL_API_KEY", "\"${tryReadProperty(rootProject.rootDir, "MIXPANEL_API_KEY")}\"")
4143
buildConfigField("String", "KADO_API_KEY", "\"${tryReadProperty(rootProject.rootDir, "KADO_API_KEY")}\"")
4244
buildConfigField("Boolean", "NOTIFY_ERRORS", "false")

app/src/main/java/com/getcode/CodeApp.kt

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -200,36 +200,7 @@ internal data object MainRoot : Screen {
200200
Box(
201201
modifier = Modifier
202202
.fillMaxSize()
203-
.background(CodeTheme.colors.background),
204-
contentAlignment = Alignment.Center
205-
) {
206-
var show by remember {
207-
mutableStateOf(false)
208-
}
209-
210-
AnimatedContent(show, transitionSpec = { fadeIn() togetherWith fadeOut() }) {
211-
if (it) {
212-
Column(
213-
horizontalAlignment = Alignment.CenterHorizontally,
214-
verticalArrangement = Arrangement.spacedBy(CodeTheme.dimens.inset)
215-
) {
216-
Image(
217-
painter = painterResource(R.drawable.ic_code_logo_near_white),
218-
contentDescription = "",
219-
modifier = Modifier
220-
.fillMaxWidth(0.65f)
221-
.fillMaxHeight(0.65f)
222-
)
223-
224-
CodeCircularProgressIndicator()
225-
}
226-
}
227-
}
228-
229-
LaunchedEffect(Unit) {
230-
delay(1_000)
231-
show = true
232-
}
233-
}
203+
.background(CodeTheme.colors.background)
204+
)
234205
}
235206
}

app/src/main/java/com/getcode/inject/ApiModule.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import com.getcode.BuildConfig
55
import com.getcode.R
66
import com.getcode.analytics.AnalyticsService
7+
import com.getcode.manager.MnemonicManager
78
import com.getcode.model.Currency
89
import com.getcode.network.BalanceController
910
import com.getcode.network.PrivacyMigration
@@ -73,12 +74,8 @@ object ApiModule {
7374
val DEV_URL = "api.codeinfra.dev"
7475
val PROD_URL = "api.codeinfra.net"
7576

76-
return AndroidChannelBuilder.usingBuilder(
77-
OkHttpChannelBuilderForcedTls12.forAddress(
78-
PROD_URL,
79-
TLS_PORT
80-
)
81-
)
77+
return AndroidChannelBuilder
78+
.usingBuilder(OkHttpChannelBuilderForcedTls12.forAddress(PROD_URL, TLS_PORT))
8279
.context(context)
8380
.userAgent("Code/Android/${BuildConfig.VERSION_NAME}")
8481
.keepAliveTime(4, TimeUnit.MINUTES)
@@ -87,7 +84,9 @@ object ApiModule {
8784

8885
@Singleton
8986
@Provides
90-
fun provideAccountAuthenticator(@ApplicationContext context: Context): AccountAuthenticator {
87+
fun provideAccountAuthenticator(
88+
@ApplicationContext context: Context,
89+
): AccountAuthenticator {
9190
return AccountAuthenticator(context)
9291
}
9392

@@ -157,7 +156,6 @@ object ApiModule {
157156
@Singleton
158157
@Provides
159158
fun provideClient(
160-
@ApplicationContext context: Context,
161159
identityRepository: IdentityRepository,
162160
transactionRepository: TransactionRepository,
163161
messagingRepository: MessagingRepository,
@@ -171,9 +169,9 @@ object ApiModule {
171169
networkObserver: NetworkConnectivityListener,
172170
chatService: ChatService,
173171
deviceService: DeviceService,
172+
mnemonicManager: MnemonicManager,
174173
): Client {
175174
return Client(
176-
context,
177175
identityRepository,
178176
transactionRepository,
179177
messagingRepository,
@@ -187,6 +185,7 @@ object ApiModule {
187185
networkObserver,
188186
chatService,
189187
deviceService,
188+
mnemonicManager
190189
)
191190
}
192191

app/src/main/java/com/getcode/util/AccountAuthenticator.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package com.getcode.util
33
import android.accounts.*
44
import android.content.Context
55
import android.os.Bundle
6+
import com.getcode.model.PrefsString
7+
import com.getcode.network.repository.PrefRepository
68
import com.getcode.utils.startupLog
79

810

9-
class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthenticator(mContext) {
11+
class AccountAuthenticator(
12+
private val context: Context,
13+
) : AbstractAccountAuthenticator(context) {
1014
@Throws(NetworkErrorException::class)
1115
override fun addAccount(
1216
response: AccountAuthenticatorResponse,
@@ -32,9 +36,9 @@ class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthe
3236
options: Bundle
3337
): Bundle {
3438
// Extract the username and password from the Account Manager, then, generate token
35-
val am = AccountManager.get(mContext)
39+
val am = AccountManager.get(context)
3640
var authToken = am.peekAuthToken(account, authTokenType)
37-
startupLog("authenticator: authToken ${authToken != null}")
41+
startupLog("authenticator: authToken ${authToken != null}, $authTokenType")
3842
// Lets give another try to authenticate the user
3943
if (null != authToken) {
4044
if (authToken.isEmpty()) {
@@ -56,7 +60,10 @@ class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthe
5660
}
5761
}
5862

59-
startupLog("authenticator failure", Throwable("Failed to retrieve authToken from AccountManager"))
63+
startupLog(
64+
"authenticator failure",
65+
Throwable("Failed to retrieve authToken from AccountManager")
66+
)
6067
// If we get here, then we couldn't access the user's password
6168
return Bundle()
6269
}

app/src/main/java/com/getcode/view/MainActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.getcode.LocalExchange
1414
import com.getcode.LocalNetworkObserver
1515
import com.getcode.LocalPhoneFormatter
1616
import com.getcode.analytics.AnalyticsService
17+
import com.getcode.network.client.Client
1718
import com.getcode.network.exchange.Exchange
1819
import com.getcode.ui.utils.handleUncaughtException
1920
import com.getcode.util.CurrencyUtils
@@ -30,6 +31,9 @@ import javax.inject.Inject
3031
@AndroidEntryPoint
3132
class MainActivity : FragmentActivity() {
3233

34+
@Inject
35+
lateinit var client: Client
36+
3337
@Inject
3438
lateinit var analyticsManager: AnalyticsService
3539

@@ -96,5 +100,15 @@ class MainActivity : FragmentActivity() {
96100
private fun setFullscreen() {
97101
enableEdgeToEdge()
98102
}
103+
104+
override fun onResume() {
105+
super.onResume()
106+
client.startTimer()
107+
}
108+
109+
override fun onStop() {
110+
super.onStop()
111+
client.stopTimer()
112+
}
99113
}
100114

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<account-authenticator
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
android:accountType="com.getcode"
4+
android:accountType="@string/applicationId"
55
android:icon="@mipmap/ic_launcher_round"
66
android:smallIcon="@mipmap/ic_launcher_round"
77
android:label="@string/app_name"/>

0 commit comments

Comments
 (0)