diff options
| author | 2017-08-30 19:07:31 +0000 | |
|---|---|---|
| committer | 2017-08-30 19:07:31 +0000 | |
| commit | ef1c48fa054b39de5f76c85c2cc0fa9a45b69bc2 (patch) | |
| tree | f96256cfc5e8572f64a03da0c8b5d58a6b1698df | |
| parent | cdd40b81b42e0b4c5b0ff56e581ed67a8e1953b9 (diff) | |
| parent | 381b228bb643badcc521d5eab511fc68cd7c531b (diff) | |
Merge "Merge "Adds ERROR state to ImsConfig" into oc-mr1-dev am: ed6e29f292 am: 063d5e871a"
| -rw-r--r-- | telephony/java/com/android/ims/ImsConfig.java | 1 | ||||
| -rw-r--r-- | telephony/java/com/android/internal/telephony/ExponentialBackoff.java | 84 |
2 files changed, 85 insertions, 0 deletions
diff --git a/telephony/java/com/android/ims/ImsConfig.java b/telephony/java/com/android/ims/ImsConfig.java index e7b22bdfadcc..cf4c47bf541f 100644 --- a/telephony/java/com/android/ims/ImsConfig.java +++ b/telephony/java/com/android/ims/ImsConfig.java @@ -524,6 +524,7 @@ public class ImsConfig { * Defines IMS feature value. */ public static class FeatureValueConstants { + public static final int ERROR = -1; public static final int OFF = 0; public static final int ON = 1; } diff --git a/telephony/java/com/android/internal/telephony/ExponentialBackoff.java b/telephony/java/com/android/internal/telephony/ExponentialBackoff.java new file mode 100644 index 000000000000..80958c077d6a --- /dev/null +++ b/telephony/java/com/android/internal/telephony/ExponentialBackoff.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.telephony; + +import android.annotation.NonNull; +import android.os.Handler; +import android.os.Looper; + +/** The implementation of exponential backoff with jitter applied. */ +public class ExponentialBackoff { + private int mRetryCounter; + private long mStartDelayMs; + private long mMaximumDelayMs; + private long mCurrentDelayMs; + private int mMultiplier; + private Runnable mRunnable; + private Handler mHandler; + + public ExponentialBackoff( + long initialDelayMs, + long maximumDelayMs, + int multiplier, + @NonNull Looper looper, + @NonNull Runnable runnable) { + this(initialDelayMs, maximumDelayMs, multiplier, new Handler(looper), runnable); + } + + public ExponentialBackoff( + long initialDelayMs, + long maximumDelayMs, + int multiplier, + @NonNull Handler handler, + @NonNull Runnable runnable) { + mRetryCounter = 0; + mStartDelayMs = initialDelayMs; + mMaximumDelayMs = maximumDelayMs; + mMultiplier = multiplier; + mHandler = handler; + mRunnable = runnable; + } + + /** Starts the backoff, the runnable will be executed after {@link #mStartDelayMs}. */ + public void start() { + mRetryCounter = 0; + mCurrentDelayMs = mStartDelayMs; + mHandler.removeCallbacks(mRunnable); + mHandler.postDelayed(mRunnable, mCurrentDelayMs); + } + + /** Stops the backoff, all pending messages will be removed from the message queue. */ + public void stop() { + mRetryCounter = 0; + mHandler.removeCallbacks(mRunnable); + } + + /** Should call when the retry action has failed and we want to retry after a longer delay. */ + public void notifyFailed() { + mRetryCounter++; + long temp = Math.min( + mMaximumDelayMs, (long) (mStartDelayMs * Math.pow(mMultiplier, mRetryCounter))); + mCurrentDelayMs = (long) (((1 + Math.random()) / 2) * temp); + mHandler.removeCallbacks(mRunnable); + mHandler.postDelayed(mRunnable, mCurrentDelayMs); + } + + /** Returns the delay for the most recently posted message. */ + public long getCurrentDelay() { + return mCurrentDelayMs; + } +} |