OSDN Git Service

Revert "AssetManager2: Fix list function"
authorAdam Lesinski <adamlesinski@google.com>
Fri, 9 Feb 2018 19:01:26 +0000 (11:01 -0800)
committerAdam Lesinski <adamlesinski@google.com>
Fri, 9 Feb 2018 20:43:04 +0000 (12:43 -0800)
This reverts commit adc0b87ec235a71d722fb8d6aad50ceaeac8c6d5.

Bug:73134570
Change-Id: I9e652245e7661eb7a34dadb5f363a08bc8c9e57e

13 files changed:
core/jni/android_util_AssetManager.cpp
libs/androidfw/ApkAssets.cpp
libs/androidfw/tests/AssetManager2_test.cpp
libs/androidfw/tests/data/app/app.apk
libs/androidfw/tests/data/app/assets/app_file.txt [deleted file]
libs/androidfw/tests/data/app/assets/file.txt [deleted file]
libs/androidfw/tests/data/app/build
libs/androidfw/tests/data/system/assets/file.txt [deleted file]
libs/androidfw/tests/data/system/assets/subdir/subdir_file.txt [deleted file]
libs/androidfw/tests/data/system/build
libs/androidfw/tests/data/system/res/values-sv/values.xml
libs/androidfw/tests/data/system/res/values/themes.xml
libs/androidfw/tests/data/system/system.apk

index 2334e03..c623ca6 100644 (file)
@@ -403,30 +403,38 @@ static jobjectArray NativeList(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring
     return nullptr;
   }
 
