Java Code Examples for android.app.ActivityManager#getAppTasks()

The following examples show how to use android.app.ActivityManager#getAppTasks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncognitoNotificationService.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void removeNonVisibleChromeTabbedRecentEntries() {
    Set<Integer> visibleTaskIds = getTaskIdsForVisibleActivities();

    Context context = ContextUtils.getApplicationContext();
    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = getPackageManager();

    for (AppTask task : manager.getAppTasks()) {
        RecentTaskInfo info = DocumentUtils.getTaskInfoFromTask(task);
        if (info == null) continue;
        String className = DocumentUtils.getTaskClassName(task, pm);

        // It is not easily possible to distinguish between tasks sitting on top of
        // ChromeLauncherActivity, so we treat them all as likely ChromeTabbedActivities and
        // close them to be on the cautious side of things.
        if ((TextUtils.equals(className, ChromeTabbedActivity.class.getName())
                || TextUtils.equals(className, ChromeLauncherActivity.class.getName()))
                && !visibleTaskIds.contains(info.id)) {
            task.finishAndRemoveTask();
        }
    }
}
 
Example 2
Source File: IncognitoNotificationService.java    From delion with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void removeNonVisibleChromeTabbedRecentEntries() {
    Set<Integer> visibleTaskIds = getTaskIdsForVisibleActivities();

    Context context = ContextUtils.getApplicationContext();
    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager pm = getPackageManager();

    for (AppTask task : manager.getAppTasks()) {
        RecentTaskInfo info = DocumentUtils.getTaskInfoFromTask(task);
        if (info == null) continue;
        String className = DocumentUtils.getTaskClassName(task, pm);

        // It is not easily possible to distinguish between tasks sitting on top of
        // ChromeLauncherActivity, so we treat them all as likely ChromeTabbedActivities and
        // close them to be on the cautious side of things.
        if ((TextUtils.equals(className, ChromeTabbedActivity.class.getName())
                || TextUtils.equals(className, ChromeLauncherActivity.class.getName()))
                && !visibleTaskIds.contains(info.id)) {
            task.finishAndRemoveTask();
        }
    }
}
 
Example 3
Source File: ActivityDelegateImpl.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public List<Entry> getTasksFromRecents(boolean isIncognito) {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List<Entry> entries = new ArrayList<Entry>();
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (!isValidActivity(isIncognito, intent)) continue;

        int tabId = getTabIdFromIntent(intent);
        if (tabId == Tab.INVALID_TAB_ID) continue;

        String initialUrl = getInitialUrlForDocument(intent);
        entries.add(new Entry(tabId, initialUrl));
    }
    return entries;
}
 
Example 4
Source File: ActivityDelegate.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Finishes all DocumentActivities that appear in Android's Recents.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void finishAllDocumentActivities() {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (isValidActivity(false, intent) || isValidActivity(true, intent)) {
            task.finishAndRemoveTask();
        }
    }
}
 
Example 5
Source File: WebappDirectoryManager.java    From delion with Apache License 2.0 5 votes vote down vote up
/** Returns a Set of Intents for all Chrome tasks currently known by the ActivityManager. */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected Set<Intent> getBaseIntentsForAllTasks() {
    Set<Intent> baseIntents = new HashSet<Intent>();

    Context context = ContextUtils.getApplicationContext();
    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (AppTask task : manager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (intent != null) baseIntents.add(intent);
    }

    return baseIntents;
}
 
Example 6
Source File: TabWebContentsDelegateAndroid.java    From delion with Apache License 2.0 5 votes vote down vote up
/** If the API allows it, returns whether a Task still exists for the parent Activity. */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean isParentInAndroidOverview() {
    ActivityManager activityManager = (ActivityManager) mTab.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent taskIntent = DocumentUtils.getBaseIntentFromTask(task);
        if (taskIntent != null && taskIntent.filterEquals(mTab.getParentIntent())) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: ActivityDelegateImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isIncognitoDocumentAccessibleToUser() {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (isValidActivity(true, intent)) return true;
    }
    return false;
}
 
Example 8
Source File: DocumentTabModelSelector.java    From delion with Apache License 2.0 5 votes vote down vote up
private int getLargestTaskIdFromRecents() {
    int biggestId = Tab.INVALID_TAB_ID;
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        RecentTaskInfo info = DocumentUtils.getTaskInfoFromTask(task);
        if (info == null) continue;
        biggestId = Math.max(biggestId, info.persistentId);
    }
    return biggestId;
}
 
Example 9
Source File: WebappDirectoryManager.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/** Returns a Set of Intents for all Chrome tasks currently known by the ActivityManager. */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected Set<Intent> getBaseIntentsForAllTasks() {
    Set<Intent> baseIntents = new HashSet<Intent>();

    Context context = ContextUtils.getApplicationContext();
    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (AppTask task : manager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (intent != null) baseIntents.add(intent);
    }

    return baseIntents;
}
 
Example 10
Source File: ActivityDelegateImpl.java    From delion with Apache License 2.0 5 votes vote down vote up
private ActivityManager.AppTask getTask(boolean isIncognito, int tabId) {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        int taskId = getTabIdFromIntent(intent);
        if (taskId == tabId && isValidActivity(isIncognito, intent)) return task;
    }
    return null;
}
 
