下面列出了android.app.ActivityManager.RecentTaskInfo#org.chromium.chrome.browser.document.DocumentUtils 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Kicks off tasks for the new state of the pipeline.
*
* We don't wait for the DocumentTabModel to finish parsing its metadata file before proceeding
* with migration because it doesn't have actionable information:
*
* 1) WE DON'T NEED TO RE-POPULATE THE "RECENTLY CLOSED" LIST:
* The metadata file contains a list of tabs Chrome knew about before it died, which
* could differ from the list of tabs in Android Overview. The canonical list of
* live tabs, however, has always been the ones displayed by the Android Overview.
*
* 2) RETARGETING MIGRATED TABS FROM THE HOME SCREEN IS A CORNER CASE:
* The only downside here is that Chrome ends up creating a new tab for a home screen
* shortcut the first time they start Chrome after migration. This was already
* broken for document mode during cold starts, anyway.
*/
private final void startStage(int newStage) {
ThreadUtils.assertOnUiThread();
if (!mIsPipelineActive) return;
if (newStage == STAGE_INITIALIZED) {
Log.d(TAG, "Migrating user into tabbed mode.");
int selectedTabId = DocumentUtils.getLastShownTabIdFromPrefs(getContext(), false);
copyTabStateFiles(selectedTabId);
} else if (newStage == STAGE_COPY_TAB_STATES_DONE) {
Log.d(TAG, "Writing tabbed mode metadata file.");
writeTabModelMetadata(mMigratedTabIds);
} else if (newStage == STAGE_WRITE_TABMODEL_METADATA_DONE) {
Log.d(TAG, "Changing user preference.");
switchToTabbedMode();
} else if (newStage == STAGE_CHANGE_SETTINGS_DONE) {
Log.d(TAG, "Cleaning up document mode data.");
deleteDocumentModeData();
}
}
/**
* Construct a DocumentTabModel.
* @param activityDelegate Delegate to use for accessing the ActivityManager.
* @param storageDelegate Delegate to use for accessing persistent storage.
* @param tabCreatorManager Used to create Tabs.
* @param isIncognito Whether or not the TabList is managing incognito tabs.
* @param prioritizedTabId ID of the tab to prioritize when loading.
* @param context Context to use for accessing SharedPreferences.
*/
public DocumentTabModelImpl(ActivityDelegate activityDelegate, StorageDelegate storageDelegate,
TabCreatorManager tabCreatorManager, boolean isIncognito, int prioritizedTabId,
Context context) {
super(isIncognito);
mActivityDelegate = activityDelegate;
mStorageDelegate = storageDelegate;
mContext = context;
mCurrentState = STATE_UNINITIALIZED;
mTabIdList = new ArrayList<Integer>();
mEntryMap = new SparseArray<Entry>();
mHistoricalTabs = new ArrayList<Integer>();
mLastShownTabId = DocumentUtils.getLastShownTabIdFromPrefs(mContext, isIncognito());
// Restore the tab list.
setCurrentState(STATE_READ_RECENT_TASKS_START);
mStorageDelegate.restoreTabEntries(
isIncognito, activityDelegate, mEntryMap, mTabIdList, mHistoricalTabs);
setCurrentState(STATE_READ_RECENT_TASKS_END);
}
@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;
}
@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();
}
}
}
@Override
public void onResume() {
if (!isFinishing()) {
if (getIntent() != null) {
// Avoid situations where Android starts two Activities with the same data.
DocumentUtils.finishOtherTasksWithData(getIntent().getData(), getTaskId());
}
updateTaskDescription();
}
super.onResume();
// Kick off the old web app cleanup (if we haven't already) now that we have queued the
// current web app's storage to be opened.
if (!mOldWebappCleanupStarted) {
WebappRegistry.unregisterOldWebapps(this, System.currentTimeMillis());
mOldWebappCleanupStarted = true;
}
}
/**
* Kicks off tasks for the new state of the pipeline.
*
* We don't wait for the DocumentTabModel to finish parsing its metadata file before proceeding
* with migration because it doesn't have actionable information:
*
* 1) WE DON'T NEED TO RE-POPULATE THE "RECENTLY CLOSED" LIST:
* The metadata file contains a list of tabs Chrome knew about before it died, which
* could differ from the list of tabs in Android Overview. The canonical list of
* live tabs, however, has always been the ones displayed by the Android Overview.
*
* 2) RETARGETING MIGRATED TABS FROM THE HOME SCREEN IS A CORNER CASE:
* The only downside here is that Chrome ends up creating a new tab for a home screen
* shortcut the first time they start Chrome after migration. This was already
* broken for document mode during cold starts, anyway.
*/
private final void startStage(int newStage) {
ThreadUtils.assertOnUiThread();
if (!mIsPipelineActive) return;
if (newStage == STAGE_INITIALIZED) {
Log.d(TAG, "Migrating user into tabbed mode.");
int selectedTabId = DocumentUtils.getLastShownTabIdFromPrefs(getContext(), false);
copyTabStateFiles(selectedTabId);
} else if (newStage == STAGE_COPY_TAB_STATES_DONE) {
Log.d(TAG, "Writing tabbed mode metadata file.");
writeTabModelMetadata(mMigratedTabIds);
} else if (newStage == STAGE_WRITE_TABMODEL_METADATA_DONE) {
Log.d(TAG, "Changing user preference.");
switchToTabbedMode();
} else if (newStage == STAGE_CHANGE_SETTINGS_DONE) {
Log.d(TAG, "Cleaning up document mode data.");
deleteDocumentModeData();
}
}
/**
* Construct a DocumentTabModel.
* @param activityDelegate Delegate to use for accessing the ActivityManager.
* @param storageDelegate Delegate to use for accessing persistent storage.
* @param tabCreatorManager Used to create Tabs.
* @param isIncognito Whether or not the TabList is managing incognito tabs.
* @param prioritizedTabId ID of the tab to prioritize when loading.
* @param context Context to use for accessing SharedPreferences.
*/
public DocumentTabModelImpl(ActivityDelegate activityDelegate, StorageDelegate storageDelegate,
TabCreatorManager tabCreatorManager, boolean isIncognito, int prioritizedTabId,
Context context) {
super(isIncognito, false);
mActivityDelegate = activityDelegate;
mStorageDelegate = storageDelegate;
mContext = context;
mCurrentState = STATE_UNINITIALIZED;
mTabIdList = new ArrayList<Integer>();
mEntryMap = new SparseArray<Entry>();
mHistoricalTabs = new ArrayList<Integer>();
mLastShownTabId = DocumentUtils.getLastShownTabIdFromPrefs(mContext, isIncognito());
// Restore the tab list.
setCurrentState(STATE_READ_RECENT_TASKS_START);
mStorageDelegate.restoreTabEntries(
isIncognito, activityDelegate, mEntryMap, mTabIdList, mHistoricalTabs);
setCurrentState(STATE_READ_RECENT_TASKS_END);
}
@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;
}
@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();
}
}
}
/**
* Kicks off tasks for the new state of the pipeline.
*
* We don't wait for the DocumentTabModel to finish parsing its metadata file before proceeding
* with migration because it doesn't have actionable information:
*
* 1) WE DON'T NEED TO RE-POPULATE THE "RECENTLY CLOSED" LIST:
* The metadata file contains a list of tabs Chrome knew about before it died, which
* could differ from the list of tabs in Android Overview. The canonical list of
* live tabs, however, has always been the ones displayed by the Android Overview.
*
* 2) RETARGETING MIGRATED TABS FROM THE HOME SCREEN IS A CORNER CASE:
* The only downside here is that Chrome ends up creating a new tab for a home screen
* shortcut the first time they start Chrome after migration. This was already
* broken for document mode during cold starts, anyway.
*/
private final void startStage(int newStage) {
ThreadUtils.assertOnUiThread();
if (!mIsPipelineActive) return;
if (newStage == STAGE_INITIALIZED) {
Log.d(TAG, "Migrating user into tabbed mode.");
int selectedTabId = DocumentUtils.getLastShownTabIdFromPrefs(getContext(), false);
copyTabStateFiles(selectedTabId);
} else if (newStage == STAGE_COPY_TAB_STATES_DONE) {
Log.d(TAG, "Writing tabbed mode metadata file.");
writeTabModelMetadata(mMigratedTabIds);
} else if (newStage == STAGE_WRITE_TABMODEL_METADATA_DONE) {
Log.d(TAG, "Changing user preference.");
switchToTabbedMode();
} else if (newStage == STAGE_CHANGE_SETTINGS_DONE) {
Log.d(TAG, "Cleaning up document mode data.");
deleteDocumentModeData();
}
}
/**
* Construct a DocumentTabModel.
* @param activityDelegate Delegate to use for accessing the ActivityManager.
* @param storageDelegate Delegate to use for accessing persistent storage.
* @param tabCreatorManager Used to create Tabs.
* @param isIncognito Whether or not the TabList is managing incognito tabs.
* @param prioritizedTabId ID of the tab to prioritize when loading.
* @param context Context to use for accessing SharedPreferences.
*/
public DocumentTabModelImpl(ActivityDelegate activityDelegate, StorageDelegate storageDelegate,
TabCreatorManager tabCreatorManager, boolean isIncognito, int prioritizedTabId,
Context context) {
super(isIncognito, false);
mActivityDelegate = activityDelegate;
mStorageDelegate = storageDelegate;
mContext = context;
mCurrentState = STATE_UNINITIALIZED;
mTabIdList = new ArrayList<Integer>();
mEntryMap = new SparseArray<Entry>();
mHistoricalTabs = new ArrayList<Integer>();
mLastShownTabId = DocumentUtils.getLastShownTabIdFromPrefs(mContext, isIncognito());
// Restore the tab list.
setCurrentState(STATE_READ_RECENT_TASKS_START);
mStorageDelegate.restoreTabEntries(
isIncognito, activityDelegate, mEntryMap, mTabIdList, mHistoricalTabs);
setCurrentState(STATE_READ_RECENT_TASKS_END);
}
@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;
}
@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 ((ChromeTabbedActivity.isTabbedModeClassName(className)
|| TextUtils.equals(className, ChromeLauncherActivity.class.getName()))
&& !visibleTaskIds.contains(info.id)) {
task.finishAndRemoveTask();
}
}
}
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;
}
@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;
}
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;
}
/**
* 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();
}
}
}
/** 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;
}
/** 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;
}
@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;
}
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;
}
@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;
}
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;
}
/**
* 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();
}
}
}
/** 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;
}
@Override
public void onResume() {
if (!isFinishing()) {
if (getIntent() != null) {
// Avoid situations where Android starts two Activities with the same data.
DocumentUtils.finishOtherTasksWithData(getIntent().getData(), getTaskId());
}
updateTaskDescription();
}
super.onResume();
}
/** 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;
}
@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;
}
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;
}
@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;
}