package com.namelessdev.mpdroid;

import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.view.Menu;
import android.view.MenuItem;

@SuppressWarnings("deprecation")
public class ConnectionSettings extends PreferenceActivity {
	public static final int MAIN = 0;

	private static final String KEY_CONNECTION_CATEGORY = "connectionCategory";

	private String mSSID;
	private PreferenceCategory mMasterCategory;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.connectionsettings);

		final PreferenceScreen preferenceScreen = getPreferenceScreen();

		mMasterCategory = (PreferenceCategory) preferenceScreen.findPreference(KEY_CONNECTION_CATEGORY);

		if (getIntent().getStringExtra("SSID") != null) {
			// WiFi-Based Settings
			mSSID = getIntent().getStringExtra("SSID");
			createDynamicSettings(mSSID, mMasterCategory);
		} else {
			// Default settings
			createDynamicSettings("", mMasterCategory);

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, MAIN, 0, R.string.mainMenu).setIcon(android.R.drawable.ic_menu_revert);

		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent i = null;

		switch (item.getItemId()) {

		case MAIN:
			i = new Intent(this, MainMenuActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			return true;
		}
		return false;
	}

	private void createDynamicSettings(String keyPrefix, PreferenceCategory toCategory) {

		EditTextPreference prefHost = new EditTextPreference(this);
		prefHost.setDialogTitle(R.string.host);
		prefHost.setTitle(R.string.host);
		prefHost.setSummary(R.string.hostDescription);
		prefHost.setDefaultValue("");
		prefHost.setKey(keyPrefix + "hostname");
		toCategory.addPreference(prefHost);

		EditTextPreference prefPort = new EditTextPreference(this);
		prefPort.setDialogTitle(R.string.port);
		prefPort.setTitle(R.string.port);
		prefPort.setSummary(R.string.portDescription);
		prefPort.setDefaultValue("6600");
		prefPort.setKey(keyPrefix + "port");
		toCategory.addPreference(prefPort);

		EditTextPreference prefPassword = new EditTextPreference(this);
		prefPassword.setDialogTitle(R.string.password);
		prefPassword.setTitle(R.string.password);
		prefPassword.setSummary(R.string.passwordDescription);
		prefPassword.setDefaultValue("");
		prefPassword.setKey(keyPrefix + "password");
		toCategory.addPreference(prefPassword);

		EditTextPreference prefHostStreaming = new EditTextPreference(this);
		prefHostStreaming.setDialogTitle(R.string.hostStreaming);
		prefHostStreaming.setTitle(R.string.hostStreaming);
		prefHostStreaming.setSummary(R.string.hostStreamingDescription);
		prefHostStreaming.setDefaultValue("");
		prefHostStreaming.setKey(keyPrefix + "hostnameStreaming");
		toCategory.addPreference(prefHostStreaming);

		// Meh.
		EditTextPreference prefStreamingPort = new EditTextPreference(this);
		prefStreamingPort.setDialogTitle(R.string.portStreaming);
		prefStreamingPort.setTitle(R.string.portStreaming);
		prefStreamingPort.setSummary(R.string.portStreamingDescription);
		prefStreamingPort.setDefaultValue("8000");
		prefStreamingPort.setKey(keyPrefix + "portStreaming");
		toCategory.addPreference(prefStreamingPort);

		EditTextPreference suffixStreamingPort = new EditTextPreference(this);
		suffixStreamingPort.setDialogTitle(R.string.suffixStreaming);
		suffixStreamingPort.setTitle(R.string.suffixStreaming);
		suffixStreamingPort.setSummary(R.string.suffixStreamingDescription);
		suffixStreamingPort.setDefaultValue("");
		suffixStreamingPort.setKey(keyPrefix + "suffixStreaming");
		toCategory.addPreference(suffixStreamingPort);

		onContentChanged();

	}
}

@@ -266,16 +266,47 @@
 Screen;%0A
+import android.text.InputType;%0A
 import a
@@ -323,16 +323,16 @@
 w.Menu;%0A
-
 import a
@@ -1877,32 +1877,132 @@
 eference(this);%0A
+%09%09prefHost.getEditText().setInputType(InputType.TYPE_CLASS_TEXT%7CInputType.TYPE_TEXT_VARIATION_URI);%0A
 %09%09prefHost.setDi
@@ -2284,24 +2284,92 @@
 ence(this);%0A
+%09%09prefPort.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);%0A
 %09%09prefPort.s
@@ -2659,24 +2659,133 @@
 ence(this);%0A
+%09%09prefPassword.getEditText().setInputType(InputType.TYPE_CLASS_TEXT%7CInputType.TYPE_TEXT_VARIATION_PASSWORD);%0A
 %09%09prefPasswo
@@ -3112,32 +3112,141 @@
 eference(this);%0A
+%09%09prefHostStreaming.getEditText().setInputType(InputType.TYPE_CLASS_TEXT%7CInputType.TYPE_TEXT_VARIATION_URI);%0A
 %09%09prefHostStream
@@ -3637,24 +3637,101 @@
 ence(this);%0A
+%09%09prefStreamingPort.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);%0A
 %09%09prefStream
@@ -4041,32 +4041,32 @@
 treamingPort);%0A%0A
-
 %09%09EditTextPrefer
@@ -4122,16 +4122,133 @@
 (this);%0A
+%09%09suffixStreamingPort.getEditText().setInputType(InputType.TYPE_CLASS_TEXT%7CInputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);%0A
 %09%09suffix

Add a few useful input type hints to the connection settings windows