Java Code Examples for org.chromium.chrome.browser.util.IntentUtils#safeGetIntExtra()

The following examples show how to use org.chromium.chrome.browser.util.IntentUtils#safeGetIntExtra() . 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: WebappInfo.java    From delion with Apache License 2.0 7 votes vote down vote up
public static int displayModeFromIntent(Intent intent) {
    String displayMode =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_DISPLAY_MODE);
    if (displayMode == null) {
        return IntentUtils.safeGetIntExtra(
                intent, ShortcutHelper.EXTRA_DISPLAY_MODE, WebDisplayMode.Standalone);
    }

    // {@link displayMode} should be one of
    // https://w3c.github.io/manifest/#dfn-display-modes-values
    if (displayMode.equals("fullscreen")) {
        return WebDisplayMode.Fullscreen;
    } else if (displayMode.equals("minimal-ui")) {
        return WebDisplayMode.MinimalUi;
    } else if (displayMode.equals("browser")) {
        return WebDisplayMode.Browser;
    } else {
        return WebDisplayMode.Standalone;
    }
}
 
Example 2
Source File: ActivityDelegate.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether or not the Intent contains an ID for document mode.
 * @param intent Intent to check.
 * @return ID for the document that has the given intent as base intent, or
 *         {@link Tab.INVALID_TAB_ID} if it couldn't be retrieved.
 */
public static int getTabIdFromIntent(Intent intent) {
    if (intent == null || intent.getData() == null) return Tab.INVALID_TAB_ID;

    // Avoid AsyncTabCreationParams related flows early returning here.
    if (AsyncTabParamsManager.hasParamsWithTabToReparent()) {
        return IntentUtils.safeGetIntExtra(
                intent, IntentHandler.EXTRA_TAB_ID, Tab.INVALID_TAB_ID);
    }

    Uri data = intent.getData();
    if (!TextUtils.equals(data.getScheme(), UrlConstants.DOCUMENT_SCHEME)) {
        return Tab.INVALID_TAB_ID;
    }

    try {
        return Integer.parseInt(data.getHost());
    } catch (NumberFormatException e) {
        return Tab.INVALID_TAB_ID;
    }
}
 
Example 3
Source File: DownloadNotificationService.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if an intent requires operations on a download.
 * @param intent An intent to validate.
 * @return true if the intent requires actions, or false otherwise.
 */
static boolean isDownloadOperationIntent(Intent intent) {
    if (intent == null) return false;
    if (ACTION_DOWNLOAD_RESUME_ALL.equals(intent.getAction())) return true;
    if (!ACTION_DOWNLOAD_CANCEL.equals(intent.getAction())
            && !ACTION_DOWNLOAD_RESUME.equals(intent.getAction())
            && !ACTION_DOWNLOAD_PAUSE.equals(intent.getAction())) {
        return false;
    }
    if (!intent.hasExtra(EXTRA_DOWNLOAD_NOTIFICATION_ID)
            || !intent.hasExtra(EXTRA_DOWNLOAD_FILE_NAME)
            || !intent.hasExtra(EXTRA_DOWNLOAD_GUID)) {
        return false;
    }
    final int notificationId =
            IntentUtils.safeGetIntExtra(intent, EXTRA_DOWNLOAD_NOTIFICATION_ID, -1);
    if (notificationId == -1) return false;
    final String fileName = IntentUtils.safeGetStringExtra(intent, EXTRA_DOWNLOAD_FILE_NAME);
    if (fileName == null) return false;
    final String guid = IntentUtils.safeGetStringExtra(intent, EXTRA_DOWNLOAD_GUID);
    if (!DownloadSharedPreferenceEntry.isValidGUID(guid)) return false;
    return true;
}
 
Example 4
Source File: DownloadNotificationService.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Retrives DownloadSharedPreferenceEntry from a download action intent.
 * @param intent Intent that contains the download action.
 */
