Java Code Examples for android.app.activitymanager#AppTask

The following examples show how to use android.app.activitymanager#AppTask . 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: DocumentUtils.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Finishes tasks other than the one with the given ID that were started with the given data
 * in the Intent, removing those tasks from Recents and leaving a unique task with the data.
 * @param data Passed in as part of the Intent's data when starting the Activity.
 * @param canonicalTaskId ID of the task will be the only one left with the ID.
 * @return Intent of one of the tasks that were finished.
 */
public static Intent finishOtherTasksWithData(Uri data, int canonicalTaskId) {
    if (data == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null;

    String dataString = data.toString();
    Context context = ContextUtils.getApplicationContext();

    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.AppTask> tasksToFinish = new ArrayList<ActivityManager.AppTask>();
    for (ActivityManager.AppTask task : manager.getAppTasks()) {
        RecentTaskInfo taskInfo = getTaskInfoFromTask(task);
        if (taskInfo == null) continue;
        int taskId = taskInfo.id;

        Intent baseIntent = taskInfo.baseIntent;
        String taskData = baseIntent == null ? null : taskInfo.baseIntent.getDataString();

        if (TextUtils.equals(dataString, taskData)
                && (taskId == -1 || taskId != canonicalTaskId)) {
            tasksToFinish.add(task);
        }
    }
    return finishAndRemoveTasks(tasksToFinish);
}
 
Example 2
Source File: SystemUtils.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * date: 2019/6/28
 * author: chenli
 * description: 退出应用
 */
public static void exitApp(Activity context){
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        List<ActivityManager.AppTask> taskInfoList = activityManager.getAppTasks();
        for (ActivityManager.AppTask task : taskInfoList) {
            task.finishAndRemoveTask();
        }
    }else context.finish();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            System.exit(0);
        }
    }, 250);
}
 
Example 3
Source File: DocumentUtils.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Finishes tasks other than the one with the given ID that were started with the given data
 * in the Intent, removing those tasks from Recents and leaving a unique task with the data.
 * @param data Passed in as part of the Intent's data when starting the Activity.
 * @param canonicalTaskId ID of the task will be the only one left with the ID.
 * @return Intent of one of the tasks that were finished.
 */
public static Intent finishOtherTasksWithData(Uri data, int canonicalTaskId) {
    if (data == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null;

    String dataString = data.toString();
    Context context = ContextUtils.getApplicationContext();

    ActivityManager manager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.AppTask> tasksToFinish = new ArrayList<ActivityManager.AppTask>();
    for (ActivityManager.AppTask task : manager.getAppTasks()) {
        RecentTaskInfo taskInfo = getTaskInfoFromTask(task);
        if (taskInfo == null) continue;
        int taskId = taskInfo.id;

        Intent baseIntent = taskInfo.baseIntent;
        String taskData = baseIntent == null ? null : taskInfo.baseIntent.getDataString();

        if (TextUtils.equals(dataString, taskData)
                && (taskId == -1 || taskId != canonicalTaskId)) {
            tasksToFinish.add(task);
        }
    }
    return finishAndRemoveTasks(tasksToFinish);
}
 
Example 4
Source File: DocumentCentricAppsInstrumentationTest.java    From android-DocumentCentricApps with Apache License 2.0 5 votes vote down vote up
public void testNewDocument_CreatesOverviewEntry() {
    // Given a initialized Activity
    assertNotNull("mDocumentCentricActivity is null", mDocumentCentricActivity);
    final Button createNewDocumentButton = (Button) mDocumentCentricActivity
            .findViewById(R.id.new_document_button);
    assertNotNull(createNewDocumentButton);

    // When "Create new Document" Button is clicked
    TouchUtils.clickView(this, createNewDocumentButton);

    // Then a entry in overview app tasks is created.
    List<ActivityManager.AppTask> recentAppTasks = getRecentAppTasks();
    assertEquals("# of recentAppTasks does not match", 2, recentAppTasks.size());
}
 
Example 5
Source File: ActivityDelegate.java    From delion 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 6
Source File: ActivityDelegateImpl.java    From AndroidChromium 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 7
Source File: ActivityDelegateImpl.java    From delion 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: DocumentUtils.java    From delion with Apache License 2.0 5 votes vote down vote up
private static Intent finishAndRemoveTasks(List<ActivityManager.AppTask> tasksToFinish) {
    Intent removedIntent = null;
    for (ActivityManager.AppTask task : tasksToFinish) {
        Log.d(TAG, "Removing task with duplicated data: " + task);
        removedIntent = getBaseIntentFromTask(task);
        task.finishAndRemoveTask();
    }
    return removedIntent;
}
 
Example 9
Source File: SetupPreferenceFragment.java    From island with Apache License 2.0 5 votes vote down vote up
private boolean startSetupActivityCleanly() {
	// Finish all tasks of Island first to avoid state inconsistency.
	final ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
	if (am != null) {
		final List<ActivityManager.AppTask> tasks = am.getAppTasks();
		if (tasks != null) for (final ActivityManager.AppTask task : tasks) task.finishAndRemoveTask();
	}

	Activities.startActivity(getActivity(), new Intent(getActivity(), SetupActivity.class));
	return true;
}
 
Example 10
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 11
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 12
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 13
Source File: TabWebContentsDelegateAndroid.java    From AndroidChromium 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 14
Source File: StreamActivity.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void navToLauncherTask(@Nonnull Context appContext) {
    ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    // iterate app tasks available and navigate to launcher task (browse task)
    final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
    for (ActivityManager.AppTask task : appTasks) {
        final Intent baseIntent = task.getTaskInfo().baseIntent;
        final Set<String> categories = baseIntent.getCategories();
        if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
            task.moveToFront();
            return;
        }
    }
}
 
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: ActivityStackCompat.java    From Recovery with Apache License 2.0 5 votes vote down vote up
public static String getBaseActivityName(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ActivityManager.AppTask appTask = getTopTaskAfterL(context);
        if (appTask == null)
            return null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return appTask.getTaskInfo().baseActivity.getClassName();
        } else {
            ComponentName componentName = RecoveryStore.getInstance().getBaseActivity();
            return componentName == null ? null : componentName.getClassName();
        }
    } else {
        ActivityManager.RunningTaskInfo taskInfo = getTopTaskBeforeL(context);
        if (taskInfo == null)
            return null;
        return taskInfo.baseActivity.getClassName();
    }
}
 
Example 18
Source File: DocumentCentricAppsInstrumentationTest.java    From user-interface-samples with Apache License 2.0 5 votes vote down vote up
public void testNewDocument_CreatesOverviewEntry() {
    // Given a initialized Activity
    assertNotNull("mDocumentCentricActivity is null", mDocumentCentricActivity);
    final Button createNewDocumentButton = (Button) mDocumentCentricActivity
            .findViewById(R.id.new_document_button);
    assertNotNull(createNewDocumentButton);

    // When "Create new Document" Button is clicked
    TouchUtils.clickView(this, createNewDocumentButton);

    // Then a entry in overview app tasks is created.
    List<ActivityManager.AppTask> recentAppTasks = getRecentAppTasks();
    assertEquals("# of recentAppTasks does not match", 2, recentAppTasks.size());
}
 
Example 19
Source File: DocumentCentricAppsInstrumentationTest.java    From android-DocumentCentricApps 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;
}
 
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;
}