Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1ea8a05 authored by Jason Monk's avatar Jason Monk
Browse files

Fix and update battery detail page

Bug: 27205329
Change-Id: Ie984348d59c1b15bec7283a94a94489f78cacb79
parent 60df76b1
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingEnd="56dp"
    android:paddingTop="4dp"
    android:orientation="vertical">

    <TextView
        android:id="@android:id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="?android:attr/textColorSecondary" />

    <com.android.settings.fuelgauge.BatteryActiveView
        android:id="@+id/battery_active"
        android:layout_width="match_parent"
        android:layout_height="6dp" />

</LinearLayout>
+93 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:orientation="vertical">

    <TextView
        android:id="@+id/charge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="36sp"
        android:textColor="?android:attr/colorAccent" />

    <TextView
        android:id="@+id/estimation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="?android:attr/textColorSecondary" />

    <com.android.settingslib.graph.UsageView
        android:id="@+id/battery_usage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        settings:sideLabels="@array/battery_labels"
        android:colorAccent="?android:attr/colorAccent"
        android:gravity="end"
        settings:textColor="?android:attr/textColorSecondary" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:background="?android:attr/listDivider" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include android:id="@+id/cell_network_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/camera_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/flashlight_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/gps_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/wifi_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/cpu_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/screen_on_group"
                layout="@layout/battery_active_view" />

            <include android:id="@+id/charging_group"
                layout="@layout/battery_active_view" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.settings.fuelgauge;

import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.View;

public class BatteryActiveView extends View {

    private final Paint mPaint = new Paint();
    private BatteryActiveProvider mProvider;

    public BatteryActiveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setProvider(BatteryActiveProvider provider) {
        mProvider = provider;
        if (getWidth() != 0) {
            postInvalidate();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (getWidth() != 0) {
            postInvalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mProvider == null) {
            return;
        }
        SparseIntArray array = mProvider.getColorArray();
        float period = mProvider.getPeriod();
        for (int i = 0; i < array.size() - 1; i++) {
            drawColor(canvas, array.keyAt(i), array.keyAt(i + 1), array.valueAt(i), period);
        }
    }

    private void drawColor(Canvas canvas, int start, int end, int color, float period) {
        if (color == 0) {
            return;
        }
        mPaint.setColor(color);
        canvas.drawRect(start / period * getWidth(), 0, end / period * getWidth(), getHeight(),
                mPaint);
    }

    public interface BatteryActiveProvider {
        boolean hasData();
        long getPeriod();
        SparseIntArray getColorArray();
    }
}
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.settings.fuelgauge;

import android.os.BatteryStats.HistoryItem;
import android.telephony.ServiceState;
import android.util.SparseIntArray;
import com.android.settings.Utils;
import com.android.settings.fuelgauge.BatteryActiveView.BatteryActiveProvider;
import com.android.settingslib.BatteryInfo;

public class BatteryCellParser implements BatteryInfo.BatteryDataParser, BatteryActiveProvider {

    private final SparseIntArray mData = new SparseIntArray();

    private int mLastValue;
    private long mLength;
    private long mLastTime;

    protected int getValue(HistoryItem rec) {
        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;
        }
        return bin;
    }

    @Override
    public void onParsingStarted(long startTime, long endTime) {
        mLength = endTime - startTime;
    }

    @Override
    public void onDataPoint(long time, HistoryItem record) {
        int value = getValue(record);
        if (value != mLastValue) {
            mData.put((int) time, value);
            mLastValue = value;
        }
        mLastTime = time;
    }

    @Override
    public void onDataGap() {
        if (mLastValue != 0) {
            mData.put((int) mLastTime, 0);
            mLastValue = 0;
        }
    }

    @Override
    public void onParsingDone() {
        if (mLastValue != 0) {
            mData.put((int) mLastTime, 0);
            mLastValue = 0;
        }
    }

    @Override
    public long getPeriod() {
        return mLength;
    }

    @Override
    public boolean hasData() {
        return mData.size() > 1;
    }

    @Override
    public SparseIntArray getColorArray() {
        SparseIntArray ret = new SparseIntArray();
        for (int i = 0; i < mData.size(); i++) {
            ret.put(mData.keyAt(i), getColor(mData.valueAt(i)));
        }
        return ret;
    }

    private int getColor(int i) {
        return Utils.BADNESS_COLORS[i];
    }
}
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.settings.fuelgauge;

import android.os.BatteryStats.HistoryItem;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import com.android.settings.fuelgauge.BatteryActiveView.BatteryActiveProvider;
import com.android.settingslib.BatteryInfo;

public class BatteryFlagParser implements BatteryInfo.BatteryDataParser, BatteryActiveProvider {

    private final SparseBooleanArray mData = new SparseBooleanArray();
    private final int mFlag;
    private final boolean mState2;
    private final int mAccentColor;

    private boolean mLastSet;
    private long mLength;
    private long mLastTime;

    public BatteryFlagParser(int accent, boolean state2, int flag) {
        mAccentColor = accent;
        mFlag = flag;
        mState2 = state2;
    }

    protected boolean isSet(HistoryItem record) {
        return ((mState2 ? record.states2 : record.states) & mFlag) != 0;
    }

    @Override
    public void onParsingStarted(long startTime, long endTime) {
        mLength = endTime - startTime;
    }

    @Override
    public void onDataPoint(long time, HistoryItem record) {
        boolean isSet = isSet(record);
        if (isSet != mLastSet) {
            mData.put((int) time, isSet);
            mLastSet = isSet;
        }
        mLastTime = time;
    }

    @Override
    public void onDataGap() {
        if (mLastSet) {
            mData.put((int) mLastTime, false);
            mLastSet = false;
        }
    }

    @Override
    public void onParsingDone() {
        if (mLastSet) {
            mData.put((int) mLastTime, false);
            mLastSet = false;
        }
    }

    @Override
    public long getPeriod() {
        return mLength;
    }

    @Override
    public boolean hasData() {
        return mData.size() > 1;
    }

    @Override
    public SparseIntArray getColorArray() {
        SparseIntArray ret = new SparseIntArray();
        for (int i = 0; i < mData.size(); i++) {
            ret.put(mData.keyAt(i), getColor(mData.valueAt(i)));
        }
        return ret;
    }

    private int getColor(boolean b) {
        if (b) {
            return mAccentColor;
        }
        return 0;
    }
}
Loading