add getUserStorageStats interface

Signed-off-by: 张文迪 <zhangwendi3@huawei.com>
This commit is contained in:
张文迪 2022-05-30 21:47:27 +08:00
parent b7672e8bab
commit f23530ce57
6 changed files with 125 additions and 17 deletions

View File

@ -14,7 +14,8 @@
"subsystem": "filemanagement",
"syscap": [],
"features": [
"storage_service_fstools"
"storage_service_fstools",
"storage_service_graphic"
],
"adapted_system_type": ["standard"],
"rom": "",

View File

@ -13,6 +13,7 @@
import("//build/ohos.gni")
import("//build/ohos/sa_profile/sa_profile.gni")
import("//foundation/filemanagement/storage_service/storage_service_aafwk.gni")
config("storage_manager_config") {
include_dirs = [
@ -23,6 +24,7 @@ config("storage_manager_config") {
"../storage_daemon/include",
"//foundation/filemanagement/storage_service/services/common/include",
"//foundation/bundlemanager/bundle_framework/services/bundlemgr/include",
"//foundation/multimedia/medialibrary_standard/interfaces/inner_api/media_library_helper/include",
]
}
@ -51,18 +53,33 @@ ohos_shared_library("storage_manager") {
deps = [
"../../interfaces/innerkits/storage_manager/native:storage_manager_sa_proxy",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager:dataobs_manager",
"//foundation/bundlemanager/bundle_framework/common:libappexecfwk_common",
"//foundation/bundlemanager/bundle_framework/services/bundlemgr:libbms",
"//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share:datashare_abilitykit",
"//utils/native/base:utils",
]
if (storage_service_graphic) {
defines += [ "STORAGE_SERVICE_GRAPHIC" ]
deps += [
"//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library_helper:media_library",
"//foundation/multimedia/medialibrary_standard/frameworks/innerkitsimpl/media_library_manager:media_library_manager",
]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"common_event_service:cesfwk_innerkits",
"dataability:native_dataability",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",
]

View File

@ -227,22 +227,32 @@ napi_value GetSystemSize(napi_env env, napi_callback_info info)
napi_value GetUserStorageStats(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs((int)NARG_CNT::ONE, (int)NARG_CNT::TWO)) {
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched 1-2");
if (!funcArg.InitArgs((int)NARG_CNT::ZERO, (int)NARG_CNT::TWO)) {
UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched 0-2");
return nullptr;
}
bool succ = false;
int64_t userId;
std::tie(succ, userId) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToInt64();
if (!succ) {
UniError(EINVAL).ThrowErr(env, "Invalid userId");
return nullptr;
bool fac = false;
int64_t userId = -1;
if (funcArg.GetArgc() >= 1) {
NVal ui(env, NVal(env, funcArg[(int)NARG_POS::FIRST]).val_);
if (ui.TypeIs(napi_number)) {
bool succ = false;
std::tie(succ, userId) = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToInt64();
if (!succ) {
UniError(EINVAL).ThrowErr(env, "Invalid userId");
return nullptr;
}
fac = true;
}
}
auto storageStats = std::make_shared<StorageStats>();
auto cbExec = [userId, storageStats](napi_env env) -> UniError {
*storageStats = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetUserStorageStats(userId);
auto cbExec = [fac, userId, storageStats](napi_env env) -> UniError {
if (!fac) {
*storageStats = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetUserStorageStats();
} else {
*storageStats = DelayedSingleton<StorageManagerConnect>::GetInstance()->GetUserStorageStats(userId);
}
return UniError(ERRNO_NOERR);
};
auto cbComplete = [storageStats](napi_env env, UniError err) -> NVal {
@ -260,9 +270,13 @@ napi_value GetUserStorageStats(napi_env env, napi_callback_info info)
};
std::string procedureName = "GetUserStorageStats";
NVal thisVar(env, funcArg.GetThisVar());
if (funcArg.GetArgc() == (uint)NARG_CNT::ONE) {
if (funcArg.GetArgc() == (uint)NARG_CNT::ZERO || (funcArg.GetArgc() == (uint)NARG_CNT::ONE && fac)) {
return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
} else {
if (!fac) {
NVal cb(env, funcArg[(int)NARG_POS::FIRST]);
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}
NVal cb(env, funcArg[(int)NARG_POS::SECOND]);
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}

View File

@ -20,7 +20,16 @@
#include "storage_service_constant.h"
#include "storage_service_errno.h"
#include "storage_service_log.h"
#include "storage/storage_total_status_service.h"
#include "installd_client.h"
#include "bundle_mgr_interface.h"
#include "application_info.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
#ifdef STORAGE_SERVICE_GRAPHIC
#include "media_library_manager.h"
#include "media_volume.h"
#endif
using namespace std;
@ -74,13 +83,75 @@ BundleStats StorageStatusService::GetBundleStats(std::string pkgName)
StorageStats StorageStatusService::GetUserStorageStats()
{
StorageStats result;
return result;
int userId = GetCurrentUserId();
return GetUserStorageStats(userId);
}
StorageStats StorageStatusService::GetUserStorageStats(int32_t userId)
{
StorageStats result;
// totalSize
int64_t totalSize = 0;
totalSize = DelayedSingleton<StorageTotalStatusService>::GetInstance()->GetTotalSize();
// appSize
LOGI("StorageStatusService::GetUserStorageStats userId is %{public}d", userId);
auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sam == nullptr) {
LOGE("StorageStatusService::GetUserStorageStats samgr == nullptr");
return result;
}
sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
if (!remoteObject) {
LOGE("StorageStatusService::GetUserStorageStats remoteObj == nullptr");
return result;
}
auto bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
vector<AppExecFwk::ApplicationInfo> appInfos;
bool res = bundleMgr->GetApplicationInfos(
AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfos);
if (!res) {
LOGE("StorageStatusService::GetUserStorageStats an error occured in querying appInfos");
return result;
}
int64_t appSize = 0;
for (auto appInfo : appInfos) {
int64_t bundleSize = 0;
LOGI("StorageStatusService::GetCurUserStorageStats pkgname is %{public}s", appInfo.name.c_str());
vector<int64_t> bundleStats;
int errorcode = AppExecFwk::InstalldClient::GetInstance()->GetBundleStats(appInfo.name, userId, bundleStats);
if (bundleStats.size() != dataDir.size() || errorcode != E_OK) {
LOGE("StorageStatusService::An error occurred in querying bundle stats.");
return result;
}
for (uint i = 0; i < bundleStats.size(); i++) {
bundleSize += bundleStats[i];
}
appSize += bundleSize;
}
// mediaSize
#ifdef STORAGE_SERVICE_GRAPHIC
Media::MediaLibraryManager mgr;
Media::MediaVolume mediaVol;
auto remoteObj = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
if (remoteObj == nullptr) {
LOGE("StorageStatusService::GetUserStorageStats remoteObj == nullptr");
return result;
}
mgr.InitMediaLibraryManager(remoteObj);
if (mgr.QueryTotalSize(mediaVol)) {
LOGE("StorageStatusService::GetUserStorageStats an error occured in querying mediaSize");
return result;
}
#endif
result.total_ = totalSize;
result.app_ = appSize;
#ifdef STORAGE_SERVICE_GRAPHIC
result.audio_ = mediaVol.GetAudiosSize();
result.video_ = mediaVol.GetVideosSize();
result.image_ = mediaVol.GetImagesSize();
result.file_ = mediaVol.GetFilesSize();
#endif
return result;
}

View File

@ -5,6 +5,8 @@
"uid": "storage_manager",
"gid": ["storage_manager"],
"start-mode" : "boot",
"secon" : "u:r:storage_manager:s0"
"secon" : "u:r:storage_manager:s0",
"apl": "system_basic",
"permission": ["ohos.permission.READ_MEDIA"]
}]
}

View File

@ -9,6 +9,9 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
aafwk_kits_path = "//foundation/aafwk/standard/frameworks/kits"
declare_args() {
storage_service_graphic = true
}