-  ScopedLock<AssetManager2> assetmanager(AssetManagerFromLong(ptr));
-  std::unique_ptr<AssetDir> asset_dir =
-      assetmanager->OpenDir(path_utf8.c_str());
-  if (asset_dir == nullptr) {
-    jniThrowException(env, "java/io/FileNotFoundException", path_utf8.c_str());
-    return nullptr;
+  std::vector<std::string> all_file_paths;
+  {
+    StringPiece normalized_path = path_utf8.c_str();
+    if (normalized_path.data()[0] == '/') {
+      normalized_path = normalized_path.substr(1);
+    }
+    std::string root_path = StringPrintf("assets/%s", normalized_path.data());
+    ScopedLock<AssetManager2> assetmanager(AssetManagerFromLong(ptr));
+    for (const ApkAssets* assets : assetmanager->GetApkAssets()) {
+      assets->ForEachFile(root_path, [&](const StringPiece& file_path, FileType type) {
+        if (type == FileType::kFileTypeRegular) {
+          all_file_paths.push_back(file_path.to_string());
+        }
+      });
+    }
   }
 
-  const size_t file_count = asset_dir->getFileCount();
-
-  jobjectArray array = env->NewObjectArray(file_count, g_stringClass, nullptr);
+  jobjectArray array = env->NewObjectArray(all_file_paths.size(), g_stringClass, nullptr);
   if (array == nullptr) {
     return nullptr;
   }
 
-  for (size_t i = 0; i < file_count; i++) {
-    jstring java_string = env->NewStringUTF(asset_dir->getFileName(i).string());
+  jsize index = 0;
+  for (const std::string& file_path : all_file_paths) {
+    jstring java_string = env->NewStringUTF(file_path.c_str());
 
     // Check for errors creating the strings (if malformed or no memory).
     if (env->ExceptionCheck()) {
      return nullptr;
     }
 
-    env->SetObjectArrayElement(array, i, java_string);
+    env->SetObjectArrayElement(array, index++, java_string);
 
     // If we have a large amount of string in our array, we might overflow the
     // local reference table of the VM.
index 60f8a18..da0205d 100644 (file)
@@ -231,16 +231,12 @@ bool ApkAssets::ForEachFile(const std::string& root_path,
   while ((result = ::Next(cookie, &entry, &name)) == 0) {
     StringPiece full_file_path(reinterpret_cast<const char*>(name.name), name.name_length);
     StringPiece leaf_file_path = full_file_path.substr(root_path_full.size());
-
-    if (!leaf_file_path.empty()) {
-      auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
-      if (iter != leaf_file_path.end()) {
-        std::string dir =
-            leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)).to_string();
-        dirs.insert(std::move(dir));
-      } else {
-        f(leaf_file_path, kFileTypeRegular);
-      }
+    auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
+    if (iter != leaf_file_path.end()) {
+      dirs.insert(
+          leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)).to_string());
+    } else if (!leaf_file_path.empty()) {
+      f(leaf_file_path, kFileTypeRegular);
     }
   }
   ::EndIteration(cookie);
index 7cac2b3..eaf79cb 100644 (file)
@@ -36,10 +36,6 @@ namespace lib_one = com::android::lib_one;
 namespace lib_two = com::android::lib_two;
 namespace libclient = com::android::libclient;
 
-using ::testing::Eq;
-using ::testing::NotNull;
-using ::testing::StrEq;
-
 namespace android {
 
 class AssetManager2Test : public ::testing::Test {
@@ -68,9 +64,6 @@ class AssetManager2Test : public ::testing::Test {
 
     system_assets_ = ApkAssets::Load(GetTestDataPath() + "/system/system.apk", true /*system*/);
     ASSERT_NE(nullptr, system_assets_);
-
-    app_assets_ = ApkAssets::Load(GetTestDataPath() + "/app/app.apk");
-    ASSERT_THAT(app_assets_, NotNull());
   }
 
  protected:
@@ -82,7 +75,6 @@ class AssetManager2Test : public ::testing::Test {
   std::unique_ptr<const ApkAssets> libclient_assets_;
   std::unique_ptr<const ApkAssets> appaslib_assets_;
   std::unique_ptr<const ApkAssets> system_assets_;
-  std::unique_ptr<const ApkAssets> app_assets_;
 };
 
 TEST_F(AssetManager2Test, FindsResourceFromSingleApkAssets) {
@@ -473,68 +465,8 @@ TEST_F(AssetManager2Test, GetResourceId) {
             assetmanager.GetResourceId("main", "layout", "com.android.basic"));
 }
 
-TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {
-  AssetManager2 assetmanager;
-  assetmanager.SetApkAssets({system_assets_.get()});
-
-  std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
-  ASSERT_THAT(asset, NotNull());
-
-  const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
-  ASSERT_THAT(data, NotNull());
-  EXPECT_THAT(std::string(data, asset->getLength()), StrEq("file\n"));
-}
-
-TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {
-  AssetManager2 assetmanager;
-  assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
-
-  std::unique_ptr<Asset> asset = assetmanager.Open("file.txt", Asset::ACCESS_BUFFER);
-  ASSERT_THAT(asset, NotNull());
-
-  const char* data = reinterpret_cast<const char*>(asset->getBuffer(false /*wordAligned*/));
-  ASSERT_THAT(data, NotNull());
-  EXPECT_THAT(std::string(data, asset->getLength()), StrEq("app override file\n"));
-}
-
-TEST_F(AssetManager2Test, OpenDir) {
-  AssetManager2 assetmanager;
-  assetmanager.SetApkAssets({system_assets_.get()});
+TEST_F(AssetManager2Test, OpensFileFromSingleApkAssets) {}
 
-  std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
-  ASSERT_THAT(asset_dir, NotNull());
-  ASSERT_THAT(asset_dir->getFileCount(), Eq(2u));
-
-  EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("file.txt")));
-  EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
-
-  EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("subdir")));
-  EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeDirectory));
-
-  asset_dir = assetmanager.OpenDir("subdir");
-  ASSERT_THAT(asset_dir, NotNull());
-  ASSERT_THAT(asset_dir->getFileCount(), Eq(1u));
-
-  EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("subdir_file.txt")));
-  EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
-}
-
-TEST_F(AssetManager2Test, OpenDirFromManyApks) {
-  AssetManager2 assetmanager;
-  assetmanager.SetApkAssets({system_assets_.get(), app_assets_.get()});
-
-  std::unique_ptr<AssetDir> asset_dir = assetmanager.OpenDir("");
-  ASSERT_THAT(asset_dir, NotNull());
-  ASSERT_THAT(asset_dir->getFileCount(), Eq(3u));
-
-  EXPECT_THAT(asset_dir->getFileName(0), Eq(String8("app_file.txt")));
-  EXPECT_THAT(asset_dir->getFileType(0), Eq(FileType::kFileTypeRegular));
-
-  EXPECT_THAT(asset_dir->getFileName(1), Eq(String8("file.txt")));
-  EXPECT_THAT(asset_dir->getFileType(1), Eq(FileType::kFileTypeRegular));
-
-  EXPECT_THAT(asset_dir->getFileName(2), Eq(String8("subdir")));
-  EXPECT_THAT(asset_dir->getFileType(2), Eq(FileType::kFileTypeDirectory));
-}
+TEST_F(AssetManager2Test, OpensFileFromMultipleApkAssets) {}
 
 }  // namespace android
