Flutter Engine
apk_asset_provider.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/android/apk_asset_provider.h"
6 
7 #include <unistd.h>
8 
9 #include <algorithm>
10 #include <sstream>
11 
12 #include "flutter/fml/logging.h"
13 
14 namespace flutter {
15 
17  jobject jassetManager,
18  std::string directory)
19  : java_asset_manager_(env, jassetManager),
20  directory_(std::move(directory)) {
21  assetManager_ = AAssetManager_fromJava(env, jassetManager);
22 }
23 
25 
26 bool APKAssetProvider::IsValid() const {
27  return true;
28 }
29 
30 bool APKAssetProvider::IsValidAfterAssetManagerChange() const {
31  return true;
32 }
33 
34 // |AssetResolver|
35 AssetResolver::AssetResolverType APKAssetProvider::GetType() const {
36  return AssetResolver::AssetResolverType::kApkAssetProvider;
37 }
38 
39 class APKAssetMapping : public fml::Mapping {
40  public:
41  APKAssetMapping(AAsset* asset) : asset_(asset) {}
42 
43  ~APKAssetMapping() override { AAsset_close(asset_); }
44 
45  size_t GetSize() const override { return AAsset_getLength(asset_); }
46 
47  const uint8_t* GetMapping() const override {
48  return reinterpret_cast<const uint8_t*>(AAsset_getBuffer(asset_));
49  }
50 
51  private:
52  AAsset* const asset_;
53 
55 };
56 
57 std::unique_ptr<fml::Mapping> APKAssetProvider::GetAsMapping(
58  const std::string& asset_name) const {
59  std::stringstream ss;
60  ss << directory_.c_str() << "/" << asset_name;
61  AAsset* asset =
62  AAssetManager_open(assetManager_, ss.str().c_str(), AASSET_MODE_BUFFER);
63  if (!asset) {
64  return nullptr;
65  }
66 
67  return std::make_unique<APKAssetMapping>(asset);
68 }
69 
70 } // namespace flutter
const uint8_t * GetMapping() const override
Definition: ref_ptr.h:252
AssetResolverType
Identifies the type of AssetResolver an instance is.
size_t GetSize() const override
APKAssetProvider(JNIEnv *env, jobject assetManager, std::string directory)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27