Ocean
Loading...
Searching...
No Matches
android/ResourceManager.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_PLATFORM_ANDROID_RESOURCE_MANAGER_H
9#define META_OCEAN_PLATFORM_ANDROID_RESOURCE_MANAGER_H
10
12
13#include "ocean/base/Lock.h"
15
16#include <android/asset_manager.h>
17#include <android/asset_manager_jni.h>
18
19namespace Ocean
20{
21
22namespace Platform
23{
24
25namespace Android
26{
27
28/**
29 * This class implements a manager for resource data.
30 * @ingroup platformandroid
31 */
32class ResourceManager : public Singleton<ResourceManager>
33{
34 friend class Singleton<ResourceManager>;
35
36 public:
37
38 /**
39 * Definition of types of external directories.
40 */
41 enum class ExternalDirectoryType : uint32_t
42 {
43 /// The default type of external directory.
44 DEFAULT_TYPE = 0u,
45 /// Indicator for external directories that hold music.
46 MUSIC,
47 /// Indicator for external directories that hold podcasts.
49 /// Indicator for external directories that hold ringtones.
51 /// Indicator for external directories that hold alarm data.
52 ALARMS,
53 /// Indicator for external directories that hold notification data.
55 /// Indicator for external directories that hold pictures.
57 /// Indicator for external directories that hold movies
58 MOVIES,
59 };
60
61 /**
62 * This class provides access to the memory of an asset file as long as the object exists.
63 */
65 {
66 friend class ResourceManager;
67
68 public:
69
70 /**
71 * Destructs the object and releases all resources.
72 */
73 inline ~ScopedResource();
74
75 /**
76 * Returns the memory pointer to the content of the asset file.
77 * @return The asset file's memory pointer
78 */
79 inline const void* data() const;
80
81 /**
82 * Returns the size of the content of the asset file in bytes.
83 * @return The asset file's memory size in bytes
84 */
85 inline size_t size() const;
86
87 protected:
88
89 /**
90 * Disabled copy constructor.
91 * @param scopedResource The object which would be copied
92 */
93 ScopedResource(const ScopedResource& scopedResource) = delete;
94
95 /**
96 * Creates a new object.
97 * @param asset The asset handle, must be valid
98 * @param data The pointer to the asset's memory, must be valid
99 * @param size the size of the asset in bytes, with range [1, infinity)
100 */
101 inline ScopedResource(AAsset* asset, const void* data, const size_t size);
102
103 /**
104 * Disabled assign operator.
105 * @param scopedResource The object which would be assigned
106 * @return The reference to this object
107 */
108 ScopedResource& operator=(const ScopedResource& scopedResource) = delete;
109
110 protected:
111
112 /// The asset handle.
113 AAsset* const asset_ = nullptr;
114
115 /// The pointer to the asset's memory.
116 const void* const data_ = nullptr;
117
118 /// The size of the asset in bytes, with range [1, infinity)
119 const size_t size_ = 0;
120 };
121
122 public:
123
124 /**
125 * Initializes the singleton using the Java virtual machine and Android activity
126 * @param javaVM The Java virtual machine, must be valid
127 * @param activity The Android main activity, must be valid
128 * @param assetManager The optional Android asset manager to be used; if not specified, it will be retrieved from the main activity
129 * @return True, if succeeded
130 */
131 bool initialize(JavaVM* javaVM, jobject activity, AAssetManager* assetManager = nullptr);
132
133 /**
134 * Reads an asset file to memory.
135 * @param assetFilename The filename of the asset to read, must be valid
136 * @param data The resulting memory with the read asset file, may be empty if the asset is empty
137 * @return True, if succeeded
138 */
139 bool readAsset(const std::string& assetFilename, std::vector<uint8_t>& data) const;
140
141 /**
142 * Accesses an asset file and returns a resource object providing the memory pointer to the asset.
143 * @param assetFilename The filename of the asset to read, must be valid
144 * @return The resource object, nullptr if invalid
145 */
146 std::unique_ptr<ScopedResource> accessAsset(const std::string& assetFilename) const;
147
148 /**
149 * Copies all assets located in a specified asset folder to a specified target location.
150 * @param targetDirectoryName The directory where the assets will be copied to; must be valid and directory must be created before calling this function
151 * @param createDirectory If true it will allow the function to create the destination directory, if it doesn't exist already.
152 * @param assetDirectoryName The name of the asset directory from which all files will be copied, an empty name to use the root asset directory
153 * @return True if succeeded
154 */
155 bool copyAssets(const std::string& targetDirectoryName, const bool createDirectory = false, const std::string& assetDirectoryName = std::string()) const;
156
157 /**
158 * Returns whether a specific asset directory exists and whether this directory holds at least one file.
159 * @param assetDirectoryName The name of the asset directory to be checked, can be any sub-directory, must be valid
160 * @return True, if so
161 */
162 bool doesAssetDirectoryExist(const std::string& assetDirectoryName) const;
163
164 /**
165 * Returns an app-specific external directory to store data
166 * Make sure to initialize the manager with a valid Java VM and the object of the main activity.
167 * @return The directory; if successful; this will be an empty string without prior initialization
168 * @sa initialize()
169 */
170 inline std::string externalFilesDirectory() const;
171
172 /**
173 * Returns true if this object is initialized
174 * @return True, if correctly initialized
175 */
176 inline bool isValid() const;
177
178 /**
179 * Returns an app-specific external directory to store data
180 * @param env The JNI environment of this application, must be valid
181 * @param activity The Android main activity, must be valid
182 * @param externalDirectoryName The resulting name of the app-specific external directory, will be terminated with the default separator of the current platform
183 * @param externalDirectoryType The type of the external directory to be returned
184 * @return True, if the directory exists and if it is readable and writable, otherwise false
185 * @sa IO::Path::defaultSeparator()
186 */
187 static bool getExternalFilesDirectory(JNIEnv* env, jobject activity, std::string& externalDirectoryName, const ExternalDirectoryType externalDirectoryType = ExternalDirectoryType::DEFAULT_TYPE);
188
189 protected:
190
191 /**
192 * Creates a new manager object.
193 */
195
196 protected:
197
198 // The Android asset manager which is used.
199 AAssetManager* assetManager_ = nullptr;
200
201 /// The lock of the manager.
202 mutable Lock lock_;
203
204 /// The name of the app-specific external directory.
206};
207
208inline ResourceManager::ScopedResource::ScopedResource(AAsset* asset, const void* data, const size_t size) :
209 asset_(asset),
210 data_(data),
211 size_(size)
212{
213 ocean_assert(asset_ != nullptr && data_ != nullptr && size_ != 0);
214}
215
217{
218 AAsset_close(asset_);
219}
220
222{
223 return data_;
224}
225
227{
228 return size_;
229}
230
231inline bool ResourceManager::isValid() const
232{
233 const ScopedLock scopedLock(lock_);
234 return assetManager_ != nullptr;
235}
236
238{
239 const ScopedLock scopedLock(lock_);
240
241 ocean_assert(!externalDirectoryName_.empty());
243}
244
245}
246
247}
248
249}
250
251#endif // META_OCEAN_PLATFORM_ANDROID_RESOURCE_MANAGER_H
This class implements a recursive lock object.
Definition Lock.h:31
This class provides access to the memory of an asset file as long as the object exists.
Definition android/ResourceManager.h:65
~ScopedResource()
Destructs the object and releases all resources.
Definition android/ResourceManager.h:216
ScopedResource & operator=(const ScopedResource &scopedResource)=delete
Disabled assign operator.
ScopedResource(const ScopedResource &scopedResource)=delete
Disabled copy constructor.
const void * data() const
Returns the memory pointer to the content of the asset file.
Definition android/ResourceManager.h:221
size_t size() const
Returns the size of the content of the asset file in bytes.
Definition android/ResourceManager.h:226
const size_t size_
The size of the asset in bytes, with range [1, infinity)
Definition android/ResourceManager.h:119
AAsset *const asset_
The asset handle.
Definition android/ResourceManager.h:113
const void *const data_
The pointer to the asset's memory.
Definition android/ResourceManager.h:116
This class implements a manager for resource data.
Definition android/ResourceManager.h:33
bool initialize(JavaVM *javaVM, jobject activity, AAssetManager *assetManager=nullptr)
Initializes the singleton using the Java virtual machine and Android activity.
bool copyAssets(const std::string &targetDirectoryName, const bool createDirectory=false, const std::string &assetDirectoryName=std::string()) const
Copies all assets located in a specified asset folder to a specified target location.
static bool getExternalFilesDirectory(JNIEnv *env, jobject activity, std::string &externalDirectoryName, const ExternalDirectoryType externalDirectoryType=ExternalDirectoryType::DEFAULT_TYPE)
Returns an app-specific external directory to store data.
AAssetManager * assetManager_
Definition android/ResourceManager.h:199
std::unique_ptr< ScopedResource > accessAsset(const std::string &assetFilename) const
Accesses an asset file and returns a resource object providing the memory pointer to the asset.
bool isValid() const
Returns true if this object is initialized.
Definition android/ResourceManager.h:231
Lock lock_
The lock of the manager.
Definition android/ResourceManager.h:202
bool doesAssetDirectoryExist(const std::string &assetDirectoryName) const
Returns whether a specific asset directory exists and whether this directory holds at least one file.
bool readAsset(const std::string &assetFilename, std::vector< uint8_t > &data) const
Reads an asset file to memory.
std::string externalDirectoryName_
The name of the app-specific external directory.
Definition android/ResourceManager.h:205
std::string externalFilesDirectory() const
Returns an app-specific external directory to store data Make sure to initialize the manager with a v...
Definition android/ResourceManager.h:237
ExternalDirectoryType
Definition of types of external directories.
Definition android/ResourceManager.h:42
@ PODCASTS
Indicator for external directories that hold podcasts.
@ NOTIFICATIONS
Indicator for external directories that hold notification data.
@ PICTURES
Indicator for external directories that hold pictures.
@ MOVIES
Indicator for external directories that hold movies.
@ RINGTONES
Indicator for external directories that hold ringtones.
@ MUSIC
Indicator for external directories that hold music.
@ ALARMS
Indicator for external directories that hold alarm data.
@ DEFAULT_TYPE
The default type of external directory.
ResourceManager()
Creates a new manager object.
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
This template class is the base class for all singleton objects.
Definition Singleton.h:71
The namespace covering the entire Ocean framework.
Definition Accessor.h:15