From 2aa75033d305f480f2557be41f2061646c1d67b9 Mon Sep 17 00:00:00 2001 From: Jose Luis Marti Date: Thu, 11 Jul 2024 23:05:42 +0200 Subject: [Android] Move GetStorageUsage function to CAndroidStorageProvider --- xbmc/platform/android/activity/XBMCApp.cpp | 69 ---------------------- xbmc/platform/android/activity/XBMCApp.h | 3 +- .../android/storage/AndroidStorageProvider.cpp | 47 +++++++++++++-- .../android/storage/AndroidStorageProvider.h | 2 + 4 files changed, 45 insertions(+), 76 deletions(-) diff --git a/xbmc/platform/android/activity/XBMCApp.cpp b/xbmc/platform/android/activity/XBMCApp.cpp index 5f4bb7865a..67a2ae030c 100644 --- a/xbmc/platform/android/activity/XBMCApp.cpp +++ b/xbmc/platform/android/activity/XBMCApp.cpp @@ -92,7 +92,6 @@ #include #include #include -#include #include #include #include @@ -106,8 +105,6 @@ #include #include -#define GIGABYTES 1073741824 - #define ACTION_XBMC_RESUME "android.intent.XBMC_RESUME" #define PLAYBACK_STATE_STOPPED 0x0000 @@ -1133,72 +1130,6 @@ bool CXBMCApp::GetExternalStorage(std::string &path, const std::string &type /* return mounted && !path.empty(); } -bool CXBMCApp::GetStorageUsage(const std::string &path, std::string &usage) -{ -#define PATH_MAXLEN 38 - - if (path.empty()) - { - std::ostringstream fmt; - - fmt.width(PATH_MAXLEN); - fmt << std::left << "Filesystem"; - - fmt.width(12); - fmt << std::right << "Size"; - - fmt.width(12); - fmt << "Used"; - - fmt.width(12); - fmt << "Avail"; - - fmt.width(12); - fmt << "Use %"; - - usage = fmt.str(); - return false; - } - - CJNIStatFs fileStat(path); - int blockSize = fileStat.getBlockSize(); - int blockCount = fileStat.getBlockCount(); - int freeBlocks = fileStat.getFreeBlocks(); - - if (blockSize <= 0 || blockCount <= 0 || freeBlocks < 0) - return false; - - float totalSize = (float)blockSize * blockCount / GIGABYTES; - float freeSize = (float)blockSize * freeBlocks / GIGABYTES; - float usedSize = totalSize - freeSize; - float usedPercentage = usedSize / totalSize * 100; - - std::ostringstream fmt; - - fmt << std::fixed; - fmt.precision(1); - - fmt.width(PATH_MAXLEN); - fmt << std::left - << (path.size() < PATH_MAXLEN - 1 ? path : StringUtils::Left(path, PATH_MAXLEN - 4) + "..."); - - fmt.width(11); - fmt << std::right << totalSize << "G"; - - fmt.width(11); - fmt << usedSize << "G"; - - fmt.width(11); - fmt << freeSize << "G"; - - fmt.precision(0); - fmt.width(11); - fmt << usedPercentage << "%"; - - usage = fmt.str(); - return true; -} - // Used in Application.cpp to figure out volume steps int CXBMCApp::GetMaxSystemVolume() { diff --git a/xbmc/platform/android/activity/XBMCApp.h b/xbmc/platform/android/activity/XBMCApp.h index ee62fc3f03..a2d27671d2 100644 --- a/xbmc/platform/android/activity/XBMCApp.h +++ b/xbmc/platform/android/activity/XBMCApp.h @@ -173,8 +173,7 @@ public: * \param type optional type. Possible values are "", "files", "music", "videos", "pictures", "photos, "downloads" * \return true if external storage is available and a valid path has been stored in the path parameter */ - static bool GetExternalStorage(std::string &path, const std::string &type = ""); - static bool GetStorageUsage(const std::string &path, std::string &usage); + static bool GetExternalStorage(std::string& path, const std::string& type = ""); static int GetMaxSystemVolume(); static float GetSystemVolume(); static void SetSystemVolume(float percent); diff --git a/xbmc/platform/android/storage/AndroidStorageProvider.cpp b/xbmc/platform/android/storage/AndroidStorageProvider.cpp index 1993d45e42..33f7ab8a23 100644 --- a/xbmc/platform/android/storage/AndroidStorageProvider.cpp +++ b/xbmc/platform/android/storage/AndroidStorageProvider.cpp @@ -27,6 +27,8 @@ #include #include +#include +#include #include #include @@ -378,19 +380,19 @@ std::vector CAndroidStorageProvider::GetDiskUsage() std::string usage; // add header - CXBMCApp::GetStorageUsage("", usage); + GetStorageUsage("", usage); result.push_back(usage); usage.clear(); // add rootfs - if (CXBMCApp::GetStorageUsage("/", usage) && !usage.empty()) + if (GetStorageUsage("/", usage) && !usage.empty()) result.push_back(usage); usage.clear(); // add external storage if available std::string path; - if (CXBMCApp::GetExternalStorage(path) && !path.empty() && - CXBMCApp::GetStorageUsage(path, usage) && !usage.empty()) + if (CXBMCApp::GetExternalStorage(path) && !path.empty() && GetStorageUsage(path, usage) && + !usage.empty()) result.push_back(usage); // add removable storage @@ -399,7 +401,7 @@ std::vector CAndroidStorageProvider::GetDiskUsage() for (unsigned int i = 0; i < drives.size(); i++) { usage.clear(); - if (CXBMCApp::GetStorageUsage(drives[i].strPath, usage) && !usage.empty()) + if (GetStorageUsage(drives[i].strPath, usage) && !usage.empty()) result.push_back(usage); } @@ -414,3 +416,38 @@ bool CAndroidStorageProvider::PumpDriveChangeEvents(IStorageEventsCallback *call m_removableDrives = std::move(drives); return changed; } + +namespace +{ +constexpr float GIGABYTES = 1073741824; +constexpr int PATH_MAXLEN = 38; +} // namespace + +bool CAndroidStorageProvider::GetStorageUsage(const std::string& path, std::string& usage) +{ + if (path.empty()) + { + usage = StringUtils::Format("{:<{}}{:>12}{:>12}{:>12}{:>12}", "Filesystem", PATH_MAXLEN, "Size", + "Used", "Avail", "Use %"); + return false; + } + + CJNIStatFs fileStat(path); + const int blockSize = fileStat.getBlockSize(); + const int blockCount = fileStat.getBlockCount(); + const int freeBlocks = fileStat.getFreeBlocks(); + + if (blockSize <= 0 || blockCount <= 0 || freeBlocks < 0) + return false; + + const float totalSize = static_cast(blockSize) * blockCount / GIGABYTES; + const float freeSize = static_cast(blockSize) * freeBlocks / GIGABYTES; + const float usedSize = totalSize - freeSize; + const float usedPercentage = usedSize / totalSize * 100; + + usage = StringUtils::Format( + "{:<{}}{:>11.1f}{}{:>11.1f}{}{:>11.1f}{}{:>11.0f}{}", + path.size() < PATH_MAXLEN - 1 ? path : StringUtils::Left(path, PATH_MAXLEN - 4) + "...", + PATH_MAXLEN, totalSize, "G", usedSize, "G", freeSize, "G", usedPercentage, "%"); + return true; +} diff --git a/xbmc/platform/android/storage/AndroidStorageProvider.h b/xbmc/platform/android/storage/AndroidStorageProvider.h index 429e6e873c..e39ff60dea 100644 --- a/xbmc/platform/android/storage/AndroidStorageProvider.h +++ b/xbmc/platform/android/storage/AndroidStorageProvider.h @@ -37,4 +37,6 @@ private: static std::set GetRemovableDrives(); static std::set GetRemovableDrivesLinux(); + + bool GetStorageUsage(const std::string& path, std::string& usage); }; -- cgit v1.2.3