Home | History | Annotate | Download | only in storagemonitoring
      1 /*
      2  * Copyright (C) 2017 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 package android.car.storagemonitoring;
     17 
     18 import android.annotation.NonNull;
     19 import android.annotation.SystemApi;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 import java.time.Instant;
     23 import java.util.Objects;
     24 
     25 import static java.util.Objects.requireNonNull;
     26 
     27 /**
     28  * Change in wear-out information.
     29  *
     30  * Contains information about the first cycle during which the system detected a change
     31  * in wear-out information of the flash storage.
     32  *
     33  * @hide
     34  */
     35 @SystemApi
     36 public class WearEstimateChange implements Parcelable {
     37     public static final Parcelable.Creator<WearEstimateChange> CREATOR =
     38         new Parcelable.Creator<WearEstimateChange>() {
     39             public WearEstimateChange createFromParcel(Parcel in) {
     40                 return new WearEstimateChange(in);
     41             }
     42 
     43             public WearEstimateChange[] newArray(int size) {
     44                 return new WearEstimateChange[size];
     45             }
     46         };
     47 
     48     /**
     49      * The previous wear estimate.
     50      */
     51     public final @NonNull WearEstimate oldEstimate;
     52 
     53     /**
     54      * The new wear estimate.
     55      */
     56     public final @NonNull WearEstimate newEstimate;
     57 
     58     /**
     59      * Total CarService uptime when this change was detected.
     60      */
     61     public final long uptimeAtChange;
     62 
     63     /**
     64      * Wall-clock time when this change was detected.
     65      */
     66     public final @NonNull Instant dateAtChange;
     67 
     68     /**
     69      * Whether this change was within the vendor range for acceptable flash degradation.
     70      */
     71     public final boolean isAcceptableDegradation;
     72 
     73     public WearEstimateChange(WearEstimate oldEstimate,
     74                               WearEstimate newEstimate,
     75                               long uptimeAtChange,
     76                               Instant dateAtChange,
     77                               boolean isAcceptableDegradation) {
     78         if (uptimeAtChange < 0) {
     79             throw new IllegalArgumentException("uptimeAtChange must be >= 0");
     80         }
     81         this.oldEstimate = requireNonNull(oldEstimate);
     82         this.newEstimate = requireNonNull(newEstimate);
     83         this.uptimeAtChange = uptimeAtChange;
     84         this.dateAtChange = requireNonNull(dateAtChange);
     85         this.isAcceptableDegradation = isAcceptableDegradation;
     86     }
     87 
     88     public WearEstimateChange(Parcel in) {
     89         oldEstimate = in.readParcelable(WearEstimate.class.getClassLoader());
     90         newEstimate = in.readParcelable(WearEstimate.class.getClassLoader());
     91         uptimeAtChange = in.readLong();
     92         dateAtChange = Instant.ofEpochMilli(in.readLong());
     93         isAcceptableDegradation = in.readInt() == 1;
     94     }
     95 
     96     @Override
     97     public int describeContents() {
     98         return 0;
     99     }
    100 
    101     @Override
    102     public void writeToParcel(Parcel dest, int flags) {
    103         dest.writeParcelable(oldEstimate, flags);
    104         dest.writeParcelable(newEstimate, flags);
    105         dest.writeLong(uptimeAtChange);
    106         dest.writeLong(dateAtChange.toEpochMilli());
    107         dest.writeInt(isAcceptableDegradation ? 1 : 0);
    108     }
    109 
    110     @Override
    111     public boolean equals(Object other) {
    112         if (other instanceof WearEstimateChange) {
    113             WearEstimateChange wo = (WearEstimateChange)other;
    114             return wo.isAcceptableDegradation == isAcceptableDegradation &&
    115                 wo.uptimeAtChange == uptimeAtChange &&
    116                 wo.dateAtChange.equals(dateAtChange) &&
    117                 wo.oldEstimate.equals(oldEstimate) &&
    118                 wo.newEstimate.equals(newEstimate);
    119         }
    120         return false;
    121     }
    122 
    123     @Override
    124     public int hashCode() {
    125         return Objects.hash(oldEstimate,
    126                             newEstimate,
    127                             uptimeAtChange,
    128                             dateAtChange,
    129                             isAcceptableDegradation);
    130     }
    131 
    132     @Override
    133     public String toString() {
    134         return String.format(
    135                 "wear change{old level=%s, new level=%s, uptime=%d, date=%s, acceptable=%s}",
    136                 oldEstimate,
    137                 newEstimate,
    138                 uptimeAtChange,
    139                 dateAtChange,
    140                 isAcceptableDegradation ? "yes" : "no");
    141     }
    142 
    143 }
    144