Calculate time to go while encrypting

@bug 15159008

Change-Id: I6a96eeab180dceb0613202ba5d252036a0d5376f
diff --git a/cryptfs.c b/cryptfs.c
index bd03c08..8680698 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -1907,6 +1907,8 @@
     char* buffer;
     off64_t last_written_sector;
     int completed;
+    time_t time_started;
+    int remaining_time;
 };
 
 static void update_progress(struct encryptGroupsData* data, int is_used)
@@ -1928,9 +1930,26 @@
         data->cur_pct = data->new_pct;
         snprintf(buf, sizeof(buf), "%lld", data->cur_pct);
         property_set("vold.encrypt_progress", buf);
-
         SLOGI("Encrypted %lld percent of drive", data->cur_pct);
     }
+
+    if (data->cur_pct >= 5) {
+        double elapsed_time = difftime(time(NULL), data->time_started);
+        off64_t remaining_blocks = data->tot_used_blocks
+                                   - data->used_blocks_already_done;
+        int remaining_time = (int)(elapsed_time * remaining_blocks
+                                   / data->used_blocks_already_done);
+        if (data->remaining_time == -1
+            || remaining_time < data->remaining_time) {
+            char buf[8];
+            snprintf(buf, sizeof(buf), "%d", remaining_time);
+            property_set("vold.encrypt_time_remaining", buf);
+
+            SLOGI("Encrypted %lld percent of drive, %d seconds to go",
+                  data->cur_pct, remaining_time);
+            data->remaining_time = remaining_time;
+        }
+    }
 }
 
 static int flush_outstanding_data(struct encryptGroupsData* data)
@@ -2106,6 +2125,8 @@
 
     data.one_pct = data.tot_used_blocks / 100;
     data.cur_pct = 0;
+    data.time_started = time(NULL);
+    data.remaining_time = -1;
 
     rc = encrypt_groups(&data);
     if (rc) {