private DownloadSharedPreferenceEntry getDownloadEntryFromIntent(Intent intent) {
    if (intent.getAction() == ACTION_DOWNLOAD_RESUME_ALL) return null;
    String guid = IntentUtils.safeGetStringExtra(intent, EXTRA_DOWNLOAD_GUID);
    DownloadSharedPreferenceEntry entry = getDownloadSharedPreferenceEntry(guid);
    if (entry != null) return entry;
    int notificationId = IntentUtils.safeGetIntExtra(
            intent, EXTRA_DOWNLOAD_NOTIFICATION_ID, -1);
    String fileName = IntentUtils.safeGetStringExtra(intent, EXTRA_DOWNLOAD_FILE_NAME);
    boolean metered = DownloadManagerService.isActiveNetworkMetered(mContext);
    boolean isOffTheRecord =  IntentUtils.safeGetBooleanExtra(
            intent, EXTRA_DOWNLOAD_IS_OFF_THE_RECORD, false);
    boolean isOfflinePage =  IntentUtils.safeGetBooleanExtra(
            intent, EXTRA_DOWNLOAD_IS_OFFLINE_PAGE, false);
    return new DownloadSharedPreferenceEntry(notificationId, isOffTheRecord, metered, guid,
            fileName, isOfflinePage ? DownloadSharedPreferenceEntry.ITEM_TYPE_OFFLINE_PAGE
                    : DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD);
}
 
Example 5
Source File: WebappInfo.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a WebappInfo.
 * @param intent Intent containing info about the app.
 */
public static WebappInfo create(Intent intent) {
    String id = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_ID);
    String icon = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_ICON);
    String url = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_URL);
    int displayMode = displayModeFromIntent(intent);
    int orientation = orientationFromIntent(intent);
    int source = IntentUtils.safeGetIntExtra(intent,
            ShortcutHelper.EXTRA_SOURCE, ShortcutSource.UNKNOWN);
    long themeColor = IntentUtils.safeGetLongExtra(intent,
            ShortcutHelper.EXTRA_THEME_COLOR,
            ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING);
    long backgroundColor = IntentUtils.safeGetLongExtra(intent,
            ShortcutHelper.EXTRA_BACKGROUND_COLOR,
            ShortcutHelper.MANIFEST_COLOR_INVALID_OR_MISSING);
    boolean isIconGenerated = IntentUtils.safeGetBooleanExtra(intent,
            ShortcutHelper.EXTRA_IS_ICON_GENERATED, false);

    String name = nameFromIntent(intent);
    String shortName = shortNameFromIntent(intent);
    String webApkPackageName = IntentUtils.safeGetStringExtra(intent,
            ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME);

    return create(id, url, icon, name, shortName, displayMode, orientation, source,
            themeColor, backgroundColor, isIconGenerated, webApkPackageName);
}
 
Example 6
Source File: PhysicalWebBroadcastService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        int state = IntentUtils.safeGetIntExtra(
                intent, BluetoothAdapter.EXTRA_STATE, BLUETOOTH_ADAPTER_DEFAULT_STATE);
        if (state == BluetoothAdapter.STATE_OFF) {
            stopSelf();
        }
    } else if (STOP_SERVICE.equals(action)) {
        stopSelf();
    } else {
        Log.e(TAG, "Unrecognized Broadcast Received");
    }
}
 
