<commit_before>package de.kisi.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.manavo.rest.RestCallback;

import de.kisi.android.model.Lock;
import de.kisi.android.model.Place;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class PlaceViewFragment extends Fragment {

	private RelativeLayout layout;
	private final long delay = 3000;
	private Location currentLocation;
	private LocationManager locationManager;

	static PlaceViewFragment newInstance(int index) {
		// Fragments must not have a custom constructor
		PlaceViewFragment f = new PlaceViewFragment();

		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}

		int index = getArguments().getInt("index");
		final Place place = ((KisiMain) getActivity()).locations.valueAt(index);

		layout = (RelativeLayout) inflater.inflate(R.layout.place_fragment,
				container, false);

		if (place.getLocks() == null) {
			KisiApi api = new KisiApi(this.getActivity());

			api.setCallback(new RestCallback() {
				public void success(Object obj) {
					JSONArray data = (JSONArray) obj;

					place.setLocks(data);
					setupButtons(place);
				}

			});
			api.setLoadingMessage(null);
			api.get("places/" + String.valueOf(place.getId()) + "/locks");
		} else {
			setupButtons(place);
		}

		return layout;
	}

	public void setupButtons(final Place place) {
		int[] buttons = { R.id.buttonLockOne, R.id.buttonLockTwo,
				R.id.buttonLockThree };

		Typeface font = Typeface.createFromAsset(getActivity()
				.getApplicationContext().getAssets(), "Roboto-Light.ttf");

		int i = 0;
		for (final Lock lock : place.getLocks()) {
			if (i >= buttons.length) {
				Log.d("waring", "more locks then buttons!");
				break;
			}

			final Button button = (Button) layout.findViewById(buttons[i]);
			button.setText(lock.getName());
			button.setTypeface(font);
			button.setVisibility(View.VISIBLE);
			i++;

			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					KisiApi api = new KisiApi(getActivity());

					// Add gps coordinates to access request if user has only
					// guest key
					if (place.getOwnerId() != KisiApi.getUserId()) {
						updateLocation();
						Log.d("location", "current location is "+currentLocation.getLongitude()+" , "+currentLocation.getLatitude());

						try {
							JSONObject location = new JSONObject();
							location.put("latitude", currentLocation.getLatitude());
							location.put("longitude", currentLocation.getLongitude());
							api.addParameter("location", location);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					api.setCallback(new RestCallback() {
						public void success(Object obj) {
							// Toast.makeText(getActivity(),
							// "Lock was opened successfully",
							// Toast.LENGTH_LONG).show();
							// change button design
							buttonToUnlock(button, lock);
						}

					});
					api.setLoadingMessage("Opening lock...");
					api.post("places/" + String.valueOf(lock.getPlaceId())
							+ "/locks/" + String.valueOf(lock.getId())
							+ "/access");
				}

			});
		}
		// set unused buttons to gone, so the automatic layout works
		for (; i < buttons.length; i++) {
			Button button = (Button) layout.findViewById(buttons[i]);

			button.setVisibility(View.GONE);

		}<commit_msg>fixed crash if there is no location<commit_after>package de.kisi.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.manavo.rest.RestCallback;

import de.kisi.android.model.Lock;
import de.kisi.android.model.Place;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class PlaceViewFragment extends Fragment {

	private RelativeLayout layout;
	private final long delay = 3000;
	private Location currentLocation;
	private LocationManager locationManager;

	static PlaceViewFragment newInstance(int index) {
		// Fragments must not have a custom constructor
		PlaceViewFragment f = new PlaceViewFragment();

		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}

		int index = getArguments().getInt("index");
		final Place place = ((KisiMain) getActivity()).locations.valueAt(index);

		layout = (RelativeLayout) inflater.inflate(R.layout.place_fragment,
				container, false);

		if (place.getLocks() == null) {
			KisiApi api = new KisiApi(this.getActivity());

			api.setCallback(new RestCallback() {
				public void success(Object obj) {
					JSONArray data = (JSONArray) obj;

					place.setLocks(data);
					setupButtons(place);
				}

			});
			api.setLoadingMessage(null);
			api.get("places/" + String.valueOf(place.getId()) + "/locks");
		} else {
			setupButtons(place);
		}

		return layout;
	}

	public void setupButtons(final Place place) {
		int[] buttons = { R.id.buttonLockOne, R.id.buttonLockTwo,
				R.id.buttonLockThree };

		Typeface font = Typeface.createFromAsset(getActivity()
				.getApplicationContext().getAssets(), "Roboto-Light.ttf");

		int i = 0;
		for (final Lock lock : place.getLocks()) {
			if (i >= buttons.length) {
				Log.d("waring", "more locks then buttons!");
				break;
			}

			final Button button = (Button) layout.findViewById(buttons[i]);
			button.setText(lock.getName());
			button.setTypeface(font);
			button.setVisibility(View.VISIBLE);
			i++;

			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					KisiApi api = new KisiApi(getActivity());

					// Add gps coordinates to access request if user has only
					// guest key
					if (place.getOwnerId() != KisiApi.getUserId()) {
						updateLocation();

						try {
							if ( currentLocation != null ) {
								JSONObject location = new JSONObject();
								location.put("latitude", currentLocation.getLatitude());
								location.put("longitude", currentLocation.getLongitude());
								api.addParameter("location", location);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					api.setCallback(new RestCallback() {
						public void success(Object obj) {
							// Toast.makeText(getActivity(),
							// "Lock was opened successfully",
							// Toast.LENGTH_LONG).show();
							// change button design
							buttonToUnlock(button, lock);
						}

					});
					api.setLoadingMessage("Opening lock...");
					api.post("places/" + String.valueOf(lock.getPlaceId())
							+ "/locks/" + String.valueOf(lock.getId())
							+ "/access");
				}

			});
		}
		// set unused buttons to gone, so the automatic layout works
		for (; i < buttons.length; i++) {
			Button button = (Button) layout.findViewById(buttons[i]);

			button.setVisibility(View.GONE);

		}
{'fraction_non_alphanumeric': 0.12702937166935185, 'fraction_numerical': 0.0014871731317387532, 'mean_word_length': 3.6674378253325624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2128657', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2328, 'n_words': 551}