<commit_before>
package info.guardianproject.panic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class PanicResponder {

    public static final String PREF_TRIGGER_PACKAGE_NAME = "panicResponderTriggerPackageName";

    /**
     * Checks whether the provided {@link Activity} was started with the action
     * {@link Panic#ACTION_DISCONNECT}, and if so, processes that {@link Intent}
     * , removing the sending app as the panic trigger if it is currently
     * configured to be so.
     *
     * @param activity the {@code Activity} to check for the {@code Intent}
     * @return whether an {@code ACTION_DISCONNECT Intent} was received
     */
    public static boolean checkForDisconnectIntent(Activity activity) {
        boolean result = false;
        Intent intent = activity.getIntent();
        if (intent != null && TextUtils.equals(intent.getAction(), Panic.ACTION_DISCONNECT)) {
            result = true;
            if (TextUtils.equals(PanicUtils.getCallingPackageName(activity),
                    getTriggerPackageName(activity))) {
                setTriggerPackageName(activity, null);
            }
        }
        return result;
    }

    /**
     * Get the {@code packageName} of the currently configured panic trigger
     * app, or {@code null} if none.
     *
     * @param context the app's {@link Context}
     * @return the {@code packageName} or null
     */
    public static String getTriggerPackageName(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(PREF_TRIGGER_PACKAGE_NAME, null);
    }

    /**
     * Set the currently configured panic trigger app using the {@link Activity}
     * that received a {@link Panic#ACTION_CONNECT} {@link Intent}. If that
     * {@code Intent} was not set with either
     * {@link Activity#startActivityForResult(Intent, int)} or
     * {@link Intent#setPackage(String)}, then this will result in no panic
     * trigger app being active.
     * <p>
     * When the user changes the panic app config, then the current app needs to
     * send {@link Intent}s to the previous app, and the currently configured
     * app to let them know about the changes. This is done by sending an
     * {@code ACTION_DISCONNECT Intent} to the previous app, and an
     * {@code ACTION_CONNECT Intent} to the newly configured app.
     * <p>
     * When this is called with an {@code Activity} in the same responder app
     * that called the {@link Activity#startActivityForResult(Intent, int)},
     * then it will <b>not</b> change the existing trigger app setting! For
     * example, if the responder app launches its Panic Config {@code Activity}.
     *
     * @param activity the {@link Activity} that received an
     *                 {@link Panic#ACTION_CONNECT} {@link Intent}
     */
    public static void setTriggerPackageName(Activity activity) {
        String intentPackageName = activity.getIntent().getPackage();
        String callingPackageName = PanicUtils.getCallingPackageName(activity);
        if (intentPackageName == null && callingPackageName == null) {
            return;<commit_msg>remove useless return statement to get rid of lint warning<commit_after>
package info.guardianproject.panic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class PanicResponder {

    public static final String PREF_TRIGGER_PACKAGE_NAME = "panicResponderTriggerPackageName";

    /**
     * Checks whether the provided {@link Activity} was started with the action
     * {@link Panic#ACTION_DISCONNECT}, and if so, processes that {@link Intent}
     * , removing the sending app as the panic trigger if it is currently
     * configured to be so.
     *
     * @param activity the {@code Activity} to check for the {@code Intent}
     * @return whether an {@code ACTION_DISCONNECT Intent} was received
     */
    public static boolean checkForDisconnectIntent(Activity activity) {
        boolean result = false;
        Intent intent = activity.getIntent();
        if (intent != null && TextUtils.equals(intent.getAction(), Panic.ACTION_DISCONNECT)) {
            result = true;
            if (TextUtils.equals(PanicUtils.getCallingPackageName(activity),
                    getTriggerPackageName(activity))) {
                setTriggerPackageName(activity, null);
            }
        }
        return result;
    }

    /**
     * Get the {@code packageName} of the currently configured panic trigger
     * app, or {@code null} if none.
     *
     * @param context the app's {@link Context}
     * @return the {@code packageName} or null
     */
    public static String getTriggerPackageName(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(PREF_TRIGGER_PACKAGE_NAME, null);
    }

    /**
     * Set the currently configured panic trigger app using the {@link Activity}
     * that received a {@link Panic#ACTION_CONNECT} {@link Intent}. If that
     * {@code Intent} was not set with either
     * {@link Activity#startActivityForResult(Intent, int)} or
     * {@link Intent#setPackage(String)}, then this will result in no panic
     * trigger app being active.
     * <p>
     * When the user changes the panic app config, then the current app needs to
     * send {@link Intent}s to the previous app, and the currently configured
     * app to let them know about the changes. This is done by sending an
     * {@code ACTION_DISCONNECT Intent} to the previous app, and an
     * {@code ACTION_CONNECT Intent} to the newly configured app.
     * <p>
     * When this is called with an {@code Activity} in the same responder app
     * that called the {@link Activity#startActivityForResult(Intent, int)},
     * then it will <b>not</b> change the existing trigger app setting! For
     * example, if the responder app launches its Panic Config {@code Activity}.
     *
     * @param activity the {@link Activity} that received an
     *                 {@link Panic#ACTION_CONNECT} {@link Intent}
     */
    public static void setTriggerPackageName(Activity activity) {
        String intentPackageName = activity.getIntent().getPackage();
        String callingPackageName = PanicUtils.getCallingPackageName(activity);
        if (intentPackageName == null && callingPackageName == null) {
            // ignored
{'fraction_non_alphanumeric': 0.08576694074284094, 'fraction_numerical': 0.0, 'mean_word_length': 3.191919191919192, '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': '7243512', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1799, 'n_words': 658}