use of android.net.NetworkScorerAppManager.NetworkScorerAppData in project android_frameworks_base by AOSPA.
the class NetworkScoreService method bindToScoringServiceIfNeeded.
private void bindToScoringServiceIfNeeded() {
if (DBG)
Log.d(TAG, "bindToScoringServiceIfNeeded");
NetworkScorerAppData scorerData = NetworkScorerAppManager.getActiveScorer(mContext);
bindToScoringServiceIfNeeded(scorerData);
}
use of android.net.NetworkScorerAppManager.NetworkScorerAppData in project android_frameworks_base by AOSPA.
the class NetworkScoreService method registerPackageMonitorIfNeeded.
private void registerPackageMonitorIfNeeded() {
if (DBG)
Log.d(TAG, "registerPackageMonitorIfNeeded");
NetworkScorerAppData scorer = NetworkScorerAppManager.getActiveScorer(mContext);
synchronized (mPackageMonitorLock) {
// Unregister the current monitor if needed.
if (mPackageMonitor != null) {
if (DBG) {
Log.d(TAG, "Unregistering package monitor for " + mPackageMonitor.mRegisteredPackage);
}
mPackageMonitor.unregister();
mPackageMonitor = null;
}
// Create and register the monitor if a scorer is active.
if (scorer != null) {
mPackageMonitor = new NetworkScorerPackageMonitor(scorer.mPackageName);
// TODO: Need to update when we support per-user scorers. http://b/23422763
mPackageMonitor.register(mContext, null, /* thread */
UserHandle.SYSTEM, false);
if (DBG) {
Log.d(TAG, "Registered package monitor for " + mPackageMonitor.mRegisteredPackage);
}
}
}
}
use of android.net.NetworkScorerAppManager.NetworkScorerAppData in project android_frameworks_base by AOSPA.
the class NetworkScoreService method setScorerInternal.
/** Set the active scorer. Callers are responsible for checking permissions as appropriate. */
private boolean setScorerInternal(String packageName) {
if (DBG)
Log.d(TAG, "setScorerInternal(" + packageName + ")");
long token = Binder.clearCallingIdentity();
try {
unbindFromScoringServiceIfNeeded();
// Preemptively clear scores even though the set operation could fail. We do this for
// 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);
// Unconditionally attempt to bind to the current scorer. If setActiveScorer() failed
// then we'll attempt to restore the previous binding (if any), otherwise an attempt
// will be made to bind to the new scorer.
bindToScoringServiceIfNeeded();
if (result) {
// new scorer successfully set
registerPackageMonitorIfNeeded();
Intent intent = new Intent(NetworkScoreManager.ACTION_SCORER_CHANGED);
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 {
Binder.restoreCallingIdentity(token);
}
}
use of android.net.NetworkScorerAppManager.NetworkScorerAppData in project android_frameworks_base by crdroidandroid.
the class NetworkScorerAppManagerTest method testGetAllValidScorers.
public void testGetAllValidScorers() throws Exception {
// Package 1 - Valid scorer.
ResolveInfoHolder package1 = buildResolveInfo("package1", 1, true, true, false, false);
// Package 2 - Receiver does not have BROADCAST_NETWORK_PRIVILEGED permission.
ResolveInfoHolder package2 = buildResolveInfo("package2", 2, false, true, false, false);
// Package 3 - App does not have SCORE_NETWORKS permission.
ResolveInfoHolder package3 = buildResolveInfo("package3", 3, true, false, false, false);
// Package 4 - Valid scorer w/ optional config activity.
ResolveInfoHolder package4 = buildResolveInfo("package4", 4, true, true, true, false);
// Package 5 - Valid scorer w/ optional service to bind to.
ResolveInfoHolder package5 = buildResolveInfo("package5", 5, true, true, false, true);
List<ResolveInfoHolder> scorers = new ArrayList<>();
scorers.add(package1);
scorers.add(package2);
scorers.add(package3);
scorers.add(package4);
scorers.add(package5);
setScorers(scorers);
Iterator<NetworkScorerAppData> result = NetworkScorerAppManager.getAllValidScorers(mMockContext).iterator();
assertTrue(result.hasNext());
NetworkScorerAppData next = result.next();
assertEquals("package1", next.mPackageName);
assertEquals(1, next.mPackageUid);
assertNull(next.mConfigurationActivityClassName);
assertTrue(result.hasNext());
next = result.next();
assertEquals("package4", next.mPackageName);
assertEquals(4, next.mPackageUid);
assertEquals(".ConfigActivity", next.mConfigurationActivityClassName);
assertTrue(result.hasNext());
next = result.next();
assertEquals("package5", next.mPackageName);
assertEquals(5, next.mPackageUid);
assertEquals(".ScoringService", next.mScoringServiceClassName);
assertFalse(result.hasNext());
}
use of android.net.NetworkScorerAppManager.NetworkScorerAppData in project android_frameworks_base by crdroidandroid.
the class NetworkScoreService method dump.
@Override
protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
mContext.enforceCallingOrSelfPermission(permission.DUMP, TAG);
NetworkScorerAppData currentScorer = NetworkScorerAppManager.getActiveScorer(mContext);
if (currentScorer == null) {
writer.println("Scoring is disabled.");
return;
}
writer.println("Current scorer: " + currentScorer.mPackageName);
for (INetworkScoreCache scoreCache : getScoreCaches()) {
try {
scoreCache.asBinder().dump(fd, args);
} catch (RemoteException e) {
writer.println("Unable to dump score cache");
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Unable to dump score cache", e);
}
}
}
if (mServiceConnection != null) {
mServiceConnection.dump(fd, writer, args);
} else {
writer.println("ScoringServiceConnection: null");
}
writer.flush();
}
Aggregations