flutter_flutter/shell/platform/android/apk_asset_provider.cc
Chinmay Garde eec74e5c92
Rename the blink namespace to flutter. (#8517)
Some components in the Flutter engine were derived from the forked blink codebase. While the forked components have either been removed or rewritten, the use of the blink namespace has mostly (and inconsistently) remained. This renames the blink namesapce to flutter for consistency. There are no functional changes in this patch.
2019-04-09 12:44:42 -07:00

56 lines
1.4 KiB
C++

#include <unistd.h>
#include <algorithm>
#include <sstream>
#include "flutter/fml/logging.h"
#include "flutter/shell/platform/android/apk_asset_provider.h"
namespace flutter {
APKAssetProvider::APKAssetProvider(JNIEnv* env,
jobject jassetManager,
std::string directory)
: java_asset_manager_(env, jassetManager),
directory_(std::move(directory)) {
assetManager_ = AAssetManager_fromJava(env, jassetManager);
}
APKAssetProvider::~APKAssetProvider() = default;
bool APKAssetProvider::IsValid() const {
return true;
}
class APKAssetMapping : public fml::Mapping {
public:
APKAssetMapping(AAsset* asset) : asset_(asset) {}
~APKAssetMapping() override { AAsset_close(asset_); }
size_t GetSize() const override { return AAsset_getLength(asset_); }
const uint8_t* GetMapping() const override {
return reinterpret_cast<const uint8_t*>(AAsset_getBuffer(asset_));
}
private:
AAsset* const asset_;
FML_DISALLOW_COPY_AND_ASSIGN(APKAssetMapping);
};
std::unique_ptr<fml::Mapping> APKAssetProvider::GetAsMapping(
const std::string& asset_name) const {
std::stringstream ss;
ss << directory_.c_str() << "/" << asset_name;
AAsset* asset =
AAssetManager_open(assetManager_, ss.str().c_str(), AASSET_MODE_BUFFER);
if (!asset) {
return nullptr;
}
return std::make_unique<APKAssetMapping>(asset);
}
} // namespace flutter