From 37c22ab16a591a3f6e2ab18ca353be1451bf6b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Tue, 25 Oct 2022 08:56:26 +0200 Subject: [PATCH] DashboardWidgetService: Prevent crash when user is not present MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- .../client/widget/DashboardWidgetService.kt | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/widget/DashboardWidgetService.kt b/app/src/main/java/com/nextcloud/client/widget/DashboardWidgetService.kt index aed8a70fff..01e7b1106a 100644 --- a/app/src/main/java/com/nextcloud/client/widget/DashboardWidgetService.kt +++ b/app/src/main/java/com/nextcloud/client/widget/DashboardWidgetService.kt @@ -94,7 +94,7 @@ class StackRemoteViewsFactory( private var hasLoadMore = false override fun onCreate() { - Log_OC.d(this, "onCreate") + Log_OC.d(TAG, "onCreate") val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) widgetConfiguration = widgetRepository.getWidget(appWidgetId) @@ -110,23 +110,27 @@ class StackRemoteViewsFactory( override fun onDataSetChanged() { CoroutineScope(Dispatchers.IO).launch { try { - val client = clientFactory.createNextcloudClient(widgetConfiguration.user.get()) - val result = - DashboardGetWidgetItemsRemoteOperation(widgetConfiguration.widgetId, LIMIT_SIZE).execute(client) - widgetItems = result.resultData[widgetConfiguration.widgetId] ?: emptyList() + if (widgetConfiguration.user.isPresent) { + val client = clientFactory.createNextcloudClient(widgetConfiguration.user.get()) + val result = + DashboardGetWidgetItemsRemoteOperation(widgetConfiguration.widgetId, LIMIT_SIZE).execute(client) + widgetItems = result.resultData[widgetConfiguration.widgetId] ?: emptyList() - hasLoadMore = widgetConfiguration.moreButton != null && - widgetItems.size == LIMIT_SIZE + hasLoadMore = widgetConfiguration.moreButton != null && + widgetItems.size == LIMIT_SIZE + } else { + Log_OC.w(TAG, "User not present for widget update") + } } catch (e: ClientFactory.CreationException) { - Log_OC.e(this, "Error updating widget", e) + Log_OC.e(TAG, "Error updating widget", e) } } - Log_OC.d("WidgetService", "onDataSetChanged") + Log_OC.d(TAG, "onDataSetChanged") } override fun onDestroy() { - Log_OC.d("WidgetService", "onDestroy") + Log_OC.d(TAG, "onDestroy") widgetItems = emptyList() } @@ -195,7 +199,7 @@ class StackRemoteViewsFactory( setImageViewBitmap(R.id.icon, glide.get()) } } catch (e: Exception) { - Log_OC.d(this, "Error setting icon", e) + Log_OC.d(TAG, "Error setting icon", e) setImageViewResource(R.id.icon, R.drawable.ic_dashboard) } } @@ -238,6 +242,7 @@ class StackRemoteViewsFactory( } companion object { + private val TAG = DashboardWidgetService::class.simpleName const val LIMIT_SIZE = 14 } }