| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "NStorage" |
| 18 | |
| 19 | #include <android/storage_manager.h> |
| 20 | #include <storage/IMountService.h> |
| 21 | |
| 22 | #include <binder/Binder.h> |
| 23 | #include <binder/IServiceManager.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/RefBase.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/String16.h> |
| 28 | |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | struct ObbActionListener : public BnObbActionListener { |
| 33 | private: |
| 34 | sp<AStorageManager> mStorageManager; |
| 35 | |
| 36 | public: |
| 37 | ObbActionListener(AStorageManager* mgr) : |
| 38 | mStorageManager(mgr) |
| 39 | {} |
| 40 | |
| 41 | virtual void onObbResult(const android::String16& filename, const android::String16& state) { |
| 42 | LOGD("Got obb result (%s, %s)\n", String8(filename).string(), String8(state).string()); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | struct AStorageManager : public RefBase { |
| 47 | protected: |
| 48 | void* mObbCallback; |
| 49 | sp<ObbActionListener> mObbActionListener; |
| 50 | sp<IMountService> mMountService; |
| 51 | |
| 52 | public: |
| 53 | AStorageManager() : |
| 54 | mObbCallback(NULL) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | bool initialize() { |
| 59 | sp<IServiceManager> sm = defaultServiceManager(); |
| 60 | if (sm == NULL) { |
| 61 | LOGE("Couldn't get default ServiceManager\n"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | mMountService = interface_cast<IMountService>(sm->getService(String16("mount"))); |
| 66 | if (mMountService == NULL) { |
| 67 | LOGE("Couldn't get connection to MountService\n"); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | mObbActionListener = new ObbActionListener(this); |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void setObbCallback(void* cb) { |
| 77 | mObbCallback = cb; |
| 78 | } |
| 79 | |
| 80 | void mountObb(const char* filename, const char* key) { |
| 81 | String16 filename16(filename); |
| 82 | String16 key16(key); |
| 83 | mMountService->mountObb(filename16, key16, mObbActionListener); |
| 84 | } |
| 85 | |
| 86 | void unmountObb(const char* filename, const bool force) { |
| 87 | String16 filename16(filename); |
| 88 | mMountService->unmountObb(filename16, force); |
| 89 | } |
| 90 | |
| 91 | int isObbMounted(const char* filename) { |
| 92 | String16 filename16(filename); |
| 93 | return mMountService->isObbMounted(filename16); |
| 94 | } |
| 95 | |
| 96 | const char* getMountedObbPath(const char* filename) { |
| 97 | String16 filename16(filename); |
| 98 | String16 path16; |
| 99 | if (mMountService->getMountedObbPath(filename16, path16)) { |
| 100 | return String8(path16).string(); |
| 101 | } else { |
| 102 | return NULL; |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | AStorageManager* AStorageManager_new() { |
| 109 | sp<AStorageManager> mgr = new AStorageManager(); |
| 110 | if (mgr == NULL || !mgr->initialize()) { |
| 111 | return NULL; |
| 112 | } |
| 113 | mgr->incStrong((void*)AStorageManager_new); |
| 114 | return static_cast<AStorageManager*>(mgr.get()); |
| 115 | } |
| 116 | |
| 117 | void AStorageManager_delete(AStorageManager* mgr) { |
| 118 | if (mgr) { |
| 119 | mgr->decStrong((void*)AStorageManager_new); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void AStorageManager_setObbCallback(AStorageManager* mgr, void* cb) { |
| 124 | mgr->setObbCallback(cb); |
| 125 | } |
| 126 | |
| 127 | void AStorageManager_mountObb(AStorageManager* mgr, const char* filename, const char* key) { |
| 128 | mgr->mountObb(filename, key); |
| 129 | } |
| 130 | |
| 131 | void AStorageManager_unmountObb(AStorageManager* mgr, const char* filename, const int force) { |
| 132 | mgr->unmountObb(filename, force != 0); |
| 133 | } |
| 134 | |
| 135 | int AStorageManager_isObbMounted(AStorageManager* mgr, const char* filename) { |
| 136 | return mgr->isObbMounted(filename) != 0; |
| 137 | } |
| 138 | |
| 139 | const char* AStorageManager_getMountedObbPath(AStorageManager* mgr, const char* filename) { |
| 140 | return mgr->getMountedObbPath(filename); |
| 141 | } |