[MS10.1] Move multiplySafeByRational to NetworkStatsUtils
Since NetworkStats related code will be moved to the module. multiplySafeByRational in the NetworkUtilsInternal cannot be accessed after that. Thus, create another utils class that will be moved with NetworkStats code and put the function into it. Test: atest NetworkStaticLibTests:com.android.net.moduletests.util.NetworkStatsUtilsTest Bug: 204830222 Change-Id: I96f3ac02e57b7325ed53988285770f478dee529e
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