Search in sources :

Example 1 with IUidObserver

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]);
        }
    }
}
Also used : RemoteException(android.os.RemoteException) Point(android.graphics.Point) IUidObserver(android.app.IUidObserver)

Example 2 with IUidObserver

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();
}
Also used : BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) IUidObserver(android.app.IUidObserver) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Signature(android.content.pm.Signature) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) Before(org.junit.Before)

Example 3 with IUidObserver

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]);
        }
    }
}
Also used : RemoteException(android.os.RemoteException) Point(android.graphics.Point) IUidObserver(android.app.IUidObserver)

Aggregations

IUidObserver (android.app.IUidObserver)3 Point (android.graphics.Point)2 RemoteException (android.os.RemoteException)2 Context (android.content.Context)1 Intent (android.content.Intent)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 Signature (android.content.pm.Signature)1 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)1 BroadcastInterceptingContext (com.android.internal.util.test.BroadcastInterceptingContext)1 NetworkPolicyManagerService (com.android.server.net.NetworkPolicyManagerService)1 Before (org.junit.Before)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1