Java Code Examples for org.chromium.base.ApplicationStatus#getStateForActivity()

The following examples show how to use org.chromium.base.ApplicationStatus#getStateForActivity() . 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 delion with Apache License 2.0 6 votes vote down vote up
private Set<Integer> getTaskIdsForVisibleActivities() {
    List<WeakReference<Activity>> runningActivities =
            ApplicationStatus.getRunningActivities();
    Set<Integer> visibleTaskIds = new HashSet<>();
    for (int i = 0; i < runningActivities.size(); i++) {
        Activity activity = runningActivities.get(i).get();
        if (activity == null) continue;

        int activityState = ApplicationStatus.getStateForActivity(activity);
        if (activityState != ActivityState.STOPPED
                && activityState != ActivityState.DESTROYED) {
            visibleTaskIds.add(activity.getTaskId());
        }
    }
    return visibleTaskIds;
}
 
Example 2
Source File: VrShellDelegate.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private VrShellDelegate(ChromeActivity activity, VrClassesWrapper wrapper) {
    mActivity = activity;
    mVrClassesWrapper = wrapper;
    // If an activity isn't resumed at the point, it must have been paused.
    mPaused = ApplicationStatus.getStateForActivity(activity) != ActivityState.RESUMED;
    updateVrSupportLevel();
    mNativeVrShellDelegate = nativeInit();
    mFeedbackFrequency = VrFeedbackStatus.getFeedbackFrequency();
    mEnterVrHandler = new Handler();
    Choreographer.getInstance().postFrameCallback(new FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {
            if (mNativeVrShellDelegate == 0) return;
            Display display =
                    ((WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE))
                            .getDefaultDisplay();
            nativeUpdateVSyncInterval(
                    mNativeVrShellDelegate, frameTimeNanos, 1.0d / display.getRefreshRate());
        }
    });
    ApplicationStatus.registerStateListenerForAllActivities(this);
}
 
Example 3
Source File: RecentTabsPage.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Updates whether the page is in the foreground based on whether the application is in the
 * foreground and whether {@link #mView} is attached to the application window. If the page is
 * no longer in the foreground, records the time that the page spent in the foreground to UMA.
 */
private void updateForegroundState() {
    boolean inForeground = mIsAttachedToWindow
            && ApplicationStatus.getStateForActivity(mActivity) == ActivityState.RESUMED;
    if (mInForeground == inForeground) {
        return;
    }

    mInForeground = inForeground;
    if (mInForeground) {
        mForegroundTimeMs = SystemClock.elapsedRealtime();
        StartupMetrics.getInstance().recordOpenedRecents();
    } else {
        RecordHistogram.recordLongTimesHistogram("NewTabPage.RecentTabsPage.TimeVisibleAndroid",
                SystemClock.elapsedRealtime() - mForegroundTimeMs, TimeUnit.MILLISECONDS);
    }
}
 
Example 4
Source File: ChromeActivity.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Called by the system when the activity changes from fullscreen mode to multi-window mode
 * and visa-versa.
 * @param isInMultiWindowMode True if the activity is in multi-window mode.
 */
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
    recordMultiWindowModeChangedUserAction(isInMultiWindowMode);

    if (!isInMultiWindowMode
            && ApplicationStatus.getStateForActivity(this) == ActivityState.RESUMED) {
        // Start a new UMA session when exiting multi-window mode if the activity is currently
        // resumed. When entering multi-window Android recents gains focus, so ChromeActivity
        // will get a call to onPauseWithNative(), ending the current UMA session. When exiting
        // multi-window, however, if ChromeActivity is resumed it stays in that state.
        markSessionEnd();
        markSessionResume();
        FeatureUtilities.setIsInMultiWindowMode(
                MultiWindowUtils.getInstance().isInMultiWindowMode(this));
    }

    super.onMultiWindowModeChanged(isInMultiWindowMode);
}
 
Example 5
Source File: ChromeActivity.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Called by the system when the activity changes from fullscreen mode to multi-window mode
 * and visa-versa.
 * @param isInMultiWindowMode True if the activity is in multi-window mode.
 */
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
    recordMultiWindowModeChangedUserAction(isInMultiWindowMode);

    if (!isInMultiWindowMode
            && ApplicationStatus.getStateForActivity(this) == ActivityState.RESUMED) {
        // Start a new UMA session when exiting multi-window mode if the activity is currently
        // resumed. When entering multi-window Android recents gains focus, so ChromeActivity
        // will get a call to onPauseWithNative(), ending the current UMA session. When exiting
        // multi-window, however, if ChromeActivity is resumed it stays in that state.
        markSessionEnd();
        markSessionResume();
        FeatureUtilities.setIsInMultiWindowMode(
                MultiWindowUtils.getInstance().isInMultiWindowMode(this));
    }
}
 
