1 /* 2 * Copyright (C) 2009 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.settings.fuelgauge; 18 19 import android.content.Context; 20 21 import com.android.settings.R; 22 23 /** 24 * Contains utility functions for formatting elapsed time and consumed bytes 25 */ 26 public class Utils { 27 private static final int SECONDS_PER_MINUTE = 60; 28 private static final int SECONDS_PER_HOUR = 60 * 60; 29 private static final int SECONDS_PER_DAY = 24 * 60 * 60; 30 31 /** 32 * Returns elapsed time for the given millis, in the following format: 33 * 2d 5h 40m 29s 34 * @param context the application context 35 * @param millis the elapsed time in milli seconds 36 * @return the formatted elapsed time 37 */ formatElapsedTime(Context context, double millis)38 public static String formatElapsedTime(Context context, double millis) { 39 StringBuilder sb = new StringBuilder(); 40 int seconds = (int) Math.floor(millis / 1000); 41 42 int days = 0, hours = 0, minutes = 0; 43 if (seconds > SECONDS_PER_DAY) { 44 days = seconds / SECONDS_PER_DAY; 45 seconds -= days * SECONDS_PER_DAY; 46 } 47 if (seconds > SECONDS_PER_HOUR) { 48 hours = seconds / SECONDS_PER_HOUR; 49 seconds -= hours * SECONDS_PER_HOUR; 50 } 51 if (seconds > SECONDS_PER_MINUTE) { 52 minutes = seconds / SECONDS_PER_MINUTE; 53 seconds -= minutes * SECONDS_PER_MINUTE; 54 } 55 if (days > 0) { 56 sb.append(context.getString(R.string.battery_history_days, 57 days, hours, minutes, seconds)); 58 } else if (hours > 0) { 59 sb.append(context.getString(R.string.battery_history_hours, hours, minutes, seconds)); 60 } else if (minutes > 0) { 61 sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds)); 62 } else { 63 sb.append(context.getString(R.string.battery_history_seconds, seconds)); 64 } 65 return sb.toString(); 66 } 67 } 68