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 class APKAssetMapping : public fml::Mapping {
35  public:
36  APKAssetMapping(AAsset* asset) : asset_(asset) {}
37 
38  ~APKAssetMapping() override { AAsset_close(asset_); }
39 
40  size_t GetSize() const override { return AAsset_getLength(asset_); }
41 
42  const uint8_t* GetMapping() const override {
43  return reinterpret_cast<const uint8_t*>(AAsset_getBuffer(asset_));
44  }
45 
46  private:
47  AAsset* const asset_;
48 
50 };
51 
52 std::unique_ptr<fml::Mapping> APKAssetProvider::GetAsMapping(
53  const std::string& asset_name) const {
54  std::stringstream ss;
55  ss << directory_.c_str() << "/" << asset_name;
56  AAsset* asset =
57  AAssetManager_open(assetManager_, ss.str().c_str(), AASSET_MODE_BUFFER);
58  if (!asset) {
59  return nullptr;
60  }
61 
62  return std::make_unique<APKAssetMapping>(asset);
63 }
64 
65 } // namespace flutter
const uint8_t * GetMapping() const override
Definition: ref_ptr.h:252
size_t GetSize() const override
APKAssetProvider(JNIEnv *env, jobject assetManager, std::string directory)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27