use of android.app.IUidObserver in project android_frameworks_base by DirtyUnicorns.
the class ActivityManagerService method dispatchUidsChanged.
private void dispatchUidsChanged() {
int N;
synchronized (this) {
N = mPendingUidChanges.size();
if (mActiveUidChanges.length < N) {
mActiveUidChanges = new UidRecord.ChangeItem[N];
}
for (int i = 0; i < N; i++) {
final UidRecord.ChangeItem change = mPendingUidChanges.get(i);
mActiveUidChanges[i] = change;
if (change.uidRecord != null) {
change.uidRecord.pendingChange = null;
change.uidRecord = null;
}
}
mPendingUidChanges.clear();
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "*** Delivering " + N + " uid changes");
}
if (mLocalPowerManager != null) {
for (int j = 0; j < N; j++) {
UidRecord.ChangeItem item = mActiveUidChanges[j];
if (item.change == UidRecord.CHANGE_GONE || item.change == UidRecord.CHANGE_GONE_IDLE) {
mLocalPowerManager.uidGone(item.uid);
} else {
mLocalPowerManager.updateUidProcState(item.uid, item.processState);
}
}
}
int i = mUidObservers.beginBroadcast();
while (i > 0) {
i--;
final IUidObserver observer = mUidObservers.getBroadcastItem(i);
final int which = (Integer) mUidObservers.getBroadcastCookie(i);
if (observer != null) {
try {
for (int j = 0; j < N; j++) {
UidRecord.ChangeItem item = mActiveUidChanges[j];
final int change = item.change;
UidRecord validateUid = null;
if (VALIDATE_UID_STATES && i == 0) {
validateUid = mValidateUids.get(item.uid);
if (validateUid == null && change != UidRecord.CHANGE_GONE && change != UidRecord.CHANGE_GONE_IDLE) {
validateUid = new UidRecord(item.uid);
mValidateUids.put(item.uid, validateUid);
}
}
if (change == UidRecord.CHANGE_IDLE || change == UidRecord.CHANGE_GONE_IDLE) {
if ((which & ActivityManager.UID_OBSERVER_IDLE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID idle uid=" + item.uid);
observer.onUidIdle(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
if (validateUid != null) {
validateUid.idle = true;
}
}
} else if (change == UidRecord.CHANGE_ACTIVE) {
if ((which & ActivityManager.UID_OBSERVER_ACTIVE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID active uid=" + item.uid);
observer.onUidActive(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
validateUid.idle = false;
}
}
if (change == UidRecord.CHANGE_GONE || change == UidRecord.CHANGE_GONE_IDLE) {
if ((which & ActivityManager.UID_OBSERVER_GONE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID gone uid=" + item.uid);
observer.onUidGone(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
if (validateUid != null) {
mValidateUids.remove(item.uid);
}
}
} else {
if ((which & ActivityManager.UID_OBSERVER_PROCSTATE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID CHANGED uid=" + item.uid + ": " + item.processState);
observer.onUidStateChanged(item.uid, item.processState);
}
if (VALIDATE_UID_STATES && i == 0) {
validateUid.curProcState = validateUid.setProcState = item.processState;
}
}
}
} catch (RemoteException e) {
}
}
}
mUidObservers.finishBroadcast();
synchronized (this) {
for (int j = 0; j < N; j++) {
mAvailUidChanges.add(mActiveUidChanges[j]);
}
}
}
use of android.app.IUidObserver in project platform_frameworks_base by android.
the class NetworkPolicyManagerServiceTest method callSystemReady.
@Before
public void callSystemReady() throws Exception {
MockitoAnnotations.initMocks(this);
final Context context = InstrumentationRegistry.getContext();
setCurrentTimeMillis(TEST_START);
// intercept various broadcasts, and pretend that uids have packages
mServiceContext = new BroadcastInterceptingContext(context) {
@Override
public PackageManager getPackageManager() {
return mPackageManager;
}
@Override
public void startActivity(Intent intent) {
// ignored
}
};
mPolicyDir = context.getFilesDir();
if (mPolicyDir.exists()) {
IoUtils.deleteContents(mPolicyDir);
}
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
mUidObserver = (IUidObserver) invocation.getArguments()[0];
Log.d(TAG, "set mUidObserver to " + mUidObserver);
return null;
}
}).when(mActivityManager).registerUidObserver(any(), anyInt());
mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
mService.bindConnectivityManager(mConnManager);
mService.bindNotificationManager(mNotifManager);
mPolicyListener = new NetworkPolicyListenerAnswer(mService);
// Sets some common expectations.
when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenAnswer(new Answer<PackageInfo>() {
@Override
public PackageInfo answer(InvocationOnMock invocation) throws Throwable {
final String packageName = (String) invocation.getArguments()[0];
final PackageInfo info = new PackageInfo();
final Signature signature;
if ("android".equals(packageName)) {
signature = new Signature("F00D");
} else {
signature = new Signature("DEAD");
}
info.signatures = new Signature[] { signature };
return info;
}
});
when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(new ApplicationInfo());
when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] { PKG_NAME_A });
when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true);
expectCurrentTime();
// Prepare NPMS.
mService.systemReady();
// catch INetworkManagementEventObserver during systemReady()
ArgumentCaptor<INetworkManagementEventObserver> networkObserver = ArgumentCaptor.forClass(INetworkManagementEventObserver.class);
verify(mNetworkManager).registerObserver(networkObserver.capture());
mNetworkObserver = networkObserver.getValue();
}
use of android.app.IUidObserver in project platform_frameworks_base by android.
the class ActivityManagerService method dispatchUidsChanged.
private void dispatchUidsChanged() {
int N;
synchronized (this) {
N = mPendingUidChanges.size();
if (mActiveUidChanges.length < N) {
mActiveUidChanges = new UidRecord.ChangeItem[N];
}
for (int i = 0; i < N; i++) {
final UidRecord.ChangeItem change = mPendingUidChanges.get(i);
mActiveUidChanges[i] = change;
if (change.uidRecord != null) {
change.uidRecord.pendingChange = null;
change.uidRecord = null;
}
}
mPendingUidChanges.clear();
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "*** Delivering " + N + " uid changes");
}
if (mLocalPowerManager != null) {
for (int j = 0; j < N; j++) {
UidRecord.ChangeItem item = mActiveUidChanges[j];
if (item.change == UidRecord.CHANGE_GONE || item.change == UidRecord.CHANGE_GONE_IDLE) {
mLocalPowerManager.uidGone(item.uid);
} else {
mLocalPowerManager.updateUidProcState(item.uid, item.processState);
}
}
}
int i = mUidObservers.beginBroadcast();
while (i > 0) {
i--;
final IUidObserver observer = mUidObservers.getBroadcastItem(i);
final int which = (Integer) mUidObservers.getBroadcastCookie(i);
if (observer != null) {
try {
for (int j = 0; j < N; j++) {
UidRecord.ChangeItem item = mActiveUidChanges[j];
final int change = item.change;
UidRecord validateUid = null;
if (VALIDATE_UID_STATES && i == 0) {
validateUid = mValidateUids.get(item.uid);
if (validateUid == null && change != UidRecord.CHANGE_GONE && change != UidRecord.CHANGE_GONE_IDLE) {
validateUid = new UidRecord(item.uid);
mValidateUids.put(item.uid, validateUid);
}
}
if (change == UidRecord.CHANGE_IDLE || change == UidRecord.CHANGE_GONE_IDLE) {
if ((which & ActivityManager.UID_OBSERVER_IDLE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID idle uid=" + item.uid);
observer.onUidIdle(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
if (validateUid != null) {
validateUid.idle = true;
}
}
} else if (change == UidRecord.CHANGE_ACTIVE) {
if ((which & ActivityManager.UID_OBSERVER_ACTIVE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID active uid=" + item.uid);
observer.onUidActive(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
validateUid.idle = false;
}
}
if (change == UidRecord.CHANGE_GONE || change == UidRecord.CHANGE_GONE_IDLE) {
if ((which & ActivityManager.UID_OBSERVER_GONE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID gone uid=" + item.uid);
observer.onUidGone(item.uid);
}
if (VALIDATE_UID_STATES && i == 0) {
if (validateUid != null) {
mValidateUids.remove(item.uid);
}
}
} else {
if ((which & ActivityManager.UID_OBSERVER_PROCSTATE) != 0) {
if (DEBUG_UID_OBSERVERS)
Slog.i(TAG_UID_OBSERVERS, "UID CHANGED uid=" + item.uid + ": " + item.processState);
observer.onUidStateChanged(item.uid, item.processState);
}
if (VALIDATE_UID_STATES && i == 0) {
validateUid.curProcState = validateUid.setProcState = item.processState;
}
}
}
} catch (RemoteException e) {
}
}
}
mUidObservers.finishBroadcast();
synchronized (this) {
for (int j = 0; j < N; j++) {
mAvailUidChanges.add(mActiveUidChanges[j]);
}
}
}
Aggregations