Merge "[MS10.1] Move multiplySafeByRational to NetworkStatsUtils" am: 6ff6240694 am: 239ffb98df am: c12c9ad508
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1920477 Change-Id: I6bff9f75d9426978d2d4077a3bc54d5c0cb0fe4a
This commit is contained in:
@@ -74,35 +74,4 @@ public class NetworkUtilsInternal {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely multiple a value by a rational.
|
||||
* <p>
|
||||
* Internally it uses integer-based math whenever possible, but switches
|
||||
* over to double-based math if values would overflow.
|
||||
* @hide
|
||||
*/
|
||||
public static long multiplySafeByRational(long value, long num, long den) {
|
||||
if (den == 0) {
|
||||
throw new ArithmeticException("Invalid Denominator");
|
||||
}
|
||||
long x = value;
|
||||
long y = num;
|
||||
|
||||
// Logic shamelessly borrowed from Math.multiplyExact()
|
||||
long r = x * y;
|
||||
long ax = Math.abs(x);
|
||||
long ay = Math.abs(y);
|
||||
if (((ax | ay) >>> 31 != 0)) {
|
||||
// Some bits greater than 2^31 that might cause overflow
|
||||
// Check the result using the divide operator
|
||||
// and check for the special case of Long.MIN_VALUE * -1
|
||||
if (((y != 0) && (r / y != x))
|
||||
|| (x == Long.MIN_VALUE && y == -1)) {
|
||||
// Use double math to avoid overflowing
|
||||
return (long) (((double) num / den) * value);
|
||||
}
|
||||
}
|
||||
return r / den;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package android.net;
|
||||
|
||||
import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
|
||||
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.annotation.NonNull;
|
||||
|
||||
@@ -30,7 +30,7 @@ import static android.net.NetworkStats.UID_ALL;
|
||||
import static android.net.TrafficStats.UID_REMOVED;
|
||||
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
|
||||
|
||||
import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
|
||||
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;
|
||||
|
||||
import android.os.Binder;
|
||||
import android.service.NetworkStatsCollectionKeyProto;
|
||||
|
||||
@@ -28,8 +28,8 @@ import static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
|
||||
import static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
|
||||
import static android.text.format.DateUtils.SECOND_IN_MILLIS;
|
||||
|
||||
import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
|
||||
import static com.android.internal.util.ArrayUtils.total;
|
||||
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;
|
||||
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.os.Build;
|
||||
|
||||
Reference in New Issue
Block a user