From 27b8787ad682a93815ee2d1fbba8530f76f25e76 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 22 Aug 2024 17:42:29 +0200 Subject: [PATCH 1/5] Check account type before sync operation execution and instead crash app warn user Signed-off-by: alperozturk --- .../operations/common/SyncOperation.java | 9 ++-- .../android/ui/activity/DrawerActivity.java | 48 +++++++++---------- app/src/main/res/values/strings.xml | 1 + 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java index 643330a09db1..89a1a42212a8 100644 --- a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java @@ -13,6 +13,8 @@ import android.os.Handler; import com.nextcloud.common.NextcloudClient; +import com.nextcloud.utils.extensions.ContextExtensionsKt; +import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; @@ -46,10 +48,11 @@ public SyncOperation(@NonNull FileDataStorageManager storageManager) { */ public RemoteOperationResult execute(Context context) { if (storageManager.getUser().isAnonymous()) { - throw new IllegalArgumentException("Trying to execute a sync operation with a " + - "storage manager for an anonymous account"); + ContextExtensionsKt.showToast(context, R.string.anonymous_account_type_warning_text); + return new RemoteOperationResult(RemoteOperationResult.ResultCode.ACCOUNT_EXCEPTION); + } else { + return super.execute(this.storageManager.getUser(), context); } - return super.execute(this.storageManager.getUser(), context); } public RemoteOperationResult execute(@NonNull NextcloudClient client) { diff --git a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 17f8bdad2f7d..4e3a0951454a 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -539,7 +539,7 @@ private void onNavigationItemClicked(final MenuItem menuItem) { } else if (itemId == R.id.nav_logout) { mCheckedMenuItem = -1; MenuItem isNewMenuItemChecked = menuItem.setChecked(false); - Log_OC.d(TAG,"onNavigationItemClicked nav_logout setChecked " + isNewMenuItemChecked); + Log_OC.d(TAG, "onNavigationItemClicked nav_logout setChecked " + isNewMenuItemChecked); final Optional optionalUser = getUser(); if (optionalUser.isPresent()) { UserInfoActivity.openAccountRemovalDialog(optionalUser.get(), getSupportFragmentManager()); @@ -1164,7 +1164,7 @@ public void showFiles(boolean onDeviceOnly, boolean onlyPersonalFiles) { public void avatarGenerated(Drawable avatarDrawable, Object callContext) { if (callContext instanceof MenuItem menuItem) { MenuItem newIcon = menuItem.setIcon(avatarDrawable); - Log_OC.d(TAG,"avatarGenerated new icon: " + newIcon); + Log_OC.d(TAG, "avatarGenerated new icon: " + newIcon); } else if (callContext instanceof ImageView imageView) { imageView.setImageDrawable(avatarDrawable); } else if (callContext instanceof MaterialButton materialButton) { @@ -1229,29 +1229,29 @@ public void onAccountRemovedEvent(AccountRemovedEvent event) { * Retrieves external links via api from 'external' app */ public void fetchExternalLinks(final boolean force) { - if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) { - Thread t = new Thread(() -> { - // fetch capabilities as early as possible - if ((getCapabilities() == null || getCapabilities().getAccountName().isEmpty()) - && getStorageManager() != null) { + if (!getBaseContext().getResources().getBoolean(R.bool.show_external_links)) { + return; + } + + Thread t = new Thread(() -> { + User user = accountManager.getUser(); + if (user.isAnonymous()) { + accountManager.startAccountCreation(this); + } else { + if ((getCapabilities() == null || getCapabilities().getAccountName() != null && getCapabilities().getAccountName().isEmpty()) && getStorageManager() != null) { GetCapabilitiesOperation getCapabilities = new GetCapabilitiesOperation(getStorageManager()); getCapabilities.execute(getBaseContext()); } - User user = accountManager.getUser(); - if (getStorageManager() != null && CapabilityUtils.getCapability(user, this) - .getExternalLinks().isTrue()) { - - int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, - FileActivity.APP_OPENED_COUNT); + if (getStorageManager() != null && CapabilityUtils.getCapability(user, this).getExternalLinks().isTrue()) { + int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT); if (count > 10 || count == -1 || force) { if (force) { Log_OC.d("ExternalLinks", "force update"); } - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, - FileActivity.APP_OPENED_COUNT, "0"); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, "0"); Log_OC.d("ExternalLinks", "update via api"); RemoteOperation getExternalLinksOperation = new ExternalLinksOperation(); @@ -1259,30 +1259,30 @@ && getStorageManager() != null) { if (result.isSuccess() && result.getData() != null) { externalLinksProvider.deleteAllExternalLinks(); - - ArrayList externalLinks = (ArrayList) (Object) result.getData(); - + ArrayList externalLinks = (ArrayList) result.getData(); for (ExternalLink link : externalLinks) { externalLinksProvider.storeExternalLink(link); } } } else { - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, - FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); } } else { externalLinksProvider.deleteAllExternalLinks(); Log_OC.d("ExternalLinks", "links disabled"); } + runOnUiThread(this::updateExternalLinksInDrawer); - }); - t.start(); - } + } + }); + t.start(); } protected void handleDeepLink(@NonNull Uri uri) { String path = uri.getLastPathSegment(); - if (path == null) return; + if (path == null) { + return; + } DeepLinkConstants deepLinkType = DeepLinkConstants.Companion.fromPath(path); if (deepLinkType == null) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5c06a52d4527..2f55aa6c78a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ ~ SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only --> + Trying to execute a sync operation with anonymous account, operation cancelled" %1$s Android app version %1$s version %1$s, build #%2$s From 9c2f7e464297c29538635604571f5fad7d599292 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 23 Aug 2024 09:14:01 +0200 Subject: [PATCH 2/5] Dont show toast Signed-off-by: alperozturk --- .../com/owncloud/android/operations/common/SyncOperation.java | 3 ++- app/src/main/res/values/strings.xml | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java index 89a1a42212a8..6cd6d97e68e0 100644 --- a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java @@ -20,6 +20,7 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.utils.Log_OC; import androidx.annotation.NonNull; @@ -48,7 +49,7 @@ public SyncOperation(@NonNull FileDataStorageManager storageManager) { */ public RemoteOperationResult execute(Context context) { if (storageManager.getUser().isAnonymous()) { - ContextExtensionsKt.showToast(context, R.string.anonymous_account_type_warning_text); + Log_OC.d("SyncOperation","Trying to execute a sync operation with anonymous account, operation cancelled"); return new RemoteOperationResult(RemoteOperationResult.ResultCode.ACCOUNT_EXCEPTION); } else { return super.execute(this.storageManager.getUser(), context); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2f55aa6c78a8..5c06a52d4527 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,7 +6,6 @@ ~ SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only --> - Trying to execute a sync operation with anonymous account, operation cancelled" %1$s Android app version %1$s version %1$s, build #%2$s From 1bbbb9549396edce65f49eafa899648a970c0850 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 23 Aug 2024 09:48:44 +0200 Subject: [PATCH 3/5] revert changes for sync operations Signed-off-by: alperozturk --- .../android/operations/common/SyncOperation.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java index 6cd6d97e68e0..643330a09db1 100644 --- a/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/common/SyncOperation.java @@ -13,14 +13,11 @@ import android.os.Handler; import com.nextcloud.common.NextcloudClient; -import com.nextcloud.utils.extensions.ContextExtensionsKt; -import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; import androidx.annotation.NonNull; @@ -49,11 +46,10 @@ public SyncOperation(@NonNull FileDataStorageManager storageManager) { */ public RemoteOperationResult execute(Context context) { if (storageManager.getUser().isAnonymous()) { - Log_OC.d("SyncOperation","Trying to execute a sync operation with anonymous account, operation cancelled"); - return new RemoteOperationResult(RemoteOperationResult.ResultCode.ACCOUNT_EXCEPTION); - } else { - return super.execute(this.storageManager.getUser(), context); + throw new IllegalArgumentException("Trying to execute a sync operation with a " + + "storage manager for an anonymous account"); } + return super.execute(this.storageManager.getUser(), context); } public RemoteOperationResult execute(@NonNull NextcloudClient client) { From 96058c7072a8332e5f0791159588a2ec526bb2e3 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 23 Aug 2024 10:23:26 +0200 Subject: [PATCH 4/5] Dont start account creation if user is anonymous in fetchExternalLinks function Signed-off-by: alperozturk --- .../android/ui/activity/DrawerActivity.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 4e3a0951454a..4721ba721b9b 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -1233,47 +1233,48 @@ public void fetchExternalLinks(final boolean force) { return; } + User user = accountManager.getUser(); + if (user.isAnonymous()) { + Log_OC.d(TAG, "Trying to execute a sync operation with a storage manager for an anonymous account"); + return; + } + Thread t = new Thread(() -> { - User user = accountManager.getUser(); - if (user.isAnonymous()) { - accountManager.startAccountCreation(this); - } else { - if ((getCapabilities() == null || getCapabilities().getAccountName() != null && getCapabilities().getAccountName().isEmpty()) && getStorageManager() != null) { - GetCapabilitiesOperation getCapabilities = new GetCapabilitiesOperation(getStorageManager()); - getCapabilities.execute(getBaseContext()); - } + if ((getCapabilities() == null || getCapabilities().getAccountName() != null && getCapabilities().getAccountName().isEmpty()) && getStorageManager() != null) { + GetCapabilitiesOperation getCapabilities = new GetCapabilitiesOperation(getStorageManager()); + getCapabilities.execute(getBaseContext()); + } - if (getStorageManager() != null && CapabilityUtils.getCapability(user, this).getExternalLinks().isTrue()) { - int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT); + if (getStorageManager() != null && CapabilityUtils.getCapability(user, this).getExternalLinks().isTrue()) { + int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT); - if (count > 10 || count == -1 || force) { - if (force) { - Log_OC.d("ExternalLinks", "force update"); - } + if (count > 10 || count == -1 || force) { + if (force) { + Log_OC.d("ExternalLinks", "force update"); + } - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, "0"); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, "0"); - Log_OC.d("ExternalLinks", "update via api"); - RemoteOperation getExternalLinksOperation = new ExternalLinksOperation(); - RemoteOperationResult result = getExternalLinksOperation.execute(user, this); + Log_OC.d("ExternalLinks", "update via api"); + RemoteOperation getExternalLinksOperation = new ExternalLinksOperation(); + RemoteOperationResult result = getExternalLinksOperation.execute(user, this); - if (result.isSuccess() && result.getData() != null) { - externalLinksProvider.deleteAllExternalLinks(); - ArrayList externalLinks = (ArrayList) result.getData(); - for (ExternalLink link : externalLinks) { - externalLinksProvider.storeExternalLink(link); - } + if (result.isSuccess() && result.getData() != null) { + externalLinksProvider.deleteAllExternalLinks(); + ArrayList externalLinks = (ArrayList) result.getData(); + for (ExternalLink link : externalLinks) { + externalLinksProvider.storeExternalLink(link); } - } else { - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); } } else { - externalLinksProvider.deleteAllExternalLinks(); - Log_OC.d("ExternalLinks", "links disabled"); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); } - - runOnUiThread(this::updateExternalLinksInDrawer); + } else { + externalLinksProvider.deleteAllExternalLinks(); + Log_OC.d("ExternalLinks", "links disabled"); } + + runOnUiThread(this::updateExternalLinksInDrawer); }); t.start(); } From afb63961d1f92a21b913bdf73685fcf3450b37aa Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 23 Aug 2024 10:26:33 +0200 Subject: [PATCH 5/5] Remove code format Signed-off-by: alperozturk --- .../android/ui/activity/DrawerActivity.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 4721ba721b9b..42991b80590e 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -539,7 +539,7 @@ private void onNavigationItemClicked(final MenuItem menuItem) { } else if (itemId == R.id.nav_logout) { mCheckedMenuItem = -1; MenuItem isNewMenuItemChecked = menuItem.setChecked(false); - Log_OC.d(TAG, "onNavigationItemClicked nav_logout setChecked " + isNewMenuItemChecked); + Log_OC.d(TAG,"onNavigationItemClicked nav_logout setChecked " + isNewMenuItemChecked); final Optional optionalUser = getUser(); if (optionalUser.isPresent()) { UserInfoActivity.openAccountRemovalDialog(optionalUser.get(), getSupportFragmentManager()); @@ -1164,7 +1164,7 @@ public void showFiles(boolean onDeviceOnly, boolean onlyPersonalFiles) { public void avatarGenerated(Drawable avatarDrawable, Object callContext) { if (callContext instanceof MenuItem menuItem) { MenuItem newIcon = menuItem.setIcon(avatarDrawable); - Log_OC.d(TAG, "avatarGenerated new icon: " + newIcon); + Log_OC.d(TAG,"avatarGenerated new icon: " + newIcon); } else if (callContext instanceof ImageView imageView) { imageView.setImageDrawable(avatarDrawable); } else if (callContext instanceof MaterialButton materialButton) { @@ -1240,20 +1240,26 @@ public void fetchExternalLinks(final boolean force) { } Thread t = new Thread(() -> { - if ((getCapabilities() == null || getCapabilities().getAccountName() != null && getCapabilities().getAccountName().isEmpty()) && getStorageManager() != null) { + // fetch capabilities as early as possible + if ((getCapabilities() == null || getCapabilities().getAccountName() != null && getCapabilities().getAccountName().isEmpty()) + && getStorageManager() != null) { GetCapabilitiesOperation getCapabilities = new GetCapabilitiesOperation(getStorageManager()); getCapabilities.execute(getBaseContext()); } - if (getStorageManager() != null && CapabilityUtils.getCapability(user, this).getExternalLinks().isTrue()) { - int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT); + if (getStorageManager() != null && CapabilityUtils.getCapability(user, this) + .getExternalLinks().isTrue()) { + + int count = arbitraryDataProvider.getIntegerValue(FilesSyncHelper.GLOBAL, + FileActivity.APP_OPENED_COUNT); if (count > 10 || count == -1 || force) { if (force) { Log_OC.d("ExternalLinks", "force update"); } - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, "0"); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, + FileActivity.APP_OPENED_COUNT, "0"); Log_OC.d("ExternalLinks", "update via api"); RemoteOperation getExternalLinksOperation = new ExternalLinksOperation(); @@ -1261,19 +1267,21 @@ public void fetchExternalLinks(final boolean force) { if (result.isSuccess() && result.getData() != null) { externalLinksProvider.deleteAllExternalLinks(); - ArrayList externalLinks = (ArrayList) result.getData(); + + ArrayList externalLinks = (ArrayList) (Object) result.getData(); + for (ExternalLink link : externalLinks) { externalLinksProvider.storeExternalLink(link); } } } else { - arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); + arbitraryDataProvider.storeOrUpdateKeyValue(FilesSyncHelper.GLOBAL, + FileActivity.APP_OPENED_COUNT, String.valueOf(count + 1)); } } else { externalLinksProvider.deleteAllExternalLinks(); Log_OC.d("ExternalLinks", "links disabled"); } - runOnUiThread(this::updateExternalLinksInDrawer); }); t.start(); @@ -1281,9 +1289,7 @@ public void fetchExternalLinks(final boolean force) { protected void handleDeepLink(@NonNull Uri uri) { String path = uri.getLastPathSegment(); - if (path == null) { - return; - } + if (path == null) return; DeepLinkConstants deepLinkType = DeepLinkConstants.Companion.fromPath(path); if (deepLinkType == null) {