Example 6
Source File: IncognitoNotificationService.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private Set<Integer> getTaskIdsForVisibleActivities() {
    List<WeakReference<Activity>> runningActivities =
            ApplicationStatus.getRunningActivities();
    Set<Integer> visibleTaskIds = new HashSet<>();
    for (int i = 0; i < runningActivities.size(); i++) {
        Activity activity = runningActivities.get(i).get();
        if (activity == null) continue;

        int activityState = ApplicationStatus.getStateForActivity(activity);
        if (activityState != ActivityState.STOPPED
                && activityState != ActivityState.DESTROYED) {
            visibleTaskIds.add(activity.getTaskId());
        }
    }
    return visibleTaskIds;
}
 
Example 7
Source File: ChromeApplication.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Shows an error dialog following a startup error, and then exits the application.
 * @param e The exception reported by Chrome initialization.
 */
public static void reportStartupErrorAndExit(final ProcessInitException e) {
    Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
    if (ApplicationStatus.getStateForActivity(activity) == ActivityState.DESTROYED) {
        return;
    }
    InvalidStartupDialog.show(activity, e.getErrorCode());
}
 
Example 8
Source File: ChromeTabbedActivity.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
    super.onMultiWindowModeChanged(isInMultiWindowMode);
    if (!FeatureUtilities.isTabModelMergingEnabled()) return;
    if (!isInMultiWindowMode) {
        // If the activity is currently resumed when multi-window mode is exited, try to merge
        // tabs from the other activity instance.
        if (ApplicationStatus.getStateForActivity(this) == ActivityState.RESUMED) {
            maybeMergeTabs();
        } else {
            mMergeTabsOnResume = true;
        }
    }
}
 
Example 9
Source File: ChromeApplication.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Shows an error dialog following a startup error, and then exits the application.
 * @param e The exception reported by Chrome initialization.
 */
public static void reportStartupErrorAndExit(final ProcessInitException e) {
    Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
    if (ApplicationStatus.getStateForActivity(activity) == ActivityState.DESTROYED) {
        return;
    }
    InvalidStartupDialog.show(activity, e.getErrorCode());
}
 
Example 10
Source File: TabWebContentsDelegateAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void activateContents() {
    ChromeActivity activity = mTab.getActivity();
    if (activity == null) {
        Log.e(TAG, "Activity not set activateContents().  Bailing out.");
        return;
    }
    if (activity.isActivityDestroyed()) {
        Log.e(TAG, "Activity destroyed before calling activateContents().  Bailing out.");
        return;
    }
    if (!mTab.isInitialized()) {
        Log.e(TAG, "Tab not initialized before calling activateContents().  Bailing out.");
        return;
    }

    // Do nothing if the tab can currently be interacted with by the user.
    if (mTab.isUserInteractable()) return;

    TabModel model = getTabModel();
    int index = model.indexOf(mTab);
    if (index == TabModel.INVALID_TAB_INDEX) return;
    TabModelUtils.setIndex(model, index);

    // Do nothing if the activity is visible (STOPPED is the only valid invisible state as we
    // explicitly check isActivityDestroyed above).
    if (ApplicationStatus.getStateForActivity(activity) == ActivityState.STOPPED) {
        bringActivityToForeground();
    }
}
 
Example 11
Source File: ChromeActivity.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    maybeRemoveWindowBackground();

    Tab tab = getActivityTab();
    if (tab == null) return;
    if (hasFocus) {
        tab.onActivityShown();
    } else {
        boolean stopped = ApplicationStatus.getStateForActivity(this) == ActivityState.STOPPED;
        if (stopped) tab.onActivityHidden();
    }
}
 
Example 12
Source File: MultiWindowUtils.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * @param activity The Activity whose visibility to test.
 * @return True iff the given Activity is currently visible.
 */
public static boolean isActivityVisible(Activity activity) {
    if (activity == null) return false;
    int activityState = ApplicationStatus.getStateForActivity(activity);
    // In Android N multi-window mode, only one activity is resumed at a time. The other
    // activity visible on the screen will be in the paused state. Activities not visible on
    // the screen will be stopped or destroyed.
    return activityState == ActivityState.RESUMED || activityState == ActivityState.PAUSED;
}
 
Example 13
Source File: ChromeApplication.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Shows an error dialog following a startup error, and then exits the application.
 * @param e The exception reported by Chrome initialization.
 */
public static void reportStartupErrorAndExit(final ProcessInitException e) {
    Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
    if (ApplicationStatus.getStateForActivity(activity) == ActivityState.DESTROYED) {
        return;
    }
    InvalidStartupDialog.show(activity, e.getErrorCode());
}
 
