summaryrefslogtreecommitdiff
path: root/com/android/server/location/ExponentialBackOff.java
diff options
context:
space:
mode:
Diffstat (limited to 'com/android/server/location/ExponentialBackOff.java')
-rw-r--r--com/android/server/location/ExponentialBackOff.java32
1 files changed, 32 insertions, 0 deletions
diff --git a/com/android/server/location/ExponentialBackOff.java b/com/android/server/location/ExponentialBackOff.java
new file mode 100644
index 00000000..8c77b217
--- /dev/null
+++ b/com/android/server/location/ExponentialBackOff.java
@@ -0,0 +1,32 @@
+package com.android.server.location;
+
+/**
+ * A simple implementation of exponential backoff.
+ */
+class ExponentialBackOff {
+ private static final int MULTIPLIER = 2;
+ private final long mInitIntervalMillis;
+ private final long mMaxIntervalMillis;
+ private long mCurrentIntervalMillis;
+
+ ExponentialBackOff(long initIntervalMillis, long maxIntervalMillis) {
+ mInitIntervalMillis = initIntervalMillis;
+ mMaxIntervalMillis = maxIntervalMillis;
+
+ mCurrentIntervalMillis = mInitIntervalMillis / MULTIPLIER;
+ }
+
+ long nextBackoffMillis() {
+ if (mCurrentIntervalMillis > mMaxIntervalMillis) {
+ return mMaxIntervalMillis;
+ }
+
+ mCurrentIntervalMillis *= MULTIPLIER;
+ return mCurrentIntervalMillis;
+ }
+
+ void reset() {
+ mCurrentIntervalMillis = mInitIntervalMillis / MULTIPLIER;
+ }
+}
+