summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBartlomiej Moskal <bartlomiej.moskal@qt.io>2022-11-21 17:28:52 +0100
committerBartlomiej Moskal <bartlomiej.moskal@qt.io>2023-01-02 08:42:14 +0100
commitaa7b2436b5a357ea7d76eebaecab0c05719b5c91 (patch)
tree593f975147454ae82b63af631e17a10ac8a21182
parent9e2eb42d63b56c3062ff1ab6d4d4381f471ccabf (diff)
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 <assam.boudjelthia@qt.io> (cherry picked from commit 4ceee3911a2ef567f614fc296475bc2b2a0e3add)
-rw-r--r--src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp9
-rw-r--r--tests/auto/other/android/testdata/top_level_dir/file_in_top_dir.txt1
-rw-r--r--tests/auto/other/android/testdata/top_level_dir/sub_dir/file_in_sub_dir.txt1
-rw-r--r--tests/auto/other/android/testdata/top_level_dir/sub_dir/sub_dir2/sub_dir3/file_in_sub_dir_3.txt1
-rw-r--r--tests/auto/other/android/tst_android.cpp21
5 files changed, 32 insertions, 1 deletions
diff --git a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp
index 14fab3dce7..301eab8b10 100644
--- a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp
+++ b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp
@@ -140,6 +140,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.
QJNIObjectPrivate files = QJNIObjectPrivate::callStaticObjectMethod(QtAndroid::applicationClass(),
"listAssetContent",
"(Landroid/content/res/AssetManager;Ljava/lang/String;)[Ljava/lang/String;",
@@ -390,8 +392,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);
}
}
diff --git a/tests/auto/other/android/testdata/top_level_dir/file_in_top_dir.txt b/tests/auto/other/android/testdata/top_level_dir/file_in_top_dir.txt
new file mode 100644
index 0000000000..87b10bd8e6
--- /dev/null
+++ b/tests/auto/other/android/testdata/top_level_dir/file_in_top_dir.txt
@@ -0,0 +1 @@
+FooBar
diff --git a/tests/auto/other/android/testdata/top_level_dir/sub_dir/file_in_sub_dir.txt b/tests/auto/other/android/testdata/top_level_dir/sub_dir/file_in_sub_dir.txt
new file mode 100644
index 0000000000..87b10bd8e6
--- /dev/null
+++ b/tests/auto/other/android/testdata/top_level_dir/sub_dir/file_in_sub_dir.txt
@@ -0,0 +1 @@
+FooBar
diff --git a/tests/auto/other/android/testdata/top_level_dir/sub_dir/sub_dir2/sub_dir3/file_in_sub_dir_3.txt b/tests/auto/other/android/testdata/top_level_dir/sub_dir/sub_dir2/sub_dir3/file_in_sub_dir_3.txt
new file mode 100644
index 0000000000..87b10bd8e6
--- /dev/null
+++ b/tests/auto/other/android/testdata/top_level_dir/sub_dir/sub_dir2/sub_dir3/file_in_sub_dir_3.txt
@@ -0,0 +1 @@
+FooBar
diff --git a/tests/auto/other/android/tst_android.cpp b/tests/auto/other/android/tst_android.cpp
index 57d592d45f..389d465427 100644
--- a/tests/auto/other/android/tst_android.cpp
+++ b/tests/auto/other/android/tst_android.cpp
@@ -27,6 +27,7 @@
****************************************************************************/
#include <QtTest/QtTest>
+#include <QtCore/qdiriterator.h>
class tst_Android : public QObject
{
@@ -34,6 +35,7 @@ Q_OBJECT
private slots:
void assetsRead();
void assetsNotWritable();
+ void assetsIterating();
};
void tst_Android::assetsRead()
@@ -59,6 +61,25 @@ void tst_Android::assetsNotWritable()
QVERIFY(!file.open(QIODevice::Append));
}
+void tst_Android::assetsIterating()
+{
+ QStringList assets = {"assets:/top_level_dir/file_in_top_dir.txt",
+ "assets:/top_level_dir/sub_dir",
+ "assets:/top_level_dir/sub_dir/file_in_sub_dir.txt",
+ "assets:/top_level_dir/sub_dir/sub_dir_2",
+ "assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3",
+ "assets:/top_level_dir/sub_dir/sub_dir_2/sub_dir_3/file_in_sub_dir_3.txt"};
+ // Note that we have an "assets:/top_level_dir/sub_dir/empty_sub_dir" in the test's
+ // assets physical directory, but empty folders are not packaged in the built apk,
+ // so it's expected to not have such folder be listed in the assets on runtime
+ QDirIterator it("assets:/top_level_dir", QDirIterator::Subdirectories);
+ QStringList iteratorAssets;
+ while (it.hasNext())
+ iteratorAssets.append(it.next());
+ QVERIFY(assets == iteratorAssets);
+}
+
+
QTEST_MAIN(tst_Android)
#include "tst_android.moc"