Example 14
Source File: ChromeTabbedActivity.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
    super.onMultiWindowModeChanged(isInMultiWindowMode);
    if (!FeatureUtilities.isTabModelMergingEnabled()) return;
    if (!isInMultiWindowMode) {
        // If the activity is currently resumed when multi-window mode is exited, try to merge
        // tabs from the other activity instance.
        if (ApplicationStatus.getStateForActivity(this) == ActivityState.RESUMED) {
            maybeMergeTabs();
        } else {
            mMergeTabsOnResume = true;
        }
    }
}
 
Example 15
Source File: ChromeActivity.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    Tab tab = getActivityTab();
    if (tab == null) return;
    if (hasFocus) {
        tab.onActivityShown();
    } else {
        boolean stopped = ApplicationStatus.getStateForActivity(this) == ActivityState.STOPPED;
        if (stopped) tab.onActivityHidden();
    }
}
 
Example 16
Source File: SuggestionsSheetVisibilityChangeObserver.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the current state of the bottom sheet and activity with the ones recorded at the
 * previous call and generates events based on the difference.
 * @see #onContentShown()
 * @see #onContentHidden()
 * @see #onContentStateChanged(int)
 */
private void onStateChange() {
    boolean newVisibility = mBottomSheet.isSheetOpen()
            && mBottomSheet.getCurrentSheetContent() == mContentObserved
            && ApplicationStatus.getStateForActivity(mActivity) == ActivityState.RESUMED;

    // As the visibility we track is the one for a specific sheet content rather than the
    // whole BottomSheet, we also need to reflect that in the state, marking it "peeking" here
    // even though the BottomSheet itself is not.
    @BottomSheet.SheetState
    int newContentState =
            newVisibility ? mBottomSheet.getSheetState() : BottomSheet.SHEET_STATE_PEEK;

    // Flag overall changes to the visible state of the content, while ignoring transient states
    // like |STATE_SCROLLING|.
    boolean hasMeaningfulStateChange = BottomSheet.isStateStable(newContentState)
            && (mCurrentContentState != newContentState || mCurrentVisibility != newVisibility);

    if (newVisibility != mCurrentVisibility) {
        if (newVisibility) {
            onContentShown();
        } else {
            onContentHidden();
        }
        mCurrentVisibility = newVisibility;
    }

    if (hasMeaningfulStateChange) {
        onContentStateChanged(newContentState);
        mCurrentContentState = newContentState;
    }
}
 
Example 17
Source File: MultiWindowUtils.java    From delion with Apache License 2.0 5 votes vote down vote up
private boolean isActivityVisible(Activity activity) {
    if (activity == null) return false;
    int activityState = ApplicationStatus.getStateForActivity(activity);
    // In Android N multi-window mode, only one activity is resumed at a time. The other
    // activity visible on the screen will be in the paused state. Activities not visible on
    // the screen will be stopped or destroyed.
    return activityState == ActivityState.RESUMED || activityState == ActivityState.PAUSED;
}
 
Example 18
Source File: TabWebContentsObserver.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public void renderProcessGone(boolean processWasOomProtected) {
    Log.i(TAG, "renderProcessGone() for tab id: " + mTab.getId()
            + ", oom protected: " + Boolean.toString(processWasOomProtected)
            + ", already needs reload: " + Boolean.toString(mTab.needsReload()));
    // Do nothing for subsequent calls that happen while the tab remains crashed. This
    // can occur when the tab is in the background and it shares the renderer with other
    // tabs. After the renderer crashes, the WebContents of its tabs are still around
    // and they still share the RenderProcessHost. When one of the tabs reloads spawning
    // a new renderer for the shared RenderProcessHost and the new renderer crashes
    // again, all tabs sharing this renderer will be notified about the crash (including
    // potential background tabs that did not reload yet).
    if (mTab.needsReload() || mTab.isShowingSadTab()) return;

    // This will replace TabRendererCrashStatus if numbers line up.
    int appState = ApplicationStatus.getStateForApplication();
    boolean applicationRunning = (appState == ApplicationState.HAS_RUNNING_ACTIVITIES);
    boolean applicationPaused = (appState == ApplicationState.HAS_PAUSED_ACTIVITIES);
    @TabRendererExitStatus int rendererExitStatus = TAB_RENDERER_EXIT_STATUS_MAX;
    if (processWasOomProtected) {
        if (applicationRunning) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_RUNNING_APP;
        } else if (applicationPaused) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_PAUSED_APP;
        } else {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_BACKGROUND_APP;
        }
    } else {
        if (applicationRunning) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_RUNNING_APP;
        } else if (applicationPaused) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_PAUSED_APP;
        } else {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_BACKGROUND_APP;
        }
    }
    RecordHistogram.recordEnumeratedHistogram(
            "Tab.RendererExitStatus", rendererExitStatus, TAB_RENDERER_EXIT_STATUS_MAX);

    int activityState = ApplicationStatus.getStateForActivity(
            mTab.getWindowAndroid().getActivity().get());
    int rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_MAX;
    if (!processWasOomProtected
            || activityState == ActivityState.PAUSED
            || activityState == ActivityState.STOPPED
            || activityState == ActivityState.DESTROYED) {
        // The tab crashed in background or was killed by the OS out-of-memory killer.
        //setNeedsReload(true);
        mTab.setNeedsReload(true);
        if (applicationRunning) {
            rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_HIDDEN_IN_FOREGROUND_APP;
        } else {
            rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_HIDDEN_IN_BACKGROUND_APP;
        }
    } else {
        rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_SHOWN_IN_FOREGROUND_APP;
        mTab.showSadTab();
        // This is necessary to correlate histogram data with stability counts.
        UmaSessionStats.logRendererCrash();
    }
    RecordHistogram.recordEnumeratedHistogram(
            "Tab.RendererCrashStatus", rendererCrashStatus, TAB_RENDERER_CRASH_STATUS_MAX);

    mTab.handleTabCrash();

    boolean sadTabShown = mTab.isShowingSadTab();
    RewindableIterator<TabObserver> observers = mTab.getTabObservers();
    while (observers.hasNext()) {
        observers.next().onCrash(mTab, sadTabShown);
    }
}
 
