Send explicit broadcasts when the scorer changes.

When the scorer is changed send a targeted broadcast to the previous
scorer (if any) and then a targeted broadcast to the new scorer.

BUG:26815773
Change-Id: If28414f4373a531b10f581ecd096cbc27a7318a4
This commit is contained in:
Jeremy Joslin
2016-02-10 07:31:33 -08:00
parent 18bdf443e3
commit da11f5cdf5
2 changed files with 17 additions and 4 deletions

View File

@@ -105,7 +105,8 @@ public class NetworkScoreManager {
/**
* Broadcast 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
* if another scorer has been selected. This is an explicit broadcast only sent to the
* previous scorer and new scorer. Note that it is unnecessary to clear existing scores as
* this is handled by the system.
*
* <p>The new scorer will be specified in {@link #EXTRA_NEW_SCORER}.

View File

@@ -234,12 +234,24 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
// safety as scores should never be compared across apps; in practice, Settings should
// only be allowing valid apps to be set as scorers, so failure here should be rare.
clearInternal();
// Get the scorer that is about to be replaced, if any, so we can notify it directly.
NetworkScorerAppData prevScorer = NetworkScorerAppManager.getActiveScorer(mContext);
boolean result = NetworkScorerAppManager.setActiveScorer(mContext, packageName);
if (result) {
if (result) { // new scorer successfully set
registerPackageReceiverIfNeeded();
Intent intent = new Intent(NetworkScoreManager.ACTION_SCORER_CHANGED);
intent.putExtra(NetworkScoreManager.EXTRA_NEW_SCORER, packageName);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
if (prevScorer != null) { // Directly notify the old scorer.
intent.setPackage(prevScorer.mPackageName);
// TODO: Need to update when we support per-user scorers. http://b/23422763
mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
}
if (packageName != null) { // Then notify the new scorer
intent.putExtra(NetworkScoreManager.EXTRA_NEW_SCORER, packageName);
intent.setPackage(packageName);
// TODO: Need to update when we support per-user scorers. http://b/23422763
mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
}
}
return result;
} finally {