[NS01] Add NetworkScore

As attested by numerous TODOs in the code, a new way of
representing network quality and policy is needed instead
of an int.

An int representing the quality of the network requires
all parties using it to know how all other parties are
using it, and implementation details about the decision
algorithm. For all intents and purposes, the selection
is left to individual network factories who try to
achieve a desired result while piecing together all
possible states of the system.

As the number of such cases and desires increases, this
becomes both intractable and unmaintainable. Indeed, at
this time in the codebase nobody can really predict exactly
how a given change in score will affect selection across
the board, and it is essentially impossible to figure out
the behavior of network selection by inspecting the code
because the moving parts are scattered throughout the
entire codebase.

Having an object encapsulating policy and quality values
will let us centralize the selection and make it again
possible to maintain without knowledge of all behaviors
of all network factories. It will also provide better
guarantees of respecting policy, and allow bugfixes that
were not possible before because they'd touch too many
parts of the code.

Test: FrameworksNetTests FrameworksWifiTests NetworkStackTests
Change-Id: I3185a6412b9b659798faf0c6882699e9c63cc115
This commit is contained in:
Chalard Jean
2020-12-21 18:36:52 +09:00
parent fb8aad8c9b
commit 5c4bb91134
12 changed files with 209 additions and 69 deletions

View File

@@ -0,0 +1,20 @@
/**
* Copyright (c) 2021, 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 android.net;
parcelable NetworkScore;

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2021 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 android.net;
import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Object representing the quality of a network as perceived by the user.
*
* A NetworkScore object represents the characteristics of a network that affects how good the
* network is considered for a particular use.
* @hide
*/
// TODO : @SystemApi when the implementation is complete
public final class NetworkScore implements Parcelable {
// This will be removed soon. Do *NOT* depend on it for any new code that is not part of
// a migration.
private final int mLegacyInt;
/** @hide */
NetworkScore(final int legacyInt) {
this.mLegacyInt = legacyInt;
}
private NetworkScore(@NonNull final Parcel in) {
mLegacyInt = in.readInt();
}
public int getLegacyInt() {
return mLegacyInt;
}
@Override
public String toString() {
return "Score(" + mLegacyInt + ")";
}
@Override
public void writeToParcel(@NonNull final Parcel dest, final int flags) {
dest.writeInt(mLegacyInt);
}
@Override
public int describeContents() {
return 0;
}
@NonNull public static final Creator<NetworkScore> CREATOR = new Creator<>() {
@Override
@NonNull
public NetworkScore createFromParcel(@NonNull final Parcel in) {
return new NetworkScore(in);
}
@Override
@NonNull
public NetworkScore[] newArray(int size) {
return new NetworkScore[size];
}
};
/**
* A builder for NetworkScore.
*/
public static final class Builder {
private static final int INVALID_LEGACY_INT = Integer.MIN_VALUE;
private int mLegacyInt = INVALID_LEGACY_INT;
/**
* Sets the legacy int for this score.
*
* Do not rely on this. It will be gone by the time S is released.
*
* @param score the legacy int
* @return this
*/
@NonNull
public Builder setLegacyInt(final int score) {
mLegacyInt = score;
return this;
}
/**
* Builds this NetworkScore.
* @return The built NetworkScore object.
*/
@NonNull
public NetworkScore build() {
return new NetworkScore(mLegacyInt);
}
}
}