Example 19
Source File: WebApkUpdateManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/** Returns whether the associated WebApkActivity is running in foreground. */
protected boolean isInForeground() {
    int state = ApplicationStatus.getStateForActivity(mActivity);
    return (state != ActivityState.STOPPED && state != ActivityState.DESTROYED);
}
 
Example 20
Source File: TabWebContentsObserver.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void renderProcessGone(boolean processWasOomProtected) {
    Log.i(TAG, "renderProcessGone() for tab id: " + mTab.getId()
            + ", oom protected: " + Boolean.toString(processWasOomProtected)
            + ", already needs reload: " + Boolean.toString(mTab.needsReload()));
    // Do nothing for subsequent calls that happen while the tab remains crashed. This
    // can occur when the tab is in the background and it shares the renderer with other
    // tabs. After the renderer crashes, the WebContents of its tabs are still around
    // and they still share the RenderProcessHost. When one of the tabs reloads spawning
    // a new renderer for the shared RenderProcessHost and the new renderer crashes
    // again, all tabs sharing this renderer will be notified about the crash (including
    // potential background tabs that did not reload yet).
    if (mTab.needsReload() || mTab.isShowingSadTab()) return;

    // This will replace TabRendererCrashStatus if numbers line up.
    int appState = ApplicationStatus.getStateForApplication();
    boolean applicationRunning = (appState == ApplicationState.HAS_RUNNING_ACTIVITIES);
    boolean applicationPaused = (appState == ApplicationState.HAS_PAUSED_ACTIVITIES);
    @TabRendererExitStatus int rendererExitStatus = TAB_RENDERER_EXIT_STATUS_MAX;
    if (processWasOomProtected) {
        if (applicationRunning) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_RUNNING_APP;
        } else if (applicationPaused) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_PAUSED_APP;
        } else {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_OOM_PROTECTED_IN_BACKGROUND_APP;
        }
    } else {
        if (applicationRunning) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_RUNNING_APP;
        } else if (applicationPaused) {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_PAUSED_APP;
        } else {
            rendererExitStatus = TAB_RENDERER_EXIT_STATUS_NOT_PROTECTED_IN_BACKGROUND_APP;
        }
    }
    RecordHistogram.recordEnumeratedHistogram(
            "Tab.RendererExitStatus", rendererExitStatus, TAB_RENDERER_EXIT_STATUS_MAX);

    int activityState = ApplicationStatus.getStateForActivity(
            mTab.getWindowAndroid().getActivity().get());
    int rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_MAX;
    if (!processWasOomProtected
            || activityState == ActivityState.PAUSED
            || activityState == ActivityState.STOPPED
            || activityState == ActivityState.DESTROYED) {
        // The tab crashed in background or was killed by the OS out-of-memory killer.
        //setNeedsReload(true);
        mTab.setNeedsReload(true);
        if (applicationRunning) {
            rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_HIDDEN_IN_FOREGROUND_APP;
        } else {
            rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_HIDDEN_IN_BACKGROUND_APP;
        }
    } else {
        rendererCrashStatus = TAB_RENDERER_CRASH_STATUS_SHOWN_IN_FOREGROUND_APP;
        mTab.showSadTab();
        // This is necessary to correlate histogram data with stability counts.
        UmaSessionStats.logRendererCrash();
    }
    RecordHistogram.recordEnumeratedHistogram(
            "Tab.RendererCrashStatus", rendererCrashStatus, TAB_RENDERER_CRASH_STATUS_MAX);

    mTab.handleTabCrash();
}