Search in sources :

Example 16 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method createErrorLog.

@Test
public void createErrorLog() throws DeviceInfoHelper.DeviceInfoException {
    /* Dummy coverage of utils class. */
    new ErrorLogHelper();
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(SystemClock.elapsedRealtime()).thenReturn(1000L);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    Device mockDevice = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
    /* Mock process name. */
    ActivityManager activityManager = mock(ActivityManager.class);
    RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
    runningAppProcessInfo.pid = 123;
    runningAppProcessInfo.processName = "right.process";
    when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
    when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
    /* Mock architecture. */
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    Whitebox.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Test. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), 900, true);
    assertNotNull(errorLog);
    assertNotNull(errorLog.getId());
    assertTrue(System.currentTimeMillis() - errorLog.getToffset() <= 1000);
    assertEquals(mockDevice, errorLog.getDevice());
    assertEquals(Integer.valueOf(123), errorLog.getProcessId());
    assertEquals("right.process", errorLog.getProcessName());
    assertNull(errorLog.getParentProcessId());
    assertNull(errorLog.getParentProcessName());
    assertEquals("armeabi-v7a", errorLog.getArchitecture());
    assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
    assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
    assertEquals(Boolean.TRUE, errorLog.getFatal());
    assertEquals(Long.valueOf(100), errorLog.getAppLaunchTOffset());
    /* Check first exception. */
    Exception topException = errorLog.getException();
    sanityCheck(topException);
    assertEquals(RuntimeException.class.getName(), topException.getType());
    assertNotNull(topException.getMessage());
    assertNotNull(topException.getInnerExceptions());
    assertEquals(1, topException.getInnerExceptions().size());
    /* Check second exception. */
    Exception middleException = topException.getInnerExceptions().get(0);
    sanityCheck(middleException);
    assertEquals(IOException.class.getName(), middleException.getType());
    assertNotNull(middleException.getInnerExceptions());
    assertEquals(1, middleException.getInnerExceptions().size());
    /* Check third exception. */
    Exception rootCauseException = middleException.getInnerExceptions().get(0);
    sanityCheck(rootCauseException);
    assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
    assertNotNull(rootCauseException.getMessage());
    assertNull(rootCauseException.getInnerExceptions());
    /* Check threads. */
    assertNotNull(errorLog.getThreads());
    assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
    for (Thread thread : errorLog.getThreads()) {
        assertNotNull(thread);
        assertTrue(thread.getId() > 0);
        assertNotNull(thread.getName());
        assertNotNull(thread.getFrames());
        for (StackFrame frame : thread.getFrames()) {
            assertNotNull(frame);
            assertNotNull(frame.getClassName());
            assertNotNull(frame.getMethodName());
        }
    }
}
Also used : Context(android.content.Context) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) Device(com.microsoft.azure.mobile.ingestion.models.Device) IOException(java.io.IOException) ActivityManager(android.app.ActivityManager) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) Exception(com.microsoft.azure.mobile.crashes.ingestion.models.Exception) Thread(com.microsoft.azure.mobile.crashes.ingestion.models.Thread) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Build(android.os.Build) StackFrame(com.microsoft.azure.mobile.crashes.ingestion.models.StackFrame) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project ABPlayer by winkstu.

the class ProcessHelper method isProcessRunning.

