From 085138bc2dea45b285cc940800e3d44759818725 Mon Sep 17 00:00:00 2001 From: Bartlomiej Moskal Date: Mon, 21 Nov 2022 17:28:52 +0100 Subject: Android: fix Android assets handler not listing dirs with only sub dirs It looks like AAssetDir_getNextFileName is not enough. Directories that contain only other directories (no files) were not listed. On the other hand, AAssetManager_openDir() will always return a pointer to initialized object (even if the specified directory does not exists), so we can't just leave only it here. Using FolderIterator as a last resort. This approach should not be too time consuming. As part of this fix, add some unit tests to cover/ensure assets listing/iterating works as expected. Fixes: QTBUG-107627 Change-Id: Id375fe8f99f4ca3f8cad4756f783ffafe5c074df Reviewed-by: Assam Boudjelthia (cherry picked from commit 4ceee3911a2ef567f614fc296475bc2b2a0e3add) Reviewed-by: Qt Cherry-pick Bot --- .../platforms/android/qandroidassetsfileenginehandler.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/plugins') diff --git a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp index e007088a22..43cc36d222 100644 --- a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp +++ b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp @@ -108,6 +108,8 @@ public: FolderIterator(const QString &path) : m_path(path) { + // Note that empty dirs in the assets dir before the build are not going to be + // included in the final apk, so no empty folders should expected to be listed. QJniObject files = QJniObject::callStaticObjectMethod(QtAndroid::applicationClass(), "listAssetContent", "(Landroid/content/res/AssetManager;Ljava/lang/String;)[Ljava/lang/String;", @@ -350,8 +352,13 @@ public: } else { auto *assetDir = AAssetManager_openDir(m_assetManager, m_fileName.toUtf8()); if (assetDir) { - if (AAssetDir_getNextFileName(assetDir)) + if (AAssetDir_getNextFileName(assetDir) + || (!FolderIterator::fromCache(m_fileName, false)->empty())) { + // If AAssetDir_getNextFileName is not valid, it still can be a directory that + // contains only other directories (no files). FolderIterator will not be called + // on the directory containing files so it should not be too time consuming now. m_assetInfo->type = AssetItem::Type::Folder; + } AAssetDir_close(assetDir); } } -- cgit v1.2.1