<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.bump;

import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.TiDict;
import org.appcelerator.titanium.TiModule;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiActivityResultHandler;
import org.appcelerator.titanium.util.TiActivitySupport;
import org.appcelerator.titanium.util.TiConvert;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;

import com.bumptech.bumpapi.BumpAPI;
import com.bumptech.bumpapi.BumpAPIListener;
import com.bumptech.bumpapi.BumpConnectFailedReason;
import com.bumptech.bumpapi.BumpConnection;
import com.bumptech.bumpapi.BumpDisconnectReason;
import com.bumptech.bumpapi.BumpResources;

public class BumpModule extends TiModule implements TiActivityResultHandler, BumpAPIListener {
	
	private static final String LCAT = "BumpModule";
	private static final boolean DBG = true;
	
	private BumpConnection conn;
	
	private String apiKey = null;
	private String username = null;
	private String bumpMessage = null;

	// This shouldn't be required - but it's belts & braces to try and get around their null ptr exception
	private TiActivityResultHandler handler;
	private final Handler baseHandler = new Handler();

	public BumpModule(TiContext context) {
		super(context);
		// Setup ourselves as the listener for the result of the Activity
		setActivityResultHandler(this);
	}
	
	protected void sendMessage(String message) {
		if (null != this.conn) {
			
			try {
				byte[] chunk = message.getBytes("UTF-8");
				this.conn.send(chunk);
			} catch (Exception e) {
				Log.e(LCAT, "Error Sending data to other party. "+e.getMessage());
			}
		} else {
			TiDict eventArgs = new TiDict();
			eventArgs.put("message", "Not Connected");
			this.fireEvent("error", eventArgs);
			
			Log.i(LCAT, "Not connected");
		}
	}
	
	protected void connectBump() {
		
		Activity activity = getTiContext().getActivity();
		TiActivitySupport activitySupport = (TiActivitySupport) activity;
		final int resultCode = activitySupport.getUniqueResultCode();
		
		try {
			// Work around for the way they implement resource management
			BumpResources bp = new BumpResources(this.getTiContext());
			
			if (DBG) {
				Log.d(LCAT, "Bump Connect Called - setting up Intent");
			}
			
			Intent bump  = new Intent(activity, BumpAPI.class);
			bump.putExtra(BumpAPI.EXTRA_API_KEY, apiKey);
			
			// Set some extra args if they are defined			
			if (null != username) {
				Log.d(LCAT, "Setting Bump Username: "+username);
				bump.putExtra(BumpAPI.EXTRA_USER_NAME, username);
			}
			
			if (null != bumpMessage) {	
				Log.d(LCAT, "Setting Bump message: "+bumpMessage);
				bump.putExtra(BumpAPI.EXTRA_ACTION_MSG, bumpMessage);				
			}
			
			activitySupport.launchActivityForResult(bump, resultCode, handler);	
			
			if (DBG) {
				Log.d(LCAT, "Launched Bump Activity");				
			}
			
			// Bubble up the event
			TiDict eventData = new TiDict();			
			this.fireEvent("ready", eventData);
		
		} catch (Exception e) {
			Log.e(LCAT, "--- Exception: "+e.toString());
		}
	}
	
	public void connect(TiDict props) {

		// Process the args to the method
		if (props.containsKey("apikey")) {
			apiKey = TiConvert.toString(props.getString("apikey"));
		} else {
			Log.e(LCAT, "Invalid argument - apikey is required");
		}
		
		if (props.containsKey("username")) {
			username = TiConvert.toString(props.getString("username"));
		} 
		
		if (props.containsKey("message")) {
			bumpMessage = TiConvert.toString(props.getString("message"));
		}
		
		// A little extra debugging
		if (DBG) {
			Log.d(LCAT, "Bump Connect arguments:");
			Log.d(LCAT, "apikey: "+apiKey);
			
			if (null != username) {
				Log.d(LCAT, "username: "+username);
			} else {
				Log.d(LCAT, "username not passed");
			}
			
			if (null != bumpMessage) {
				Log.d(LCAT, "message: "+bumpMessage);
			} else {
				Log.d(LCAT, "No bump message passed");
			}
			
		}
		
		// Call the master connect
		this.connectBump();
		
	}

	public void setActivityResultHandler(TiActivityResultHandler handler) {
		this.handler = handler;
	}

	@Override
	public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
		
		if (DBG) {
			Log.d(LCAT, "Activity onResult with Result: "+resultCode);
		}

