package fi_81.cwp_morse_mangle;

import java.util.Arrays;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class MainSettingsActivity extends PreferenceActivity {
	private static final String TAG = "MainSettingsActivity";
	
	/* default setup values */
	public static final String HOSTNAME_DEFAULT = "cwp.opimobi.com";
	public static final String HOSTPORT_DEFAULT = "20000";
	public static final String MORSE_SPEED_DEFAULT = "med";

	/* cached references to preference objects */
	private EditTextPreference hostAddr;
	private EditTextPreference hostPort;
	private ListPreference morseSpeed;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate()");
		
		super.onCreate(savedInstanceState);

		/* load settings.xml for preferences layout */
		addPreferencesFromResource(R.xml.settings);

		hostAddr = (EditTextPreference) findPreference("hostname");
		hostPort = (EditTextPreference) findPreference("hostport");
		morseSpeed = (ListPreference) findPreference("morse_speed");

		/* load saved settings for display */
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

		hostAddr.setSummary(settings.getString("hostname", HOSTNAME_DEFAULT));
		hostPort.setSummary(settings.getString("hostport", HOSTPORT_DEFAULT));
		
		/* convert speed value to speed text */
		String speedValue = settings.getString("morse_speed", MORSE_SPEED_DEFAULT);
		String[] values = getResources().getStringArray(R.array.pref_morse_speed_entryvalues);
		String[] entries = getResources().getStringArray(R.array.pref_morse_speed_entries);
		
		int idx = Arrays.binarySearch(values, speedValue);
		if (idx < 0) {
			/*
			 * Invalid setting value, maybe from old version of Morse Mangle.
			 * Reset to "med".
			 */
			speedValue = "med";
			idx = Arrays.binarySearch(values, speedValue);
			
			morseSpeed.setValueIndex(idx);
		}
		
		morseSpeed.setSummary(entries[idx]);
	}
	
	@Override
	public void onRestart() {
		Log.d(TAG, "onRestart()");

		super.onRestart();
	}

	@Override
	public void onStart() {
		Log.d(TAG, "onStart()");

		super.onStart();
	}
	
	@Override
	public void onResume() {
		Log.d(TAG, "onResume()");

		super.onResume();
	}
	
	@Override
	public void onPause() {
		Log.d(TAG, "onPause()");
		
		super.onPause();
	}
	
	@Override
	public void onStop() {
		Log.d(TAG, "onStop()");
		
		super.onStop();
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy()");
		
		super.onDestroy();
	}
}

@@ -447,18 +447,17 @@
 ivity%22;%0A
-%09
 %0A
+
 %09/* defa
@@ -902,27 +902,25 @@
 Create()%22);%0A
-%09%09
 %0A
+
 %09%09super.onCr
@@ -1318,16 +1318,21 @@
 eManager
+%0A%09%09%09%09
 .getDefa
@@ -1506,19 +1506,17 @@
 AULT));%0A
-%09%09
 %0A
+
 %09%09/* con
@@ -1604,17 +1604,21 @@
 _speed%22,
- 
+%0A%09%09%09%09
 MORSE_SP
@@ -1673,32 +1673,37 @@
 .getStringArray(
+%0A%09%09%09%09
 R.array.pref_mor
@@ -1776,16 +1776,21 @@
 ngArray(
+%0A%09%09%09%09
 R.array.
@@ -1816,18 +1816,16 @@
 tries);%0A
-%09%09
 %0A%09%09int i
@@ -2068,12 +2068,9 @@
 e);%0A
-%09%09%09
 %0A
+
 %09%09%09m
@@ -2103,19 +2103,17 @@
 x);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09morseS
@@ -2142,26 +2142,25 @@
 s%5Bidx%5D);%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -2322,26 +2322,25 @@
 Start();%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -2408,34 +2408,33 @@
 .onResume();%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09publ
@@ -2476,27 +2476,25 @@
 nPause()%22);%0A
-%09%09
 %0A
+
 %09%09super.onPa
@@ -2499,26 +2499,25 @@
 Pause();%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -2561,27 +2561,25 @@
 onStop()%22);%0A
-%09%09
 %0A
+
 %09%09super.onSt
@@ -2587,18 +2587,17 @@
 p();%0A%09%7D%0A
-%09
 %0A
+
 %09@Overri
@@ -2655,18 +2655,16 @@
 oy()%22);%0A
-%09%09
 %0A%09%09super

Fix formating