diff options
| author | 2019-12-12 19:13:42 +0000 | |
|---|---|---|
| committer | 2019-12-12 19:13:42 +0000 | |
| commit | 5ba888c60947d27f5e777c260dcfe8c352a2fe9f (patch) | |
| tree | 1cb031aad02780154f0142409cb52250d67aa5f2 | |
| parent | 876469ea3a4955125b57f5a79a68d6a8a21b13d1 (diff) | |
| parent | c32c015715a036c6db11e13ee22f63838bac5b3c (diff) | |
Merge "Remove some duplication of validation logic" am: d8aa23e1ca am: 94131bee05 am: c32c015715
Change-Id: I11db4139682d9845820e1718b1d585f54115a546
| -rw-r--r-- | services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java index 8441edb3e4b7..1b1ac6d3ed07 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorStrategyImpl.java @@ -114,16 +114,10 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } @Override - public synchronized void suggestManualTime(ManualTimeSuggestion suggestion) { + public synchronized void suggestManualTime(@NonNull ManualTimeSuggestion suggestion) { final TimestampedValue<Long> newUtcTime = suggestion.getUtcTime(); - // We can validate the suggestion against the reference time clock. - long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis(); - if (elapsedRealtimeMillis < newUtcTime.getReferenceTimeMillis()) { - // elapsedRealtime clock went backwards? - Slog.w(LOG_TAG, "New reference time is in the future? Ignoring." - + " elapsedRealtimeMillis=" + elapsedRealtimeMillis - + ", timeSuggestion=" + suggestion); + if (!validateSuggestionTime(newUtcTime, suggestion)) { return; } @@ -202,62 +196,76 @@ public final class TimeDetectorStrategyImpl implements TimeDetectorStrategy { } @GuardedBy("this") - private boolean validateAndStorePhoneSuggestion(@NonNull PhoneTimeSuggestion timeSuggestion) { - if (timeSuggestion.getUtcTime().getValue() == null) { - Slog.w(LOG_TAG, "Suggestion utcTime contains null value" - + " timeSuggestion=" + timeSuggestion); + private boolean validateAndStorePhoneSuggestion(@NonNull PhoneTimeSuggestion suggestion) { + TimestampedValue<Long> newUtcTime = suggestion.getUtcTime(); + if (!validateSuggestionTime(newUtcTime, suggestion)) { + // There's probably nothing useful we can do: elsewhere we assume that reference + // times are in the past so just stop here. return false; } - int phoneId = timeSuggestion.getPhoneId(); + int phoneId = suggestion.getPhoneId(); LinkedList<PhoneTimeSuggestion> phoneSuggestions = mSuggestionByPhoneId.get(phoneId); if (phoneSuggestions == null) { // The first time we've seen this phoneId. phoneSuggestions = new LinkedList<>(); mSuggestionByPhoneId.put(phoneId, phoneSuggestions); } else if (phoneSuggestions.isEmpty()) { - Slog.w(LOG_TAG, "Suggestions unexpectedly empty when adding" - + " timeSuggestion=" + timeSuggestion); + Slog.w(LOG_TAG, "Suggestions unexpectedly empty when adding suggestion=" + suggestion); } if (!phoneSuggestions.isEmpty()) { - PhoneTimeSuggestion previousSuggestion = phoneSuggestions.getFirst(); - // We can log / discard suggestions with obvious issues with the reference time clock. - long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis(); - TimestampedValue<Long> newTime = timeSuggestion.getUtcTime(); - if (elapsedRealtimeMillis < newTime.getReferenceTimeMillis()) { - // elapsedRealtime clock went backwards? - Slog.w(LOG_TAG, "New reference time is in the future?" - + " elapsedRealtimeMillis=" + elapsedRealtimeMillis - + ", timeSuggestion=" + timeSuggestion); - // There's probably nothing useful we can do: elsewhere we assume that reference - // times are in the past so just stop here. + PhoneTimeSuggestion previousSuggestion = phoneSuggestions.getFirst(); + if (previousSuggestion == null + || previousSuggestion.getUtcTime() == null + || previousSuggestion.getUtcTime().getValue() == null) { + // This should be impossible given we only store validated suggestions. + Slog.w(LOG_TAG, "Previous suggestion is null or has a null time." + + " previousSuggestion=" + previousSuggestion + + ", suggestion=" + suggestion); return false; } - if (previousSuggestion.getUtcTime() != null) { - long referenceTimeDifference = TimestampedValue.referenceTimeDifference( - timeSuggestion.getUtcTime(), previousSuggestion.getUtcTime()); - if (referenceTimeDifference < 0) { - // The reference time is before the previously received suggestion. Ignore it. - Slog.w(LOG_TAG, "Out of order phone suggestion received." - + " referenceTimeDifference=" + referenceTimeDifference - + " lastSuggestion=" + previousSuggestion - + " newSuggestion=" + timeSuggestion); - return false; - } + long referenceTimeDifference = TimestampedValue.referenceTimeDifference( + newUtcTime, previousSuggestion.getUtcTime()); + if (referenceTimeDifference < 0) { + // The reference time is before the previously received suggestion. Ignore it. + Slog.w(LOG_TAG, "Out of order phone suggestion received." + + " referenceTimeDifference=" + referenceTimeDifference + + " previousSuggestion=" + previousSuggestion + + " suggestion=" + suggestion); + return false; } } // Store the latest suggestion. - phoneSuggestions.addFirst(timeSuggestion); + phoneSuggestions.addFirst(suggestion); if (phoneSuggestions.size() > KEEP_SUGGESTION_HISTORY_SIZE) { phoneSuggestions.removeLast(); } return true; } + private boolean validateSuggestionTime( + @NonNull TimestampedValue<Long> newUtcTime, @NonNull Object suggestion) { + if (newUtcTime.getValue() == null) { + Slog.w(LOG_TAG, "Suggested time value is null. suggestion=" + suggestion); + return false; + } + + // We can validate the suggestion against the reference time clock. + long elapsedRealtimeMillis = mCallback.elapsedRealtimeMillis(); + if (elapsedRealtimeMillis < newUtcTime.getReferenceTimeMillis()) { + // elapsedRealtime clock went backwards? + Slog.w(LOG_TAG, "New reference time is in the future? Ignoring." + + " elapsedRealtimeMillis=" + elapsedRealtimeMillis + + ", suggestion=" + suggestion); + return false; + } + return true; + } + @GuardedBy("this") private void doAutoTimeDetection(@NonNull String detectionReason) { if (!mCallback.isAutoTimeDetectionEnabled()) { |