<commit_before>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.tabmodel.document;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import org.chromium.base.ApplicationStatus;
import org.chromium.chrome.browser.Tab;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.document.DocumentUtils;
import org.chromium.chrome.browser.tabmodel.document.DocumentTabModel.Entry;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Interfaces with the ActivityManager to identify Tabs/Tasks that are being tracked by
 * Android's Recents list.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class ActivityDelegate {
    private final Class<?> mRegularClass;
    private final Class<?> mIncognitoClass;

    /**
     * Creates a ActivityDelegate.
     * @param regularClass Class of the regular DocumentActivity.
     * @param incognitoClass Class of the Incognito DocumentActivity.
     */
    public ActivityDelegate(Class<?> regularClass, Class<?> incognitoClass) {
        mRegularClass = regularClass;
        mIncognitoClass = incognitoClass;
    }

    /**
     * Returns whether an Activity is a DocumentActivity.  Assumes the Incognito Activity inherits
     * from the regular Activity.
     * @param activity Activity to check.
     * @return Whether the Activity is a DocumentActivity.
     */
    public boolean isDocumentActivity(Activity activity) {
        return mRegularClass.isInstance(activity);
    }

    /**
     * Checks whether or not the Intent corresponds to an Activity that should be tracked.
     * @param isIncognito Whether or not the TabModel is managing incognito tabs.
     * @param intent Intent used to launch the Activity.
     * @return Whether or not to track the Activity.
     */
    public boolean isValidActivity(boolean isIncognito, Intent intent) {
        if (intent == null) return false;
        String desiredClassName = isIncognito ? mIncognitoClass.getName() : mRegularClass.getName();
        String className = null;
        if (intent.getComponent() == null) {
            Context context = ApplicationStatus.getApplicationContext();
            PackageManager pm = context.getPackageManager();
            ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
            if (resolveInfo != null) className = resolveInfo.activityInfo.name;
        } else {
            className = intent.getComponent().getClassName();
        }

        return TextUtils.equals(className, desiredClassName);
    }

    /**<commit_msg>Fix disappearing tasks when upgrading from M43 to M44.<commit_after>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.tabmodel.document;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import org.chromium.base.ApplicationStatus;
import org.chromium.chrome.browser.Tab;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.document.DocumentUtils;
import org.chromium.chrome.browser.tabmodel.document.DocumentTabModel.Entry;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Interfaces with the ActivityManager to identify Tabs/Tasks that are being tracked by
 * Android's Recents list.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class ActivityDelegate {
    public static final String LEGACY_CLASS_NAME =
            "com.google.android.apps.chrome.document.DocumentActivity";
    public static final String LEGACY_INCOGNITO_CLASS_NAME =
            "com.google.android.apps.chrome.document.IncognitoDocumentActivity";

    private final Class<?> mRegularClass;
    private final Class<?> mIncognitoClass;

    /**
     * Creates a ActivityDelegate.
     * @param regularClass Class of the regular DocumentActivity.
     * @param incognitoClass Class of the Incognito DocumentActivity.
     */
    public ActivityDelegate(Class<?> regularClass, Class<?> incognitoClass) {
        mRegularClass = regularClass;
        mIncognitoClass = incognitoClass;
    }

    /**
     * Returns whether an Activity is a DocumentActivity.  Assumes the Incognito Activity inherits
     * from the regular Activity.
     * @param activity Activity to check.
     * @return Whether the Activity is a DocumentActivity.
     */
    public boolean isDocumentActivity(Activity activity) {
        return mRegularClass.isInstance(activity);
    }

    /**
     * Checks whether or not the Intent corresponds to an Activity that should be tracked.
     * @param isIncognito Whether or not the TabModel is managing incognito tabs.
     * @param intent Intent used to launch the Activity.
     * @return Whether or not to track the Activity.
     */
    public boolean isValidActivity(boolean isIncognito, Intent intent) {
        if (intent == null) return false;
        String desiredClassName = isIncognito ? mIncognitoClass.getName() : mRegularClass.getName();
        String desiredLegacyClassName = isIncognito
                ? LEGACY_INCOGNITO_CLASS_NAME
                : LEGACY_CLASS_NAME;
        String className = null;
        if (intent.getComponent() == null) {
            Context context = ApplicationStatus.getApplicationContext();
            PackageManager pm = context.getPackageManager();
            ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
            if (resolveInfo != null) className = resolveInfo.activityInfo.name;
        } else {
            className = intent.getComponent().getClassName();
        }

        return TextUtils.equals(className, desiredClassName)
                || TextUtils.equals(className, desiredLegacyClassName);
    }

    /**
{'fraction_non_alphanumeric': 0.08301128458803525, 'fraction_numerical': 0.0021641675684031534, 'mean_word_length': 3.4406314344543585, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821067', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1667, 'n_words': 501}