Home | History | Annotate | Download | only in fuelgauge
      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      */
     38     public static String formatElapsedTime(Context context, double millis, boolean inclSeconds) {
     39         StringBuilder sb = new StringBuilder();
     40         int seconds = (int) Math.floor(millis / 1000);
     41         if (!inclSeconds) {
     42             // Round up.
     43             seconds += 30;
     44         }
     45 
     46         int days = 0, hours = 0, minutes = 0;
     47         if (seconds > SECONDS_PER_DAY) {
     48             days = seconds / SECONDS_PER_DAY;
     49             seconds -= days * SECONDS_PER_DAY;
     50         }
     51         if (seconds > SECONDS_PER_HOUR) {
     52             hours = seconds / SECONDS_PER_HOUR;
     53             seconds -= hours * SECONDS_PER_HOUR;
     54         }
     55         if (seconds > SECONDS_PER_MINUTE) {
     56             minutes = seconds / SECONDS_PER_MINUTE;
     57             seconds -= minutes * SECONDS_PER_MINUTE;
     58         }
     59         if (inclSeconds) {
     60             if (days > 0) {
     61                 sb.append(context.getString(R.string.battery_history_days,
     62                         days, hours, minutes, seconds));
     63             } else if (hours > 0) {
     64                 sb.append(context.getString(R.string.battery_history_hours,
     65                         hours, minutes, seconds));
     66             } else if (minutes > 0) {
     67                 sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds));
     68             } else {
     69                 sb.append(context.getString(R.string.battery_history_seconds, seconds));
     70             }
     71         } else {
     72             if (days > 0) {
     73                 sb.append(context.getString(R.string.battery_history_days_no_seconds,
     74                         days, hours, minutes));
     75             } else if (hours > 0) {
     76                 sb.append(context.getString(R.string.battery_history_hours_no_seconds,
     77                         hours, minutes));
     78             } else {
     79                 sb.append(context.getString(R.string.battery_history_minutes_no_seconds, minutes));
     80             }
     81         }
     82         return sb.toString();
     83     }
     84 }
     85