Search in sources :

Example 1 with HistoryItem

use of android.os.BatteryStats.HistoryItem in project platform_frameworks_base by android.

the class BatteryInfo method bindHistory.

public void bindHistory(final UsageView view, BatteryDataParser... parsers) {
    BatteryDataParser parser = new BatteryDataParser() {

        SparseIntArray points = new SparseIntArray();

        @Override
        public void onParsingStarted(long startTime, long endTime) {
            timePeriod = endTime - startTime - remainingTimeUs / 1000;
            view.clearPaths();
            view.configureGraph((int) (endTime - startTime), 100, remainingTimeUs != 0, mCharging);
        }

        @Override
        public void onDataPoint(long time, HistoryItem record) {
            points.put((int) time, record.batteryLevel);
        }

        @Override
        public void onDataGap() {
            if (points.size() > 1) {
                view.addPath(points);
            }
            points.clear();
        }

        @Override
        public void onParsingDone() {
            if (points.size() > 1) {
                view.addPath(points);
            }
        }
    };
    BatteryDataParser[] parserList = new BatteryDataParser[parsers.length + 1];
    for (int i = 0; i < parsers.length; i++) {
        parserList[i] = parsers[i];
    }
    parserList[parsers.length] = parser;
    parse(mStats, remainingTimeUs, parserList);
    final Context context = view.getContext();
    String timeString = context.getString(R.string.charge_length_format, Formatter.formatShortElapsedTime(context, timePeriod));
    String remaining = "";
    if (remainingTimeUs != 0) {
        remaining = context.getString(R.string.remaining_length_format, Formatter.formatShortElapsedTime(context, remainingTimeUs / 1000));
    }
    view.setBottomLabels(new CharSequence[] { timeString, remaining });
}
Also used : Context(android.content.Context) SparseIntArray(android.util.SparseIntArray) HistoryItem(android.os.BatteryStats.HistoryItem)

Example 2 with HistoryItem

use of android.os.BatteryStats.HistoryItem in project platform_frameworks_base by android.

the class BatteryInfo method parse.

private static void parse(BatteryStats stats, long remainingTimeUs, BatteryDataParser... parsers) {
    long startWalltime = 0;
    long endDateWalltime = 0;
    long endWalltime = 0;
    long historyStart = 0;
    long historyEnd = 0;
    byte lastLevel = -1;
    long curWalltime = startWalltime;
    long lastWallTime = 0;
    long lastRealtime = 0;
    int lastInteresting = 0;
    int pos = 0;
    boolean first = true;
    if (stats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (stats.getNextHistoryLocked(rec)) {
            pos++;
            if (first) {
                first = false;
                historyStart = rec.time;
            }
            if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                // pretty much just noise.
                if (rec.currentTime > (lastWallTime + (180 * 24 * 60 * 60 * 1000L)) || rec.time < (historyStart + (5 * 60 * 1000L))) {
                    startWalltime = 0;
                }
                lastWallTime = rec.currentTime;
                lastRealtime = rec.time;
                if (startWalltime == 0) {
                    startWalltime = lastWallTime - (lastRealtime - historyStart);
                }
            }
            if (rec.isDeltaData()) {
                if (rec.batteryLevel != lastLevel || pos == 1) {
                    lastLevel = rec.batteryLevel;
                }
                lastInteresting = pos;
                historyEnd = rec.time;
            }
        }
    }
    stats.finishIteratingHistoryLocked();
    endDateWalltime = lastWallTime + historyEnd - lastRealtime;
    endWalltime = endDateWalltime + (remainingTimeUs / 1000);
    int i = 0;
    final int N = lastInteresting;
    for (int j = 0; j < parsers.length; j++) {
        parsers[j].onParsingStarted(startWalltime, endWalltime);
    }
    if (endDateWalltime > startWalltime && stats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (stats.getNextHistoryLocked(rec) && i < N) {
            if (rec.isDeltaData()) {
                curWalltime += rec.time - lastRealtime;
                lastRealtime = rec.time;
                long x = (curWalltime - startWalltime);
                if (x < 0) {
                    x = 0;
                }
                for (int j = 0; j < parsers.length; j++) {
                    parsers[j].onDataPoint(x, rec);
                }
            } else {
                long lastWalltime = curWalltime;
                if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                    if (rec.currentTime >= startWalltime) {
                        curWalltime = rec.currentTime;
                    } else {
                        curWalltime = startWalltime + (rec.time - historyStart);
                    }
                    lastRealtime = rec.time;
                }
                if (rec.cmd != HistoryItem.CMD_OVERFLOW && (rec.cmd != HistoryItem.CMD_CURRENT_TIME || Math.abs(lastWalltime - curWalltime) > (60 * 60 * 1000))) {
                    for (int j = 0; j < parsers.length; j++) {
                        parsers[j].onDataGap();
                    }
                }
            }
            i++;
        }
    }
    stats.finishIteratingHistoryLocked();
    for (int j = 0; j < parsers.length; j++) {
        parsers[j].onParsingDone();
    }
}
Also used : HistoryItem(android.os.BatteryStats.HistoryItem)

