| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.server.tare; |
| 18 |
|
| 19 |
import static android.app.tare.EconomyManager.CAKE_IN_ARC; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.SuppressLint; |
| 23 |
import android.util.IndentingPrintWriter; |
| 24 |
|
| 25 |
import com.android.internal.annotations.VisibleForTesting; |
| 26 |
|
| 27 |
import java.text.SimpleDateFormat; |
| 28 |
import java.time.Clock; |
| 29 |
|
| 30 |
class TareUtils { |
| 31 |
@SuppressLint("SimpleDateFormat") |
| 32 |
private static final SimpleDateFormat sDumpDateFormat = |
| 33 |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 34 |
|
| 35 |
@VisibleForTesting |
| 36 |
static Clock sSystemClock = Clock.systemUTC(); |
| 37 |
|
| 38 |
static void dumpTime(IndentingPrintWriter pw, long time) { |
| 39 |
pw.print(sDumpDateFormat.format(time)); |
| 40 |
} |
| 41 |
|
| 42 |
static long getCurrentTimeMillis() { |
| 43 |
return sSystemClock.millis(); |
| 44 |
} |
| 45 |
|
| 46 |
static int cakeToArc(long cakes) { |
| 47 |
return (int) (cakes / CAKE_IN_ARC); |
| 48 |
} |
| 49 |
|
| 50 |
@NonNull |
| 51 |
static String cakeToString(long cakes) { |
| 52 |
if (cakes == 0) { |
| 53 |
return "0 ARCs"; |
| 54 |
} |
| 55 |
final long sub = cakes % CAKE_IN_ARC; |
| 56 |
final long arcs = cakeToArc(cakes); |
| 57 |
if (arcs == 0) { |
| 58 |
return sub == 1 |
| 59 |
? sub + " cake" |
| 60 |
: sub + " cakes"; |
| 61 |
} |
| 62 |
StringBuilder sb = new StringBuilder(); |
| 63 |
sb.append(arcs); |
| 64 |
if (sub != 0) { |
| 65 |
sb.append(".").append(String.format("%03d", Math.abs(sub) / (CAKE_IN_ARC / 1000))); |
| 66 |
} |
| 67 |
sb.append(" ARC"); |
| 68 |
if (arcs != 1 || sub != 0) { |
| 69 |
sb.append("s"); |
| 70 |
} |
| 71 |
return sb.toString(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
@NonNull |
| 76 |
static String appToString(int userId, String pkgName) { |
| 77 |
return "<" + userId + ">" + pkgName; |
| 78 |
} |
| 79 |
} |