index c8ad86d..ccb0824 100644 (file)
Binary files a/libs/androidfw/tests/data/app/app.apk and b/libs/androidfw/tests/data/app/app.apk differ
diff --git a/libs/androidfw/tests/data/app/assets/app_file.txt b/libs/androidfw/tests/data/app/assets/app_file.txt
deleted file mode 100644 (file)
index b214e06..0000000
+++ /dev/null
@@ -1 +0,0 @@
-app file
diff --git a/libs/androidfw/tests/data/app/assets/file.txt b/libs/androidfw/tests/data/app/assets/file.txt
deleted file mode 100644 (file)
index 0811542..0000000
+++ /dev/null
@@ -1 +0,0 @@
-app override file
index 09af842..d418158 100755 (executable)
 
 set -e
 
-aapt2 compile --dir res -o compiled.flata
-aapt2 link \
-    --manifest AndroidManifest.xml \
-    -I ../system/system.apk \
-    -A assets \
-    -o app.apk \
-    compiled.flata
-rm compiled.flata
+aapt package -I ../system/system.apk -M AndroidManifest.xml -S res -F app.apk -f
diff --git a/libs/androidfw/tests/data/system/assets/file.txt b/libs/androidfw/tests/data/system/assets/file.txt
deleted file mode 100644 (file)
index f73f309..0000000
+++ /dev/null
@@ -1 +0,0 @@
-file
diff --git a/libs/androidfw/tests/data/system/assets/subdir/subdir_file.txt b/libs/androidfw/tests/data/system/assets/subdir/subdir_file.txt
deleted file mode 100644 (file)
index 3f74eb6..0000000
+++ /dev/null
@@ -1 +0,0 @@
-subdir file
index b65145a..bfbdf4c 100755 (executable)
@@ -17,6 +17,4 @@
 
 set -e
 
-aapt2 compile --dir res -o compiled.flata
-aapt2 link --manifest AndroidManifest.xml -A assets -o system.apk compiled.flata
-rm compiled.flata
+aapt package -x -M AndroidManifest.xml -S res -F system.apk -f
index 5f60d21..b97bdb6 100644 (file)
@@ -15,5 +15,6 @@
 -->
 
 <resources>
+    <public type="integer" name="number" id="0x01030000" />
     <integer name="number">1</integer>
 </resources>
index 7893c94..35d43c7 100644 (file)
@@ -18,7 +18,6 @@
     <public name="background" type="attr" id="0x01010000"/>
     <public name="foreground" type="attr" id="0x01010001"/>
     <public name="Theme.One" type="style" id="0x01020000"/>
-    <public type="integer" name="number" id="0x01030000" />
 
     <attr name="background" format="color|reference"/>
     <attr name="foreground" format="color|reference"/>
index 9045d6c..1299016 100644 (file)
Binary files a/libs/androidfw/tests/data/system/system.apk and b/libs/androidfw/tests/data/system/system.apk differ