| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EMBEDDERS_ANDROID_RESOURCE_H_ | 5 #ifndef EMBEDDERS_ANDROID_RESOURCE_H_ |
| 6 #define EMBEDDERS_ANDROID_RESOURCE_H_ | 6 #define EMBEDDERS_ANDROID_RESOURCE_H_ |
| 7 | 7 |
| 8 #include <android_native_app_glue.h> | 8 #include <android_native_app_glue.h> |
| 9 | 9 |
| 10 #include "embedders/android/log.h" | 10 #include "embedders/android/log.h" |
| 11 | 11 |
| 12 class Resource { | 12 class Resource { |
| 13 public: | 13 public: |
| 14 Resource(android_app* application, const char* path) | 14 Resource(android_app* application, const char* path) |
| 15 : asset_manager_(application->activity->assetManager), | 15 : asset_manager_(application->activity->assetManager), |
| 16 path_(path), | 16 path_(path), |
| 17 asset_(NULL), | 17 asset_(NULL), |
| 18 descriptor_(-1), | 18 descriptor_(-1), |
| 19 start_(0), | 19 start_(0), |
| 20 length_(0) { | 20 length_(-1) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 const char* path() { | 23 const char* path() { |
| 24 return path_; | 24 return path_; |
| 25 } | 25 } |
| 26 | 26 |
| 27 int32_t descriptor() { | 27 int32_t descriptor() { |
| 28 return descriptor_; | 28 if (Open() == 0) { |
| 29 descriptor_ = AAsset_openFileDescriptor(asset_, &start_, &length_); |
| 30 LOGI("%s has start %d, length %d, fd %d", |
| 31 path_, static_cast<int>(start_), static_cast<int>(length_), |
| 32 descriptor_); |
| 33 return descriptor_; |
| 34 } |
| 35 return -1; |
| 29 } | 36 } |
| 30 | 37 |
| 31 off_t start() { | 38 off_t start() { |
| 32 return start_; | 39 return start_; |
| 33 } | 40 } |
| 34 | 41 |
| 35 off_t length() { | 42 off_t length() { |
| 43 if (length_ < 0) { |
| 44 length_ = AAsset_getLength(asset_); |
| 45 } |
| 36 return length_; | 46 return length_; |
| 37 } | 47 } |
| 38 | 48 |
| 39 int32_t open() { | 49 int32_t Open() { |
| 50 LOGI("Attempting to open asset %s", path_); |
| 40 asset_ = AAssetManager_open(asset_manager_, path_, AASSET_MODE_UNKNOWN); | 51 asset_ = AAssetManager_open(asset_manager_, path_, AASSET_MODE_UNKNOWN); |
| 41 if (asset_ != NULL) { | 52 if (asset_ != NULL) { |
| 42 descriptor_ = AAsset_openFileDescriptor(asset_, &start_, &length_); | |
| 43 LOGI("%s has start %d, length %d, fd %d", | |
| 44 path_, static_cast<int>(start_), | |
| 45 static_cast<int>(length_), descriptor_); | |
| 46 return 0; | 53 return 0; |
| 47 } | 54 } |
| 55 LOGE("Could not open asset %s", path_); |
| 48 return -1; | 56 return -1; |
| 49 } | 57 } |
| 50 | 58 |
| 51 void close() { | 59 void Close() { |
| 52 if (asset_ != NULL) { | 60 if (asset_ != NULL) { |
| 53 AAsset_close(asset_); | 61 AAsset_close(asset_); |
| 54 asset_ = NULL; | 62 asset_ = NULL; |
| 55 } | 63 } |
| 56 } | 64 } |
| 57 | 65 |
| 58 int32_t read(void* buffer, size_t count) { | 66 int32_t Read(void* buffer, size_t count) { |
| 59 size_t actual = AAsset_read(asset_, buffer, count); | 67 size_t actual = AAsset_read(asset_, buffer, count); |
| 60 return (actual == count) ? 0 : -1; | 68 return (actual == count) ? 0 : -1; |
| 61 } | 69 } |
| 62 | 70 |
| 63 private: | 71 private: |
| 64 AAssetManager* asset_manager_; | 72 AAssetManager* asset_manager_; |
| 65 const char* path_; | 73 const char* path_; |
| 66 AAsset* asset_; | 74 AAsset* asset_; |
| 67 int32_t descriptor_; | 75 int32_t descriptor_; |
| 68 off_t start_; | 76 off_t start_; |
| 69 off_t length_; | 77 off_t length_; |
| 70 }; | 78 }; |
| 71 | 79 |
| 72 #endif // EMBEDDERS_ANDROID_RESOURCE_H_ | 80 #endif // EMBEDDERS_ANDROID_RESOURCE_H_ |
| OLD | NEW |