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

Commit a3894e67 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add new database table and proto for battery usage reattribution (2/5)" into main

parents c0f7e758 f5cf54bc
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -201,6 +201,20 @@ public final class ConvertUtils {
        return defaultInstance;
    }

    /** Gets the encoded string from {@link BatteryReattribute} instance. */
    @NonNull
    public static String encodeBatteryReattribute(
            @NonNull BatteryReattribute batteryReattribute) {
        return Base64.encodeToString(batteryReattribute.toByteArray(), Base64.DEFAULT);
    }

    /** Gets the decoded {@link BatteryReattribute} instance from string. */
    @NonNull
    public static BatteryReattribute decodeBatteryReattribute(@NonNull String content) {
        return BatteryUtils.parseProtoFromString(
                content, BatteryReattribute.getDefaultInstance());
    }

    /** Converts to {@link BatteryHistEntry} */
    public static BatteryHistEntry convertToBatteryHistEntry(
            BatteryEntry entry, BatteryUsageStats batteryUsageStats) {
+3 −0
Original line number Diff line number Diff line
@@ -429,6 +429,7 @@ public final class DatabaseUtils {
                        database.batteryEventDao().clearAll();
                        database.batteryStateDao().clearAll();
                        database.batteryUsageSlotDao().clearAll();
                        database.batteryReattributeDao().clearAll();
                    } catch (RuntimeException e) {
                        Log.e(TAG, "clearAll() failed", e);
                    }
@@ -446,6 +447,7 @@ public final class DatabaseUtils {
                        database.batteryEventDao().clearAllAfter(startTimestamp);
                        database.batteryStateDao().clearAllAfter(startTimestamp);
                        database.batteryUsageSlotDao().clearAllAfter(startTimestamp);
                        database.batteryReattributeDao().clearAllAfter(startTimestamp);
                    } catch (RuntimeException e) {
                        Log.e(TAG, "clearAllAfter() failed", e);
                    }
@@ -466,6 +468,7 @@ public final class DatabaseUtils {
                        database.batteryEventDao().clearAllBefore(earliestTimestamp);
                        database.batteryStateDao().clearAllBefore(earliestTimestamp);
                        database.batteryUsageSlotDao().clearAllBefore(earliestTimestamp);
                        database.batteryReattributeDao().clearAllBefore(earliestTimestamp);
                    } catch (RuntimeException e) {
                        Log.e(TAG, "clearAllBefore() failed", e);
                    }
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.batteryusage.db;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.List;

/** DAO for accessing {@link BatteryReattributeEntity} in the database. */
@Dao
public interface BatteryReattributeDao {

    /** Inserts a {@link BatteryReattributeEntity} data into the database. */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(event: BatteryReattributeEntity)

    /** Gets all recorded data after a specific timestamp. */
    @Query(
            "SELECT * FROM BatteryReattributeEntity WHERE "
                    + "timestampStart >= :timestampStart ORDER BY timestampStart DESC")
    fun getAllAfter(timestampStart: Long): List<BatteryReattributeEntity>

    /** Deletes all recorded data before a specific timestamp. */
    @Query("DELETE FROM BatteryReattributeEntity WHERE timestampStart <= :timestampStart")
    fun clearAllBefore(timestampStart: Long)

    /** Deletes all recorded data after a specific timestamp. */
    @Query("DELETE FROM BatteryReattributeEntity WHERE timestampStart >= :timestampStart")
    fun clearAllAfter(timestampStart: Long)

    /** Clears all recorded data in the database. */
    @Query("DELETE FROM BatteryReattributeEntity") fun clearAll()
}
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.batteryusage.db;

import static com.android.settings.fuelgauge.batteryusage.ConvertUtils.utcToLocalTimeForLogging;

import com.android.settings.fuelgauge.batteryusage.BatteryReattribute;
import com.android.settings.fuelgauge.batteryusage.ConvertUtils;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

/** A {@link Entity} for battery usage reattribution data in the database. */
@Entity
public class BatteryReattributeEntity {

    /** The start timestamp of this record data. */
    @PrimaryKey
    public final long timestampStart;

    /** The end timestamp of this record data. */
    public final long timestampEnd;

    /** The battery usage reattribution data for corresponding  uids. */
    public final String reattributeData;

    public BatteryReattributeEntity(@NonNull BatteryReattribute batteryReattribute) {
        this(
                batteryReattribute.getTimestampStart(),
                batteryReattribute.getTimestampEnd(),
                ConvertUtils.encodeBatteryReattribute(batteryReattribute));
    }

    @VisibleForTesting
    BatteryReattributeEntity(
            long timestampStart, long timestampEnd, @NonNull String reattributeData) {
        this.timestampStart = timestampStart;
        this.timestampEnd = timestampEnd;
        this.reattributeData = reattributeData;
    }

    @NonNull
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder()
                .append("\nBatteryReattributeEntity{")
                .append("\n\t" + utcToLocalTimeForLogging(timestampStart))
                .append("\n\t" + utcToLocalTimeForLogging(timestampEnd))
                .append("\n}");
        return builder.toString();
    }
}
+9 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge.batteryusage.db;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@@ -29,11 +30,13 @@ import androidx.room.RoomDatabase;
            AppUsageEventEntity.class,
            BatteryEventEntity.class,
            BatteryState.class,
            BatteryUsageSlotEntity.class
            BatteryUsageSlotEntity.class,
            BatteryReattributeEntity.class
        },
        version = 1)
public abstract class BatteryStateDatabase extends RoomDatabase {
    private static final String TAG = "BatteryStateDatabase";
    private static final String DB_FILE_NAME = "battery-usage-db-v10";

    private static BatteryStateDatabase sBatteryStateDatabase;

@@ -49,11 +52,15 @@ public abstract class BatteryStateDatabase extends RoomDatabase {
    /** Provides DAO for battery usage slot table. */
    public abstract BatteryUsageSlotDao batteryUsageSlotDao();

    /** Provides DAO for battery reattribution table. */
    @NonNull
    public abstract BatteryReattributeDao batteryReattributeDao();

    /** Gets or creates an instance of {@link RoomDatabase}. */
    public static BatteryStateDatabase getInstance(Context context) {
        if (sBatteryStateDatabase == null) {
            sBatteryStateDatabase =
                    Room.databaseBuilder(context, BatteryStateDatabase.class, "battery-usage-db-v9")
                    Room.databaseBuilder(context, BatteryStateDatabase.class, DB_FILE_NAME)
                            // Allows accessing data in the main thread for dumping bugreport.
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigration()
Loading