<commit_before># -------------------------------------------------------------------
# Broadcast receiver bridge

from jnius import autoclass, PythonJavaClass, java_method

class BroadcastReceiver(object):

    class Callback(PythonJavaClass):
        __javainterfaces__ = ['org/renpy/android/GenericBroadcastReceiverCallback']
        __javacontext__ = 'app'

        def __init__(self, callback, *args, **kwargs):
            self.callback = callback
            PythonJavaClass.__init__(self, *args, **kwargs)

        @java_method('(Landroid/content/Context;Landroid/content/Intent;)V')
        def onReceive(self, context, intent):
            self.callback(context, intent)

    def __init__(self, callback, actions=None, categories=None):
        super(BroadcastReceiver, self).__init__()
        self.callback = callback

        if not actions and not categories:
            raise Exception('You need to define at least actions or categories')

        # resolve actions/categories first
        Intent = autoclass('android.content.Intent')
        resolved_actions = []
        if actions:
            for x in actions:
                if '.' in x:
                    full_name = x
                else:
                    name = 'ACTION_{}'.format(x.upper())
                    if not hasattr(Intent, name):
                        raise Exception('The intent {} doesnt exist'.format(name))
                    full_name = getattr(Intent, name)
                resolved_actions += [full_name]

        resolved_categories = []
        if categories:
            for x in categories:
                if '.' in x:
                    full_name = x
                else:
                    name = 'CATEGORY_{}'.format(x.upper())
                    if not hasattr(Intent, name):
                        raise Exception('The intent {} doesnt exist'.format(name))
                    full_name = getattr(Intent, name)
                resolved_categories += [full_name]

        # resolve android API
        PythonActivity = autoclass('org.renpy.android.PythonActivity')
        GenericBroadcastReceiver = autoclass('org.renpy.android.GenericBroadcastReceiver')
        IntentFilter = autoclass('android.content.IntentFilter')
        HandlerThread = autoclass('android.os.HandlerThread')

        # create a thread for handling events from the receiver
        self.handlerthread = HandlerThread('handlerthread')

        # create a listener
        self.context = PythonActivity.mActivity
        self.listener = BroadcastReceiver.Callback(self.callback)
        self.receiver = GenericBroadcastReceiver(self.listener)<commit_msg>Factored out the Intent name expansion<commit_after># -------------------------------------------------------------------
# Broadcast receiver bridge

from jnius import autoclass, PythonJavaClass, java_method

class BroadcastReceiver(object):

    class Callback(PythonJavaClass):
        __javainterfaces__ = ['org/renpy/android/GenericBroadcastReceiverCallback']
        __javacontext__ = 'app'

        def __init__(self, callback, *args, **kwargs):
            self.callback = callback
            PythonJavaClass.__init__(self, *args, **kwargs)

        @java_method('(Landroid/content/Context;Landroid/content/Intent;)V')
        def onReceive(self, context, intent):
            self.callback(context, intent)

    def __init__(self, callback, actions=None, categories=None):
        super(BroadcastReceiver, self).__init__()
        self.callback = callback

        if not actions and not categories:
            raise Exception('You need to define at least actions or categories')

        def _expand_partial_name(partial_name):
            if '.' in partial_name:
                return partial_name # Its actually a full dotted name
            else:
                name = 'ACTION_{}'.format(partial_name.upper())
                if not hasattr(Intent, name):
                    raise Exception('The intent {} doesnt exist'.format(name))
                return getattr(Intent, name)

        # resolve actions/categories first
        Intent = autoclass('android.content.Intent')
        resolved_actions = []
        if actions:
            for x in actions:
                resolved_actions += [_expand_partial_name(x)]

        resolved_categories = []
        if categories:
            for x in categories:
                resolved_categories += [_expand_partial_name(x)]

        # resolve android API
        PythonActivity = autoclass('org.renpy.android.PythonActivity')
        GenericBroadcastReceiver = autoclass('org.renpy.android.GenericBroadcastReceiver')
        IntentFilter = autoclass('android.content.IntentFilter')
        HandlerThread = autoclass('android.os.HandlerThread')

        # create a thread for handling events from the receiver
        self.handlerthread = HandlerThread('handlerthread')

        # create a listener
        self.context = PythonActivity.mActivity
        self.listener = BroadcastReceiver.Callback(self.callback)
        self.receiver = GenericBroadcastReceiver(self.listener)
{'fraction_non_alphanumeric': 0.11285882815572158, 'fraction_numerical': 0.0, 'mean_word_length': 2.1537507749535028, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7576255', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1188, 'n_words': 300}