public static boolean isProcessRunning(Context ctx, String name) {
    ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> apps = am.getRunningAppProcesses();
    for (RunningAppProcessInfo app : apps) {
        if (app.processName.equals(name)) {
            return true;
        }
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager)

Example 18 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project JustAndroid by chinaltz.

the class AbAppUtil method getRunningAppProcesses.

/**
     * 获取运行的进程列表.
     *
     * @param context
     * @return
     */
public static List<AbAppProcessInfo> getRunningAppProcesses(Context context) {
    ActivityManager activityManager = null;
    List<AbAppProcessInfo> list = null;
    PackageManager packageManager = null;
    try {
        activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        packageManager = context.getApplicationContext().getPackageManager();
        list = new ArrayList<AbAppProcessInfo>();
        //所有运行的进程
        List<RunningAppProcessInfo> appProcessList = activityManager.getRunningAppProcesses();
        ApplicationInfo appInfo = null;
        AbAppProcessInfo abAppProcessInfo = null;
        PackageInfo packageInfo = getPackageInfo(context);
        if (mProcessList != null) {
            mProcessList.clear();
        }
        mProcessList = getProcessRunningInfo();
        for (RunningAppProcessInfo appProcessInfo : appProcessList) {
            abAppProcessInfo = new AbAppProcessInfo(appProcessInfo.processName, appProcessInfo.pid, appProcessInfo.uid);
            appInfo = getApplicationInfo(context, appProcessInfo.processName);
            if (appInfo != null) {
                Drawable icon = appInfo.loadIcon(packageManager);
                String appName = appInfo.loadLabel(packageManager).toString();
                abAppProcessInfo.icon = icon;
                abAppProcessInfo.appName = appName;
            } else {
                //:服务的命名
                if (appProcessInfo.processName.indexOf(":") != -1) {
                    appInfo = getApplicationInfo(context, appProcessInfo.processName.split(":")[0]);
                    Drawable icon = appInfo.loadIcon(packageManager);
                    abAppProcessInfo.icon = icon;
                }
                abAppProcessInfo.appName = appProcessInfo.processName;
            }
            /*AbPsRow psRow = getPsRow(appProcessInfo.processName);
	    		if(psRow!=null){
	    			abAppProcessInfo.memory = psRow.mem;
	    		}*/
            AbProcessInfo processInfo = getMemInfo(appProcessInfo.processName);
            abAppProcessInfo.memory = processInfo.memory;
            abAppProcessInfo.cpu = processInfo.cpu;
            abAppProcessInfo.status = processInfo.status;
            abAppProcessInfo.threadsCount = processInfo.threadsCount;
            list.add(abAppProcessInfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}
Also used : AbProcessInfo(com.ningcui.mylibrary.model.AbProcessInfo) PackageInfo(android.content.pm.PackageInfo) AbAppProcessInfo(com.ningcui.mylibrary.model.AbAppProcessInfo) ApplicationInfo(android.content.pm.ApplicationInfo) Drawable(android.graphics.drawable.Drawable) ActivityManager(android.app.ActivityManager) IOException(java.io.IOException) PackageManager(android.content.pm.PackageManager) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo)

Example 19 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project android_frameworks_base by crdroidandroid.

the class MemoryUsageTest method getPss.

private int getPss(String processName) {
    ActivityManager am = (ActivityManager) getInstrumentation().getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> apps = am.getRunningAppProcesses();
    for (RunningAppProcessInfo proc : apps) {
        if (!proc.processName.equals(processName)) {
            continue;
        }
        int[] pids = { proc.pid };
        MemoryInfo meminfo = am.getProcessMemoryInfo(pids)[0];
        return meminfo.getTotalPss();
    }
    return -1;
}
Also used : MemoryInfo(android.os.Debug.MemoryInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ActivityManager(android.app.ActivityManager) IActivityManager(android.app.IActivityManager)

Example 20 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project android_frameworks_base by crdroidandroid.

the class FingerprintService method isForegroundActivity.

private boolean isForegroundActivity(int uid, int pid) {
    try {
        List<RunningAppProcessInfo> procs = ActivityManagerNative.getDefault().getRunningAppProcesses();
        int N = procs.size();
        for (int i = 0; i < N; i++) {
            RunningAppProcessInfo proc = procs.get(i);
            if (proc.pid == pid && proc.uid == uid && proc.importance == IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
    } catch (RemoteException e) {
        Slog.w(TAG, "am.getRunningAppProcesses() failed");
    }
    return false;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RemoteException(android.os.RemoteException) Fingerprint(android.hardware.fingerprint.Fingerprint)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)35 ActivityManager (android.app.ActivityManager)27 IActivityManager (android.app.IActivityManager)6 RemoteException (android.os.RemoteException)6 Fingerprint (android.hardware.fingerprint.Fingerprint)5 MemoryInfo (android.os.Debug.MemoryInfo)5 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)4 Context (android.content.Context)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 Build (android.os.Build)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 SuppressLint (android.annotation.SuppressLint)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 Drawable (android.graphics.drawable.Drawable)2 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2