Skip to content

Commit

Permalink
Inject signal
Browse files Browse the repository at this point in the history
  • Loading branch information
SeniorZhai committed Dec 2, 2024
1 parent a09ba34 commit b8803cb
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 63 deletions.
22 changes: 1 addition & 21 deletions app/src/main/java/one/mixin/android/MixinApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.microsoft.appcenter.crashes.Crashes
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.android.internal.managers.ApplicationComponentManager
import dagger.hilt.components.SingletonComponent
import io.reactivex.plugins.RxJavaPlugins
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -117,11 +118,6 @@ open class MixinApplication :
fun getHiltWorkerFactory(): HiltWorkerFactory
}

@InstallIn(SingletonComponent::class)
@EntryPoint
interface AppEntryPoint {
fun inject(app: MixinApplication)
}

@Inject
lateinit var databaseProvider: DatabaseProvider
Expand Down Expand Up @@ -280,27 +276,11 @@ open class MixinApplication :
applicationScope.launch {
clearData(sessionId)
withContext(Dispatchers.Main) {
val entryPoint =
EntryPointAccessors.fromApplication(
this@MixinApplication,
AppEntryPoint::class.java,
)
entryPoint.inject(this@MixinApplication)
LandingActivity.show(this@MixinApplication)
}
}
}
reject()
}

fun reject() {
databaseProvider.closeAllDatabases()
val entryPoint =
EntryPointAccessors.fromApplication(
this@MixinApplication,
AppEntryPoint::class.java,
)
entryPoint.inject(this@MixinApplication)
}

