1 /*
2  * Copyright (C) 2024 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.batteryusage.db;
18 
19 import static com.android.settings.fuelgauge.batteryusage.ConvertUtils.utcToLocalTimeForLogging;
20 
21 import com.android.settings.fuelgauge.batteryusage.BatteryReattribute;
22 import com.android.settings.fuelgauge.batteryusage.ConvertUtils;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.VisibleForTesting;
26 import androidx.room.Entity;
27 import androidx.room.PrimaryKey;
28 
29 /** A {@link Entity} for battery usage reattribution data in the database. */
30 @Entity
31 public class BatteryReattributeEntity {
32 
33     /** The start timestamp of this record data. */
34     @PrimaryKey
35     public final long timestampStart;
36 
37     /** The end timestamp of this record data. */
38     public final long timestampEnd;
39 
40     /** The battery usage reattribution data for corresponding  uids. */
41     public final String reattributeData;
42 
BatteryReattributeEntity(@onNull BatteryReattribute batteryReattribute)43     public BatteryReattributeEntity(@NonNull BatteryReattribute batteryReattribute) {
44         this(
45                 batteryReattribute.getTimestampStart(),
46                 batteryReattribute.getTimestampEnd(),
47                 ConvertUtils.encodeBatteryReattribute(batteryReattribute));
48     }
49 
50     @VisibleForTesting
BatteryReattributeEntity( long timestampStart, long timestampEnd, @NonNull String reattributeData)51     BatteryReattributeEntity(
52             long timestampStart, long timestampEnd, @NonNull String reattributeData) {
53         this.timestampStart = timestampStart;
54         this.timestampEnd = timestampEnd;
55         this.reattributeData = reattributeData;
56     }
57 
58     @NonNull
59     @Override
toString()60     public String toString() {
61         final BatteryReattribute batteryReattribute =
62                 ConvertUtils.decodeBatteryReattribute(reattributeData);
63         final StringBuilder builder = new StringBuilder()
64                 .append("\nBatteryReattributeEntity{")
65                 .append("\n\t" + utcToLocalTimeForLogging(timestampStart))
66                 .append("\n\t" + utcToLocalTimeForLogging(timestampEnd))
67                 .append("\n\t" + batteryReattribute);
68         if (batteryReattribute != null) {
69             builder.append("\n\t" + batteryReattribute.getReattributeDataMap());
70         }
71         return builder.append("\n}").toString();
72     }
73 }
74