		if (resultCode == Activity.RESULT_OK) {
			// Bump connected successfully, set its listener			<commit_msg>Module was using RootActivity instead of topmost which prevented the result handler from being called correctly.<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.bump;

import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.TiDict;
import org.appcelerator.titanium.TiModule;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiActivityResultHandler;
import org.appcelerator.titanium.util.TiActivitySupport;
import org.appcelerator.titanium.util.TiConvert;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;

import com.bumptech.bumpapi.BumpAPI;
import com.bumptech.bumpapi.BumpAPIListener;
import com.bumptech.bumpapi.BumpConnectFailedReason;
import com.bumptech.bumpapi.BumpConnection;
import com.bumptech.bumpapi.BumpDisconnectReason;
import com.bumptech.bumpapi.BumpResources;

public class BumpModule extends TiModule implements TiActivityResultHandler, BumpAPIListener {
	
	private static final String LCAT = "BumpModule";
	private static final boolean DBG = true;
	
	private BumpConnection conn;
	
	private String apiKey = null;
	private String username = null;
	private String bumpMessage = null;

	private final Handler baseHandler = new Handler();

	public BumpModule(TiContext context) {
		super(context);
		// Setup ourselves as the listener for the result of the Activity
	}
	
	protected void sendMessage(String message) {
		if (null != this.conn) {
			
			try {
				byte[] chunk = message.getBytes("UTF-8");
				this.conn.send(chunk);
			} catch (Exception e) {
				Log.e(LCAT, "Error Sending data to other party. "+e.getMessage());
			}
		} else {
			TiDict eventArgs = new TiDict();
			eventArgs.put("message", "Not Connected");
			this.fireEvent("error", eventArgs);
			
			Log.i(LCAT, "Not connected");
		}
	}
	
	protected void connectBump() {
		
		Activity activity = getTiContext().getTiApp().getCurrentActivity();
		TiActivitySupport activitySupport = (TiActivitySupport) activity;
		final int resultCode = activitySupport.getUniqueResultCode();
		
		try {
			// Work around for the way they implement resource management
			BumpResources bp = new BumpResources(this.getTiContext());
			
			if (DBG) {
				Log.d(LCAT, "Bump Connect Called - setting up Intent");
			}
			
			Intent bump  = new Intent(activity, BumpAPI.class);
			bump.putExtra(BumpAPI.EXTRA_API_KEY, apiKey);
			
			// Set some extra args if they are defined			
			if (null != username) {
				Log.d(LCAT, "Setting Bump Username: "+username);
				bump.putExtra(BumpAPI.EXTRA_USER_NAME, username);
			}
			
			if (null != bumpMessage) {	
				Log.d(LCAT, "Setting Bump message: "+bumpMessage);
				bump.putExtra(BumpAPI.EXTRA_ACTION_MSG, bumpMessage);				
			}
			
			activitySupport.launchActivityForResult(bump, resultCode, this);	
			
			if (DBG) {
				Log.d(LCAT, "Launched Bump Activity");				
			}
			
			// Bubble up the event
			TiDict eventData = new TiDict();			
			this.fireEvent("ready", eventData);
		
		} catch (Exception e) {
			Log.e(LCAT, "--- Exception: "+e.toString());
		}
	}
	
	public void connect(TiDict props) {

		// Process the args to the method
		if (props.containsKey("apikey")) {
			apiKey = TiConvert.toString(props.getString("apikey"));
		} else {
			Log.e(LCAT, "Invalid argument - apikey is required");
		}
		
		if (props.containsKey("username")) {
			username = TiConvert.toString(props.getString("username"));
		} 
		
		if (props.containsKey("message")) {
			bumpMessage = TiConvert.toString(props.getString("message"));
		}
		
		// A little extra debugging
		if (DBG) {
			Log.d(LCAT, "Bump Connect arguments:");
			Log.d(LCAT, "apikey: "+apiKey);
			
			if (null != username) {
				Log.d(LCAT, "username: "+username);
			} else {
				Log.d(LCAT, "username not passed");
			}
			
			if (null != bumpMessage) {
				Log.d(LCAT, "message: "+bumpMessage);
			} else {
				Log.d(LCAT, "No bump message passed");
			}
			
		}
		
		// Call the master connect
		this.connectBump();
		
	}

	@Override
	public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
		
		if (DBG) {
			Log.d(LCAT, "Activity onResult with Result: "+resultCode);
		}

		if (resultCode == Activity.RESULT_OK) {
			// Bump connected successfully, set its listener			
{'fraction_non_alphanumeric': 0.11486855023453693, 'fraction_numerical': 0.0019635649612741355, 'mean_word_length': 3.7601246105919004, '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': '2652893', 'n_tokens_mistral': 3396, 'n_tokens_neox': 2863, 'n_words': 748}