Avoid !! operator

This commit is contained in:
sim 2022-09-20 01:32:17 +02:00
parent e77b1188da
commit fed27ca4fc
4 changed files with 24 additions and 14 deletions

View File

@ -97,14 +97,18 @@ object ApiUtils {
override fun onComplete() {
saveUrl(context, "${ssoAccount.url}${mApiEndpoint}")
// Sync once it is registered
cSync(context, deviceId!!)
deviceId?.let {
cSync(context, it)
}
Log.d(TAG, "mApi register: onComplete")
}
})
} else {
// Sync directly
Log.d(TAG, "Found deviceId: $deviceId")
cSync(context, deviceId!!)
deviceId?.let {
cSync(context, it)
}
}
}
@ -184,10 +188,13 @@ object ApiUtils {
callback: () -> Unit
) {
// The unity of connector token must already be checked here
val parameters = mutableMapOf(
"deviceId" to getDeviceId(context)!!,
"appName" to appName
)
val parameters = getDeviceId(context)?.let {
mutableMapOf(
"deviceId" to it,
"appName" to appName
)
} ?: return
mApi.createApp(parameters)
?.subscribeOn(Schedulers.newThread())
?.observeOn(Schedulers.newThread())

View File

@ -17,13 +17,13 @@ object DistributorUtils {
const val TOKEN_REGISTERED_OK = "token_registered_ok"
const val TOKEN_NOK = "token_nok"
private var db: MessagingDatabase? = null
private lateinit var db: MessagingDatabase
fun getDb(context: Context): MessagingDatabase {
if (db == null) {
if (!this::db.isInitialized) {
db = MessagingDatabase(context)
}
return db!!
return db
}
fun sendMessage(context: Context, appToken: String, message: ByteArray) {

View File

@ -40,7 +40,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)
}
wakeLock?.acquire(30000L /*30 secs*/)
when (intent!!.action) {
when (intent?.action) {
ACTION_REGISTER ->{
Log.i(TAG,"REGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
@ -49,7 +49,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
Log.w(TAG,"Trying to register an app without packageName")
return
}
when (checkToken(context!!, connectorToken, application)) {
when (checkToken(context, connectorToken, application)) {
TOKEN_REGISTERED_OK -> sendEndpoint(context.applicationContext, connectorToken)
TOKEN_NOK -> sendRegistrationFailed(
context,
@ -84,7 +84,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
ACTION_UNREGISTER ->{
Log.i(TAG,"UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
val application = getDb(context!!).getPackageName(connectorToken)
val application = getDb(context).getPackageName(connectorToken)
if (application.isBlank()) {
return
}

View File

@ -153,8 +153,11 @@ class StartService: Service(){
private fun registerNetworkCallback() {
Log.d(TAG, "Registering Network Callback")
try {
connectivityManager = this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager!!.registerDefaultNetworkCallback(networkCallback)
connectivityManager = (
this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
).apply {
registerDefaultNetworkCallback(networkCallback)
}
} catch (e: Exception) {
e.printStackTrace()
}