|
| 1 | +/* |
| 2 | + * Copyright 2026 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package im.vector.app.features.importer |
| 9 | + |
| 10 | +import android.app.Service |
| 11 | +import android.content.Intent |
| 12 | +import android.os.Bundle |
| 13 | +import android.os.Handler |
| 14 | +import android.os.IBinder |
| 15 | +import android.os.Looper |
| 16 | +import android.os.Message |
| 17 | +import android.os.Messenger |
| 18 | +import android.os.RemoteException |
| 19 | +import dagger.hilt.android.AndroidEntryPoint |
| 20 | +import im.vector.app.core.di.ActiveSessionHolder |
| 21 | +import kotlinx.coroutines.runBlocking |
| 22 | +import timber.log.Timber |
| 23 | +import javax.inject.Inject |
| 24 | + |
| 25 | +@AndroidEntryPoint |
| 26 | +class ImporterService : Service() { |
| 27 | + private companion object { |
| 28 | + /** |
| 29 | + * Command to the service to get the data. |
| 30 | + */ |
| 31 | + const val MSG_GET_DATA = 1 |
| 32 | + |
| 33 | + const val KEY_ERROR_STR = "error" |
| 34 | + const val KEY_USER_ID_STR = "userId" |
| 35 | + const val KEY_IS_VERIFIED_BOOLEAN = "isVerified" |
| 36 | + const val KEY_MASTER_PRIVATE_KEY_STR = "masterPrivateKey" |
| 37 | + const val KEY_USER_PRIVATE_KEY_STR = "userPrivateKey" |
| 38 | + const val KEY_SELF_SIGNING_PRIVATE_KEY_STR = "selfSigningPrivateKey" |
| 39 | + const val KEY_KEY_BACKUP_KEY_STR = "keyBackupKey" |
| 40 | + } |
| 41 | + |
| 42 | + @Inject lateinit var activeSessionHolder: ActiveSessionHolder |
| 43 | + private val signaturePermissionChecker = SignaturePermissionChecker() |
| 44 | + |
| 45 | + /** |
| 46 | + * Handler of incoming messages from clients. |
| 47 | + */ |
| 48 | + private inner class IncomingHandler : Handler(Looper.getMainLooper()) { |
| 49 | + override fun handleMessage(msg: Message) { |
| 50 | + Timber.w("ImporterService: handling message ${msg.what}") |
| 51 | + val replyTo = msg.replyTo |
| 52 | + if (replyTo == null) { |
| 53 | + Timber.e("ImporterService: no replyTo in the message, cannot answer") |
| 54 | + } else { |
| 55 | + val bundle = Bundle() |
| 56 | + if (signaturePermissionChecker.check(msg.sendingUid, packageManager)) { |
| 57 | + Timber.w("ImporterService: Authorized caller") |
| 58 | + when (msg.what) { |
| 59 | + MSG_GET_DATA -> bundle.putSessionData() |
| 60 | + else -> bundle.putString(KEY_ERROR_STR, "Unknown command ${msg.what}") |
| 61 | + } |
| 62 | + } else { |
| 63 | + Timber.w("ImporterService: Unauthorized caller") |
| 64 | + bundle.putString(KEY_ERROR_STR, "Unauthorized") |
| 65 | + } |
| 66 | + replyTo.sendResponse(msg.what, bundle) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private fun Bundle.putSessionData() { |
| 72 | + val session = activeSessionHolder.getSafeActiveSession() |
| 73 | + if (session == null) { |
| 74 | + // Keep an empty Bundle to indicate that there is no session |
| 75 | + } else { |
| 76 | + putString(KEY_USER_ID_STR, session.myUserId) |
| 77 | + val crossSigningService = session.cryptoService().crossSigningService() |
| 78 | + val keysBackupService = session.cryptoService().keysBackupService() |
| 79 | + runBlocking { |
| 80 | + // TODO Use method exposed by the SDK (see https://github.com/matrix-org/matrix-rust-sdk/issues/6037) |
| 81 | + putBoolean(KEY_IS_VERIFIED_BOOLEAN, crossSigningService.isCrossSigningVerified()) |
| 82 | + crossSigningService.getCrossSigningPrivateKeys()?.let { privateKeys -> |
| 83 | + putString(KEY_MASTER_PRIVATE_KEY_STR, privateKeys.master) |
| 84 | + putString(KEY_SELF_SIGNING_PRIVATE_KEY_STR, privateKeys.selfSigned) |
| 85 | + putString(KEY_USER_PRIVATE_KEY_STR, privateKeys.user) |
| 86 | + } |
| 87 | + val keyBackupKey = keysBackupService.getKeyBackupRecoveryKeyInfo()?.recoveryKey?.toBase64() |
| 88 | + putString(KEY_KEY_BACKUP_KEY_STR, keyBackupKey) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun Messenger.sendResponse(what: Int, bundle: Bundle) { |
| 94 | + Timber.d("ImporterService: send response to client") |
| 95 | + try { |
| 96 | + val message = Message.obtain(null, what).also { |
| 97 | + it.data = bundle |
| 98 | + } |
| 99 | + send(message) |
| 100 | + } catch (e: RemoteException) { |
| 101 | + // The client is dead. |
| 102 | + Timber.e(e, "ImporterService: The client is dead.") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * When binding to the service, we return an interface to our messenger |
| 108 | + * for sending messages to the service. |
| 109 | + */ |
| 110 | + override fun onBind(intent: Intent): IBinder? { |
| 111 | + Timber.w("ImporterService: onBind") |
| 112 | + val messenger = Messenger(IncomingHandler()) |
| 113 | + return messenger.binder |
| 114 | + } |
| 115 | + |
| 116 | + override fun onUnbind(intent: Intent?): Boolean { |
| 117 | + Timber.w("ImporterService: onUnbind") |
| 118 | + return super.onUnbind(intent) |
| 119 | + } |
| 120 | +} |
0 commit comments