Example 11
Source File: VisibilityLifeCycleCallback.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private void finishAndRemoveTaskIfInBackground() {
    if (activitiesInStartedState == 0) {
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager == null) {
            return;
        }

        for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
            task.finishAndRemoveTask();
        }
    }
}
 
Example 12
Source File: ChromeTabbedActivity.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean isMergedInstanceTaskRunning() {
    if (!FeatureUtilities.isTabModelMergingEnabled() || sMergedInstanceTaskId == 0) {
        return false;
    }

    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (AppTask task : manager.getAppTasks()) {
        RecentTaskInfo info = DocumentUtils.getTaskInfoFromTask(task);
        if (info == null) continue;
        if (info.id == sMergedInstanceTaskId) return true;
    }
    return false;
}
 
Example 13
Source File: ActivityDelegateImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private ActivityManager.AppTask getTask(boolean isIncognito, int tabId) {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        int taskId = getTabIdFromIntent(intent);
        if (taskId == tabId && isValidActivity(isIncognito, intent)) return task;
    }
    return null;
}
 
Example 14
Source File: VisibilityLifeCycleCallback.java    From firefox-echo-show with Mozilla Public License 2.0 5 votes vote down vote up
private void finishAndRemoveTaskIfInBackground() {
    if (activitiesInStartedState == 0) {
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager == null) {
            return;
        }

        for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
            task.finishAndRemoveTask();
        }
    }
}
 
Example 15
Source File: Utils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public static void hardRestartApp(Context c) {
    ActivityManager activityManager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks())
        task.finishAndRemoveTask();

    Intent intent = c.getPackageManager().getLaunchIntentForPackage(c.getPackageName());
    c.startActivity(intent);
    System.exit(0);
}
 
Example 16
Source File: Utils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public static void softRestartApp(Context c) {
    ActivityManager activityManager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks())
        task.finishAndRemoveTask();

    Intent intent = c.getPackageManager().getLaunchIntentForPackage(c.getPackageName());
    c.startActivity(intent);
}
 
Example 17
Source File: ActivityDelegateImpl.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isIncognitoDocumentAccessibleToUser() {
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        Intent intent = DocumentUtils.getBaseIntentFromTask(task);
        if (isValidActivity(true, intent)) return true;
    }
    return false;
}
 
Example 18
Source File: DocumentTabModelSelector.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
private int getLargestTaskIdFromRecents() {
    int biggestId = Tab.INVALID_TAB_ID;
    Context context = ContextUtils.getApplicationContext();
    ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
        RecentTaskInfo info = DocumentUtils.getTaskInfoFromTask(task);
        if (info == null) continue;
        biggestId = Math.max(biggestId, info.persistentId);
    }
    return biggestId;
}
 
Example 19
Source File: ApplicationUtils.java    From QPM with Apache License 2.0 4 votes vote down vote up
/**
 * 通过ActivityManager拿到栈顶Activity的ClassName,然后跟列表中的Activity比对,获取栈顶Activity。
 * 注意:这种方式有个很严重的缺陷,如果某个Activity在栈里多次出现将导致判断错误,这时候将返回null。
 * 可靠性相对一般,不会返回错误结果。
 *
 * @param application application
 * @param activities  所有的Activity列表
 * @return 栈顶Activity,获取不到的时候将返回null,不会返回错误结果。
 * @throws ReflectUtils.ReflectException 可能会发生异常,强制要求处理异常情况。
 */
public static Activity getTopActivityByActivityManager(Context application
        , List<Activity> activities) throws ReflectUtils.ReflectException {
    ActivityManager activityManager = (ActivityManager) application
            .getSystemService(Context.ACTIVITY_SERVICE);
    String topActivity = null;
    String packageName = application.getPackageName();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
        for (int i = 0, size = appTasks.size(); i < size; i++) {
            ActivityManager.RecentTaskInfo taskInfo = appTasks.get(size - 1 - i).getTaskInfo();
            if (packageName.equals(taskInfo.baseActivity.getPackageName())) {
                topActivity = taskInfo.topActivity.getClassName();
            }
        }
    }
    if (TextUtils.isEmpty(topActivity)) {
        Log.i(TAG, "尝试通过getTopActivityByActivityManager获取Activity失败");
        return null;
    }

    boolean check = false;
    Activity result = null;
    if (activities != null) {
        for (Activity activity : activities) {
            if (topActivity.equals(activity.getClass().getName())) {
                if (check) {
                    // 出现重复Activity,返回null
                    Log.i(TAG, "尝试通过getTopActivityByActivityManager获取Activity失败");
                    return null;
                }
                check = true;
                result = activity;
            }
        }
    }
    if (result == null) {
        Log.w(TAG, "尝试通过getTopActivityByActivityManager获取Activity失败");
    } else {
        Log.i(TAG, "尝试通过getTopActivityByActivityManager获取Activity成功");
    }
    return result;
}
 
Example 20
Source File: DocumentCentricAppsInstrumentationTest.java    From user-interface-samples with Apache License 2.0 4 votes vote down vote up
private List<ActivityManager.AppTask> getRecentAppTasks() {
    ActivityManager activityManager = (ActivityManager) getInstrumentation().getTargetContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
    return appTasks;
}