Example 3 with HistoryItem

use of android.os.BatteryStats.HistoryItem in project android_frameworks_base by DirtyUnicorns.

the class BatteryInfo method parse.

private static void parse(BatteryStats stats, long remainingTimeUs, BatteryDataParser... parsers) {
    long startWalltime = 0;
    long endDateWalltime = 0;
    long endWalltime = 0;
    long historyStart = 0;
    long historyEnd = 0;
    byte lastLevel = -1;
    long curWalltime = startWalltime;
    long lastWallTime = 0;
    long lastRealtime = 0;
    int lastInteresting = 0;
    int pos = 0;
    boolean first = true;
    if (stats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (stats.getNextHistoryLocked(rec)) {
            pos++;
            if (first) {
                first = false;
                historyStart = rec.time;
            }
            if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                // pretty much just noise.
                if (rec.currentTime > (lastWallTime + (180 * 24 * 60 * 60 * 1000L)) || rec.time < (historyStart + (5 * 60 * 1000L))) {
                    startWalltime = 0;
                }
                lastWallTime = rec.currentTime;
                lastRealtime = rec.time;
                if (startWalltime == 0) {
                    startWalltime = lastWallTime - (lastRealtime - historyStart);
                }
            }
            if (rec.isDeltaData()) {
                if (rec.batteryLevel != lastLevel || pos == 1) {
                    lastLevel = rec.batteryLevel;
                }
                lastInteresting = pos;
                historyEnd = rec.time;
            }
        }
    }
    stats.finishIteratingHistoryLocked();
    endDateWalltime = lastWallTime + historyEnd - lastRealtime;
    endWalltime = endDateWalltime + (remainingTimeUs / 1000);
    int i = 0;
    final int N = lastInteresting;
    for (int j = 0; j < parsers.length; j++) {
        parsers[j].onParsingStarted(startWalltime, endWalltime);
    }
    if (endDateWalltime > startWalltime && stats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (stats.getNextHistoryLocked(rec) && i < N) {
            if (rec.isDeltaData()) {
                curWalltime += rec.time - lastRealtime;
                lastRealtime = rec.time;
                long x = (curWalltime - startWalltime);
                if (x < 0) {
                    x = 0;
                }
                for (int j = 0; j < parsers.length; j++) {
                    parsers[j].onDataPoint(x, rec);
                }
            } else {
                long lastWalltime = curWalltime;
                if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                    if (rec.currentTime >= startWalltime) {
                        curWalltime = rec.currentTime;
                    } else {
                        curWalltime = startWalltime + (rec.time - historyStart);
                    }
                    lastRealtime = rec.time;
                }
                if (rec.cmd != HistoryItem.CMD_OVERFLOW && (rec.cmd != HistoryItem.CMD_CURRENT_TIME || Math.abs(lastWalltime - curWalltime) > (60 * 60 * 1000))) {
                    for (int j = 0; j < parsers.length; j++) {
                        parsers[j].onDataGap();
                    }
                }
            }
            i++;
        }
    }
    stats.finishIteratingHistoryLocked();
    for (int j = 0; j < parsers.length; j++) {
        parsers[j].onParsingDone();
    }
}
Also used : HistoryItem(android.os.BatteryStats.HistoryItem)

