blob: 2f2064173988fcd60ba609e2ccb6d123d9817bf0 [file] [log] [blame]
Kenny Root086d0842010-08-19 17:55:56 -07001/*
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
30using namespace android;
31
32struct ObbActionListener : public BnObbActionListener {
33private:
34 sp<AStorageManager> mStorageManager;
35
36public:
37 ObbActionListener(AStorageManager* mgr) :
38 mStorageManager(mgr)
39 {}
40
Kenny Root05105f72010-09-22 17:29:43 -070041 virtual void onObbResult(const android::String16& filename, const android::String16& state);
Kenny Root086d0842010-08-19 17:55:56 -070042};
43
44struct AStorageManager : public RefBase {
45protected:
Kenny Root05105f72010-09-22 17:29:43 -070046 AStorageManager_obbCallbackFunc mObbCallback;
47 void* mObbCallbackData;
Kenny Root086d0842010-08-19 17:55:56 -070048 sp<ObbActionListener> mObbActionListener;
49 sp<IMountService> mMountService;
50
51public:
Kenny Root05105f72010-09-22 17:29:43 -070052 AStorageManager()
53 : mObbCallback(NULL)
54 , mObbCallbackData(NULL)
Kenny Root086d0842010-08-19 17:55:56 -070055 {
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 Root05105f72010-09-22 17:29:43 -070076 void setObbCallback(AStorageManager_obbCallbackFunc cb, void* data) {
Kenny Root086d0842010-08-19 17:55:56 -070077 mObbCallback = cb;
Kenny Root05105f72010-09-22 17:29:43 -070078 mObbCallbackData = data;
79 }
80
81 void fireCallback(const char* filename, const char* state) {
82 if (mObbCallback != NULL) {
83 mObbCallback(filename, state, mObbCallbackData);
84 }
Kenny Root086d0842010-08-19 17:55:56 -070085 }
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 Root05105f72010-09-22 17:29:43 -070095 mMountService->unmountObb(filename16, force, mObbActionListener);
Kenny Root086d0842010-08-19 17:55:56 -070096 }
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 Root05105f72010-09-22 17:29:43 -0700114void ObbActionListener::onObbResult(const android::String16& filename, const android::String16& state) {
115 mStorageManager->fireCallback(String8(filename).string(), String8(state).string());
116}
117
Kenny Root086d0842010-08-19 17:55:56 -0700118
119AStorageManager* 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
128void AStorageManager_delete(AStorageManager* mgr) {
129 if (mgr) {
130 mgr->decStrong((void*)AStorageManager_new);
131 }
132}
133
Kenny Root05105f72010-09-22 17:29:43 -0700134void AStorageManager_setObbCallback(AStorageManager* mgr, AStorageManager_obbCallbackFunc cb, void* data) {
135 mgr->setObbCallback(cb, data);
Kenny Root086d0842010-08-19 17:55:56 -0700136}
137
138void AStorageManager_mountObb(AStorageManager* mgr, const char* filename, const char* key) {
139 mgr->mountObb(filename, key);
140}
141
142void AStorageManager_unmountObb(AStorageManager* mgr, const char* filename, const int force) {
143 mgr->unmountObb(filename, force != 0);
144}
145
146int AStorageManager_isObbMounted(AStorageManager* mgr, const char* filename) {
147 return mgr->isObbMounted(filename) != 0;
148}
149
150const char* AStorageManager_getMountedObbPath(AStorageManager* mgr, const char* filename) {
151 return mgr->getMountedObbPath(filename);
152}