Search in sources :

Example 41 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project platform_frameworks_base by android.

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)

Example 42 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project devbricks by dailystudio.

the class AndroidActivity method getTopActivityAboveL.

public static ComponentName getTopActivityAboveL(Context context) {
    if (context == null) {
        return null;
    }
    ActivityManager actmgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (actmgr == null) {
        return null;
    }
    List<RunningTaskInfo> tasks = actmgr.getRunningTasks(1);
    if (tasks == null || tasks.size() <= 0) {
        return null;
    }
    List<RunningAppProcessInfo> processes = actmgr.getRunningAppProcesses();
    if (processes == null) {
        return null;
    }
    RunningAppProcessInfo p0 = processes.get(0);
    if (p0 == null) {
        return null;
    }
    String pkg = p0.processName;
    String[] pkglist = p0.pkgList;
    if (pkglist != null && pkglist.length > 0) {
        pkg = pkglist[0];
    }
    Logger.debug("top app-pkg: %s", pkg);
    if (TextUtils.isEmpty(pkg)) {
        return null;
    }
    AndroidApplication app = new AndroidApplication(pkg);
    Intent i = app.getLaunchIntent(context);
    if (i == null) {
        return null;
    }
    final ComponentName comp = i.getComponent();
    Logger.debug("top app-comp: %s", comp);
    return comp;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) Intent(android.content.Intent) ComponentName(android.content.ComponentName) ActivityManager(android.app.ActivityManager) RunningTaskInfo(android.app.ActivityManager.RunningTaskInfo)

Example 43 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project AppCenter-SDK-Android by Microsoft.

the class ErrorLogHelperTest method getErrorReportFromErrorLog.

@Test
public void getErrorReportFromErrorLog() throws java.lang.Exception {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    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. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
    TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
    /* Create an error log. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new TestCrashException()), java.lang.Thread.getAllStackTraces(), 900);
    assertNotNull(errorLog);
    /* Test. */
    Throwable throwable = new RuntimeException();
    ErrorReport report = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    assertNotNull(report);
    assertEquals(errorLog.getId().toString(), report.getId());
    assertEquals(errorLog.getErrorThreadName(), report.getThreadName());
    assertEquals(throwable, report.getThrowable());
    assertEquals(errorLog.getAppLaunchTimestamp(), report.getAppStartTime());
    assertEquals(errorLog.getTimestamp(), report.getAppErrorTime());
    assertEquals(errorLog.getDevice(), report.getDevice());
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Device(com.microsoft.appcenter.ingestion.models.Device) Build(android.os.Build) ActivityManager(android.app.ActivityManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project MVPFrames by RockyQu.

the class AppUtils method gc.

/**
 * 清理后台进程与服务
 *
 * @param context 应用上下文对象context
 * @return 被清理的数量
 */
public static int gc(Context context) {
    long i = getDeviceUsableMemory(context);
    // 清理掉的进程数
    int count = 0;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    // 获取正在运行的service列表
    List<RunningServiceInfo> serviceList = am.getRunningServices(100);
    if (serviceList != null) {
        for (RunningServiceInfo service : serviceList) {
            if (service.pid == android.os.Process.myPid())
                continue;
            try {
                android.os.Process.killProcess(service.pid);
                count++;
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }
    // 获取正在运行的进程列表
    List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
    if (processList != null) {
        for (RunningAppProcessInfo process : processList) {
            // 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
            if (process.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                // pkgList 得到该进程下运行的包名
                String[] pkgList = process.pkgList;
                for (String pkgName : pkgList) {
                    try {
                        am.killBackgroundProcesses(pkgName);
                        count++;
                    } catch (Exception e) {
                        // 防止意外发生
                        e.getStackTrace();
                    }
                }
            }
        }
    }
    return count;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) ActivityManager(android.app.ActivityManager) Point(android.graphics.Point)

Example 45 with RunningAppProcessInfo

use of android.app.ActivityManager.RunningAppProcessInfo in project AnExplorer by 1hakr.

the class AppsProvider method queryChildDocuments.

@Override
public Cursor queryChildDocuments(String docId, String[] projection, String sortOrder) throws FileNotFoundException {
    final MatrixCursor result = new DocumentCursor(resolveDocumentProjection(projection), docId);
    // Delegate to real provider
    final long token = Binder.clearCallingIdentity();
    try {
        if (docId.startsWith(ROOT_ID_USER_APP)) {
            List<PackageInfo> allAppList = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (PackageInfo packageInfo : allAppList) {
                includeAppFromPackage(result, docId, packageInfo, false, null);
            }
        } else if (docId.startsWith(ROOT_ID_SYSTEM_APP)) {
            List<PackageInfo> allAppList = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (PackageInfo packageInfo : allAppList) {
                includeAppFromPackage(result, docId, packageInfo, true, null);
            }
        } else if (docId.startsWith(ROOT_ID_PROCESS)) {
            if (Utils.hasNougat()) {
                List<RunningServiceInfo> runningServices = activityManager.getRunningServices(1000);
                for (RunningServiceInfo process : runningServices) {
                    includeAppFromService(result, docId, process, null);
                }
            } else if (Utils.hasLollipopMR1()) {
                List<AndroidAppProcess> runningAppProcesses = AndroidProcesses.getRunningAppProcesses();
                for (AndroidAppProcess process : runningAppProcesses) {
                    includeAppFromProcess(result, docId, process, null);
                }
            } else {
                List<RunningAppProcessInfo> runningProcessesList = activityManager.getRunningAppProcesses();
                for (RunningAppProcessInfo processInfo : runningProcessesList) {
                    includeAppFromProcess(result, docId, processInfo, null);
                }
            }
        }
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    return result;
}
Also used : RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) PackageInfo(android.content.pm.PackageInfo) RunningServiceInfo(android.app.ActivityManager.RunningServiceInfo) AndroidAppProcess(com.jaredrummler.android.processes.models.AndroidAppProcess) ArrayList(java.util.ArrayList) List(java.util.List) MatrixCursor(dev.dworks.apps.anexplorer.cursor.MatrixCursor)

Aggregations

RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)61 ActivityManager (android.app.ActivityManager)52 ArrayList (java.util.ArrayList)9 RunningServiceInfo (android.app.ActivityManager.RunningServiceInfo)8 IOException (java.io.IOException)8 IActivityManager (android.app.IActivityManager)6 Context (android.content.Context)6 Build (android.os.Build)6 RemoteException (android.os.RemoteException)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 Fingerprint (android.hardware.fingerprint.Fingerprint)5 MemoryInfo (android.os.Debug.MemoryInfo)5 SuppressLint (android.annotation.SuppressLint)4 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)4 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)4 Device (com.microsoft.appcenter.ingestion.models.Device)4