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