blob: 64115b9240ec7eb1a6e8f1ab3870ae4bed2fc794 [file] [log] [blame]
Ethan Yonker472f5062016-02-25 13:47:30 -06001/*
2 Copyright 2016 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19// Progress tracking class for tracking backup progess and updating the progress bar as appropriate
20
21
22#include "progresstracking.hpp"
23#include "twcommon.h"
24#ifndef BUILD_TWRPTAR_MAIN
25#include "gui/gui.hpp"
26#include "data.hpp"
27#endif
28#include "twrp-functions.hpp"
29#include <time.h>
30
31const int32_t update_interval_ms = 200; // Update interval in ms
32
33ProgressTracking::ProgressTracking(const unsigned long long backup_size) {
34 total_backup_size = backup_size;
35 partition_size = 0;
36 file_count = 0;
37 current_size = 0;
38 current_count = 0;
39 previous_partitions_size = 0;
40 display_file_count = false;
41 clock_gettime(CLOCK_MONOTONIC, &last_update);
42}
43
44void ProgressTracking::SetPartitionSize(const unsigned long long part_size) {
45 previous_partitions_size += partition_size;
46 partition_size = part_size;
47 UpdateDisplayDetails(true);
48}
49
50void ProgressTracking::SetSizeCount(const unsigned long long part_size, unsigned long long f_count) {
51 previous_partitions_size += partition_size;
52 partition_size = part_size;
53 file_count = f_count;
54 display_file_count = (file_count != 0);
55 UpdateDisplayDetails(true);
56}
57
58void ProgressTracking::UpdateSize(const unsigned long long size) {
59 current_size = size;
60 UpdateDisplayDetails(false);
61}
62
63void ProgressTracking::UpdateSizeCount(const unsigned long long size, const unsigned long long count) {
64 current_size = size;
65 current_count = count;
66 UpdateDisplayDetails(false);
67}
68
69void ProgressTracking::DisplayFileCount(const bool display) {
70 display_file_count = display;
71 UpdateDisplayDetails(true);
72}
73
74void ProgressTracking::UpdateDisplayDetails(const bool force) {
75#ifndef BUILD_TWRPTAR_MAIN
76 if (!force) {
77 // Do something to check the time frame and only update periodically to reduce the total number of GUI updates
78 timespec now;
79 clock_gettime(CLOCK_MONOTONIC, &now);
80
81 int32_t diff = TWFunc::timespec_diff_ms(last_update, now);
82 if (diff < update_interval_ms)
83 return;
84 }
85 clock_gettime(CLOCK_MONOTONIC, &last_update);
Matt Mower14f8ac12016-03-31 12:26:53 -050086 double display_percent = 0.0, progress_percent;
Ethan Yonker472f5062016-02-25 13:47:30 -060087 string size_prog = gui_lookup("size_progress", "%lluMB of %lluMB, %i%%");
88 char size_progress[1024];
89
90 if (total_backup_size != 0) // prevent division by 0
91 display_percent = (double)(current_size + previous_partitions_size) / (double)(total_backup_size) * 100;
92 sprintf(size_progress, size_prog.c_str(), (current_size + previous_partitions_size) / 1048576, total_backup_size / 1048576, (int)(display_percent));
93 DataManager::SetValue("tw_size_progress", size_progress);
94 progress_percent = (display_percent / 100);
95 DataManager::SetProgress((float)(progress_percent));
96
97 if (!display_file_count || file_count == 0) {
98 DataManager::SetValue("tw_file_progress", "");
99 } else {
100 string file_prog = gui_lookup("file_progress", "%llu of %llu files, %i%%");
101 char file_progress[1024];
102
103 display_percent = (double)(current_count) / (double)(file_count) * 100;
104 sprintf(file_progress, file_prog.c_str(), current_count, file_count, (int)(display_percent));
105 DataManager::SetValue("tw_file_progress", file_progress);
106 }
107#endif
108}