<commit_before>package cz.vutbr.fit.iha;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.device.BaseDevice;
import cz.vutbr.fit.iha.adapter.device.Facility;
import cz.vutbr.fit.iha.controller.Controller;
import cz.vutbr.fit.iha.util.Timezone;

public class SensorListAdapter extends BaseAdapter {

	private static final int MARGIN_LEFT_RIGHT = 2;
	private static final int MARGIN_TOP = 10;
	private static final int MARGIN_BOTTOM = 0;
	private static final int MARGIN_TOP_M_L = -2;
	private static final int PADDING_LEFT_RIGHT = 5;
	private static final int PADDING_TOP = 6;
	private static final int PADDING_BOTTOM = 5;

	// Declare Variables
	private Context mContext;
	private LayoutInflater inflater;
	private boolean mShowAdd;
	private OnClickListener mListener;
	private List<BaseDevice> mDevices;

	private final Controller mController;

	public SensorListAdapter(Context context, List<BaseDevice> devices, OnClickListener listener) {
		mContext = context;
		mController = Controller.getInstance(context.getApplicationContext());
		mDevices = devices;
		mShowAdd = !devices.isEmpty();
		mListener = listener;
	}

	@Override
	public int getCount() {
		return mDevices.size() + (mShowAdd ? 1 : 0);
	}

	@Override
	public Object getItem(int position) {
		return mDevices.get(position).getName();
	}

	@Override
	public long getItemId(int position) {
		return position; // TODO: what's this?
	}
	
	public BaseDevice getDevice(int position) {
		return mDevices.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position < mDevices.size()) {
			return addItem(position, convertView, parent);
		}
		return addAddSensor(convertView, parent);
	}

	private View addAddSensor(View convertView, ViewGroup parent) {
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View itemView = inflater.inflate(R.layout.sensor_listview_addsensor, parent, false);
		itemView.setClickable(false);
		itemView.setOnClickListener(null);
		ImageView img = (ImageView) itemView.findViewById(R.id.sensor_listview_addsensor_image);
		img.setOnClickListener(mListener);
		return itemView;
	}

	private View addItem(int position, View convertView, ViewGroup parent) {
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(R.layout.sensor_listview_item, parent, false);

		// Locate the TextViews in drawer_list_item.xml
		TextView txtTitle = (TextView) itemView.findViewById(R.id.titleofsensor);
		TextView txtValue = (TextView) itemView.findViewById(R.id.valueofsensor);
		TextView txtUnit = (TextView) itemView.findViewById(R.id.unitofsensor);
		TextView txtTime = (TextView) itemView.findViewById(R.id.timeofsensor);

		// Locate the ImageView in drawer_list_item.xml
		ImageView imgIcon = (ImageView) itemView.findViewById(R.id.iconofsensor);

		BaseDevice device = mDevices.get(position);
		Facility facility = device.getFacility();
		
		Adapter adapter = mController.getAdapter(facility.getAdapterId());
		Timezone timezone = Timezone.fromPreferences(mController.getUserSettings());
		String lastUpdate = timezone.formatLastUpdate(facility.getLastUpdate(), adapter);
		
		// Set the results into TextViews<commit_msg>Add controll for potencial crash of Bug #158<commit_after>package cz.vutbr.fit.iha;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.device.BaseDevice;
import cz.vutbr.fit.iha.adapter.device.Facility;
import cz.vutbr.fit.iha.controller.Controller;
import cz.vutbr.fit.iha.util.Timezone;

public class SensorListAdapter extends BaseAdapter {

	private static final int MARGIN_LEFT_RIGHT = 2;
	private static final int MARGIN_TOP = 10;
	private static final int MARGIN_BOTTOM = 0;
	private static final int MARGIN_TOP_M_L = -2;
	private static final int PADDING_LEFT_RIGHT = 5;
	private static final int PADDING_TOP = 6;
	private static final int PADDING_BOTTOM = 5;

	// Declare Variables
	private Context mContext;
	private LayoutInflater inflater;
	private boolean mShowAdd;
	private OnClickListener mListener;
	private List<BaseDevice> mDevices;

	private final Controller mController;

	public SensorListAdapter(Context context, List<BaseDevice> devices, OnClickListener listener) {
		mContext = context;
		mController = Controller.getInstance(context.getApplicationContext());
		mDevices = devices;
		mShowAdd = !devices.isEmpty();
		mListener = listener;
	}

	@Override
	public int getCount() {
		return mDevices.size() + (mShowAdd ? 1 : 0);
	}

	@Override
	public Object getItem(int position) {
		return mDevices.get(position).getName();
	}

	@Override
	public long getItemId(int position) {
		return position; // TODO: what's this?
	}
	
	public BaseDevice getDevice(int position) {
		return mDevices.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position < mDevices.size()) {
			return addItem(position, convertView, parent);
		}
		return addAddSensor(convertView, parent);
	}

	private View addAddSensor(View convertView, ViewGroup parent) {
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View itemView = inflater.inflate(R.layout.sensor_listview_addsensor, parent, false);
		itemView.setClickable(false);
		itemView.setOnClickListener(null);
		ImageView img = (ImageView) itemView.findViewById(R.id.sensor_listview_addsensor_image);
		if(img != null)
			img.setOnClickListener(mListener);
		return itemView;
	}

	private View addItem(int position, View convertView, ViewGroup parent) {
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(R.layout.sensor_listview_item, parent, false);

		// Locate the TextViews in drawer_list_item.xml
		TextView txtTitle = (TextView) itemView.findViewById(R.id.titleofsensor);
		TextView txtValue = (TextView) itemView.findViewById(R.id.valueofsensor);
		TextView txtUnit = (TextView) itemView.findViewById(R.id.unitofsensor);
		TextView txtTime = (TextView) itemView.findViewById(R.id.timeofsensor);

		// Locate the ImageView in drawer_list_item.xml
		ImageView imgIcon = (ImageView) itemView.findViewById(R.id.iconofsensor);

		BaseDevice device = mDevices.get(position);
		Facility facility = device.getFacility();
		
		Adapter adapter = mController.getAdapter(facility.getAdapterId());
		Timezone timezone = Timezone.fromPreferences(mController.getUserSettings());
		String lastUpdate = timezone.formatLastUpdate(facility.getLastUpdate(), adapter);
		
		// Set the results into TextViews
{'fraction_non_alphanumeric': 0.09556636928899703, 'fraction_numerical': 0.003108948364422817, 'mean_word_length': 5.571047957371226, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3825247', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2227, 'n_words': 530}