<commit_before>/*
 * Copyright 2014-2015 Neofect Co., Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.neofect.communicator;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.neofect.communicator.bluetooth.a2dp.BluetoothA2dpConnection;
import com.neofect.communicator.bluetooth.spp.BluetoothSppConnection;
import com.neofect.communicator.message.CommunicationMessage;
import com.neofect.communicator.usb.UsbConnection;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.neofect.communicator.ConnectionType.BLUETOOTH_A2DP;

/**
 * @author <EMAIL>
 * @date 2014. 2. 4.
 */
public class Communicator {

	private static final String LOG_TAG = Communicator.class.getSimpleName();

	private static Communicator instance = new Communicator();

	static Communicator getInstance() {
		return instance;
	}

	/** Aliases for short type names */
	@SuppressWarnings("serial")
	private static class HandlerList extends ArrayList<CommunicationHandler<? extends Device>> {}
	@SuppressWarnings("serial")
	private static class HandlerListMap extends HashMap<Class<? extends Device>, HandlerList> {}

	private List<Connection> connections = new ArrayList<>();
	private List<Device> devices = new ArrayList<>();
	private HandlerListMap handlers = new HandlerListMap();

	public static void connect(Context context, ConnectionType connectionType, String connectIdentifier, CommunicationController<? extends Device> controller) {
		Connection connection;
		switch (connectionType) {
			case BLUETOOTH_SPP:
			case BLUETOOTH_SPP_INSECURE:
			case BLUETOOTH_A2DP: {
				BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
				BluetoothDevice device = bluetoothAdapter.getRemoteDevice(connectIdentifier);
				if (connectionType == BLUETOOTH_A2DP) {
					connection = new BluetoothA2dpConnection(device, controller);
				} else {
					connection = new BluetoothSppConnection(device, controller, connectionType);
				}
				break;
			}
			case USB_SERIAL: {
				UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
				UsbDevice device = usbManager.getDeviceList().get(connectIdentifier);
				connection = new UsbConnection(context, device, controller);
				break;
			}
			default: {
				Log.e(LOG_TAG, "Unsupported connection type! '" + connectionType + "'");
				return;
			}
		}

		try {
			connection.connect();
		} catch(Exception e) {
			String identifier = connectionType + ":" + connectIdentifier;
			instance.notifyFailedToConnect(connection, controller.getDeviceClass(), new Exception("Failed to connect to '" + identifier + "'!", e));
		}
	}

	public static void disconnect(Device device) {<commit_msg>Make `Communicator.connect()` return success or failure<commit_after>/*
 * Copyright 2014-2015 Neofect Co., Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.neofect.communicator;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.neofect.communicator.bluetooth.a2dp.BluetoothA2dpConnection;
import com.neofect.communicator.bluetooth.spp.BluetoothSppConnection;
import com.neofect.communicator.message.CommunicationMessage;
import com.neofect.communicator.usb.UsbConnection;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.neofect.communicator.ConnectionType.BLUETOOTH_A2DP;

/**
 * @author <EMAIL>
 * @date 2014. 2. 4.
 */
public class Communicator {

	private static final String LOG_TAG = Communicator.class.getSimpleName();

	private static Communicator instance = new Communicator();

	static Communicator getInstance() {
		return instance;
	}

	/** Aliases for short type names */
	@SuppressWarnings("serial")
	private static class HandlerList extends ArrayList<CommunicationHandler<? extends Device>> {}
	@SuppressWarnings("serial")
	private static class HandlerListMap extends HashMap<Class<? extends Device>, HandlerList> {}

	private List<Connection> connections = new ArrayList<>();
	private List<Device> devices = new ArrayList<>();
	private HandlerListMap handlers = new HandlerListMap();

	public static boolean connect(Context context, ConnectionType connectionType, String connectIdentifier, CommunicationController<? extends Device> controller) {
		Connection connection;
		switch (connectionType) {
			case BLUETOOTH_SPP:
			case BLUETOOTH_SPP_INSECURE:
			case BLUETOOTH_A2DP: {
				BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
				BluetoothDevice device = bluetoothAdapter.getRemoteDevice(connectIdentifier);
				if (connectionType == BLUETOOTH_A2DP) {
					connection = new BluetoothA2dpConnection(device, controller);
				} else {
					connection = new BluetoothSppConnection(device, controller, connectionType);
				}
				break;
			}
			case USB_SERIAL: {
				UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
				UsbDevice device = usbManager.getDeviceList().get(connectIdentifier);
				if (device == null) {
					Log.e(LOG_TAG, "Not existing USB device! connectIdentifier=" + connectIdentifier);
					return false;
				}
				connection = new UsbConnection(context, device, controller);
				break;
			}
			default: {
				Log.e(LOG_TAG, "Unsupported connection type! '" + connectionType + "'");
				return false;
			}
		}

		try {
			connection.connect();
			return true;
		} catch(Exception e) {
			String identifier = connectionType + ":" + connectIdentifier;
			instance.notifyFailedToConnect(connection, controller.getDeviceClass(), new Exception("Failed to connect to '" + identifier + "'!", e));
			return false;
		}
	}

	public static void disconnect(Device device) {
{'fraction_non_alphanumeric': 0.09533101045296168, 'fraction_numerical': 0.006689895470383275, 'mean_word_length': 4.8964667214461794, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 23, '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': '5940101', 'n_tokens_mistral': 2210, 'n_tokens_neox': 1947, 'n_words': 564}