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.car.internal.property;
18 
19 import android.car.hardware.CarPropertyValue;
20 
21 /**
22  * A {@link CarPropertyEventTracker} implementation for on-change property
23  *
24  * @hide
25  */
26 public final class OnChangeCarPropertyEventTracker implements CarPropertyEventTracker {
27     private static final String TAG = "OnChangeCarPropertyEventTracker";
28     private static final float INVALID_UPDATE_RATE_HZ = 0f;
29 
30     private final Logger mLogger;
31     private long mPreviousEventTimeNanos;
32     private CarPropertyValue<?> mCurrentCarPropertyValue;
33 
OnChangeCarPropertyEventTracker(boolean useSystemLogger)34     public OnChangeCarPropertyEventTracker(boolean useSystemLogger) {
35         mLogger = new Logger(useSystemLogger, TAG);
36     }
37 
38     @Override
getUpdateRateHz()39     public float getUpdateRateHz() {
40         return INVALID_UPDATE_RATE_HZ;
41     }
42 
43     @Override
getCurrentCarPropertyValue()44     public CarPropertyValue<?> getCurrentCarPropertyValue() {
45         return mCurrentCarPropertyValue;
46     }
47 
48     /** Returns true if the client needs to be updated for this event. */
49     @Override
hasUpdate(CarPropertyValue<?> carPropertyValue)50     public boolean hasUpdate(CarPropertyValue<?> carPropertyValue) {
51         if (carPropertyValue.getTimestamp() < mPreviousEventTimeNanos) {
52             if (mLogger.dbg()) {
53                 mLogger.logD(String.format("hasUpdate: Dropping carPropertyValue: %s, "
54                         + "because getTimestamp()=%d < previousEventTimeNanos=%d",
55                         carPropertyValue, carPropertyValue.getTimestamp(),
56                         mPreviousEventTimeNanos));
57             }
58             return false;
59         }
60         mPreviousEventTimeNanos = carPropertyValue.getTimestamp();
61         mCurrentCarPropertyValue = carPropertyValue;
62         return true;
63     }
64 }
65