<commit_before>package com.afzaln.awakedebug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

import timber.log.Timber;

/**
 * Receives the power connected/disconnected intents from the system
 */
public class PowerConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isAwakeDebugEnabled = PrefUtils.isAwakeDebugEnabled(context.getApplicationContext());
        boolean isAwakeAcEnabled = PrefUtils.isAwakeAcEnabled(context.getApplicationContext());

        if (isAwakeDebugEnabled || isAwakeAcEnabled) {
            toggleStayAwake(context);
        }
    }

    public static void toggleStayAwake(Context context) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.getApplicationContext().registerReceiver(null, filter);

        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB && PrefUtils.isAwakeDebugEnabled(context);
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC && PrefUtils.isAwakeAcEnabled(context);

        int adb;
        if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR1) {
            adb = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0);
        } else {
            adb = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ADB_ENABLED, 0);
        }

        if ((usbCharge && adb == 1) || acCharge) {
            enableStayAwake(context);
        } else {
            disableStayAwake(context);
        }
    }

    public static void disableStayAwake(Context context) {
        int timeout = getScreenOffTimeout(context);
        if (timeout == Integer.MAX_VALUE) {
            int savedTimeout = PrefUtils.getSavedTimeout(context);
            changeScreenOffTimeout(context, savedTimeout);
        }
    }

    public static void enableStayAwake(Context context) {
        int timeout = getScreenOffTimeout(context);
        if (timeout != Integer.MAX_VALUE) {
            changeScreenOffTimeout(context, Integer.MAX_VALUE, timeout);
        }
    }

    private static void changeScreenOffTimeout(Context context, int newTimeout) {
        setScreenOffTimeout(context, newTimeout);
    }

    private static void changeScreenOffTimeout(Context context, int newTimeout, int oldTimeout) {<commit_msg>Make debugging a requirement for AC awake<commit_after>package com.afzaln.awakedebug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

import timber.log.Timber;

/**
 * Receives the power connected/disconnected intents from the system
 */
public class PowerConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isAwakeDebugEnabled = PrefUtils.isAwakeDebugEnabled(context.getApplicationContext());
        boolean isAwakeAcEnabled = PrefUtils.isAwakeAcEnabled(context.getApplicationContext());

        if (isAwakeDebugEnabled || isAwakeAcEnabled) {
            toggleStayAwake(context);
        }
    }

    public static void toggleStayAwake(Context context) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.getApplicationContext().registerReceiver(null, filter);

        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB && PrefUtils.isAwakeDebugEnabled(context);
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC && PrefUtils.isAwakeAcEnabled(context);

        int adb;
        if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR1) {
            adb = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0);
        } else {
            adb = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ADB_ENABLED, 0);
        }

        if ((usbCharge || acCharge) && adb == 1) {
            enableStayAwake(context);
        } else {
            disableStayAwake(context);
        }
    }

    public static void disableStayAwake(Context context) {
        int timeout = getScreenOffTimeout(context);
        if (timeout == Integer.MAX_VALUE) {
            int savedTimeout = PrefUtils.getSavedTimeout(context);
            changeScreenOffTimeout(context, savedTimeout);
        }
    }

    public static void enableStayAwake(Context context) {
        int timeout = getScreenOffTimeout(context);
        if (timeout != Integer.MAX_VALUE) {
            changeScreenOffTimeout(context, Integer.MAX_VALUE, timeout);
        }
    }

    private static void changeScreenOffTimeout(Context context, int newTimeout) {
        setScreenOffTimeout(context, newTimeout);
    }

    private static void changeScreenOffTimeout(Context context, int newTimeout, int oldTimeout) {
{'fraction_non_alphanumeric': 0.08603311735061195, 'fraction_numerical': 0.0017998560115190785, 'mean_word_length': 3.5963606286186933, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4333541', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1508, 'n_words': 304}