NetworkScoreManagerpublic class NetworkScoreManager extends Object Class that manages communication between network subsystems and a network scorer.
You can get an instance of this class by calling
{@link android.content.Context#getSystemService(String)}:
NetworkScoreManager manager =
(NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE)
A network scorer is any application which:
- Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
- Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the
{@link android.Manifest.permission#BROADCAST_NETWORK_PRIVILEGED} permission which scores
networks and (eventually) calls {@link #updateScores} with the results. If this receiver
specifies an android:label attribute, this label will be used when referring to the
application throughout system settings; otherwise, the application label will be used.
The system keeps track of an active scorer application; at any time, only this application
will receive {@link #ACTION_SCORE_NETWORKS} broadcasts and will be permitted to call
{@link #updateScores}. Applications may determine the current active scorer with
{@link #getActiveScorerPackage()} and request to change the active scorer by sending an
{@link #ACTION_CHANGE_ACTIVE} broadcast with another scorer. |
Fields Summary |
---|
public static final String | ACTION_CHANGE_ACTIVEActivity action: ask the user to change the active network scorer. This will show a dialog
that asks the user whether they want to replace the current active scorer with the one
specified in {@link #EXTRA_PACKAGE_NAME}. The activity will finish with RESULT_OK if the
active scorer was changed or RESULT_CANCELED if it failed for any reason. | public static final String | EXTRA_PACKAGE_NAMEExtra used with {@link #ACTION_CHANGE_ACTIVE} to specify the new scorer package. Set with
{@link android.content.Intent#putExtra(String, String)}. | public static final String | ACTION_SCORE_NETWORKSBroadcast action: new network scores are being requested. This intent will only be delivered
to the current active scorer app. That app is responsible for scoring the networks and
calling {@link #updateScores} when complete. The networks to score are specified in
{@link #EXTRA_NETWORKS_TO_SCORE}, and will generally consist of all networks which have been
configured by the user as well as any open networks.
This is a protected intent that can only be sent by the system. | public static final String | EXTRA_NETWORKS_TO_SCOREExtra used with {@link #ACTION_SCORE_NETWORKS} to specify the networks to be scored, as an
array of {@link NetworkKey}s. Can be obtained with
{@link android.content.Intent#getParcelableArrayExtra(String)}}. | public static final String | ACTION_CUSTOM_ENABLEActivity action: launch a custom activity for configuring a scorer before enabling it.
Scorer applications may choose to specify an activity for this action, in which case the
framework will launch that activity which should return RESULT_OK if scoring was enabled.
If no activity is included in a scorer which implements this action, the system dialog for
selecting a scorer will be shown instead. | public static final String | ACTION_SCORER_CHANGEDBroadcast action: the active scorer has been changed. Scorer apps may listen to this to
perform initialization once selected as the active scorer, or clean up unneeded resources
if another scorer has been selected. Note that it is unnecessary to clear existing scores as
this is handled by the system.
The new scorer will be specified in {@link #EXTRA_NEW_SCORER}.
This is a protected intent that can only be sent by the system. | public static final String | EXTRA_NEW_SCORERExtra used with {@link #ACTION_SCORER_CHANGED} to specify the newly selected scorer's package
name. Will be null if scoring was disabled. Can be obtained with
{@link android.content.Intent#getStringExtra(String)}. | private final android.content.Context | mContext | private final INetworkScoreService | mService |
Constructors Summary |
---|
public NetworkScoreManager(android.content.Context context)
mContext = context;
IBinder iBinder = ServiceManager.getService(Context.NETWORK_SCORE_SERVICE);
mService = INetworkScoreService.Stub.asInterface(iBinder);
|
Methods Summary |
---|
public boolean | clearScores()Clear network scores.
Should be called when all scores need to be invalidated, i.e. because the scoring
algorithm has changed and old scores can no longer be compared to future scores.
Note that scores will be cleared automatically when the active scorer changes, as scores
from one scorer cannot be compared to those from another scorer.
try {
return mService.clearScores();
} catch (RemoteException e) {
return false;
}
| public void | disableScoring()Turn off network scoring.
May only be called by the current scorer app, or the system.
try {
mService.disableScoring();
} catch (RemoteException e) {
}
| public java.lang.String | getActiveScorerPackage()Obtain the package name of the current active network scorer.
At any time, only one scorer application will receive {@link #ACTION_SCORE_NETWORKS}
broadcasts and be allowed to call {@link #updateScores}. Applications may use this method to
determine the current scorer and offer the user the ability to select a different scorer via
the {@link #ACTION_CHANGE_ACTIVE} intent.
NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
if (app == null) {
return null;
}
return app.mPackageName;
| public void | registerNetworkScoreCache(int networkType, INetworkScoreCache scoreCache)Register a network score cache.
try {
mService.registerNetworkScoreCache(networkType, scoreCache);
} catch (RemoteException e) {
}
| public boolean | requestScores(NetworkKey[] networks)Request scoring for networks.
Note that this is just a helper method to assemble the broadcast, and will run in the
calling process.
String activeScorer = getActiveScorerPackage();
if (activeScorer == null) {
return false;
}
Intent intent = new Intent(ACTION_SCORE_NETWORKS);
intent.setPackage(activeScorer);
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(EXTRA_NETWORKS_TO_SCORE, networks);
// A scorer should never become active if its package doesn't hold SCORE_NETWORKS, but
// ensure the package still holds it to be extra safe.
mContext.sendBroadcastAsUser(intent, UserHandle.OWNER, Manifest.permission.SCORE_NETWORKS);
return true;
| public boolean | setActiveScorer(java.lang.String packageName)Set the active scorer to a new package and clear existing scores.
Should never be called directly without obtaining user consent. This can be done by using
the {@link #ACTION_CHANGE_ACTIVE} broadcast, or using a custom configuration activity.
try {
return mService.setActiveScorer(packageName);
} catch (RemoteException e) {
return false;
}
| public boolean | updateScores(ScoredNetwork[] networks)Update network scores.
This may be called at any time to re-score active networks. Scores will generally be
updated quickly, but if this method is called too frequently, the scores may be held and
applied at a later time.
try {
return mService.updateScores(networks);
} catch (RemoteException e) {
return false;
}
|
|