<commit_before>#######################################################
#
# ruboto.rb
#
# - Wrapper for using RubotoActivity, RubotoService, and
#     RubotoBroadcastReceiver. 
# - Provides interface for generating UI elements. 
# - Imports and configures callback classes.
#
#######################################################

$RUBOTO_VERSION = 8

def confirm_ruboto_version(required_version, exact=true)
  raise "requires $RUBOTO_VERSION=#{required_version} or greater, current version #{$RUBOTO_VERSION}" if $RUBOTO_VERSION < required_version and not exact
  raise "requires $RUBOTO_VERSION=#{required_version}, current version #{$RUBOTO_VERSION}" if $RUBOTO_VERSION != required_version and exact
end

require 'java'

$package_name = ($activity || $service || $broadcast_receiver).package_name
$package      = eval("Java::#{$package_name}")

# Create convenience method for top-level android package so we do not need to prefix with 'Java::'.
module Kernel
  def android
    JavaUtilities.get_package_module_dot_format('android')
  end
end

java_import "android.R"

module Ruboto
  java_import "#{$package_name}.R"
  begin
    Id = JavaUtilities.get_proxy_class("#{$package_name}.R$id")
  rescue NameError
    Java::android.util.Log.d "RUBOTO", "no R$id"
  end
end
AndroidIds = JavaUtilities.get_proxy_class("android.R$id")

class Object
  def with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    opts = {:name => 'Block with large stack'}.update(opts)
    exception = nil
    result = nil
    t = Thread.with_large_stack(opts, &proc{result = block.call rescue exception = $!})
    t.join
    raise exception if exception
    result
  end
end

class Thread
  def self.with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    stack_size_kb = opts.delete(:size) || 64
    name = opts.delete(:name) || "Thread with large stack"
    raise "Unknown option(s): #{opts.inspect}" unless opts.empty?
    t = java.lang.Thread.new(nil, block, name, stack_size_kb * 1024)
    t.start
    t
  end
end

#############################################################################
#
# Activity
#

def setup_activity
  java_import "android.app.Activity"

  Activity.class_eval do
    def start_ruboto_dialog(remote_variable, theme=R.style::Theme_Dialog, &block)
      start_ruboto_activity(remote_variable, RubotoDialog, theme, &block)
    end

    def start_ruboto_activity(remote_variable, klass=RubotoActivity, theme=nil, &block)
      $activity_init_block = block

      if @initialized or self == $activity<commit_msg>Issue #88: Additional check on start_ruboto_activity for non-RubotoActivity activities<commit_after>#######################################################
#
# ruboto.rb
#
# - Wrapper for using RubotoActivity, RubotoService, and
#     RubotoBroadcastReceiver. 
# - Provides interface for generating UI elements. 
# - Imports and configures callback classes.
#
#######################################################

$RUBOTO_VERSION = 8

def confirm_ruboto_version(required_version, exact=true)
  raise "requires $RUBOTO_VERSION=#{required_version} or greater, current version #{$RUBOTO_VERSION}" if $RUBOTO_VERSION < required_version and not exact
  raise "requires $RUBOTO_VERSION=#{required_version}, current version #{$RUBOTO_VERSION}" if $RUBOTO_VERSION != required_version and exact
end

require 'java'

$package_name = ($activity || $service || $broadcast_receiver).package_name
$package      = eval("Java::#{$package_name}")

# Create convenience method for top-level android package so we do not need to prefix with 'Java::'.
module Kernel
  def android
    JavaUtilities.get_package_module_dot_format('android')
  end
end

java_import "android.R"

module Ruboto
  java_import "#{$package_name}.R"
  begin
    Id = JavaUtilities.get_proxy_class("#{$package_name}.R$id")
  rescue NameError
    Java::android.util.Log.d "RUBOTO", "no R$id"
  end
end
AndroidIds = JavaUtilities.get_proxy_class("android.R$id")

class Object
  def with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    opts = {:name => 'Block with large stack'}.update(opts)
    exception = nil
    result = nil
    t = Thread.with_large_stack(opts, &proc{result = block.call rescue exception = $!})
    t.join
    raise exception if exception
    result
  end
end

class Thread
  def self.with_large_stack(opts = {}, &block)
    opts = {:size => opts} if opts.is_a? Integer
    stack_size_kb = opts.delete(:size) || 64
    name = opts.delete(:name) || "Thread with large stack"
    raise "Unknown option(s): #{opts.inspect}" unless opts.empty?
    t = java.lang.Thread.new(nil, block, name, stack_size_kb * 1024)
    t.start
    t
  end
end

#############################################################################
#
# Activity
#

def setup_activity
  java_import "android.app.Activity"

  Activity.class_eval do
    def start_ruboto_dialog(remote_variable, theme=R.style::Theme_Dialog, &block)
      start_ruboto_activity(remote_variable, RubotoDialog, theme, &block)
    end

    def start_ruboto_activity(remote_variable, klass=RubotoActivity, theme=nil, &block)
      $activity_init_block = block

      if @initialized or (self == $activity && !$activity.kind_of?(RubotoActivity))
{'fraction_non_alphanumeric': 0.1744318181818182, 'fraction_numerical': 0.0030303030303030303, 'mean_word_length': 4.874304783092325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1416330', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1592, 'n_words': 408}