1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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 23 import com.android.internal.annotations.VisibleForTesting; 24 25 import java.time.Clock; 26 27 class TareUtils { 28 @VisibleForTesting 29 static Clock sSystemClock = Clock.systemUTC(); 30 getCurrentTimeMillis()31 static long getCurrentTimeMillis() { 32 return sSystemClock.millis(); 33 } 34 cakeToArc(long cakes)35 static int cakeToArc(long cakes) { 36 return (int) (cakes / CAKE_IN_ARC); 37 } 38 39 @NonNull cakeToString(long cakes)40 static String cakeToString(long cakes) { 41 if (cakes == 0) { 42 return "0 ARCs"; 43 } 44 final long sub = cakes % CAKE_IN_ARC; 45 final long arcs = cakeToArc(cakes); 46 if (arcs == 0) { 47 return sub == 1 48 ? sub + " cake" 49 : sub + " cakes"; 50 } 51 StringBuilder sb = new StringBuilder(); 52 sb.append(arcs); 53 if (sub != 0) { 54 sb.append(".").append(String.format("%03d", Math.abs(sub) / (CAKE_IN_ARC / 1000))); 55 } 56 sb.append(" ARC"); 57 if (arcs != 1 || sub != 0) { 58 sb.append("s"); 59 } 60 return sb.toString(); 61 } 62 63 /** Returns a standardized format for printing userId+pkgName combinations. */ 64 @NonNull appToString(int userId, String pkgName)65 static String appToString(int userId, String pkgName) { 66 return "<" + userId + ">" + pkgName; 67 } 68 } 69