1 /* 2 * Copyright (C) 2006 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.traceview; 18 19 import java.text.DecimalFormat; 20 21 // This should be a singleton. 22 public class TraceUnits { 23 24 private TimeScale mTimeScale = TimeScale.MicroSeconds; 25 private double mScale = 1.0; 26 DecimalFormat mFormatter = new DecimalFormat(); 27 28 public double getScaledValue(long value) { 29 return value * mScale; 30 } 31 32 public double getScaledValue(double value) { 33 return value * mScale; 34 } 35 36 public String valueOf(long value) { 37 return valueOf((double) value); 38 } 39 40 public String valueOf(double value) { 41 String pattern; 42 double scaled = value * mScale; 43 if ((int) scaled == scaled) 44 pattern = "###,###"; 45 else 46 pattern = "###,###.###"; 47 mFormatter.applyPattern(pattern); 48 return mFormatter.format(scaled); 49 } 50 51 public String labelledString(double value) { 52 String units = label(); 53 String num = valueOf(value); 54 return String.format("%s: %s", units, num); 55 } 56 57 public String labelledString(long value) { 58 return labelledString((double) value); 59 } 60 61 public String label() { 62 if (mScale == 1.0) 63 return "usec"; 64 if (mScale == 0.001) 65 return "msec"; 66 if (mScale == 0.000001) 67 return "sec"; 68 return null; 69 } 70 71 public void setTimeScale(TimeScale val) { 72 mTimeScale = val; 73 switch (val) { 74 case Seconds: 75 mScale = 0.000001; 76 break; 77 case MilliSeconds: 78 mScale = 0.001; 79 break; 80 case MicroSeconds: 81 mScale = 1.0; 82 break; 83 } 84 } 85 86 public TimeScale getTimeScale() { 87 return mTimeScale; 88 } 89 90 public enum TimeScale { 91 Seconds, MilliSeconds, MicroSeconds 92 }; 93 } 94