Example 4 with HistoryItem

use of android.os.BatteryStats.HistoryItem in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class BatteryHistoryChart method onSizeChanged.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (DEBUG)
        Log.d(TAG, "onSizeChanged: " + oldw + "x" + oldh + " to " + w + "x" + h);
    if (mLastWidth == w && mLastHeight == h) {
        return;
    }
    if (mLastWidth == 0 || mLastHeight == 0) {
        return;
    }
    if (DEBUG)
        Log.d(TAG, "Rebuilding chart for: " + w + "x" + h);
    mLastWidth = w;
    mLastHeight = h;
    mBitmap = null;
    mCanvas = null;
    int textHeight = mTextDescent - mTextAscent;
    if (h > ((textHeight * 10) + mChartMinHeight)) {
        mLargeMode = true;
        if (h > (textHeight * 15)) {
            // Plenty of room for the chart.
            mLineWidth = textHeight / 2;
        } else {
            // Compress lines to make more room for chart.
            mLineWidth = textHeight / 3;
        }
    } else {
        mLargeMode = false;
        mLineWidth = mThinLineWidth;
    }
    if (mLineWidth <= 0)
        mLineWidth = 1;
    mLevelTop = mHeaderHeight;
    mLevelLeft = mMaxPercentLabelStringWidth + mThinLineWidth * 3;
    mLevelRight = w;
    int levelWidth = mLevelRight - mLevelLeft;
    mTextPaint.setStrokeWidth(mThinLineWidth);
    mBatteryGoodPaint.setStrokeWidth(mThinLineWidth);
    mBatteryWarnPaint.setStrokeWidth(mThinLineWidth);
    mBatteryCriticalPaint.setStrokeWidth(mThinLineWidth);
    mChargingPaint.setStrokeWidth(mLineWidth);
    mScreenOnPaint.setStrokeWidth(mLineWidth);
    mGpsOnPaint.setStrokeWidth(mLineWidth);
    mCameraOnPaint.setStrokeWidth(mLineWidth);
    mFlashlightOnPaint.setStrokeWidth(mLineWidth);
    mWifiRunningPaint.setStrokeWidth(mLineWidth);
    mCpuRunningPaint.setStrokeWidth(mLineWidth);
    mDebugRectPaint.setStrokeWidth(1);
    int fullBarOffset = textHeight + mLineWidth;
    if (mLargeMode) {
        mChargingOffset = mLineWidth;
        mScreenOnOffset = mChargingOffset + fullBarOffset;
        mCpuRunningOffset = mScreenOnOffset + fullBarOffset;
        mWifiRunningOffset = mCpuRunningOffset + fullBarOffset;
        mGpsOnOffset = mWifiRunningOffset + (mHaveWifi ? fullBarOffset : 0);
        mFlashlightOnOffset = mGpsOnOffset + (mHaveGps ? fullBarOffset : 0);
        mCameraOnOffset = mFlashlightOnOffset + (mHaveFlashlight ? fullBarOffset : 0);
        mPhoneSignalOffset = mCameraOnOffset + (mHaveCamera ? fullBarOffset : 0);
        mLevelOffset = mPhoneSignalOffset + (mHavePhoneSignal ? fullBarOffset : 0) + mLineWidth * 2 + mLineWidth / 2;
        if (mHavePhoneSignal) {
            mPhoneSignalChart.init(w);
        }
    } else {
        mScreenOnOffset = mGpsOnOffset = mCameraOnOffset = mFlashlightOnOffset = mWifiRunningOffset = mCpuRunningOffset = mChargingOffset = mPhoneSignalOffset = 0;
        mLevelOffset = fullBarOffset + mThinLineWidth * 4;
        if (mHavePhoneSignal) {
            mPhoneSignalChart.init(0);
        }
    }
    mBatLevelPath.reset();
    mBatGoodPath.reset();
    mBatWarnPath.reset();
    mTimeRemainPath.reset();
    mBatCriticalPath.reset();
    mScreenOnPath.reset();
    mGpsOnPath.reset();
    mFlashlightOnPath.reset();
    mCameraOnPath.reset();
    mWifiRunningPath.reset();
    mCpuRunningPath.reset();
    mChargingPath.reset();
    mTimeLabels.clear();
    mDateLabels.clear();
    final long walltimeStart = mStartWallTime;
    final long walltimeChange = mEndWallTime > walltimeStart ? (mEndWallTime - walltimeStart) : 1;
    long curWalltime = mStartWallTime;
    long lastRealtime = 0;
    final int batLow = mBatLow;
    final int batChange = mBatHigh - mBatLow;
    final int levelh = h - mLevelOffset - mLevelTop;
    mLevelBottom = mLevelTop + levelh;
    int x = mLevelLeft, y = 0, startX = mLevelLeft, lastX = -1, lastY = -1;
    int i = 0;
    Path curLevelPath = null;
    Path lastLinePath = null;
    boolean lastCharging = false, lastScreenOn = false, lastGpsOn = false;
    boolean lastFlashlightOn = false, lastCameraOn = false;
    boolean lastWifiRunning = false, lastWifiSupplRunning = false, lastCpuRunning = false;
    int lastWifiSupplState = BatteryStats.WIFI_SUPPL_STATE_INVALID;
    final int N = mNumHist;
    if (mEndDataWallTime > mStartWallTime && mStats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (mStats.getNextHistoryLocked(rec) && i < N) {
            if (rec.isDeltaData()) {
                curWalltime += rec.time - lastRealtime;
                lastRealtime = rec.time;
                x = mLevelLeft + (int) (((curWalltime - walltimeStart) * levelWidth) / walltimeChange);
                if (x < 0) {
                    x = 0;
                }
                if (false) {
                    StringBuilder sb = new StringBuilder(128);
                    sb.append("walloff=");
                    TimeUtils.formatDuration(curWalltime - walltimeStart, sb);
                    sb.append(" wallchange=");
                    TimeUtils.formatDuration(walltimeChange, sb);
                    sb.append(" x=");
                    sb.append(x);
                    Log.d("foo", sb.toString());
                }
                y = mLevelTop + levelh - ((rec.batteryLevel - batLow) * (levelh - 1)) / batChange;
                if (lastX != x) {
                    // We have moved by at least a pixel.
                    if (lastY != y) {
                        // Don't plot changes within a pixel.
                        Path path;
                        byte value = rec.batteryLevel;
                        if (value <= mBatteryCriticalLevel)
                            path = mBatCriticalPath;
                        else if (value <= mBatteryWarnLevel)
                            path = mBatWarnPath;
                        else
                            //mBatGoodPath;
                            path = null;
                        if (path != lastLinePath) {
                            if (lastLinePath != null) {
                                lastLinePath.lineTo(x, y);
                            }
                            if (path != null) {
                                path.moveTo(x, y);
                            }
                            lastLinePath = path;
                        } else if (path != null) {
                            path.lineTo(x, y);
                        }
                        if (curLevelPath == null) {
                            curLevelPath = mBatLevelPath;
                            curLevelPath.moveTo(x, y);
                            startX = x;
                        } else {
                            curLevelPath.lineTo(x, y);
                        }
                        lastX = x;
                        lastY = y;
                    }
                }
                if (mLargeMode) {
                    final boolean charging = (rec.states & HistoryItem.STATE_BATTERY_PLUGGED_FLAG) != 0;
                    if (charging != lastCharging) {
                        if (charging) {
                            mChargingPath.moveTo(x, h - mChargingOffset);
                        } else {
                            mChargingPath.lineTo(x, h - mChargingOffset);
                        }
                        lastCharging = charging;
                    }
                    final boolean screenOn = (rec.states & HistoryItem.STATE_SCREEN_ON_FLAG) != 0;
                    if (screenOn != lastScreenOn) {
                        if (screenOn) {
                            mScreenOnPath.moveTo(x, h - mScreenOnOffset);
                        } else {
                            mScreenOnPath.lineTo(x, h - mScreenOnOffset);
                        }
                        lastScreenOn = screenOn;
                    }
                    final boolean gpsOn = (rec.states & HistoryItem.STATE_GPS_ON_FLAG) != 0;
                    if (gpsOn != lastGpsOn) {
                        if (gpsOn) {
                            mGpsOnPath.moveTo(x, h - mGpsOnOffset);
                        } else {
                            mGpsOnPath.lineTo(x, h - mGpsOnOffset);
                        }
                        lastGpsOn = gpsOn;
                    }
                    final boolean flashlightOn = (rec.states2 & HistoryItem.STATE2_FLASHLIGHT_FLAG) != 0;
                    if (flashlightOn != lastFlashlightOn) {
                        if (flashlightOn) {
                            mFlashlightOnPath.moveTo(x, h - mFlashlightOnOffset);
                        } else {
                            mFlashlightOnPath.lineTo(x, h - mFlashlightOnOffset);
                        }
                        lastFlashlightOn = flashlightOn;
                    }
                    final boolean cameraOn = (rec.states2 & HistoryItem.STATE2_CAMERA_FLAG) != 0;
                    if (cameraOn != lastCameraOn) {
                        if (cameraOn) {
                            mCameraOnPath.moveTo(x, h - mCameraOnOffset);
                        } else {
                            mCameraOnPath.lineTo(x, h - mCameraOnOffset);
                        }
                        lastCameraOn = cameraOn;
                    }
                    final int wifiSupplState = ((rec.states2 & HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK) >> HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
                    boolean wifiRunning;
                    if (lastWifiSupplState != wifiSupplState) {
                        lastWifiSupplState = wifiSupplState;
                        switch(wifiSupplState) {
                            case BatteryStats.WIFI_SUPPL_STATE_DISCONNECTED:
                            case BatteryStats.WIFI_SUPPL_STATE_DORMANT:
                            case BatteryStats.WIFI_SUPPL_STATE_INACTIVE:
                            case BatteryStats.WIFI_SUPPL_STATE_INTERFACE_DISABLED:
                            case BatteryStats.WIFI_SUPPL_STATE_INVALID:
                            case BatteryStats.WIFI_SUPPL_STATE_UNINITIALIZED:
                                wifiRunning = lastWifiSupplRunning = false;
                                break;
                            default:
                                wifiRunning = lastWifiSupplRunning = true;
                                break;
                        }
                    } else {
                        wifiRunning = lastWifiSupplRunning;
                    }
                    if ((rec.states & (HistoryItem.STATE_WIFI_FULL_LOCK_FLAG | HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG | HistoryItem.STATE_WIFI_SCAN_FLAG)) != 0) {
                        wifiRunning = true;
                    }
                    if (wifiRunning != lastWifiRunning) {
                        if (wifiRunning) {
                            mWifiRunningPath.moveTo(x, h - mWifiRunningOffset);
                        } else {
                            mWifiRunningPath.lineTo(x, h - mWifiRunningOffset);
                        }
                        lastWifiRunning = wifiRunning;
                    }
                    final boolean cpuRunning = (rec.states & HistoryItem.STATE_CPU_RUNNING_FLAG) != 0;
                    if (cpuRunning != lastCpuRunning) {
                        if (cpuRunning) {
                            mCpuRunningPath.moveTo(x, h - mCpuRunningOffset);
                        } else {
                            mCpuRunningPath.lineTo(x, h - mCpuRunningOffset);
                        }
                        lastCpuRunning = cpuRunning;
                    }
                    if (mLargeMode && mHavePhoneSignal) {
                        int bin;
                        if (((rec.states & HistoryItem.STATE_PHONE_STATE_MASK) >> HistoryItem.STATE_PHONE_STATE_SHIFT) == ServiceState.STATE_POWER_OFF) {
                            bin = 0;
                        } else if ((rec.states & HistoryItem.STATE_PHONE_SCANNING_FLAG) != 0) {
                            bin = 1;
                        } else {
                            bin = (rec.states & HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK) >> HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
                            bin += 2;
                        }
                        mPhoneSignalChart.addTick(x, bin);
                    }
                }
            } else {
                long lastWalltime = curWalltime;
                if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                    if (rec.currentTime >= mStartWallTime) {
                        curWalltime = rec.currentTime;
                    } else {
                        curWalltime = mStartWallTime + (rec.time - mHistStart);
                    }
                    lastRealtime = rec.time;
                }
                if (rec.cmd != HistoryItem.CMD_OVERFLOW && (rec.cmd != HistoryItem.CMD_CURRENT_TIME || Math.abs(lastWalltime - curWalltime) > (60 * 60 * 1000))) {
                    if (curLevelPath != null) {
                        finishPaths(x + 1, h, levelh, startX, lastY, curLevelPath, lastX, lastCharging, lastScreenOn, lastGpsOn, lastFlashlightOn, lastCameraOn, lastWifiRunning, lastCpuRunning, lastLinePath);
                        lastX = lastY = -1;
                        curLevelPath = null;
                        lastLinePath = null;
                        lastCharging = lastScreenOn = lastGpsOn = lastFlashlightOn = lastCameraOn = lastCpuRunning = false;
                    }
                }
            }
            i++;
        }
        mStats.finishIteratingHistoryLocked();
    }
    if (lastY < 0 || lastX < 0) {
        // Didn't get any data...
        x = lastX = mLevelLeft;
        y = lastY = mLevelTop + levelh - ((mInfo.mBatteryLevel - batLow) * (levelh - 1)) / batChange;
        Path path;
        byte value = (byte) mInfo.mBatteryLevel;
        if (value <= mBatteryCriticalLevel)
            path = mBatCriticalPath;
        else if (value <= mBatteryWarnLevel)
            path = mBatWarnPath;
        else
            //mBatGoodPath;
            path = null;
        if (path != null) {
            path.moveTo(x, y);
            lastLinePath = path;
        }
        mBatLevelPath.moveTo(x, y);
        curLevelPath = mBatLevelPath;
        x = w;
    } else {
        // Figure out where the actual data ends on the screen.
        x = mLevelLeft + (int) (((mEndDataWallTime - walltimeStart) * levelWidth) / walltimeChange);
        if (x < 0) {
            x = 0;
        }
    }
    finishPaths(x, h, levelh, startX, lastY, curLevelPath, lastX, lastCharging, lastScreenOn, lastGpsOn, lastFlashlightOn, lastCameraOn, lastWifiRunning, lastCpuRunning, lastLinePath);
    if (x < w) {
        // If we reserved room for the remaining time, create a final path to draw
        // that part of the UI.
        mTimeRemainPath.moveTo(x, lastY);
        int fullY = mLevelTop + levelh - ((100 - batLow) * (levelh - 1)) / batChange;
        int emptyY = mLevelTop + levelh - ((0 - batLow) * (levelh - 1)) / batChange;
        if (mInfo.mDischarging) {
            mTimeRemainPath.lineTo(mLevelRight, emptyY);
        } else {
            mTimeRemainPath.lineTo(mLevelRight, fullY);
            mTimeRemainPath.lineTo(mLevelRight, emptyY);
        }
        mTimeRemainPath.lineTo(x, emptyY);
        mTimeRemainPath.close();
    }
    if (mStartWallTime > 0 && mEndWallTime > mStartWallTime) {
        // Create the time labels at the bottom.
        boolean is24hr = is24Hour();
        Calendar calStart = Calendar.getInstance();
        calStart.setTimeInMillis(mStartWallTime);
        calStart.set(Calendar.MILLISECOND, 0);
        calStart.set(Calendar.SECOND, 0);
        calStart.set(Calendar.MINUTE, 0);
        long startRoundTime = calStart.getTimeInMillis();
        if (startRoundTime < mStartWallTime) {
            calStart.set(Calendar.HOUR_OF_DAY, calStart.get(Calendar.HOUR_OF_DAY) + 1);
            startRoundTime = calStart.getTimeInMillis();
        }
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTimeInMillis(mEndWallTime);
        calEnd.set(Calendar.MILLISECOND, 0);
        calEnd.set(Calendar.SECOND, 0);
        calEnd.set(Calendar.MINUTE, 0);
        long endRoundTime = calEnd.getTimeInMillis();
        if (startRoundTime < endRoundTime) {
            addTimeLabel(calStart, mLevelLeft, mLevelRight, is24hr);
            Calendar calMid = Calendar.getInstance();
            calMid.setTimeInMillis(mStartWallTime + ((mEndWallTime - mStartWallTime) / 2));
            calMid.set(Calendar.MILLISECOND, 0);
            calMid.set(Calendar.SECOND, 0);
            calMid.set(Calendar.MINUTE, 0);
            long calMidMillis = calMid.getTimeInMillis();
            if (calMidMillis > startRoundTime && calMidMillis < endRoundTime) {
                addTimeLabel(calMid, mLevelLeft, mLevelRight, is24hr);
            }
            addTimeLabel(calEnd, mLevelLeft, mLevelRight, is24hr);
        }
        // Create the date labels if the chart includes multiple days
        if (calStart.get(Calendar.DAY_OF_YEAR) != calEnd.get(Calendar.DAY_OF_YEAR) || calStart.get(Calendar.YEAR) != calEnd.get(Calendar.YEAR)) {
            boolean isDayFirst = isDayFirst();
            calStart.set(Calendar.HOUR_OF_DAY, 0);
            startRoundTime = calStart.getTimeInMillis();
            if (startRoundTime < mStartWallTime) {
                calStart.set(Calendar.DAY_OF_YEAR, calStart.get(Calendar.DAY_OF_YEAR) + 1);
                startRoundTime = calStart.getTimeInMillis();
            }
            calEnd.set(Calendar.HOUR_OF_DAY, 0);
            endRoundTime = calEnd.getTimeInMillis();
            if (startRoundTime < endRoundTime) {
                addDateLabel(calStart, mLevelLeft, mLevelRight, isDayFirst);
                Calendar calMid = Calendar.getInstance();
                // The middle between two beginnings of days can be anywhere between -1 to 13
                // after the beginning of the "median" day.
                calMid.setTimeInMillis(startRoundTime + ((endRoundTime - startRoundTime) / 2) + 2 * 60 * 60 * 1000);
                calMid.set(Calendar.HOUR_OF_DAY, 0);
                calMid.set(Calendar.MINUTE, 0);
                long calMidMillis = calMid.getTimeInMillis();
                if (calMidMillis > startRoundTime && calMidMillis < endRoundTime) {
                    addDateLabel(calMid, mLevelLeft, mLevelRight, isDayFirst);
                }
            }
            addDateLabel(calEnd, mLevelLeft, mLevelRight, isDayFirst);
        }
    }
    if (mTimeLabels.size() < 2) {
        // If there are fewer than 2 time labels, then they are useless.  Just
        // show an axis label giving the entire duration.
        mDurationString = Formatter.formatShortElapsedTime(getContext(), mEndWallTime - mStartWallTime);
        mDurationStringWidth = (int) mTextPaint.measureText(mDurationString);
    } else {
        mDurationString = null;
        mDurationStringWidth = 0;
    }
}
Also used : Path(android.graphics.Path) HistoryItem(android.os.BatteryStats.HistoryItem) Calendar(java.util.Calendar) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 5 with HistoryItem

