• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.content.ContentValues;
20 
21 import androidx.room.Entity;
22 import androidx.room.PrimaryKey;
23 
24 import com.android.settings.fuelgauge.batteryusage.ConvertUtils;
25 
26 import com.google.errorprone.annotations.CanIgnoreReturnValue;
27 
28 import java.util.Locale;
29 
30 /** A {@link Entity} class to save battery events into database. */
31 @Entity
32 public class BatteryEventEntity {
33     /** Keys for accessing {@link ContentValues}. */
34     public static final String KEY_TIMESTAMP = "timestamp";
35 
36     public static final String KEY_BATTERY_EVENT_TYPE = "batteryEventType";
37     public static final String KEY_BATTERY_LEVEL = "batteryLevel";
38 
39     @PrimaryKey(autoGenerate = true)
40     private long mId;
41 
42     public final long timestamp;
43     public final int batteryEventType;
44     public final int batteryLevel;
45 
BatteryEventEntity( final long timestamp, final int batteryEventType, final int batteryLevel)46     public BatteryEventEntity(
47             final long timestamp, final int batteryEventType, final int batteryLevel) {
48         this.timestamp = timestamp;
49         this.batteryEventType = batteryEventType;
50         this.batteryLevel = batteryLevel;
51     }
52 
53     /** Sets the auto-generated content ID. */
setId(long id)54     public void setId(long id) {
55         this.mId = id;
56     }
57 
58     /** Gets the auto-generated content ID. */
getId()59     public long getId() {
60         return mId;
61     }
62 
63     @Override
toString()64     public String toString() {
65         final String recordAtDateTime = ConvertUtils.utcToLocalTimeForLogging(timestamp);
66         final StringBuilder builder =
67                 new StringBuilder()
68                         .append("\nBatteryEvent{")
69                         .append(
70                                 String.format(
71                                         Locale.US,
72                                         "\n\ttimestamp=%s|batteryEventType=%d|batteryLevel=%d",
73                                         recordAtDateTime,
74                                         batteryEventType,
75                                         batteryLevel))
76                         .append("\n}");
77         return builder.toString();
78     }
79 
80     /** Creates new {@link BatteryEventEntity} from {@link ContentValues}. */
create(ContentValues contentValues)81     public static BatteryEventEntity create(ContentValues contentValues) {
82         Builder builder = BatteryEventEntity.newBuilder();
83         if (contentValues.containsKey(KEY_TIMESTAMP)) {
84             builder.setTimestamp(contentValues.getAsLong(KEY_TIMESTAMP));
85         }
86         if (contentValues.containsKey(KEY_BATTERY_EVENT_TYPE)) {
87             builder.setBatteryEventType(contentValues.getAsInteger(KEY_BATTERY_EVENT_TYPE));
88         }
89         if (contentValues.containsKey(KEY_BATTERY_LEVEL)) {
90             builder.setBatteryLevel(contentValues.getAsInteger(KEY_BATTERY_LEVEL));
91         }
92         return builder.build();
93     }
94 
95     /** Creates a new {@link Builder} instance. */
newBuilder()96     public static Builder newBuilder() {
97         return new Builder();
98     }
99 
100     /** A convenience builder class to improve readability. */
101     public static class Builder {
102         private long mTimestamp;
103         private int mBatteryEventType;
104         private int mBatteryLevel;
105 
106         /** Sets the timestamp. */
107         @CanIgnoreReturnValue
setTimestamp(final long timestamp)108         public Builder setTimestamp(final long timestamp) {
109             mTimestamp = timestamp;
110             return this;
111         }
112 
113         /** Sets the battery event type. */
114         @CanIgnoreReturnValue
setBatteryEventType(final int batteryEventType)115         public Builder setBatteryEventType(final int batteryEventType) {
116             mBatteryEventType = batteryEventType;
117             return this;
118         }
119 
120         /** Sets the battery level. */
121         @CanIgnoreReturnValue
setBatteryLevel(final int batteryLevel)122         public Builder setBatteryLevel(final int batteryLevel) {
123             mBatteryLevel = batteryLevel;
124             return this;
125         }
126 
127         /** Builds the {@link BatteryEventEntity}. */
build()128         public BatteryEventEntity build() {
129             return new BatteryEventEntity(mTimestamp, mBatteryEventType, mBatteryLevel);
130         }
131 
Builder()132         private Builder() {}
133     }
134 }
135