<commit_before>// Copyright © 2016 <NAME> using the MIT License.
package ca.frozen.rpicameraviewer.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ca.frozen.rpicameraviewer.App;
import ca.frozen.rpicameraviewer.classes.Camera;
import ca.frozen.rpicameraviewer.classes.Network;
import ca.frozen.rpicameraviewer.classes.Source;
import ca.frozen.rpicameraviewer.classes.Utils;
import ca.frozen.rpicameraviewer.R;

public class SourceFragment extends Fragment
{
	// public constants
	public final static int MIN_PORT = 1;
	public final static int MAX_PORT = 65535;

	// instance variables
	private Source source;
	private List<Source.ConnectionType> connectionTypes;

	//******************************************************************************
	// onCreateView
	//******************************************************************************
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_source, container, false);
	}

	//******************************************************************************
	// configureForSettings
	//******************************************************************************
	public void configureForSettings(Source source)
	{
		// save the source
		this.source = source;

		final View view = getView();
		setConnectionTypes(false, false);
		EditText edit = (EditText) view.findViewById(R.id.source_port);
		edit.setHint("");
		edit = (EditText) view.findViewById(R.id.source_width);
		edit.setHint(R.string.use_stream_width);
		edit = (EditText) view.findViewById(R.id.source_height);
		edit.setHint(R.string.use_stream_height);
		edit = (EditText) view.findViewById(R.id.source_fps);
		edit.setHint("");
		edit = (EditText) view.findViewById(R.id.source_bps);
		edit.setHint("");
		setViews();
	}

	//******************************************************************************
	// configureForNetwork
	//******************************************************************************
	public void configureForNetwork(Source source)
	{
		// save the source
		this.source = source;

		setConnectionTypes(true, false);
		setViews();
	}

	//******************************************************************************
	// configureForCamera
	//******************************************************************************
	public void configureForCamera(Source source)
	{
		// save the source
		this.source = source;

		setConnectionTypes(true, true);
		setViews();
	}

	//******************************************************************************
	// getSource
	//******************************************************************************
	public Source getSource()
	{
		// get the view and create a new source
		View view = getView();
		Source source = new Source();

		// get the connection type
		source.connectionType = getConnectionType(view);

		// get the address
		EditText edit = (EditText) view.findViewById(R.id.source_address);
		source.address = edit.getText().toString().trim();

		// get the numeric values
		source.port = getNumber(view, R.id.source_port);
		source.width = getNumber(view, R.id.source_width);
		source.height = getNumber(view, R.id.source_height);
		source.fps = getNumber(view, R.id.source_fps);
		source.bps = getNumber(view, R.id.source_bps);

		return source;
	}

	//******************************************************************************
	// setConnectionTypes
	//******************************************************************************
	private void setConnectionTypes(boolean includeDefault, final boolean forCamera)
	{
		final View view = getView();
		Spinner spinner = (Spinner) view.findViewById(R.id.source_connection_type);
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
			{
				configureConnectionType(view, connectionTypes.get(position), forCamera);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parentView)
			{
			}
		});
		List<String> names = new ArrayList<String>();
		connectionTypes = new ArrayList<Source.ConnectionType>();
		if (includeDefault)
		{
			names.add(App.getStr(R.string.default1));
			connectionTypes.add(Source.ConnectionType.Default);
		}
		names.add(App.getStr(R.string.raw_tcp_ip));
		connectionTypes.add(Source.ConnectionType.RawTcpIp);
		names.add(App.getStr(R.string.raw_multicast));
		connectionTypes.add(Source.ConnectionType.RawMulticast);
		names.add(App.getStr(R.string.raw_http));
		connectionTypes.add(Source.ConnectionType.RawHttp);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_spinner_item, names);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
		configureConnectionType(view, getConnectionType(view), false);
	}

	//******************************************************************************
	// configureConnectionType
	//******************************************************************************
	private void configureConnectionType(View view, Source.ConnectionType connectionType, boolean forCamera)
	{
		EditText addressEdit = (EditText) view.findViewById(R.id.source_address);
		if (connectionType == Source.ConnectionType.RawMulticast)
		{
			String address = addressEdit.getText().toString().trim();
			if (address.length() == 0 || checkMulticastAddress(address) < 0)
			{
				addressEdit.setText("192.168.127.12");
			}
			if (source.port == R.integer.default_http_port)
			{
				source.port = R.integer.default_port;
			}
		}
		else if (connectionType == Source.ConnectionType.RawHttp)
		{
			addressEdit.setText(forCamera ? (source.address.isEmpty() ? Utils.getBaseIpAddress() : source.address) : "");
			if (source.port == R.integer.default_port)
			{
				source.port = R.integer.default_http_port;
			}
		}
		else
		{
			addressEdit.setText(forCamera ? (source.address.isEmpty() ? Utils.getBaseIpAddress() : source.address) : "");
			if (source.port == R.integer.default_http_port)
			{
				source.port = R.integer.default_port;
			}
		}
		EditText portEdit = (EditText) view.findViewById(R.id.source_port);
		portEdit.setText(Integer.toString(source.port));

		// enable/disable the address and its prompt
		boolean enableAddress = connectionType == Source.ConnectionType.RawMulticast || forCamera;
		addressEdit.setEnabled(enableAddress);
		TextView prompt = (TextView) view.findViewById(R.id.source_address_prompt);
		prompt.setEnabled(enableAddress);
	}

	//******************************************************************************
	// setViews
	//******************************************************************************
	private void setViews()
	{
		// get the view
		View view = getView();

		// set the connection type
		Spinner spinner = (Spinner) view.findViewById(R.id.source_connection_type);
		for (int i = 0; i < connectionTypes.size(); i++)
		{
			Source.ConnectionType connectionType = connectionTypes.get(i);
			if (connectionType == source.connectionType)
			{<commit_msg>Was using the integer IDs instead of the integers themselves when checking the default ports.<commit_after>// Copyright © 2016 <NAME> using the MIT License.
package ca.frozen.rpicameraviewer.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import ca.frozen.rpicameraviewer.App;
import ca.frozen.rpicameraviewer.classes.Camera;
import ca.frozen.rpicameraviewer.classes.Network;
import ca.frozen.rpicameraviewer.classes.Source;
import ca.frozen.rpicameraviewer.classes.Utils;
import ca.frozen.rpicameraviewer.R;

public class SourceFragment extends Fragment
{
	// public constants
	public final static int MIN_PORT = 1;
	public final static int MAX_PORT = 65535;

	// instance variables
	private Source source;
	private List<Source.ConnectionType> connectionTypes;

	//******************************************************************************
	// onCreateView
	//******************************************************************************
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_source, container, false);
	}

	//******************************************************************************
	// configureForSettings
	//******************************************************************************
	public void configureForSettings(Source source)
	{
		// save the source
		this.source = source;

		final View view = getView();
		setConnectionTypes(false, false);
		EditText edit = (EditText) view.findViewById(R.id.source_port);
		edit.setHint("");
		edit = (EditText) view.findViewById(R.id.source_width);
		edit.setHint(R.string.use_stream_width);
		edit = (EditText) view.findViewById(R.id.source_height);
		edit.setHint(R.string.use_stream_height);
		edit = (EditText) view.findViewById(R.id.source_fps);
		edit.setHint("");
		edit = (EditText) view.findViewById(R.id.source_bps);
		edit.setHint("");
		setViews();
	}

	//******************************************************************************
	// configureForNetwork
	//******************************************************************************
	public void configureForNetwork(Source source)
	{
		// save the source
		this.source = source;

		setConnectionTypes(true, false);
		setViews();
	}

	//******************************************************************************
	// configureForCamera
	//******************************************************************************
	public void configureForCamera(Source source)
	{
		// save the source
		this.source = source;

		setConnectionTypes(true, true);
		setViews();
	}

	//******************************************************************************
	// getSource
	//******************************************************************************
	public Source getSource()
	{
		// get the view and create a new source
		View view = getView();
		Source source = new Source();

		// get the connection type
		source.connectionType = getConnectionType(view);

		// get the address
		EditText edit = (EditText) view.findViewById(R.id.source_address);
		source.address = edit.getText().toString().trim();

		// get the numeric values
		source.port = getNumber(view, R.id.source_port);
		source.width = getNumber(view, R.id.source_width);
		source.height = getNumber(view, R.id.source_height);
		source.fps = getNumber(view, R.id.source_fps);
		source.bps = getNumber(view, R.id.source_bps);

		return source;
	}

	//******************************************************************************
	// setConnectionTypes
	//******************************************************************************
	private void setConnectionTypes(boolean includeDefault, final boolean forCamera)
	{
		final View view = getView();
		Spinner spinner = (Spinner) view.findViewById(R.id.source_connection_type);
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
			{
				configureConnectionType(view, connectionTypes.get(position), forCamera);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parentView)
			{
			}
		});
		List<String> names = new ArrayList<String>();
		connectionTypes = new ArrayList<Source.ConnectionType>();
		if (includeDefault)
		{
			names.add(App.getStr(R.string.default1));
			connectionTypes.add(Source.ConnectionType.Default);
		}
		names.add(App.getStr(R.string.raw_tcp_ip));
		connectionTypes.add(Source.ConnectionType.RawTcpIp);
		names.add(App.getStr(R.string.raw_multicast));
		connectionTypes.add(Source.ConnectionType.RawMulticast);
		names.add(App.getStr(R.string.raw_http));
		connectionTypes.add(Source.ConnectionType.RawHttp);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_spinner_item, names);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
		configureConnectionType(view, getConnectionType(view), false);
	}

	//******************************************************************************
	// configureConnectionType
	//******************************************************************************
	private void configureConnectionType(View view, Source.ConnectionType connectionType, boolean forCamera)
	{
		EditText addressEdit = (EditText) view.findViewById(R.id.source_address);
		String address = addressEdit.getText().toString().trim();
		int port = getNumber(view, R.id.source_port);
		int defaultPort = App.getInt(R.integer.default_port);
		int defaultHttpPort = App.getInt(R.integer.default_http_port);
		if (connectionType == Source.ConnectionType.RawMulticast)
		{
			if (address.length() == 0 || checkMulticastAddress(address) < 0)
			{
				addressEdit.setText("192.168.127.12");
			}
			if (port == defaultHttpPort || port < 0)
			{
				port = defaultPort;
			}
		}
		else if (connectionType == Source.ConnectionType.RawHttp)
		{
			addressEdit.setText(forCamera ? (address.isEmpty() ? Utils.getBaseIpAddress() : address) : "");
			if (port == defaultPort || port < 0)
			{
				port = defaultHttpPort;
			}
		}
		else
		{
			addressEdit.setText(forCamera ? (address.isEmpty() ? Utils.getBaseIpAddress() : address) : "");
			if (port == defaultHttpPort || port < 0)
			{
				port = defaultPort;
			}
		}
		setNumber(view, R.id.source_port, port);

		// enable/disable the address and its prompt
		boolean enableAddress = connectionType == Source.ConnectionType.RawMulticast || forCamera;
		addressEdit.setEnabled(enableAddress);
		TextView prompt = (TextView) view.findViewById(R.id.source_address_prompt);
		prompt.setEnabled(enableAddress);
	}

	//******************************************************************************
	// setViews
	//******************************************************************************
	private void setViews()
	{
		// get the view
		View view = getView();

		// set the connection type
		Spinner spinner = (Spinner) view.findViewById(R.id.source_connection_type);
		for (int i = 0; i < connectionTypes.size(); i++)
		{
			Source.ConnectionType connectionType = connectionTypes.get(i);
			if (connectionType == source.connectionType)
			{
{'fraction_non_alphanumeric': 0.2659804628597653, 'fraction_numerical': 0.0036058480298957582, 'mean_word_length': 5.521590423257803, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5219434', 'n_tokens_mistral': 4605, 'n_tokens_neox': 4065, 'n_words': 792}