private fun clearData(sessionId: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import one.mixin.android.crypto.vo.RatchetSenderKey
import one.mixin.android.crypto.vo.SenderKey
import one.mixin.android.crypto.vo.Session
import one.mixin.android.crypto.vo.SignedPreKey
import one.mixin.android.db.MixinDatabase

@Database(
entities = [
Expand Down Expand Up @@ -67,4 +68,9 @@ abstract class SignalDatabase : RoomDatabase() {
object : RoomDatabase.Callback() {
}
}

override fun close() {
INSTANCE = null
super.close()
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/one/mixin/android/db/DatabaseProvider.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package one.mixin.android.db

import android.content.Context
import one.mixin.android.crypto.db.SignalDatabase
import one.mixin.android.fts.FtsDatabase
import one.mixin.android.db.pending.PendingDatabase
import one.mixin.android.db.pending.PendingDatabaseImp
Expand Down Expand Up @@ -44,6 +45,7 @@ class DatabaseProvider @Inject constructor(

pendingDatabase?.close()
pendingDatabase = PendingDatabaseImp.getDatabase(context, db.floodMessageDao(), db.jobDao())

}

@Synchronized
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/java/one/mixin/android/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -666,26 +666,6 @@ object AppModule {
isLenient = true
}

@Provides
@Singleton
fun provideJobSenderKey(
participantSessionDao: ParticipantSessionDao,
signalProtocol: SignalProtocol,
conversationApi: ConversationService,
participantDao: ParticipantDao,
chatWebSocket: ChatWebSocket,
linkState: LinkState,
messageHistoryDao: MessageHistoryDao,
) = JobSenderKey(
participantSessionDao,
signalProtocol,
conversationApi,
participantDao,
chatWebSocket,
linkState,
messageHistoryDao,
)

private const val DATA_STORE_FILE_NAME = "safe_box_%s.store"

@Singleton
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/one/mixin/android/job/BaseJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,17 @@ abstract class BaseJob(params: Params) : Job(params) {
@Inject
lateinit var applicationScope: CoroutineScope

@Transient
@Inject
lateinit var jobSenderKey: JobSenderKey
fun jobSenderKey(): JobSenderKey {
return JobSenderKey(
participantSessionDao(),
signalProtocol,
conversationApi,
participantDao(),
chatWebSocket,
linkState,
messageHistoryDao(),
)
}

fun database(): MixinDatabase = databaseProvider.getMixinDatabase()

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/one/mixin/android/job/ConversationJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ConversationJob(
}
participantDao().insertList(participants)
cr.participantSessions?.let {
jobSenderKey.syncParticipantSession(cr.conversationId, it)
jobSenderKey().syncParticipantSession(cr.conversationId, it)
}
jobManager.addJobInBackground(GenerateAvatarJob(cr.conversationId))
} else if (type == TYPE_MUTE) {
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/one/mixin/android/job/MixinJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract class MixinJob(
sessionId: String,
): Boolean {
val blazeMessage = createConsumeSessionSignalKeys(createConsumeSignalKeysParam(arrayListOf(BlazeMessageParamSession(recipientId, sessionId))))
val data = jobSenderKey.signalKeysChannel(blazeMessage) ?: return false
val data = jobSenderKey().signalKeysChannel(blazeMessage) ?: return false
val keys = Gson().fromJson<ArrayList<SignalKey>>(data)
if (!keys.isNullOrEmpty()) {
val preKeyBundle = createPreKeyBundle(keys[0])
Expand All @@ -86,9 +86,9 @@ abstract class MixinJob(
val (cipherText, err) = signalProtocol.encryptSenderKey(conversationId, recipientId, sessionId.getDeviceId())
if (err) return false
val signalKeyMessages = createBlazeSignalKeyMessage(recipientId, cipherText!!, sessionId)
val checksum = jobSenderKey.getCheckSum(conversationId)
val checksum = jobSenderKey().getCheckSum(conversationId)
val bm = createSignalKeyMessage(createSignalKeyMessageParam(conversationId, arrayListOf(signalKeyMessages), checksum))
val result = jobSenderKey.deliverNoThrow(bm)
val result = jobSenderKey().deliverNoThrow(bm)
if (result.retry) {
return sendSenderKey(conversationId, recipientId, sessionId)
}
Expand All @@ -108,7 +108,7 @@ abstract class MixinJob(
createConsumeSignalKeysParam(arrayListOf(BlazeMessageParamSession(recipientId, sessionId))),
)

val data = jobSenderKey.signalKeysChannel(blazeMessage) ?: return false
val data = jobSenderKey().signalKeysChannel(blazeMessage) ?: return false
val keys = Gson().fromJson<ArrayList<SignalKey>>(data)
if (!keys.isNullOrEmpty()) {
val preKeyBundle = createPreKeyBundle(keys[0])
Expand All @@ -122,7 +122,7 @@ abstract class MixinJob(

protected fun deliver(blazeMessage: BlazeMessage): Boolean {
blazeMessage.params?.conversation_id?.let {
blazeMessage.params.conversation_checksum = jobSenderKey.getCheckSum(it)
blazeMessage.params.conversation_checksum = jobSenderKey().getCheckSum(it)
}
val bm = chatWebSocket.sendMessage(blazeMessage)
if (bm == null) {
Expand All @@ -132,7 +132,7 @@ abstract class MixinJob(
when (bm.error.code) {
CONVERSATION_CHECKSUM_INVALID_ERROR -> {
blazeMessage.params?.conversation_id?.let {
jobSenderKey.syncConversation(it)
jobSenderKey().syncConversation(it)
}
throw ChecksumException()
}
Expand Down Expand Up @@ -169,13 +169,13 @@ abstract class MixinJob(
MessageStatus.SENDING.name,
)
val bm = BlazeMessage(UUID.randomUUID().toString(), CREATE_MESSAGE, params)
jobSenderKey.deliverNoThrow(bm)
jobSenderKey().deliverNoThrow(bm)
}

protected fun checkConversation(conversationId: String) {
val conversation = conversationDao().findConversationById(conversationId) ?: return
if (conversation.isGroupConversation()) {
jobSenderKey.syncConversation(conversation.conversationId)
jobSenderKey().syncConversation(conversation.conversationId)
} else {
checkConversationExist(conversation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RefreshConversationJob(val conversationId: String, private val skipRefresh

participantDao().replaceAll(data.conversationId, participants)
data.participantSessions?.let {
jobSenderKey.syncParticipantSession(conversationId, it)
jobSenderKey().syncParticipantSession(conversationId, it)
}

if (conversationUserIds.isNotEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/one/mixin/android/job/SendMessageJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ open class SendMessageJob(
val accountId = Session.getAccountId()!!
var participantSessionKey = getBotSessionKey(accountId)
if (participantSessionKey == null || participantSessionKey.publicKey.isNullOrBlank()) {
jobSenderKey.syncConversation(message.conversationId)
jobSenderKey().syncConversation(message.conversationId)
participantSessionKey = getBotSessionKey(accountId)
}
// Workaround No session key, can't encrypt message, send PLAIN directly
Expand Down Expand Up @@ -327,7 +327,7 @@ open class SendMessageJob(
if (!signalProtocol.isExistSenderKey(message.conversationId, message.userId)) {
checkConversation(message.conversationId)
}
jobSenderKey.checkSessionSenderKey(message.conversationId)
jobSenderKey().checkSessionSenderKey(message.conversationId)
deliver(encryptNormalMessage(expireIn))
callback(expireIn)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SendPlaintextJob(
}

override fun onRun() {
jobSenderKey.deliverNoThrow(blazeMessage)
jobSenderKey().deliverNoThrow(blazeMessage)
}

override fun cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import one.mixin.android.vo.User
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class AccountRepository
@Inject
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import one.mixin.android.vo.SearchMessageItem
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ConversationRepository
@Inject
internal constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TokenRepository
@Inject
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import one.mixin.android.vo.generateConversationId
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class UserRepository
@Inject
constructor(
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/one/mixin/android/ui/landing/LandingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,19 @@ class LandingFragment : Fragment(R.layout.fragment_landing) {
MobileFragment.TAG,
)
}
binding.userTv1.setOnClickListener { v ->
navTo(MnemonicPhraseFragment.newInstance(words = ArrayList<String>().apply {
addAll(testAccount[0].split(" "))
}), MnemonicPhraseFragment.TAG)
}
binding.userTv2.setOnClickListener { v ->
navTo(MnemonicPhraseFragment.newInstance(words = ArrayList<String>().apply {
addAll(testAccount[1].split(" "))
}), MnemonicPhraseFragment.TAG)
}
}

private val testAccount = listOf(
"" , "" // Todo add test account
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class AccountFragment : BaseFragment(R.layout.fragment_account) {
// BackupMnemonicPhraseWarningBottomSheetDialogFragment.newInstance()
// .show(parentFragmentManager, BackupMnemonicPhraseWarningBottomSheetDialogFragment.TAG)
// } else {
LogoutPinBottomSheetDialogFragment.newInstance()
.showNow(parentFragmentManager, VerifyBottomSheetDialogFragment.TAG)
// LogoutPinBottomSheetDialogFragment.newInstance()
// .showNow(parentFragmentManager, VerifyBottomSheetDialogFragment.TAG)
// }
MixinApplication.get().closeAndClear()
}
deleteRl.setOnClickListener {
navTo(DeleteAccountFragment.newInstance(), DeleteAccountFragment.TAG)
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/res/layout/fragment_landing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,38 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
style="@style/AppTheme.MaterialButton"
android:id="@+id/user_tv_1"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:text="user1"
android:textColor="@color/white"
android:layout_marginStart="36dp"
android:layout_marginEnd="18dp"
android:textSize="14sp"
android:textFontWeight="500"
app:layout_constraintBottom_toTopOf="@id/create_tv"
app:layout_constraintEnd_toStartOf="@id/user_tv_2"
app:layout_constraintStart_toStartOf="parent" />

<Button
style="@style/AppTheme.MaterialButton"
android:id="@+id/user_tv_2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:text="user2"
android:textColor="@color/white"
android:layout_marginStart="18dp"
android:layout_marginEnd="36dp"
android:textSize="14sp"
android:textFontWeight="500"
app:layout_constraintBottom_toTopOf="@id/create_tv"
app:layout_constraintStart_toEndOf="@id/user_tv_1"
app:layout_constraintEnd_toEndOf="parent"/>

<Button
style="@style/AppTheme.MaterialButton"
android:id="@+id/create_tv"
Expand Down

0 comments on commit b8803cb

Please sign in to comment.