10422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani/*
20422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * Copyright (C) 2009 The Android Open Source Project
30422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani *
40422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
50422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * you may not use this file except in compliance with the License.
60422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * You may obtain a copy of the License at
70422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani *
80422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
90422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani *
100422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * Unless required by applicable law or agreed to in writing, software
110422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
120422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * See the License for the specific language governing permissions and
140422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * limitations under the License.
150422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani */
160422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
170422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasanipackage com.android.settings.fuelgauge;
180422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
190422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasaniimport android.content.Context;
200422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
210422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasaniimport com.android.settings.R;
220422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
230422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani/**
240422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani * Contains utility functions for formatting elapsed time and consumed bytes
250422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani */
260422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasanipublic class Utils {
270422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    private static final int SECONDS_PER_MINUTE = 60;
280422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    private static final int SECONDS_PER_HOUR = 60 * 60;
290422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    private static final int SECONDS_PER_DAY = 24 * 60 * 60;
300422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
310422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    /**
320422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * Returns elapsed time for the given millis, in the following format:
330422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * 2d 5h 40m 29s
340422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @param context the application context
350422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @param millis the elapsed time in milli seconds
360422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @return the formatted elapsed time
370422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     */
380422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    public static String formatElapsedTime(Context context, double millis) {
390422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        StringBuilder sb = new StringBuilder();
400422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        int seconds = (int) Math.floor(millis / 1000);
410422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
420422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        int days = 0, hours = 0, minutes = 0;
430422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        if (seconds > SECONDS_PER_DAY) {
440422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            days = seconds / SECONDS_PER_DAY;
450422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            seconds -= days * SECONDS_PER_DAY;
460422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        }
470422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        if (seconds > SECONDS_PER_HOUR) {
480422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            hours = seconds / SECONDS_PER_HOUR;
490422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            seconds -= hours * SECONDS_PER_HOUR;
500422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        }
510422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        if (seconds > SECONDS_PER_MINUTE) {
520422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            minutes = seconds / SECONDS_PER_MINUTE;
530422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            seconds -= minutes * SECONDS_PER_MINUTE;
540422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        }
550422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        if (days > 0) {
560422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            sb.append(context.getString(R.string.battery_history_days,
570422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani                    days, hours, minutes, seconds));
580422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        } else if (hours > 0) {
590422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            sb.append(context.getString(R.string.battery_history_hours, hours, minutes, seconds));
600422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        } else if (minutes > 0) {
610422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds));
620422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        } else {
630422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            sb.append(context.getString(R.string.battery_history_seconds, seconds));
640422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        }
650422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        return sb.toString();
660422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    }
670422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani
680422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    /**
690422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * Formats data size in KB, MB, from the given bytes.
700422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @param context the application context
710422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @param bytes data size in bytes
720422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     * @return the formatted size such as 4.52 MB or 245 KB or 332 bytes
730422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani     */
740422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    public static String formatBytes(Context context, double bytes) {
750422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        // TODO: I18N
760422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        if (bytes > 1000 * 1000) {
770422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            return String.format("%.2f MB", ((int) (bytes / 1000)) / 1000f);
780422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        } else if (bytes > 1024) {
790422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            return String.format("%.2f KB", ((int) (bytes / 10)) / 100f);
800422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        } else {
810422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani            return String.format("%d bytes", (int) bytes);
820422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani        }
830422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani    }
840422a3bf61c17851abd87a67a0de30e412459b5cAmith Yamasani}
85