aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Luis Marti <joseluis.marti@gmail.com>2024-07-11 23:05:42 +0200
committerJose Luis Marti <joseluis.marti@gmail.com>2024-07-13 23:16:39 +0200
commit2aa75033d305f480f2557be41f2061646c1d67b9 (patch)
tree31a4a82b85eff08b82f37c2e8ecfb7c956ee4ff2
parentd7876de3e6d9045941cc12eb4150248e01c1ea64 (diff)
[Android] Move GetStorageUsage function to CAndroidStorageProvider
-rw-r--r--xbmc/platform/android/activity/XBMCApp.cpp69
-rw-r--r--xbmc/platform/android/activity/XBMCApp.h3
-rw-r--r--xbmc/platform/android/storage/AndroidStorageProvider.cpp47
-rw-r--r--xbmc/platform/android/storage/AndroidStorageProvider.h2
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 <androidjni/NetworkInfo.h>
#include <androidjni/PackageManager.h>
#include <androidjni/Resources.h>
-#include <androidjni/StatFs.h>
#include <androidjni/System.h>
#include <androidjni/SystemClock.h>
#include <androidjni/SystemProperties.h>
@@ -106,8 +105,6 @@
#include <rapidjson/document.h>
#include <unistd.h>
-#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 <androidjni/Context.h>
#include <androidjni/Environment.h>
+#include <androidjni/File.h>
+#include <androidjni/StatFs.h>
#include <androidjni/StorageManager.h>
#include <androidjni/StorageVolume.h>
@@ -378,19 +380,19 @@ std::vector<std::string> 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<std::string> 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<float>(blockSize) * blockCount / GIGABYTES;
+ const float freeSize = static_cast<float>(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<std::string> GetRemovableDrives();
static std::set<std::string> GetRemovableDrivesLinux();
+
+ bool GetStorageUsage(const std::string& path, std::string& usage);
};