public class NetworkFactory extends Handler
register()
and
their factory will start receiving scored NetworkRequests. NetworkRequests
can be filtered 3 ways: by NetworkCapabilities, by score and more complexly by
overridden function. All of these can be dynamic - changing NetworkCapabilities
or score forces re-evaluation of all current requests.
If any requests pass the filter some overrideable functions will be called.
If the bearer only cares about very simple start/stopNetwork callbacks, those
functions can be overridden. If the bearer needs more interaction, it can
override addNetworkRequest and removeNetworkRequest which will give it each
request that passes their current filters.Handler.Callback
Modifier and Type | Field and Description |
---|---|
static int |
CMD_CANCEL_REQUEST
Cancel a network request
msg.obj = NetworkRequest
|
static int |
CMD_REQUEST_NETWORK
Pass a network request to the bearer.
|
Constructor and Description |
---|
NetworkFactory(Looper looper,
Context context,
String logTag,
NetworkCapabilities filter) |
Modifier and Type | Method and Description |
---|---|
boolean |
acceptRequest(NetworkRequest request,
int score)
Overridable function to provide complex filtering.
|
void |
addNetworkRequest(NetworkRequest networkRequest,
int score) |
void |
dump(FileDescriptor fd,
PrintWriter writer,
String[] args) |
protected int |
getRequestCount() |
protected void |
handleAddRequest(NetworkRequest request,
int score) |
void |
handleMessage(Message msg)
Subclasses must implement this to receive messages.
|
protected void |
handleRemoveRequest(NetworkRequest request) |
protected void |
log(String s) |
protected void |
needNetworkFor(NetworkRequest networkRequest,
int score) |
void |
register() |
protected void |
releaseNetworkFor(NetworkRequest networkRequest) |
void |
removeNetworkRequest(NetworkRequest networkRequest) |
void |
setCapabilityFilter(NetworkCapabilities netCap) |
void |
setScoreFilter(int score) |
protected void |
startNetwork() |
protected void |
stopNetwork() |
String |
toString()
Returns a string representation of the object.
|
void |
unregister() |
dispatchMessage, dump, getLooper, getMessageName, getTraceName, hasCallbacks, hasMessages, hasMessages, obtainMessage, obtainMessage, obtainMessage, obtainMessage, obtainMessage, post, postAtFrontOfQueue, postAtTime, postAtTime, postDelayed, removeCallbacks, removeCallbacks, removeCallbacksAndMessages, removeMessages, removeMessages, runWithScissors, sendEmptyMessage, sendEmptyMessageAtTime, sendEmptyMessageDelayed, sendMessage, sendMessageAtFrontOfQueue, sendMessageAtTime, sendMessageDelayed
public static final int CMD_REQUEST_NETWORK
public static final int CMD_CANCEL_REQUEST
public NetworkFactory(Looper looper, Context context, String logTag, NetworkCapabilities filter)
public void register()
public void unregister()
public void handleMessage(Message msg)
Handler
handleMessage
in class Handler
protected void handleAddRequest(NetworkRequest request, int score)
protected void handleRemoveRequest(NetworkRequest request)
public boolean acceptRequest(NetworkRequest request, int score)
needNetworkFor(android.net.NetworkRequest, int)
and
releaseNetworkFor(android.net.NetworkRequest)
for every request that passes the filters.
If you don't need to see every request, you can leave the base
implementations of those two functions and instead override
startNetwork()
and stopNetwork()
.
If you want to see every score fluctuation on every request, set
your score filter to a very high number and watch needNetworkFor(android.net.NetworkRequest, int)
.true
to accept the request.protected void startNetwork()
protected void stopNetwork()
protected void needNetworkFor(NetworkRequest networkRequest, int score)
protected void releaseNetworkFor(NetworkRequest networkRequest)
public void addNetworkRequest(NetworkRequest networkRequest, int score)
public void removeNetworkRequest(NetworkRequest networkRequest)
public void setScoreFilter(int score)
public void setCapabilityFilter(NetworkCapabilities netCap)
protected int getRequestCount()
protected void log(String s)
public void dump(FileDescriptor fd, PrintWriter writer, String[] args)
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())