| 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 | |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 41 | virtual void onObbResult(const android::String16& filename, const android::String16& state); |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct AStorageManager : public RefBase { |
| 45 | protected: |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 46 | AStorageManager_obbCallbackFunc mObbCallback; |
| 47 | void* mObbCallbackData; |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 48 | sp<ObbActionListener> mObbActionListener; |
| 49 | sp<IMountService> mMountService; |
| 50 | |
| 51 | public: |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 52 | AStorageManager() |
| 53 | : mObbCallback(NULL) |
| 54 | , mObbCallbackData(NULL) |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 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 | |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 76 | void setObbCallback(AStorageManager_obbCallbackFunc cb, void* data) { |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 77 | mObbCallback = cb; |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 78 | mObbCallbackData = data; |
| 79 | } |
| 80 | |
| 81 | void fireCallback(const char* filename, const char* state) { |
| 82 | if (mObbCallback != NULL) { |
| 83 | mObbCallback(filename, state, mObbCallbackData); |
| 84 | } |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void mountObb(const char* filename, const char* key) { |
| 88 | String16 filename16(filename); |
| 89 | String16 key16(key); |
| 90 | mMountService->mountObb(filename16, key16, mObbActionListener); |
| 91 | } |
| 92 | |
| 93 | void unmountObb(const char* filename, const bool force) { |
| 94 | String16 filename16(filename); |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 95 | mMountService->unmountObb(filename16, force, mObbActionListener); |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | int isObbMounted(const char* filename) { |
| 99 | String16 filename16(filename); |
| 100 | return mMountService->isObbMounted(filename16); |
| 101 | } |
| 102 | |
| 103 | const char* getMountedObbPath(const char* filename) { |
| 104 | String16 filename16(filename); |
| 105 | String16 path16; |
| 106 | if (mMountService->getMountedObbPath(filename16, path16)) { |
| 107 | return String8(path16).string(); |
| 108 | } else { |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 114 | void ObbActionListener::onObbResult(const android::String16& filename, const android::String16& state) { |
| 115 | mStorageManager->fireCallback(String8(filename).string(), String8(state).string()); |
| 116 | } |
| 117 | |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 118 | |
| 119 | AStorageManager* AStorageManager_new() { |
| 120 | sp<AStorageManager> mgr = new AStorageManager(); |
| 121 | if (mgr == NULL || !mgr->initialize()) { |
| 122 | return NULL; |
| 123 | } |
| 124 | mgr->incStrong((void*)AStorageManager_new); |
| 125 | return static_cast<AStorageManager*>(mgr.get()); |
| 126 | } |
| 127 | |
| 128 | void AStorageManager_delete(AStorageManager* mgr) { |
| 129 | if (mgr) { |
| 130 | mgr->decStrong((void*)AStorageManager_new); |
| 131 | } |
| 132 | } |
| 133 | |
| Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 134 | void AStorageManager_setObbCallback(AStorageManager* mgr, AStorageManager_obbCallbackFunc cb, void* data) { |
| 135 | mgr->setObbCallback(cb, data); |
| Kenny Root | 086d084 | 2010-08-19 17:55:56 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void AStorageManager_mountObb(AStorageManager* mgr, const char* filename, const char* key) { |
| 139 | mgr->mountObb(filename, key); |
| 140 | } |
| 141 | |
| 142 | void AStorageManager_unmountObb(AStorageManager* mgr, const char* filename, const int force) { |
| 143 | mgr->unmountObb(filename, force != 0); |
| 144 | } |
| 145 | |
| 146 | int AStorageManager_isObbMounted(AStorageManager* mgr, const char* filename) { |
| 147 | return mgr->isObbMounted(filename) != 0; |
| 148 | } |
| 149 | |
| 150 | const char* AStorageManager_getMountedObbPath(AStorageManager* mgr, const char* filename) { |
| 151 | return mgr->getMountedObbPath(filename); |
| 152 | } |