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

View File

@ -17,13 +17,13 @@ object DistributorUtils {
const val TOKEN_REGISTERED_OK = "token_registered_ok" const val TOKEN_REGISTERED_OK = "token_registered_ok"
const val TOKEN_NOK = "token_nok" const val TOKEN_NOK = "token_nok"
private var db: MessagingDatabase? = null private lateinit var db: MessagingDatabase
fun getDb(context: Context): MessagingDatabase { fun getDb(context: Context): MessagingDatabase {
if (db == null) { if (!this::db.isInitialized) {
db = MessagingDatabase(context) db = MessagingDatabase(context)
} }
return db!! return db
} }
fun sendMessage(context: Context, appToken: String, message: ByteArray) { 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) newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)
} }
wakeLock?.acquire(30000L /*30 secs*/) wakeLock?.acquire(30000L /*30 secs*/)
when (intent!!.action) { when (intent?.action) {
ACTION_REGISTER ->{ ACTION_REGISTER ->{
Log.i(TAG,"REGISTER") Log.i(TAG,"REGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: "" val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
@ -49,7 +49,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
Log.w(TAG,"Trying to register an app without packageName") Log.w(TAG,"Trying to register an app without packageName")
return return
} }
when (checkToken(context!!, connectorToken, application)) { when (checkToken(context, connectorToken, application)) {
TOKEN_REGISTERED_OK -> sendEndpoint(context.applicationContext, connectorToken) TOKEN_REGISTERED_OK -> sendEndpoint(context.applicationContext, connectorToken)
TOKEN_NOK -> sendRegistrationFailed( TOKEN_NOK -> sendRegistrationFailed(
context, context,
@ -84,7 +84,7 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
ACTION_UNREGISTER ->{ ACTION_UNREGISTER ->{
Log.i(TAG,"UNREGISTER") Log.i(TAG,"UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: "" val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
val application = getDb(context!!).getPackageName(connectorToken) val application = getDb(context).getPackageName(connectorToken)
if (application.isBlank()) { if (application.isBlank()) {
return return
} }

View File

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