Example 7
Source File: BookmarkWidgetService.java    From delion with Apache License 2.0 5 votes vote down vote up
static void changeFolder(Context context, Intent intent) {
    int widgetId = IntentUtils.safeGetIntExtra(intent, AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    String serializedFolder = IntentUtils.safeGetStringExtra(intent, EXTRA_FOLDER_ID);
    if (widgetId >= 0 && serializedFolder != null) {
        SharedPreferences prefs = getWidgetState(context, widgetId);
        prefs.edit().putString(PREF_CURRENT_FOLDER, serializedFolder).apply();
        AppWidgetManager.getInstance(context)
                .notifyAppWidgetViewDataChanged(widgetId, R.id.bookmarks_list);
    }
}
 
Example 8
Source File: BookmarkWidgetService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@UiThread
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    int widgetId = IntentUtils.safeGetIntExtra(intent, AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    if (widgetId < 0) {
        Log.w(TAG, "Missing EXTRA_APPWIDGET_ID!");
        return null;
    }
    return new BookmarkAdapter(this, widgetId);
}
 
Example 9
Source File: DownloadBroadcastReceiver.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Called to open a particular download item.  Falls back to opening Download Home.
 * @param context Context of the receiver.
 * @param intent Intent from the android DownloadManager.
 */
private void openDownload(final Context context, Intent intent) {
    int notificationId = IntentUtils.safeGetIntExtra(
            intent, NotificationConstants.EXTRA_NOTIFICATION_ID, -1);
    DownloadNotificationService.hideDanglingSummaryNotification(context, notificationId);

    long ids[] =
            intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
    if (ids == null || ids.length == 0) {
        DownloadManagerService.openDownloadsPage(context);
        return;
    }

    long id = ids[0];
    Uri uri = DownloadManagerDelegate.getContentUriFromDownloadManager(context, id);
    if (uri == null) {
        DownloadManagerService.openDownloadsPage(context);
        return;
    }

    String downloadFilename = IntentUtils.safeGetStringExtra(
            intent, DownloadNotificationService.EXTRA_DOWNLOAD_FILE_PATH);
    boolean isSupportedMimeType =  IntentUtils.safeGetBooleanExtra(
            intent, DownloadNotificationService.EXTRA_IS_SUPPORTED_MIME_TYPE, false);
    boolean isOffTheRecord = IntentUtils.safeGetBooleanExtra(
            intent, DownloadNotificationService.EXTRA_IS_OFF_THE_RECORD, false);
    ContentId contentId = DownloadNotificationService.getContentIdFromIntent(intent);
    DownloadManagerService.openDownloadedContent(
            context, downloadFilename, isSupportedMimeType, isOffTheRecord, contentId.id, id);
}
 
Example 10
Source File: WebappInfo.java    From 365browser with Apache License 2.0 5 votes vote down vote up
protected static int sourceFromIntent(Intent intent) {
    int source = IntentUtils.safeGetIntExtra(
            intent, ShortcutHelper.EXTRA_SOURCE, ShortcutSource.UNKNOWN);
    if (source >= ShortcutSource.COUNT) {
        source = ShortcutSource.UNKNOWN;
    }
    return source;
}
 
Example 11
Source File: InstantAppsHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * In the case where Chrome is called through the fallback mechanism from Instant Apps,
 * record the amount of time the whole trip took and which UI took the user back to Chrome,
 * if any.
 * @param intent The current intent.
 */
private void maybeRecordFallbackStats(Intent intent) {
    Long startTime = IntentUtils.safeGetLongExtra(intent, INSTANT_APP_START_TIME_EXTRA, 0);
    if (startTime > 0) {
        sFallbackIntentTimes.record(SystemClock.elapsedRealtime() - startTime);
        intent.removeExtra(INSTANT_APP_START_TIME_EXTRA);
    }
    int callSource = IntentUtils.safeGetIntExtra(intent, BROWSER_LAUNCH_REASON, 0);
    if (callSource > 0 && callSource < SOURCE_BOUNDARY) {
        sFallbackCallSource.record(callSource);
        intent.removeExtra(BROWSER_LAUNCH_REASON);
    } else if (callSource >= SOURCE_BOUNDARY) {
        Log.e(TAG, "Unexpected call source constant for Instant Apps: " + callSource);
    }
}
 
Example 12
Source File: CustomTabIntentDataProvider.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Must be called after calling {@link #retrieveToolbarColor(Intent, Context)}.
 */
private void retrieveBottomBarColor(Intent intent) {
    int defaultColor = mToolbarColor;
    int color = IntentUtils.safeGetIntExtra(intent,
            CustomTabsIntent.EXTRA_SECONDARY_TOOLBAR_COLOR, defaultColor);
    mBottomBarColor = removeTransparencyFromColor(color);
}
 
Example 13
Source File: IntentHandler.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Some applications may request to load the URL with a particular transition type.
 * @param context The application context.
 * @param intent Intent causing the URL load, may be null.
 * @param defaultTransition The transition to return if none specified in the intent.
 * @return The transition type to use for loading the URL.
 */
public static int getTransitionTypeFromIntent(Context context, Intent intent,
        int defaultTransition) {
    if (intent == null) return defaultTransition;
    int transitionType = IntentUtils.safeGetIntExtra(
            intent, IntentHandler.EXTRA_PAGE_TRANSITION_TYPE, PageTransition.LINK);
    if (transitionType == PageTransition.TYPED) {
        return transitionType;
    } else if (transitionType != PageTransition.LINK
            && isIntentChromeOrFirstParty(intent, context)) {
        // 1st party applications may specify any transition type.
        return transitionType;
    }
    return defaultTransition;
}
 
Example 14
Source File: IntentHandler.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
private TabOpenType getTabOpenType(Intent intent) {
    if (IntentUtils.safeGetBooleanExtra(
                intent, ShortcutHelper.REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, false)) {
        return TabOpenType.REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB;
    }

    if (IntentUtils.safeGetBooleanExtra(intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)) {
        return TabOpenType.OPEN_NEW_INCOGNITO_TAB;
    }

    if (IntentUtils.safeGetIntExtra(intent, TabOpenType.BRING_TAB_TO_FRONT.name(),
                Tab.INVALID_TAB_ID) != Tab.INVALID_TAB_ID) {
        return TabOpenType.BRING_TAB_TO_FRONT;
    }

    String appId = IntentUtils.safeGetStringExtra(intent, Browser.EXTRA_APPLICATION_ID);
    // Due to users complaints, we are NOT reusing tabs for apps that do not specify an appId.
    if (appId == null
            || IntentUtils.safeGetBooleanExtra(intent, Browser.EXTRA_CREATE_NEW_TAB, false)) {
        return TabOpenType.OPEN_NEW_TAB;
    }

    // Intents from chrome open in the same tab by default, all others only clobber
    // tabs created by the same app.
    return mPackageName.equals(appId) ? TabOpenType.CLOBBER_CURRENT_TAB
                                      : TabOpenType.REUSE_APP_ID_MATCHING_TAB_ELSE_NEW_TAB;
}
 
Example 15
Source File: WebappInfo.java    From delion with Apache License 2.0 5 votes vote down vote up
public static int orientationFromIntent(Intent intent) {
    String orientation =
            IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_ORIENTATION);
    if (orientation == null) {
        return IntentUtils.safeGetIntExtra(
                intent, ShortcutHelper.EXTRA_ORIENTATION, ScreenOrientationValues.DEFAULT);
    }

    // {@link orientation} should be one of
    // w3c.github.io/screen-orientation/#orientationlocktype-enum
    if (orientation.equals("any")) {
        return ScreenOrientationValues.ANY;
    } else if (orientation.equals("natural")) {
        return ScreenOrientationValues.NATURAL;
    } else if (orientation.equals("landscape")) {
        return ScreenOrientationValues.LANDSCAPE;
    } else if (orientation.equals("landscape-primary")) {
        return ScreenOrientationValues.LANDSCAPE_PRIMARY;
    } else if (orientation.equals("landscape-secondary")) {
        return ScreenOrientationValues.LANDSCAPE_SECONDARY;
    } else if (orientation.equals("portrait")) {
        return ScreenOrientationValues.PORTRAIT;
    } else if (orientation.equals("portrait-primary")) {
        return ScreenOrientationValues.PORTRAIT_PRIMARY;
    } else if (orientation.equals("portrait-secondary")) {
        return ScreenOrientationValues.PORTRAIT_SECONDARY;
    } else {
        return ScreenOrientationValues.DEFAULT;
    }
}
 
Example 16
Source File: CustomTabIntentDataProvider.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Must be called after calling {@link #retrieveToolbarColor(Intent, Context)}.
 */
private void retrieveBottomBarColor(Intent intent) {
    int defaultColor = mToolbarColor;
    int color = IntentUtils.safeGetIntExtra(intent,
            CustomTabsIntent.EXTRA_SECONDARY_TOOLBAR_COLOR, defaultColor);
    mBottomBarColor = removeTransparencyFromColor(color, defaultColor);
}
 
Example 17
Source File: IntentHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private TabOpenType getTabOpenType(Intent intent) {
    if (IntentUtils.safeGetBooleanExtra(
                intent, ShortcutHelper.REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, false)) {
        return TabOpenType.REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB;
    }

    if (IntentUtils.safeGetBooleanExtra(intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)) {
        return TabOpenType.OPEN_NEW_INCOGNITO_TAB;
    }

    if (IntentUtils.safeGetIntExtra(intent, TabOpenType.BRING_TAB_TO_FRONT.name(),
                Tab.INVALID_TAB_ID) != Tab.INVALID_TAB_ID) {
        return TabOpenType.BRING_TAB_TO_FRONT;
    }

    String appId = IntentUtils.safeGetStringExtra(intent, Browser.EXTRA_APPLICATION_ID);
    // Due to users complaints, we are NOT reusing tabs for apps that do not specify an appId.
    if (appId == null
            || IntentUtils.safeGetBooleanExtra(intent, Browser.EXTRA_CREATE_NEW_TAB, false)) {
        return TabOpenType.OPEN_NEW_TAB;
    }

    // Intents from chrome open in the same tab by default, all others only clobber
    // tabs created by the same app.
    return mPackageName.equals(appId) ? TabOpenType.CLOBBER_CURRENT_TAB
                                      : TabOpenType.REUSE_APP_ID_MATCHING_TAB_ELSE_NEW_TAB;
}
 
Example 18
Source File: IntentHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Some applications may request to load the URL with a particular transition type.
 * @param intent Intent causing the URL load, may be null.
 * @param defaultTransition The transition to return if none specified in the intent.
 * @return The transition type to use for loading the URL.
 */
public static int getTransitionTypeFromIntent(Intent intent, int defaultTransition) {
    if (intent == null) return defaultTransition;
    int transitionType = IntentUtils.safeGetIntExtra(
            intent, IntentHandler.EXTRA_PAGE_TRANSITION_TYPE, PageTransition.LINK);
    if (transitionType == PageTransition.TYPED) {
        return transitionType;
    } else if (transitionType != PageTransition.LINK
            && isIntentChromeOrFirstParty(intent)) {
        // 1st party applications may specify any transition type.
        return transitionType;
    }
    return defaultTransition;
}
 
Example 19
Source File: MultiWindowUtils.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * Determines the correct ChromeTabbedActivity class to use for an incoming intent.
 * @param intent The incoming intent that is starting ChromeTabbedActivity.
 * @param context The current Context, used to retrieve the ActivityManager system service.
 * @return The ChromeTabbedActivity to use for the incoming intent.
 */
public Class<? extends ChromeTabbedActivity> getTabbedActivityForIntent(Intent intent,
        Context context) {
    // 1. Exit early if the build version doesn't support Android N+ multi-window mode or
    // ChromeTabbedActivity2 isn't running.
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M
            || (mTabbedActivity2TaskRunning != null && !mTabbedActivity2TaskRunning)) {
        return ChromeTabbedActivity.class;
    }

    // 2. If the intent has a window id set, use that.
    if (intent.hasExtra(IntentHandler.EXTRA_WINDOW_ID)) {
        int windowId = IntentUtils.safeGetIntExtra(intent, IntentHandler.EXTRA_WINDOW_ID, 0);
        if (windowId == 1) return ChromeTabbedActivity.class;
        if (windowId == 2) return ChromeTabbedActivity2.class;
    }

    // 3. If only one ChromeTabbedActivity is currently in Android recents, use it.
    boolean tabbed2TaskRunning = isActivityTaskInRecents(
            ChromeTabbedActivity2.class.getName(), context);

    // Exit early if ChromeTabbedActivity2 isn't running.
    if (!tabbed2TaskRunning) {
        mTabbedActivity2TaskRunning = false;
        return ChromeTabbedActivity.class;
    }

    boolean tabbedTaskRunning = isActivityTaskInRecents(
            ChromeTabbedActivity.class.getName(), context);
    if (!tabbedTaskRunning) {
        return ChromeTabbedActivity2.class;
    }

    // 4. If only one of the ChromeTabbedActivity's is currently visible use it.
    // e.g. ChromeTabbedActivity is docked to the top of the screen and another app is docked
    // to the bottom.

    // Find the activities.
    Activity tabbedActivity = null;
    Activity tabbedActivity2 = null;
    for (WeakReference<Activity> reference : ApplicationStatus.getRunningActivities()) {
        Activity activity = reference.get();
        if (activity == null) continue;
        if (activity.getClass().equals(ChromeTabbedActivity.class)) {
            tabbedActivity = activity;
        } else if (activity.getClass().equals(ChromeTabbedActivity2.class)) {
            tabbedActivity2 = activity;
        }
    }

    // Determine if only one is visible.
    boolean tabbedActivityVisible = isActivityVisible(tabbedActivity);
    boolean tabbedActivity2Visible = isActivityVisible(tabbedActivity2);
    if (tabbedActivityVisible ^ tabbedActivity2Visible) {
        if (tabbedActivityVisible) return ChromeTabbedActivity.class;
        return ChromeTabbedActivity2.class;
    }

    // 5. Use the ChromeTabbedActivity that was resumed most recently if it's still running.
    if (mLastResumedTabbedActivity != null) {
        ChromeTabbedActivity lastResumedActivity = mLastResumedTabbedActivity.get();
        if (lastResumedActivity != null) {
            Class<?> lastResumedClassName = lastResumedActivity.getClass();
            if (tabbedTaskRunning
                    && lastResumedClassName.equals(ChromeTabbedActivity.class)) {
                return ChromeTabbedActivity.class;
            }
            if (tabbed2TaskRunning
                    && lastResumedClassName.equals(ChromeTabbedActivity2.class)) {
                return ChromeTabbedActivity2.class;
            }
        }
    }

    // 6. Default to regular ChromeTabbedActivity.
    return ChromeTabbedActivity.class;
}
 
Example 20
Source File: MultiWindowUtils.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Determines the correct ChromeTabbedActivity class to use for an incoming intent.
 * @param intent The incoming intent that is starting ChromeTabbedActivity.
 * @param context The current Context, used to retrieve the ActivityManager system service.
 * @return The ChromeTabbedActivity to use for the incoming intent.
 */
public Class<? extends ChromeTabbedActivity> getTabbedActivityForIntent(Intent intent,
        Context context) {
    // 1. Exit early if the build version doesn't support Android N+ multi-window mode or
    // ChromeTabbedActivity2 isn't running.
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M
            || (mTabbedActivity2TaskRunning != null && !mTabbedActivity2TaskRunning)) {
        return ChromeTabbedActivity.class;
    }

    // 2. If the intent has a window id set, use that.
    if (intent.hasExtra(IntentHandler.EXTRA_WINDOW_ID)) {
        int windowId = IntentUtils.safeGetIntExtra(intent, IntentHandler.EXTRA_WINDOW_ID, 0);
        if (windowId == 1) return ChromeTabbedActivity.class;
        if (windowId == 2) return ChromeTabbedActivity2.class;
    }

    // 3. If only one ChromeTabbedActivity is currently in Android recents, use it.
    boolean tabbed2TaskRunning = isActivityTaskInRecents(
            ChromeTabbedActivity2.class.getName(), context);

    // Exit early if ChromeTabbedActivity2 isn't running.
    if (!tabbed2TaskRunning) {
        mTabbedActivity2TaskRunning = false;
        return ChromeTabbedActivity.class;
    }

    boolean tabbedTaskRunning = isActivityTaskInRecents(
            ChromeTabbedActivity.class.getName(), context);
    if (!tabbedTaskRunning) {
        return ChromeTabbedActivity2.class;
    }

    // 4. If only one of the ChromeTabbedActivity's is currently visible use it.
    // e.g. ChromeTabbedActivity is docked to the top of the screen and another app is docked
    // to the bottom.

    // Find the activities.
    Activity tabbedActivity = null;
    Activity tabbedActivity2 = null;
    for (WeakReference<Activity> reference : ApplicationStatus.getRunningActivities()) {
        Activity activity = reference.get();
        if (activity == null) continue;
        if (activity.getClass().equals(ChromeTabbedActivity.class)) {
            tabbedActivity = activity;
        } else if (activity.getClass().equals(ChromeTabbedActivity2.class)) {
            tabbedActivity2 = activity;
        }
    }

    // Determine if only one is visible.
    boolean tabbedActivityVisible = isActivityVisible(tabbedActivity);
    boolean tabbedActivity2Visible = isActivityVisible(tabbedActivity2);
    if (tabbedActivityVisible ^ tabbedActivity2Visible) {
        if (tabbedActivityVisible) return ChromeTabbedActivity.class;
        return ChromeTabbedActivity2.class;
    }

    // 5. Use the ChromeTabbedActivity that was resumed most recently if it's still running.
    if (mLastResumedTabbedActivity != null) {
        ChromeTabbedActivity lastResumedActivity = mLastResumedTabbedActivity.get();
        if (lastResumedActivity != null) {
            Class<?> lastResumedClassName = lastResumedActivity.getClass();
            if (tabbedTaskRunning
                    && lastResumedClassName.equals(ChromeTabbedActivity.class)) {
                return ChromeTabbedActivity.class;
            }
            if (tabbed2TaskRunning
                    && lastResumedClassName.equals(ChromeTabbedActivity2.class)) {
                return ChromeTabbedActivity2.class;
            }
        }
    }

    // 6. Default to regular ChromeTabbedActivity.
    return ChromeTabbedActivity.class;
}