Cleanup
parent
a27f81db30
commit
bcf925c132
@ -1,52 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.util.Log
|
|
||||||
import com.huawei.hmf.tasks.Tasks
|
|
||||||
import com.huawei.hms.aaid.HmsInstanceId
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.MainScope
|
|
||||||
import kotlinx.coroutines.isActive
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
private val TAG = HuaweiPushManager::class.java.name
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
class HuaweiPushManager @Inject constructor(
|
|
||||||
@ApplicationContext private val context: Context,
|
|
||||||
private val genericPushManager: GenericPushManager
|
|
||||||
): PushManager {
|
|
||||||
private var huaweiPushInstanceIdJob: Job? = null
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
override fun refresh(force: Boolean) {
|
|
||||||
Log.d(TAG, "refresh() called with: force = $force")
|
|
||||||
|
|
||||||
val huaweiPushInstanceIdJob = huaweiPushInstanceIdJob
|
|
||||||
|
|
||||||
huaweiPushInstanceIdJob?.apply {
|
|
||||||
if (force) cancel() else if (isActive) return
|
|
||||||
}
|
|
||||||
|
|
||||||
val appId = "107205081"
|
|
||||||
val tokenScope = "HCM"
|
|
||||||
val hmsInstanceId = HmsInstanceId.getInstance(context)
|
|
||||||
|
|
||||||
Log.d(TAG, "hmsInstanceId: $hmsInstanceId")
|
|
||||||
|
|
||||||
// genericPushManager.refresh(TextSecurePreferences.getFCMToken(context), force)
|
|
||||||
|
|
||||||
MainScope().launch(Dispatchers.IO) {
|
|
||||||
Log.d(TAG, "hmInstanceId getting token...")
|
|
||||||
val token = hmsInstanceId.getToken(appId, tokenScope)
|
|
||||||
Log.d(TAG, "refresh() hmsInstanceId => huawei token: $token")
|
|
||||||
//
|
|
||||||
//// genericPushManager.refresh(token, force)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +1,13 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import dagger.Binds
|
import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import dagger.hilt.components.SingletonComponent
|
import dagger.hilt.components.SingletonComponent
|
||||||
import javax.inject.Singleton
|
|
||||||
@Module
|
@Module
|
||||||
@InstallIn(SingletonComponent::class)
|
@InstallIn(SingletonComponent::class)
|
||||||
abstract class HuaweiBindingModule {
|
abstract class HuaweiBindingModule {
|
||||||
@Binds
|
@Binds
|
||||||
abstract fun bindPushManager(pushManager: HuaweiPushManager): PushManager
|
abstract fun bindTokenFetcher(tokenFetcher: HuaweiTokenFetcher): TokenFetcher
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.huawei.hms.aaid.HmsInstanceId
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.MainScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class HuaweiTokenFetcher @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context
|
||||||
|
): TokenFetcher {
|
||||||
|
override fun fetch(): Job {
|
||||||
|
val hmsInstanceId = HmsInstanceId.getInstance(context)
|
||||||
|
|
||||||
|
return MainScope().launch(Dispatchers.IO) {
|
||||||
|
val appId = "107205081"
|
||||||
|
val tokenScope = "HCM"
|
||||||
|
// getToken returns an empty string, but triggers the service to initialize.
|
||||||
|
hmsInstanceId.getToken(appId, tokenScope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
|
||||||
|
interface TokenFetcher {
|
||||||
|
fun fetch(): Job
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
@file:JvmName("FcmUtils")
|
|
||||||
package org.thoughtcrime.securesms.notifications
|
|
||||||
|
|
||||||
import com.google.android.gms.tasks.Task
|
|
||||||
import com.google.android.gms.tasks.Tasks
|
|
||||||
import com.google.firebase.iid.FirebaseInstanceId
|
|
||||||
import com.google.firebase.iid.InstanceIdResult
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
|
|
||||||
|
|
||||||
fun getFcmInstanceId(body: (Task<InstanceIdResult>)->Unit): Job = MainScope().launch(Dispatchers.IO) {
|
|
||||||
val task = FirebaseInstanceId.getInstance().instanceId
|
|
||||||
Tasks.await(task)
|
|
||||||
if (!isActive) return@launch // don't 'complete' task if we were canceled
|
|
||||||
body(task)
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
|
||||||
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import org.session.libsignal.utilities.Log
|
|
||||||
import javax.inject.Inject
|
|
||||||
import javax.inject.Singleton
|
|
||||||
|
|
||||||
private const val TAG = "FirebasePushManager"
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
class FirebasePushManager @Inject constructor(
|
|
||||||
private val genericPushManager: GenericPushManager
|
|
||||||
): PushManager {
|
|
||||||
|
|
||||||
private var firebaseInstanceIdJob: Job? = null
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
override fun refresh(force: Boolean) {
|
|
||||||
Log.d(TAG, "refresh() called with: force = $force")
|
|
||||||
|
|
||||||
firebaseInstanceIdJob?.apply {
|
|
||||||
when {
|
|
||||||
force -> cancel()
|
|
||||||
isActive -> return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
firebaseInstanceIdJob = getFcmInstanceId { task ->
|
|
||||||
when {
|
|
||||||
task.isSuccessful -> try { task.result?.token?.let {
|
|
||||||
genericPushManager.refresh(it, force).get()
|
|
||||||
} } catch(e: Exception) { Log.e(TAG, "refresh() failed", e) }
|
|
||||||
else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,45 @@
|
|||||||
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
|
import com.google.android.gms.tasks.Task
|
||||||
|
import com.google.android.gms.tasks.Tasks
|
||||||
|
import com.google.firebase.iid.FirebaseInstanceId
|
||||||
|
import com.google.firebase.iid.InstanceIdResult
|
||||||
|
import dagger.Lazy
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.MainScope
|
||||||
|
import kotlinx.coroutines.isActive
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.session.libsignal.utilities.Log
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
private val TAG = FirebaseTokenFetcher::class.java.name
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class FirebaseTokenFetcher @Inject constructor(
|
||||||
|
private val pushRegistry: Lazy<PushRegistry>,
|
||||||
|
): TokenFetcher {
|
||||||
|
override fun fetch(): Job = MainScope().launch(Dispatchers.IO) {
|
||||||
|
FirebaseInstanceId.getInstance().instanceId
|
||||||
|
.also(Tasks::await)
|
||||||
|
.also { if (!isActive) return@launch } // don't 'complete' task if we were canceled
|
||||||
|
.process()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Task<InstanceIdResult>.process() {
|
||||||
|
when {
|
||||||
|
isSuccessful -> try {
|
||||||
|
result?.token?.let {
|
||||||
|
pushRegistry.get().refresh(it, force = true).get()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
onFail(e)
|
||||||
|
}
|
||||||
|
else -> exception?.let(::onFail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onFail(e: Exception) = Log.e(TAG, "fetch failed", e)
|
||||||
|
}
|
||||||
|
|
@ -1,10 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
|
||||||
|
|
||||||
import org.session.libsignal.utilities.Log
|
|
||||||
|
|
||||||
class NoOpPushManager: PushManager {
|
|
||||||
|
|
||||||
override fun refresh(force: Boolean) {
|
|
||||||
Log.d("NoOpPushManager", "Push notifications not supported, not registering for push notifications")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue