<commit_before>package com.rho;

import java.util.Hashtable;

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.CodeModuleGroup;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.io.http.HttpHeaders;
import com.rho.rubyext.Alert;
import rhomobile.NativeBar;
import com.rho.rubyext.RhoPhonebook;
import com.rho.rubyext.RhoCalendar;
import rhomobile.RhodesApplication;
import rhomobile.RingtoneManager;
import com.rho.rubyext.WebView;

import rhomobile.bluetooth.BluetoothManager;
import rhomobile.camera.Camera;
import rhomobile.datetime.DateTimePicker;
import rhomobile.mapview.MapView;
import com.rho.rubyext.GeoLocation;

import com.rho.db.HsqlDBStorage;
import com.rho.db.IDBStorage;
import com.rho.file.*;
import com.rho.net.SSLSocket;
import com.rho.net.TCPSocket;
import com.xruby.runtime.builtin.RubyArray;
import com.xruby.runtime.lang.RubyProgram;
import com.xruby.runtime.lang.RubyRuntime;
import com.rho.net.NetResponse;

public class RhoRubyHelper implements IRhoRubyHelper {

	// WARNING!!! Be very careful when modify these lines! There was a case when
	// entire application has verification error in case if this line is not at start
	// of class. It is impossible to explain why it happened but need to be remembered
	public static final String USE_PERSISTENT = "use_persistent_storage";
	
	public static final int COVERAGE_BIS_B = 4;
	  
	public void initRubyExtensions(){
        RhoPhonebook.initMethods(RubyRuntime.PhonebookClass);
        RhoCalendar.initConstants(RubyRuntime.EventModule);
        RhoCalendar.initMethods(RubyRuntime.CalendarClass);
        Camera.initMethods(RubyRuntime.CameraClass);
        BluetoothManager.initMethods(RubyRuntime.RhoBluetoothClass);
        WebView.initMethods(RubyRuntime.WebViewClass);
        RhoConf.initMethods(RubyRuntime.RhoConfClass);
        Alert.initMethods(RubyRuntime.AlertClass);        
        DateTimePicker.initMethods(RubyRuntime.DateTimePickerClass);
        RingtoneManager.initMethods(RubyRuntime.RingtoneManagerClass);
        NativeBar.initMethods(RubyRuntime.NativeBarClass);
        TCPSocket.initMethods(RubyRuntime.TCPSocketClass);
        SSLSocket.initMethods(RubyRuntime.SSLSocketClass);
        MapView.initMethods(RubyRuntime.MapViewClass);
        GeoLocation.initMethods(RubyRuntime.GeoLocationClass);
        com.rho.rubyext.System.initMethods(RubyRuntime.SystemClass);
        com.rho.rubyext.XMLParser.initMethods(RubyRuntime.XMLParserClass);

	}
	
	public RubyProgram createMainObject() throws Exception
	{
    	/*RhoRubyHelper helper = new RhoRubyHelper();
    	String appName = RhoSupport.getAppName();
		
		String strName = appName + ".ServeME.main";//com.xruby.runtime.lang.RhoSupport.createMainClassName("");
		
        Class c = Class.forName(strName);
        Object o = c.newInstance();
        RubyProgram p = (RubyProgram) o;
		
		return p;*/
		return new xruby.rhoframework.main();
	}

	public String getPlatform() {
		return "Blackberry";
	}

	public void loadBackTrace(RubyArray backtrace) {
		//TODO:
	}

	public String getDeviceId(){
		return new Integer( DeviceInfo.getDeviceId() ).toString();
    }

	public boolean isSimulator(){
		return DeviceInfo.isSimulator();
		//return false;
    }

	public String getModuleName()
	{
		return ApplicationDescriptor.currentApplicationDescriptor().getModuleName();
	}
	
	public void showLog()
	{
		synchronized ( RhodesApplication.getEventLock() ) {		
			RhodesApplication.getInstance().showLogScreen();
		}
	}
	
	public NetResponse postUrl(String url, String body)
	{
		RhodesApplication.NetCallback netCallback = new RhodesApplication.NetCallback();
		
		HttpHeaders headers = new HttpHeaders();
		headers.addProperty("Content-Type", "application/x-www-form-urlencoded");
		
		RhodesApplication.getInstance().postUrlWithCallback(url, body, headers, netCallback);
		netCallback.waitForResponse();
		
		return netCallback.m_response;
	}
	
	public void navigateUrl(String url)
	{
		WebView.navigate(url);
	}
	
	public void navigateBack()
	{
		RhodesApplication.getInstance().navigateBack();
	}
	
	public void app_exit()
	{
		RhodesApplication.getInstance().close();
	}
	
	static Hashtable m_appProperties = new Hashtable(); 
	public String getAppProperty(String name)
	{
		String strRes = null;
		synchronized (m_appProperties)
		{
			if ( m_appProperties.containsKey(name) )
				strRes = (String)m_appProperties.get(name);
			else	
			{
				CodeModuleGroup[] codeModule = CodeModuleGroup.loadAll();

				if ( codeModule != null )
				{
					String moduleName = ApplicationDescriptor
					   .currentApplicationDescriptor().getModuleName();
					
					for(int i = 0; i < codeModule.length; i++) 
					{
						String module = codeModule[i].getName();
						if( module.indexOf( moduleName ) != -1)
						{
							CodeModuleGroup group = codeModule[i];
							if ( group != null )
								strRes = group.getProperty(name);
							break;
						}
					}
				}
/*				
				CodeModuleGroup[] allGroups = CodeModuleGroupManager.loadAll();
				if ( allGroups != null )
				{<commit_msg>fix slow BB start<commit_after>package com.rho;

import java.util.Hashtable;

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.CodeModuleGroup;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.io.http.HttpHeaders;
import com.rho.rubyext.Alert;
import rhomobile.NativeBar;
import com.rho.rubyext.RhoPhonebook;
import com.rho.rubyext.RhoCalendar;
import rhomobile.RhodesApplication;
import rhomobile.RingtoneManager;
import com.rho.rubyext.WebView;

import rhomobile.bluetooth.BluetoothManager;
import rhomobile.camera.Camera;
import rhomobile.datetime.DateTimePicker;
import rhomobile.mapview.MapView;
import com.rho.rubyext.GeoLocation;

import com.rho.db.HsqlDBStorage;
import com.rho.db.IDBStorage;
import com.rho.file.*;
import com.rho.net.SSLSocket;
import com.rho.net.TCPSocket;
import com.xruby.runtime.builtin.RubyArray;
import com.xruby.runtime.lang.RubyProgram;
import com.xruby.runtime.lang.RubyRuntime;
import com.rho.net.NetResponse;

public class RhoRubyHelper implements IRhoRubyHelper {

	// WARNING!!! Be very careful when modify these lines! There was a case when
	// entire application has verification error in case if this line is not at start
	// of class. It is impossible to explain why it happened but need to be remembered
	public static final String USE_PERSISTENT = "use_persistent_storage";
	
	public static final int COVERAGE_BIS_B = 4;
	  
	public void initRubyExtensions(){
        RhoPhonebook.initMethods(RubyRuntime.PhonebookClass);
        RhoCalendar.initConstants(RubyRuntime.EventModule);
        RhoCalendar.initMethods(RubyRuntime.CalendarClass);
        Camera.initMethods(RubyRuntime.CameraClass);
        BluetoothManager.initMethods(RubyRuntime.RhoBluetoothClass);
        WebView.initMethods(RubyRuntime.WebViewClass);
        RhoConf.initMethods(RubyRuntime.RhoConfClass);
        Alert.initMethods(RubyRuntime.AlertClass);        
        DateTimePicker.initMethods(RubyRuntime.DateTimePickerClass);
        RingtoneManager.initMethods(RubyRuntime.RingtoneManagerClass);
        NativeBar.initMethods(RubyRuntime.NativeBarClass);
        TCPSocket.initMethods(RubyRuntime.TCPSocketClass);
        SSLSocket.initMethods(RubyRuntime.SSLSocketClass);
        MapView.initMethods(RubyRuntime.MapViewClass);
        GeoLocation.initMethods(RubyRuntime.GeoLocationClass);
        com.rho.rubyext.System.initMethods(RubyRuntime.SystemClass);
        com.rho.rubyext.XMLParser.initMethods(RubyRuntime.XMLParserClass);

	}
	
	public RubyProgram createMainObject() throws Exception
	{
    	/*RhoRubyHelper helper = new RhoRubyHelper();
    	String appName = RhoSupport.getAppName();
		
		String strName = appName + ".ServeME.main";//com.xruby.runtime.lang.RhoSupport.createMainClassName("");
		
        Class c = Class.forName(strName);
        Object o = c.newInstance();
        RubyProgram p = (RubyProgram) o;
		
		return p;*/
		return new xruby.rhoframework.main();
	}

	public String getPlatform() {
		return "Blackberry";
	}

	public void loadBackTrace(RubyArray backtrace) {
		//TODO:
	}

	public String getDeviceId(){
		return new Integer( DeviceInfo.getDeviceId() ).toString();
    }

	public boolean isSimulator(){
		return DeviceInfo.isSimulator();
		//return false;
    }

	public String getModuleName()
	{
		return ApplicationDescriptor.currentApplicationDescriptor().getModuleName();
	}
	
	public void showLog()
	{
		synchronized ( RhodesApplication.getEventLock() ) {		
			RhodesApplication.getInstance().showLogScreen();
		}
	}
	
	public NetResponse postUrl(String url, String body)
	{
		RhodesApplication.NetCallback netCallback = new RhodesApplication.NetCallback();
		
		HttpHeaders headers = new HttpHeaders();
		headers.addProperty("Content-Type", "application/x-www-form-urlencoded");
		
		RhodesApplication.getInstance().postUrlWithCallback(url, body, headers, netCallback);
		netCallback.waitForResponse();
		
		return netCallback.m_response;
	}
	
	public void navigateUrl(String url)
	{
		WebView.navigate(url);
	}
	
	public void navigateBack()
	{
		RhodesApplication.getInstance().navigateBack();
	}
	
	public void app_exit()
	{
		RhodesApplication.getInstance().close();
	}
	
	static Hashtable m_appProperties = new Hashtable();
	static CodeModuleGroup m_groupRhodes = null;
	static boolean m_bGroupsInited = false;
	public String getAppProperty(String name)
	{
		String strRes = null;
		synchronized (m_appProperties)
		{
			if ( m_appProperties.containsKey(name) )
				strRes = (String)m_appProperties.get(name);
			else	
			{
				if (!m_bGroupsInited)
				{
					m_bGroupsInited = true;
					CodeModuleGroup[] codeModule = CodeModuleGroup.loadAll();
	
					if ( codeModule != null )
					{
						String moduleName = ApplicationDescriptor
						   .currentApplicationDescriptor().getModuleName();
						
						for(int i = 0; i < codeModule.length; i++) 
						{
							String module = codeModule[i].getName();
							if( module.indexOf( moduleName ) != -1)
							{
								m_groupRhodes = codeModule[i];
								break;
							}
						}
					}
				}
				
				if (m_groupRhodes != null)				
					strRes = m_groupRhodes.getProperty(name);
				
/*				
				CodeModuleGroup[] allGroups = CodeModuleGroupManager.loadAll();
				if ( allGroups != null )
				{
{'fraction_non_alphanumeric': 0.10210181257231006, 'fraction_numerical': 0.0005784805244890089, 'mean_word_length': 3.9798367738838216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3101055', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3059, 'n_words': 600}