Skip to content

Commit cf44ea1

Browse files
committed
format code + cleanup
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent a885736 commit cf44ea1

10 files changed

Lines changed: 72 additions & 83 deletions

File tree

app/src/main/java/com/nextcloud/talk/account/AccountVerificationActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class AccountVerificationActivity : BaseActivity() {
357357
"""
358358
${binding.progressText.text}
359359
${resources!!.getString(R.string.nc_capabilities_failed)}
360-
""".trimIndent()
360+
""".trimIndent()
361361
}
362362
abortVerification()
363363
} else {
@@ -371,7 +371,7 @@ class AccountVerificationActivity : BaseActivity() {
371371
"""
372372
${binding.progressText.text}
373373
${resources!!.getString(R.string.nc_push_disabled)}
374-
""".trimIndent()
374+
""".trimIndent()
375375
}
376376
}
377377
fetchAndStoreExternalSignalingSettings()
@@ -383,7 +383,7 @@ class AccountVerificationActivity : BaseActivity() {
383383
"""
384384
${binding.progressText.text}
385385
${resources!!.getString(R.string.nc_external_server_failed)}
386-
""".trimIndent()
386+
""".trimIndent()
387387
}
388388
}
389389
proceedWithLogin()
@@ -414,7 +414,8 @@ class AccountVerificationActivity : BaseActivity() {
414414
eventBus.post(EventStatus(internalAccountId, EventStatus.EventType.PUSH_REGISTRATION, true))
415415
} else if (userManager.users.blockingGet().size == 1 &&
416416
UnifiedPush.getDistributors(context).isNotEmpty() &&
417-
userManager.getUserWithId(internalAccountId).blockingGet().hasWebPushCapability) {
417+
userManager.getUserWithId(internalAccountId).blockingGet().hasWebPushCapability
418+
) {
418419
useUnifiedPushIntroduced()
419420
} else {
420421
Log.w(TAG, "Skipping push registration.")

app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ class ConversationsListActivity : BaseActivity() {
305305
// handle notification permission on API level >= 33
306306
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
307307
!platformPermissionUtil.isPostNotificationsPermissionGranted() &&
308-
(ClosedInterfaceImpl().isGooglePlayServicesAvailable ||
309-
appPreferences.useUnifiedPush)
308+
(
309+
ClosedInterfaceImpl().isGooglePlayServicesAvailable ||
310+
appPreferences.useUnifiedPush
311+
)
310312
) {
311313
requestPermissions(
312314
arrayOf(Manifest.permission.POST_NOTIFICATIONS),

app/src/main/java/com/nextcloud/talk/jobs/PushRegistrationWorker.kt

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/*
22
* Nextcloud Talk - Android Client
33
*
4-
* SPDX-FileCopyrightText: 2022 Andy Scherzinger <info@andy-scherzinger.de>
5-
* SPDX-FileCopyrightText: 2022 Marcel Hibbe <dev@mhibbe.de>
6-
* SPDX-FileCopyrightText: 2017 Mario Danic <mario@lovelyhq.com>
4+
* SPDX-FileCopyrightText: 2017-2026 Nextcloud GmbH and Nextcloud contributors
75
* SPDX-License-Identifier: GPL-3.0-or-later
86
*/
97
package com.nextcloud.talk.jobs
@@ -39,21 +37,20 @@ import retrofit2.Retrofit
3937
import javax.inject.Inject
4038

4139
/**
42-
* Can be used for 4 different things:
40+
* Can be used for 5 different things:
4341
* - if inputData contains [USER_ID] and [ACTIVATION_TOKEN]: activate web push for user (on server) and unregister
4442
* for proxy push (on server) (received from [com.nextcloud.talk.services.UnifiedPushService])
4543
* - if inputData contains [USER_ID] and [UNIFIEDPUSH_ENDPOINT]: register for web push (on server)
4644
* (received from [com.nextcloud.talk.services.UnifiedPushService])
45+
* - if inputData contains [USER_ID] and [UNREGISTER_WEBPUSH]: unregister web push for user (on server)
4746
* - if inputData contains [USE_UNIFIEDPUSH] or if [AppPreferences.getUseUnifiedPush]: get the server VAPID key and
4847
* register for UnifiedPush to the distributor (on device)
4948
* - if [AppPreferences.getUseUnifiedPush] is false: unregister UnifiedPush (on device) and unregister for web push
5049
* (on server), then register for proxy push (on server)
5150
*/
5251
@AutoInjector(NextcloudTalkApplication::class)
53-
class PushRegistrationWorker(
54-
context: Context,
55-
workerParams: WorkerParameters
56-
): Worker(context, workerParams) {
52+
@Suppress("TooManyFunctions")
53+
class PushRegistrationWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
5754
@Inject
5855
lateinit var retrofit: Retrofit
5956

@@ -96,7 +93,7 @@ class PushRegistrationWorker(
9693
if (userId != -1L && activationToken != null) {
9794
Log.d(TAG, "PushRegistrationWorker called via $origin (webPushActivationWork)")
9895
webPushActivationWork(userId, activationToken)
99-
} else if (userId != -1L && pushEndpoint != null) {
96+
} else if (userId != -1L && pushEndpoint != null) {
10097
Log.d(TAG, "PushRegistrationWorker called via $origin (webPushWork)")
10198
webPushWork(userId, pushEndpoint)
10299
} else if (userId != -1L && unregisterWebPush) {
@@ -228,25 +225,27 @@ class PushRegistrationWorker(
228225
}
229226
}
230227

231-
private fun defaultUseUnifiedPush(): Boolean = preferences.useUnifiedPush &&
232-
// If this is the first registration, we have never called [UnifiedPush.register]
233-
// because it happens after this function
234-
// => we can't be acked by the distributor yet, [UnifiedPush.getAckDistributor] == null
235-
// So we check the SavedDistributor instead
236-
UnifiedPush.getSavedDistributor(applicationContext).also {
237-
// It is null if the distributor has unregistered all the accounts,
238-
// or if it has been uninstalled from the system
239-
if (it == null) {
240-
Log.d(TAG, "No saved distributor found: disabling UnifiedPush")
241-
preferences.useUnifiedPush = false
242-
if (inputData.keyValueMap.any { (key, _) ->
243-
RESTART_ON_DISTRIB_UNINSTALL.contains(key)
244-
}) {
245-
enqueueWorkerWithoutData("defaultUseDistributor")
246-
enqueueNotifUnifiedPushDisabled()
228+
private fun defaultUseUnifiedPush(): Boolean =
229+
preferences.useUnifiedPush &&
230+
// If this is the first registration, we have never called [UnifiedPush.register]
231+
// because it happens after this function
232+
// => we can't be acked by the distributor yet, [UnifiedPush.getAckDistributor] == null
233+
// So we check the SavedDistributor instead
234+
UnifiedPush.getSavedDistributor(applicationContext).also {
235+
// It is null if the distributor has unregistered all the accounts,
236+
// or if it has been uninstalled from the system
237+
if (it == null) {
238+
Log.d(TAG, "No saved distributor found: disabling UnifiedPush")
239+
preferences.useUnifiedPush = false
240+
if (inputData.keyValueMap.any { (key, _) ->
241+
RESTART_ON_DISTRIB_UNINSTALL.contains(key)
242+
}
243+
) {
244+
enqueueWorkerWithoutData("defaultUseDistributor")
245+
enqueueNotifUnifiedPushDisabled()
246+
}
247247
}
248-
}
249-
} != null
248+
} != null
250249

251250
/**
252251
* Run the default worker, to use FCM if available
@@ -304,8 +303,8 @@ class PushRegistrationWorker(
304303
/**
305304
* Unregister on NC server and NC proxy
306305
*/
307-
private fun unregisterProxyPush(user: User): Observable<Void> {
308-
return if (ClosedInterfaceImpl().isGooglePlayServicesAvailable) {
306+
private fun unregisterProxyPush(user: User): Observable<Void> =
307+
if (ClosedInterfaceImpl().isGooglePlayServicesAvailable) {
309308
Log.d(TAG, "Unregistering proxy push for ${user.userId}")
310309
ncApi.unregisterDeviceForNotificationsWithNextcloud(
311310
user.getCredentials(),
@@ -322,17 +321,14 @@ class PushRegistrationWorker(
322321
} else {
323322
Observable.empty()
324323
}
325-
}
326324

327325
/**
328326
* Register web push with the unifiedpush endpoint, if the server supports web push
329327
*
330328
* @return `Observable<Pair<User, Boolean>>`, true if registration succeed, false if server doesn't support web push
331329
*/
332-
private fun registerWebPushForAccount(
333-
user: User,
334-
pushEndpoint: PushEndpoint
335-
): Observable<Pair<User, Boolean>> {
330+
@Suppress("ReturnCount")
331+
private fun registerWebPushForAccount(user: User, pushEndpoint: PushEndpoint): Observable<Pair<User, Boolean>> {
336332
if (user.hasWebPushCapability) {
337333
Log.d(TAG, "Registering web push for ${user.userId}")
338334
if (user.userId == null || user.baseUrl == null) {
@@ -353,16 +349,19 @@ class PushRegistrationWorker(
353349
"talk"
354350
).map { r ->
355351
return@map when (r.code()) {
356-
200 -> {
352+
HTTP_OK -> {
357353
Log.d(TAG, "Web push registration for ${user.userId} was already registered and activated\n")
358354
user to true
359355
}
360-
201 -> {
356+
HTTP_CREATED -> {
361357
Log.d(TAG, "New web push registration for ${user.userId}")
362358
user to true
363359
}
364360
else -> {
365-
Log.d(TAG, "An error occurred while registering web push for ${user.userId} (status=${r.code()})")
361+
Log.d(
362+
TAG,
363+
"An error occurred while registering web push for ${user.userId} (status=${r.code()})"
364+
)
366365
user to false
367366
}
368367
}
@@ -373,10 +372,7 @@ class PushRegistrationWorker(
373372
}
374373
}
375374

376-
private fun activateWebPushForAccount(
377-
user: User,
378-
activationToken: String
379-
) : Observable<Boolean> {
375+
private fun activateWebPushForAccount(user: User, activationToken: String): Observable<Boolean> {
380376
Log.d(TAG, "Activating web push for ${user.userId}")
381377
if (user.userId == null || user.baseUrl == null) {
382378
Log.w(TAG, "Null userId or baseUrl (userId=${user.userId}, baseUrl=${user.baseUrl}")
@@ -388,11 +384,11 @@ class PushRegistrationWorker(
388384
activationToken
389385
).map { r ->
390386
return@map when (r.code()) {
391-
200 -> {
387+
HTTP_OK -> {
392388
Log.d(TAG, "Web push registration for ${user.userId} was already activated\n")
393389
true
394390
}
395-
202 -> {
391+
HTTP_CREATED -> {
396392
Log.d(TAG, "Web push registration for ${user.userId} activated")
397393
true
398394
}
@@ -404,9 +400,7 @@ class PushRegistrationWorker(
404400
}
405401
}
406402

407-
private fun unregisterWebPushForAccount(
408-
user: User
409-
) : Observable<Boolean> {
403+
private fun unregisterWebPushForAccount(user: User): Observable<Boolean> {
410404
Log.d(TAG, "Unregistering web push for ${user.userId}")
411405
if (user.userId == null || user.baseUrl == null) {
412406
Log.w(TAG, "Null userId or baseUrl (userId=${user.userId}, baseUrl=${user.baseUrl}")
@@ -416,7 +410,6 @@ class PushRegistrationWorker(
416410
user.getCredentials(),
417411
ApiUtils.getUrlForWebPush(user.baseUrl!!)
418412
).map { true }
419-
420413
}
421414

422415
/**
@@ -426,16 +419,15 @@ class PushRegistrationWorker(
426419
*
427420
* @return `Observable<Pair<User, Boolean>>`, true if registration succeed, false if server doesn't support web push
428421
*/
429-
private fun registerUnifiedPushForAccount(
430-
user: User
431-
): Observable<Pair<User, Boolean>> {
422+
@Suppress("ReturnCount")
423+
private fun registerUnifiedPushForAccount(user: User): Observable<Pair<User, Boolean>> {
432424
if (user.hasWebPushCapability) {
433425
Log.d(TAG, "Registering UnifiedPush for ${user.userId} (${user.id})")
434426
if (user.userId == null || user.baseUrl == null) {
435427
Log.w(TAG, "Null userId or baseUrl (userId=${user.userId}, baseUrl=${user.baseUrl}")
436428
return Observable.empty()
437429
}
438-
return ncApi.getVapidKey(user.getCredentials(),ApiUtils.getUrlForVapid(user.baseUrl!!))
430+
return ncApi.getVapidKey(user.getCredentials(), ApiUtils.getUrlForVapid(user.baseUrl!!))
439431
.flatMap { ocs ->
440432
ocs.ocs?.data?.vapid?.let { vapid ->
441433
UnifiedPush.register(
@@ -464,6 +456,8 @@ class PushRegistrationWorker(
464456
const val USE_UNIFIEDPUSH = "use_unifiedpush"
465457
const val UNIFIEDPUSH_ENDPOINT = "unifiedpush_endpoint"
466458
const val UNREGISTER_WEBPUSH = "unregister_webpush"
459+
private const val HTTP_OK: Int = 200
460+
private const val HTTP_CREATED: Int = 201
467461

468462
/**
469463
* If any of these actions are present when we observe the distributor is uninstalled,

app/src/main/java/com/nextcloud/talk/services/UnifiedPushService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Nextcloud Talk - Android Client
33
*
4-
* SPDX-FileCopyrightText: 2026 Your Name <your@email.com>
4+
* SPDX-FileCopyrightText: 2017-2026 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: GPL-3.0-or-later
66
*/
77

@@ -22,7 +22,7 @@ import org.unifiedpush.android.connector.PushService
2222
import org.unifiedpush.android.connector.data.PushEndpoint
2323
import org.unifiedpush.android.connector.data.PushMessage
2424

25-
class UnifiedPushService: PushService() {
25+
class UnifiedPushService : PushService() {
2626
override fun onNewEndpoint(endpoint: PushEndpoint, instance: String) {
2727
Log.d(TAG, "New endpoint for $instance")
2828
val endpointBA = endpoint.toByteArray() ?: run {

app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,9 @@ class SettingsActivity :
325325
setupServerNotificationAppCheck()
326326
}
327327

328-
private fun showUnifiedPushToggle(): Boolean {
329-
return UnifiedPush.getDistributors(this).isNotEmpty() &&
328+
private fun showUnifiedPushToggle(): Boolean =
329+
UnifiedPush.getDistributors(this).isNotEmpty() &&
330330
userManager.users.blockingGet().all { it.hasWebPushCapability }
331-
}
332331

333332
private fun setupUnifiedPushSettings() {
334333
// If any user doesn't support web push, or there is no UnifiedPush

app/src/main/java/com/nextcloud/talk/ui/dialog/IntroduceUnifiedPushDialog.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
22
* Nextcloud Talk - Android Client
33
*
4-
* SPDX-FileCopyrightText: 2026 Your Name <your@email.com>
4+
* SPDX-FileCopyrightText: 2017-2026 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: GPL-3.0-or-later
66
*/
77

8-
package com.nextcloud.talk.ui.dialog;
8+
package com.nextcloud.talk.ui.dialog
99

1010
import androidx.compose.material3.AlertDialog
1111
import androidx.compose.material3.Text
1212
import androidx.compose.material3.TextButton
13-
import androidx.compose.runtime.Composable;
13+
import androidx.compose.runtime.Composable
1414
import androidx.compose.runtime.getValue
1515
import androidx.compose.runtime.mutableStateOf
1616
import androidx.compose.runtime.remember
@@ -19,9 +19,7 @@ import androidx.compose.ui.res.stringResource
1919
import com.nextcloud.talk.R
2020

2121
@Composable
22-
fun IntroduceUnifiedPushDialog(
23-
onResponse: (Boolean) -> Unit
24-
) {
22+
fun IntroduceUnifiedPushDialog(onResponse: (Boolean) -> Unit) {
2523
var showDialog by remember { mutableStateOf(true) }
2624
if (showDialog) {
2725
AlertDialog(
@@ -50,7 +48,7 @@ fun IntroduceUnifiedPushDialog(
5048
},
5149
text = {
5250
Text(stringResource(R.string.nc_dialog_introduce_unifiedpush_selection))
53-
},
51+
}
5452
)
5553
}
5654
}

app/src/main/java/com/nextcloud/talk/utils/ApiUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,14 @@ object ApiUtils {
390390

391391
@JvmStatic
392392
fun getUrlForVapid(baseUrl: String): String = "$baseUrl$OCS_API_VERSION/apps/notifications/api/v2/webpush/vapid"
393+
393394
@JvmStatic
394395
fun getUrlForWebPush(baseUrl: String): String = "$baseUrl$OCS_API_VERSION/apps/notifications/api/v2/webpush"
396+
395397
@JvmStatic
396398
fun getUrlForWebPushActivation(baseUrl: String): String =
397399
"$baseUrl$OCS_API_VERSION/apps/notifications/api/v2/webpush/activate"
400+
398401
@JvmStatic
399402
fun getUrlNextcloudPush(baseUrl: String): String = "$baseUrl$OCS_API_VERSION/apps/notifications/api/v2/push"
400403

app/src/main/java/com/nextcloud/talk/utils/PushUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class PushUtils {
157157
return result.toString()
158158
}
159159

160+
@Suppress("ReturnCount")
160161
fun generateRsa2048KeyPair(): Int {
161162
if (!publicKeyFile.exists() && !privateKeyFile.exists()) {
162163
var keyGen: KeyPairGenerator? = null

0 commit comments

Comments
 (0)