<commit_before>package org.mozilla.mozstumbler.service.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import java.lang.ref.WeakReference;


public class BatteryCheckReceiver extends BroadcastReceiver {
    public interface BatteryCheckCallback {
        void batteryCheckCallback(BatteryCheckReceiver receiver);
    }

    private final BatteryInfo mCurrentInfo = new BatteryInfo();
    private final WeakReference<BatteryCheckCallback> mCallback;
    private final Context mContext;

    public static class BatteryInfo {
        public int level;
        public boolean isCharging;
    }

    public BatteryCheckReceiver(Context context, BatteryCheckCallback callback) {
        mCallback = new WeakReference<BatteryCheckCallback>(callback);
        mContext = context;
    }

    public void start() {
        mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    public void stop() {
        mContext.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        mCurrentInfo.isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
        mCurrentInfo.level = Math.round(rawLevel * scale / 100.0f);
        if (mCallback.get() != null) {
            mCallback.get().batteryCheckCallback(this);
        }
    }

    public boolean isBatteryNotChargingAndLessThan(int percent) {
        if (mCurrentInfo.level < 1)
            return false;
        return !mCurrentInfo.isCharging && mCurrentInfo.level < percent;
    }

    public boolean isBatteryNotChargingAndGreaterThan(int percent) {
        if (mCurrentInfo.level < 1)
            return false;
        return !mCurrentInfo.isCharging && mCurrentInfo.level < percent;<commit_msg>Wrap unregisterReceiver in try/catch<commit_after>package org.mozilla.mozstumbler.service.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import java.lang.ref.WeakReference;


public class BatteryCheckReceiver extends BroadcastReceiver {
    public interface BatteryCheckCallback {
        void batteryCheckCallback(BatteryCheckReceiver receiver);
    }

    private final BatteryInfo mCurrentInfo = new BatteryInfo();
    private final WeakReference<BatteryCheckCallback> mCallback;
    private final Context mContext;

    public static class BatteryInfo {
        public int level;
        public boolean isCharging;
    }

    public BatteryCheckReceiver(Context context, BatteryCheckCallback callback) {
        mCallback = new WeakReference<BatteryCheckCallback>(callback);
        mContext = context;
    }

    public void start() {
        mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    public void stop() {
        try {
            mContext.unregisterReceiver(this);
        } catch (Exception ex) {}
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        mCurrentInfo.isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
        mCurrentInfo.level = Math.round(rawLevel * scale / 100.0f);
        if (mCallback.get() != null) {
            mCallback.get().batteryCheckCallback(this);
        }
    }

    public boolean isBatteryNotChargingAndLessThan(int percent) {
        if (mCurrentInfo.level < 1)
            return false;
        return !mCurrentInfo.isCharging && mCurrentInfo.level < percent;
    }

    public boolean isBatteryNotChargingAndGreaterThan(int percent) {
        if (mCurrentInfo.level < 1)
            return false;
        return !mCurrentInfo.isCharging && mCurrentInfo.level < percent;
{'fraction_non_alphanumeric': 0.08214369295576877, 'fraction_numerical': 0.004212497074654809, 'mean_word_length': 3.3790983606557377, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '6724387', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1157, 'n_words': 256}