<commit_before>package de.j4velin.pedometer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;

import de.j4velin.pedometer.background.SensorListener;
import de.j4velin.pedometer.util.Logger;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SettingsFragment extends PreferenceFragment implements OnPreferenceClickListener {

	final static int DEFAULT_GOAL = 10000;
	final static float DEFAULT_STEP_SIZE = Locale.getDefault() == Locale.US ? 2.5f : 75f;
	final static String DEFAULT_STEP_UNIT = Locale.getDefault() == Locale.US ? "ft" : "cm";

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.settings);
		findPreference("about").setOnPreferenceClickListener(this);
		findPreference("import").setOnPreferenceClickListener(this);
		findPreference("export").setOnPreferenceClickListener(this);

		findPreference("notification").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				getActivity().getSharedPreferences("pedometer", Context.MODE_MULTI_PROCESS).edit()
						.putBoolean("notification", (Boolean) newValue).commit();

				getActivity().startService(
						new Intent(getActivity(), SensorListener.class).putExtra("updateNotificationState", true));
				return true;
			}
		});

		Preference account = findPreference("account");
		account.setOnPreferenceClickListener(this);
		// If created for the first time, the GameClient should be setup and be
		// connected, but when recreating the fragment (due to orientation
		// change for example), then the fragment's onCreate is called before
		// the new GamesClient is setup. In this case, just use the player name
		// saved in the savedInstanceState bundle
		if ((savedInstanceState == null && ((MainActivity) getActivity()).getGC().isConnected())
				|| (savedInstanceState != null && savedInstanceState.containsKey("player"))) {
			account.setSummary(getString(R.string.signed_in, savedInstanceState == null ? ((MainActivity) getActivity()).getGC()
					.getCurrentPlayer().getDisplayName() : savedInstanceState.getString("player")));
		}

		final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();

		Preference goal = findPreference("goal");
		goal.setOnPreferenceClickListener(this);
		goal.setSummary(getString(R.string.goal_summary, prefs.getInt("goal", DEFAULT_GOAL)));

		Preference stepsize = findPreference("stepsize");
		stepsize.setOnPreferenceClickListener(this);
		stepsize.setSummary(getString(R.string.step_size_summary, prefs.getFloat("stepsize_value", DEFAULT_STEP_SIZE),
				prefs.getString("stepsize_unit", DEFAULT_STEP_UNIT)));

		setHasOptionsMenu(true);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		try {
			if (((MainActivity) getActivity()).getGC().isConnected())
				outState.putString("player", ((MainActivity) getActivity()).getGC().getCurrentPlayer().getDisplayName());
			else
				outState.remove("player");
		} catch (Exception e) {
			if (Logger.LOG)
				Logger.log(e);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main, menu);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		menu.findItem(R.id.action_settings).setVisible(false);
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		return ((MainActivity) getActivity()).optionsItemSelected(item);
	}

	@Override
	public boolean onPreferenceClick(final Preference preference) {
		AlertDialog.Builder builder;
		View v;
		final SharedPreferences prefs;
		switch (preference.getTitleRes()) {
		case R.string.goal:
			builder = new AlertDialog.Builder(getActivity());
			final NumberPicker np = new NumberPicker(getActivity());
			prefs = getPreferenceManager().getSharedPreferences();
			np.setMinValue(1);
			np.setMaxValue(100000);
			np.setValue(prefs.getInt("goal", 10000));
			builder.setView(np);
			builder.setTitle(R.string.set_goal);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					np.clearFocus();
					prefs.edit().putInt("goal", np.getValue()).apply();
					preference.setSummary(getString(R.string.goal_summary, np.getValue()));
					dialog.dismiss();
					getActivity().startService(
							new Intent(getActivity(), SensorListener.class).putExtra("updateNotificationState", true));
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			Dialog dialog = builder.create();
			dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
			dialog.show();
			break;
		case R.string.step_size:
			builder = new AlertDialog.Builder(getActivity());
			prefs = getPreferenceManager().getSharedPreferences();
			v = getActivity().getLayoutInflater().inflate(R.layout.stepsize, null);
			final RadioGroup unit = (RadioGroup) v.findViewById(R.id.unit);
			final EditText value = (EditText) v.findViewById(R.id.value);
			unit.check(prefs.getString("stepsize_unit", DEFAULT_STEP_UNIT).equals("cm") ? R.id.cm : R.id.ft);
			value.setText(String.valueOf(prefs.getFloat("stepsize_value", DEFAULT_STEP_SIZE)));
			builder.setView(v);
			builder.setTitle(R.string.set_step_size);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					prefs.edit().putFloat("stepsize_value", Float.valueOf(value.getText().toString()))
							.putString("stepsize_unit", unit.getCheckedRadioButtonId() == R.id.cm ? "cm" : "ft").apply();
					preference.setSummary(getString(R.string.step_size_summary, Float.valueOf(value.getText().toString()),
							unit.getCheckedRadioButtonId() == R.id.cm ? "cm" : "ft"));
					dialog.dismiss();
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();<commit_msg>fixed NumberFormatException when entering empty string<commit_after>package de.j4velin.pedometer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;

import de.j4velin.pedometer.background.SensorListener;
import de.j4velin.pedometer.util.Logger;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SettingsFragment extends PreferenceFragment implements OnPreferenceClickListener {

	final static int DEFAULT_GOAL = 10000;
	final static float DEFAULT_STEP_SIZE = Locale.getDefault() == Locale.US ? 2.5f : 75f;
	final static String DEFAULT_STEP_UNIT = Locale.getDefault() == Locale.US ? "ft" : "cm";

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.settings);
		findPreference("about").setOnPreferenceClickListener(this);
		findPreference("import").setOnPreferenceClickListener(this);
		findPreference("export").setOnPreferenceClickListener(this);

		findPreference("notification").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				getActivity().getSharedPreferences("pedometer", Context.MODE_MULTI_PROCESS).edit()
						.putBoolean("notification", (Boolean) newValue).commit();

				getActivity().startService(
						new Intent(getActivity(), SensorListener.class).putExtra("updateNotificationState", true));
				return true;
			}
		});

		Preference account = findPreference("account");
		account.setOnPreferenceClickListener(this);
		// If created for the first time, the GameClient should be setup and be
		// connected, but when recreating the fragment (due to orientation
		// change for example), then the fragment's onCreate is called before
		// the new GamesClient is setup. In this case, just use the player name
		// saved in the savedInstanceState bundle
		if ((savedInstanceState == null && ((MainActivity) getActivity()).getGC().isConnected())
				|| (savedInstanceState != null && savedInstanceState.containsKey("player"))) {
			account.setSummary(getString(R.string.signed_in, savedInstanceState == null ? ((MainActivity) getActivity()).getGC()
					.getCurrentPlayer().getDisplayName() : savedInstanceState.getString("player")));
		}

		final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();

		Preference goal = findPreference("goal");
		goal.setOnPreferenceClickListener(this);
		goal.setSummary(getString(R.string.goal_summary, prefs.getInt("goal", DEFAULT_GOAL)));

		Preference stepsize = findPreference("stepsize");
		stepsize.setOnPreferenceClickListener(this);
		stepsize.setSummary(getString(R.string.step_size_summary, prefs.getFloat("stepsize_value", DEFAULT_STEP_SIZE),
				prefs.getString("stepsize_unit", DEFAULT_STEP_UNIT)));

		setHasOptionsMenu(true);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		try {
			if (((MainActivity) getActivity()).getGC().isConnected())
				outState.putString("player", ((MainActivity) getActivity()).getGC().getCurrentPlayer().getDisplayName());
			else
				outState.remove("player");
		} catch (Exception e) {
			if (Logger.LOG)
				Logger.log(e);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main, menu);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		menu.findItem(R.id.action_settings).setVisible(false);
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		return ((MainActivity) getActivity()).optionsItemSelected(item);
	}

	@Override
	public boolean onPreferenceClick(final Preference preference) {
		AlertDialog.Builder builder;
		View v;
		final SharedPreferences prefs;
		switch (preference.getTitleRes()) {
		case R.string.goal:
			builder = new AlertDialog.Builder(getActivity());
			final NumberPicker np = new NumberPicker(getActivity());
			prefs = getPreferenceManager().getSharedPreferences();
			np.setMinValue(1);
			np.setMaxValue(100000);
			np.setValue(prefs.getInt("goal", 10000));
			builder.setView(np);
			builder.setTitle(R.string.set_goal);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					np.clearFocus();
					prefs.edit().putInt("goal", np.getValue()).apply();
					preference.setSummary(getString(R.string.goal_summary, np.getValue()));
					dialog.dismiss();
					getActivity().startService(
							new Intent(getActivity(), SensorListener.class).putExtra("updateNotificationState", true));
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			Dialog dialog = builder.create();
			dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
			dialog.show();
			break;
		case R.string.step_size:
			builder = new AlertDialog.Builder(getActivity());
			prefs = getPreferenceManager().getSharedPreferences();
			v = getActivity().getLayoutInflater().inflate(R.layout.stepsize, null);
			final RadioGroup unit = (RadioGroup) v.findViewById(R.id.unit);
			final EditText value = (EditText) v.findViewById(R.id.value);
			unit.check(prefs.getString("stepsize_unit", DEFAULT_STEP_UNIT).equals("cm") ? R.id.cm : R.id.ft);
			value.setText(String.valueOf(prefs.getFloat("stepsize_value", DEFAULT_STEP_SIZE)));
			builder.setView(v);
			builder.setTitle(R.string.set_step_size);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						prefs.edit().putFloat("stepsize_value", Float.valueOf(value.getText().toString()))
								.putString("stepsize_unit", unit.getCheckedRadioButtonId() == R.id.cm ? "cm" : "ft").apply();
						preference.setSummary(getString(R.string.step_size_summary, Float.valueOf(value.getText().toString()),
								unit.getCheckedRadioButtonId() == R.id.cm ? "cm" : "ft"));
					} catch (NumberFormatException nfe) {
						nfe.printStackTrace();
					}
					dialog.dismiss();
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
{'fraction_non_alphanumeric': 0.12354853692522062, 'fraction_numerical': 0.0031849246898016056, 'mean_word_length': 5.477009024495058, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5003859', 'n_tokens_mistral': 4943, 'n_tokens_neox': 4285, 'n_words': 815}