use of android.os.BatteryStats.HistoryItem in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class BatteryHistoryChart method setStats.

void setStats(BatteryStats stats, Intent broadcast) {
    mStats = stats;
    mBatteryBroadcast = broadcast;
    if (DEBUG)
        Log.d(TAG, "Setting stats...");
    final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
    long uSecTime = mStats.computeBatteryRealtime(elapsedRealtimeUs, BatteryStats.STATS_SINCE_CHARGED);
    mStatsPeriod = uSecTime;
    mChargingLabel = getContext().getString(R.string.battery_stats_charging_label);
    mScreenOnLabel = getContext().getString(R.string.battery_stats_screen_on_label);
    mGpsOnLabel = getContext().getString(R.string.battery_stats_gps_on_label);
    mCameraOnLabel = getContext().getString(R.string.battery_stats_camera_on_label);
    mFlashlightOnLabel = getContext().getString(R.string.battery_stats_flashlight_on_label);
    mWifiRunningLabel = getContext().getString(R.string.battery_stats_wifi_running_label);
    mCpuRunningLabel = getContext().getString(R.string.battery_stats_wake_lock_label);
    mPhoneSignalLabel = getContext().getString(R.string.battery_stats_phone_signal_label);
    mMaxPercentLabelString = Utils.formatPercentage(100);
    mMinPercentLabelString = Utils.formatPercentage(0);
    mInfo = BatteryInfo.getBatteryInfo(getContext(), mBatteryBroadcast, mStats, elapsedRealtimeUs);
    mDrainString = "";
    mChargeDurationString = "";
    setContentDescription(mInfo.mChargeLabelString);
    int pos = 0;
    int lastInteresting = 0;
    byte lastLevel = -1;
    mBatLow = 0;
    mBatHigh = 100;
    mStartWallTime = 0;
    mEndDataWallTime = 0;
    mEndWallTime = 0;
    mHistStart = 0;
    mHistEnd = 0;
    long lastWallTime = 0;
    long lastRealtime = 0;
    int aggrStates = 0;
    int aggrStates2 = 0;
    boolean first = true;
    if (stats.startIteratingHistoryLocked()) {
        final HistoryItem rec = new HistoryItem();
        while (stats.getNextHistoryLocked(rec)) {
            pos++;
            if (first) {
                first = false;
                mHistStart = rec.time;
            }
            if (rec.cmd == HistoryItem.CMD_CURRENT_TIME || rec.cmd == HistoryItem.CMD_RESET) {
                // pretty much just noise.
                if (rec.currentTime > (lastWallTime + (180 * 24 * 60 * 60 * 1000L)) || rec.time < (mHistStart + (5 * 60 * 1000L))) {
                    mStartWallTime = 0;
                }
                lastWallTime = rec.currentTime;
                lastRealtime = rec.time;
                if (mStartWallTime == 0) {
                    mStartWallTime = lastWallTime - (lastRealtime - mHistStart);
                }
            }
            if (rec.isDeltaData()) {
                if (rec.batteryLevel != lastLevel || pos == 1) {
                    lastLevel = rec.batteryLevel;
                }
                lastInteresting = pos;
                mHistDataEnd = rec.time;
                aggrStates |= rec.states;
                aggrStates2 |= rec.states2;
            }
        }
    }
    mHistEnd = mHistDataEnd + (mInfo.remainingTimeUs / 1000);
    mEndDataWallTime = lastWallTime + mHistDataEnd - lastRealtime;
    mEndWallTime = mEndDataWallTime + (mInfo.remainingTimeUs / 1000);
    mNumHist = lastInteresting;
    mHaveGps = (aggrStates & HistoryItem.STATE_GPS_ON_FLAG) != 0;
    mHaveFlashlight = (aggrStates2 & HistoryItem.STATE2_FLASHLIGHT_FLAG) != 0;
    mHaveCamera = (aggrStates2 & HistoryItem.STATE2_CAMERA_FLAG) != 0;
    mHaveWifi = (aggrStates2 & HistoryItem.STATE2_WIFI_RUNNING_FLAG) != 0 || (aggrStates & (HistoryItem.STATE_WIFI_FULL_LOCK_FLAG | HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG | HistoryItem.STATE_WIFI_SCAN_FLAG)) != 0;
    if (!com.android.settings.Utils.isWifiOnly(getContext())) {
        mHavePhoneSignal = true;
    }
    if (mHistEnd <= mHistStart)
        mHistEnd = mHistStart + 1;
}
Also used : HistoryItem(android.os.BatteryStats.HistoryItem) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

HistoryItem (android.os.BatteryStats.HistoryItem)12 Context (android.content.Context)5 SparseIntArray (android.util.SparseIntArray)5 Paint (android.graphics.Paint)2 TextPaint (android.text.TextPaint)2 Path (android.graphics.Path)1 Calendar (java.util.Calendar)1