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 /**
123 * This class provides access to the file descriptor of an asset file as long as the object exists.
124 */
126 {
127 friend class ResourceManager;
128
129 public:
130
131 /**
132 * Default constructor creating an invalid object.
133 */
134 ScopedFile() = default;
135
136 /**
137 * Move constructor.
138 * @param scopedFile The file object to be moved
139 */
140 inline ScopedFile(ScopedFile&& scopedFile);
141
142 /**
143 * Destructs the object and releases the file descriptor.
144 */
145 inline ~ScopedFile();
146
147 /**
148 * Returns the file descriptor of the asset file.
149 * @return The object's file descriptor, -1 if the object is invalid
150 * @see isValid()
151 */
152 inline int fileDescriptor() const;
153
154 /**
155 * Returns the offset of the asset file.
156 * @return The object's file offset
157 * @see isValid()
158 */
159 inline off64_t offset() const;
160
161 /**
162 * Returns the size of the asset file.
163 * @return The object's file size
164 * @see isValid()
165 */
166 inline off64_t size() const;
167
168 /**
169 * Explicitly releases the file descriptor.
170 */
171 inline void release();
172
173 /**
174 * Returns whether the object is valid.
175 * @return True, if so
176 */
177 inline bool isValid() const;
178
179 /**
180 * Move operator.
181 * @param scopedFile The file object to be moved
182 * @return Reference to this object
183 */
184 inline ScopedFile& operator=(ScopedFile&& scopedFile);
185
186 /**
187 * Returns whether the object is valid.
188 * @return True, if so
189 */
190 inline operator bool() const;
191
192 protected:
193
194 /**
195 * Creates a new object with valid file descriptor.
196 * @param asset The asset handle, must be valid
197 * @param fileDescriptor The file descriptor of the asset, must be valid
198 * @param offset The offset of the asset, with range [0, infinity)
199 * @param size The size of the asset, with range [1, infinity)
200 */
201 inline ScopedFile(AAsset* asset, const int fileDescriptor, const off64_t offset, const off64_t size);
202
203 protected:
204
205 /// The asset handle.
206 AAsset* asset_ = nullptr;
207
208 /// The file descriptor of the asset.
210
211 /// The offset of the asset.
212 off64_t offset_ = 0;
213
214 /// The size of the asset.
215 off64_t size_ = 0;
216 };
217
218 public:
219
220 /**
221 * Initializes the singleton using the Java virtual machine and Android activity
222 * @param javaVM The Java virtual machine, must be valid
223 * @param activity The Android main activity, must be valid
224 * @param assetManager The optional Android asset manager to be used; if not specified, it will be retrieved from the main activity
225 * @return True, if succeeded
226 */
227 bool initialize(JavaVM* javaVM, jobject activity, AAssetManager* assetManager = nullptr);
228
229 /**
230 * Reads an asset file to memory.
231 * @param assetFilename The filename of the asset to read, must be valid
232 * @param data The resulting memory with the read asset file, may be empty if the asset is empty
233 * @return True, if succeeded
234 */
235 bool readAsset(const std::string& assetFilename, std::vector<uint8_t>& data) const;
236
237 /**
238 * Accesses an asset file and returns a resource object providing the memory pointer to the asset.
239 * @param assetFilename The filename of the asset to read, must be valid
240 * @return The resource object, nullptr if invalid
241 */
242 std::unique_ptr<ScopedResource> accessAsset(const std::string& assetFilename) const;
243
244 /**
245 * Opens an asset file and returns a file object providing access to the file descriptor of the asset.
246 * @param assetFilename The filename of the asset to open, must be valid
247 * @return The file object, invalid if the asset file could not be opened
248 */
249 ScopedFile openAsset(const std::string& assetFilename) const;
250
251 /**
252 * Copies all assets located in a specified asset folder to a specified target location.
253 * @param targetDirectoryName The directory where the assets will be copied to; must be valid and directory must be created before calling this function
254 * @param createDirectory If true it will allow the function to create the destination directory, if it doesn't exist already.
255 * @param assetDirectoryName The name of the asset directory from which all files will be copied, an empty name to use the root asset directory
256 * @return True if succeeded
257 */
258 bool copyAssets(const std::string& targetDirectoryName, const bool createDirectory = false, const std::string& assetDirectoryName = std::string()) const;
259
260 /**
261 * Returns whether a specific asset directory exists and whether this directory holds at least one file.
262 * @param assetDirectoryName The name of the asset directory to be checked, can be any sub-directory, must be valid
263 * @return True, if so
264 */
265 bool doesAssetDirectoryExist(const std::string& assetDirectoryName) const;
266
267 /**
268 * Returns an app-specific external directory to store data
269 * Make sure to initialize the manager with a valid Java VM and the object of the main activity.
270 * @return The directory; if successful; this will be an empty string without prior initialization
271 * @sa initialize()
272 */
273 inline std::string externalFilesDirectory() const;
274
275 /**
276 * Returns true if this object is initialized
277 * @return True, if correctly initialized
278 */
279 inline bool isValid() const;
280
281 /**
282 * Returns an app-specific external directory to store data
283 * @param env The JNI environment of this application, must be valid
284 * @param activity The Android main activity, must be valid
285 * @param externalDirectoryName The resulting name of the app-specific external directory, will be terminated with the default separator of the current platform
286 * @param externalDirectoryType The type of the external directory to be returned
287 * @return True, if the directory exists and if it is readable and writable, otherwise false
288 * @sa IO::Path::defaultSeparator()
289 */
290 static bool getExternalFilesDirectory(JNIEnv* env, jobject activity, std::string& externalDirectoryName, const ExternalDirectoryType externalDirectoryType = ExternalDirectoryType::DEFAULT_TYPE);
291
292 protected:
293
294 /**
295 * Creates a new manager object.
296 */
298
299 protected:
300
301 // The Android asset manager which is used.
302 AAssetManager* assetManager_ = nullptr;
303
304 /// The lock of the manager.
305 mutable Lock lock_;
306
307 /// The name of the app-specific external directory.
309};
310
311inline ResourceManager::ScopedResource::ScopedResource(AAsset* asset, const void* data, const size_t size) :
312 asset_(asset),
313 data_(data),
314 size_(size)
315{
316 ocean_assert(asset_ != nullptr && data_ != nullptr && size_ != 0);
317}
318
320{
321 AAsset_close(asset_);
322}
323
325{
326 return data_;
327}
328
330{
331 return size_;
332}
333
334inline ResourceManager::ScopedFile::ScopedFile(AAsset* asset, const int fileDescriptor, const off64_t offset, const off64_t size) :
335 asset_(asset),
336 fileDescriptor_(fileDescriptor),
337 offset_(offset),
338 size_(size)
339{
340 ocean_assert(asset_ != nullptr);
341 ocean_assert(fileDescriptor_ >= 0);
342 ocean_assert(size_ > 0);
343}
344
346{
347 *this = std::move(scopedFile);
348}
349
351{
352 release();
353}
354
356{
357 return fileDescriptor_;
358}
359
361{
362 return offset_;
363}
364
366{
367 return size_;
368}
369
371{
372 if (asset_ != nullptr)
373 {
374 AAsset_close(asset_);
375 }
376
377 asset_ = nullptr;
378 fileDescriptor_ = -1;
379 offset_ = 0;
380 size_ = 0;
381}
382
384{
385 ocean_assert(fileDescriptor_ == -1 || (asset_ != nullptr && size_ > 0));
386
387 return fileDescriptor_ != -1;
388}
389
391{
392 if (this != &scopedFile)
393 {
394 release();
395
396 asset_ = scopedFile.asset_;
397 scopedFile.asset_ = nullptr;
398
399 fileDescriptor_ = scopedFile.fileDescriptor_;
400 scopedFile.fileDescriptor_ = -1;
401
402 offset_ = scopedFile.offset_;
403 scopedFile.offset_ = 0;
404
405 size_ = scopedFile.size_;
406 scopedFile.size_ = 0;
407 }
408
409 return *this;
410}
411
412inline ResourceManager::ScopedFile::operator bool() const
413{
414 return isValid();
415}
416
417inline bool ResourceManager::isValid() const
418{
419 const ScopedLock scopedLock(lock_);
420 return assetManager_ != nullptr;
421}
422
424{
425 const ScopedLock scopedLock(lock_);
426
428}
429
430}
431
432}
433
434}
435
436#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 file descriptor of an asset file as long as the object exists.
Definition android/ResourceManager.h:126
off64_t offset() const
Returns the offset of the asset file.
Definition android/ResourceManager.h:360
int fileDescriptor() const
Returns the file descriptor of the asset file.
Definition android/ResourceManager.h:355
~ScopedFile()
Destructs the object and releases the file descriptor.
Definition android/ResourceManager.h:350
bool isValid() const
Returns whether the object is valid.
Definition android/ResourceManager.h:383
ScopedFile & operator=(ScopedFile &&scopedFile)
Move operator.
Definition android/ResourceManager.h:390
AAsset * asset_
The asset handle.
Definition android/ResourceManager.h:206
int fileDescriptor_
The file descriptor of the asset.
Definition android/ResourceManager.h:209
off64_t offset_
The offset of the asset.
Definition android/ResourceManager.h:212
off64_t size_
The size of the asset.
Definition android/ResourceManager.h:215
off64_t size() const
Returns the size of the asset file.
Definition android/ResourceManager.h:365
void release()
Explicitly releases the file descriptor.
Definition android/ResourceManager.h:370
ScopedFile()=default
Default constructor creating an invalid object.
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:319
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:324
size_t size() const
Returns the size of the content of the asset file in bytes.
Definition android/ResourceManager.h:329
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.
ScopedFile openAsset(const std::string &assetFilename) const
Opens an asset file and returns a file object providing access to the file descriptor of the asset.
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:302
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:417
Lock lock_
The lock of the manager.
Definition android/ResourceManager.h:305
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:308
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:423
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