Improve progress bar handling for backup / restore / image flash

The progress bar will now be updated during image backups, restores
and during image flashing (except for sparse images which will require
significant changes to libsparse, and except for mtd nand using
flash_utils).

The progress bar will now be updated mid-file for file systems (tar) so
the user will see changes even during large file backup / restore.

Add a new progress tracking class to simplify handling of progress bar
updates. The class will only update the progress bar 5 times a second to
reduce the CPU load from updating the GUI frequently which does affect
backup times.

Change-Id: Iff382faef3df1f86604af336c1a8ce8993cd12c5
diff --git a/progresstracking.cpp b/progresstracking.cpp
new file mode 100644
index 0000000..b43e130
--- /dev/null
+++ b/progresstracking.cpp
@@ -0,0 +1,108 @@
+/*
+        Copyright 2016 bigbiff/Dees_Troy TeamWin
+        This file is part of TWRP/TeamWin Recovery Project.
+
+        TWRP is free software: you can redistribute it and/or modify
+        it under the terms of the GNU General Public License as published by
+        the Free Software Foundation, either version 3 of the License, or
+        (at your option) any later version.
+
+        TWRP is distributed in the hope that it will be useful,
+        but WITHOUT ANY WARRANTY; without even the implied warranty of
+        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+        GNU General Public License for more details.
+
+        You should have received a copy of the GNU General Public License
+        along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// Progress tracking class for tracking backup progess and updating the progress bar as appropriate
+
+
+#include "progresstracking.hpp"
+#include "twcommon.h"
+#ifndef BUILD_TWRPTAR_MAIN
+#include "gui/gui.hpp"
+#include "data.hpp"
+#endif
+#include "twrp-functions.hpp"
+#include <time.h>
+
+const int32_t update_interval_ms = 200; // Update interval in ms
+
+ProgressTracking::ProgressTracking(const unsigned long long backup_size) {
+	total_backup_size = backup_size;
+	partition_size = 0;
+	file_count = 0;
+	current_size = 0;
+	current_count = 0;
+	previous_partitions_size = 0;
+	display_file_count = false;
+	clock_gettime(CLOCK_MONOTONIC, &last_update);
+}
+
+void ProgressTracking::SetPartitionSize(const unsigned long long part_size) {
+	previous_partitions_size += partition_size;
+	partition_size = part_size;
+	UpdateDisplayDetails(true);
+}
+
+void ProgressTracking::SetSizeCount(const unsigned long long part_size, unsigned long long f_count) {
+	previous_partitions_size += partition_size;
+	partition_size = part_size;
+	file_count = f_count;
+	display_file_count = (file_count != 0);
+	UpdateDisplayDetails(true);
+}
+
+void ProgressTracking::UpdateSize(const unsigned long long size) {
+	current_size = size;
+	UpdateDisplayDetails(false);
+}
+
+void ProgressTracking::UpdateSizeCount(const unsigned long long size, const unsigned long long count) {
+	current_size = size;
+	current_count = count;
+	UpdateDisplayDetails(false);
+}
+
+void ProgressTracking::DisplayFileCount(const bool display) {
+	display_file_count = display;
+	UpdateDisplayDetails(true);
+}
+
+void ProgressTracking::UpdateDisplayDetails(const bool force) {
+#ifndef BUILD_TWRPTAR_MAIN
+	if (!force) {
+		// Do something to check the time frame and only update periodically to reduce the total number of GUI updates
+		timespec now;
+		clock_gettime(CLOCK_MONOTONIC, &now);
+
+		int32_t diff = TWFunc::timespec_diff_ms(last_update, now);
+		if (diff < update_interval_ms)
+			return;
+	}
+	clock_gettime(CLOCK_MONOTONIC, &last_update);
+	double display_percent, progress_percent;
+	string size_prog = gui_lookup("size_progress", "%lluMB of %lluMB, %i%%");
+	char size_progress[1024];
+
+	if (total_backup_size != 0) // prevent division by 0
+		display_percent = (double)(current_size + previous_partitions_size) / (double)(total_backup_size) * 100;
+	sprintf(size_progress, size_prog.c_str(), (current_size + previous_partitions_size) / 1048576, total_backup_size / 1048576, (int)(display_percent));
+	DataManager::SetValue("tw_size_progress", size_progress);
+	progress_percent = (display_percent / 100);
+	DataManager::SetProgress((float)(progress_percent));
+
+	if (!display_file_count || file_count == 0) {
+		DataManager::SetValue("tw_file_progress", "");
+	} else {
+		string file_prog = gui_lookup("file_progress", "%llu of %llu files, %i%%");
+		char file_progress[1024];
+
+		display_percent = (double)(current_count) / (double)(file_count) * 100;
+		sprintf(file_progress, file_prog.c_str(), current_count, file_count, (int)(display_percent));
+		DataManager::SetValue("tw_file_progress", file_progress);
+	}
+#endif
+}