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 usage slot into database. */
31 @Entity
32 public class BatteryUsageSlotEntity {
33     /** Keys for accessing {@link ContentValues}. */
34     public static final String KEY_TIMESTAMP = "timestamp";
35 
36     public static final String KEY_BATTERY_USAGE_SLOT = "batteryUsageSlot";
37 
38     @PrimaryKey(autoGenerate = true)
39     private long mId;
40 
41     public final long timestamp;
42     public final String batteryUsageSlot;
43 
BatteryUsageSlotEntity(final long timestamp, final String batteryUsageSlot)44     public BatteryUsageSlotEntity(final long timestamp, final String batteryUsageSlot) {
45         this.timestamp = timestamp;
46         this.batteryUsageSlot = batteryUsageSlot;
47     }
48 
49     /** Sets the auto-generated content ID. */
setId(long id)50     public void setId(long id) {
51         this.mId = id;
52     }
53 
54     /** Gets the auto-generated content ID. */
getId()55     public long getId() {
56         return mId;
57     }
58 
59     @Override
toString()60     public String toString() {
61         final String recordAtDateTime = ConvertUtils.utcToLocalTimeForLogging(timestamp);
62         final StringBuilder builder =
63                 new StringBuilder()
64                         .append("\nBatteryUsageSlot{")
65                         .append(
66                                 String.format(
67                                         Locale.US,
68                                         "\n\ttimestamp=%s|batteryUsageSlot=%s",
69                                         recordAtDateTime,
70                                         batteryUsageSlot))
71                         .append("\n}");
72         return builder.toString();
73     }
74 
75     /** Creates new {@link BatteryUsageSlotEntity} from {@link ContentValues}. */
create(ContentValues contentValues)76     public static BatteryUsageSlotEntity create(ContentValues contentValues) {
77         Builder builder = BatteryUsageSlotEntity.newBuilder();
78         if (contentValues.containsKey(KEY_TIMESTAMP)) {
79             builder.setTimestamp(contentValues.getAsLong(KEY_TIMESTAMP));
80         }
81         if (contentValues.containsKey(KEY_BATTERY_USAGE_SLOT)) {
82             builder.setBatteryUsageSlot(contentValues.getAsString(KEY_BATTERY_USAGE_SLOT));
83         }
84         return builder.build();
85     }
86 
87     /** Creates a new {@link Builder} instance. */
newBuilder()88     public static Builder newBuilder() {
89         return new Builder();
90     }
91 
92     /** A convenience builder class to improve readability. */
93     public static class Builder {
94         private long mTimestamp;
95         private String mBatteryUsageSlot;
96 
97         /** Sets the timestamp. */
98         @CanIgnoreReturnValue
setTimestamp(final long timestamp)99         public Builder setTimestamp(final long timestamp) {
100             mTimestamp = timestamp;
101             return this;
102         }
103 
104         /** Sets the battery usage slot. */
105         @CanIgnoreReturnValue
setBatteryUsageSlot(final String batteryUsageSlot)106         public Builder setBatteryUsageSlot(final String batteryUsageSlot) {
107             mBatteryUsageSlot = batteryUsageSlot;
108             return this;
109         }
110 
111         /** Builds the {@link BatteryUsageSlotEntity}. */
build()112         public BatteryUsageSlotEntity build() {
113             return new BatteryUsageSlotEntity(mTimestamp, mBatteryUsageSlot);
114         }
115 
Builder()116         private Builder() {}
117     }
118 }
119