<commit_before>/**
 * openHAB, the open Home Automation Bus.
 * Copyright (C) 2010-2012, openHAB.org <<EMAIL>>
 *
 * See the contributors.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Additional permission under GNU GPL version 3 section 7
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with Eclipse (or a modified version of that library),
 * containing parts covered by the terms of the Eclipse Public License
 * (EPL), the licensors of this Program grant you additional permission
 * to convey the resulting work.
 */

package org.openhab.habdroid.ui;

import java.net.MalformedURLException;
import java.net.URL;

import org.openhab.habdroid.R;

import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

/**
 * This is a class to provide preferences activity for application.
 * 
 * @author <NAME>
 *
 */

public class OpenHABPreferencesActivity extends PreferenceActivity {
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    addPreferencesFromResource(R.xml.preferences);
	    Preference urlPreference = getPreferenceScreen().findPreference("default_openhab_url");
	    Preference altUrlPreference = getPreferenceScreen().findPreference("default_openhab_alturl");
	    urlPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				Log.d("OpenHABPreferencesActivity", "Validating new url = " + (String)newValue);
				String newUrl = (String)newValue;
				if (newUrl.length() == 0)
					return true;
				if (urlIsValid(newUrl)) {
					return true;
				}
				showAlertDialog(getString(R.string.erorr_invalid_url));
				return false;
			}
	    });
	    altUrlPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				String newUrl = (String)newValue;
				if (newUrl.length() == 0)
					return true;
				if (urlIsValid(newUrl)) {
					return true;
				}
				showAlertDialog(getString(R.string.erorr_invalid_url));
				return false;
			}
	    });
	    setResult(RESULT_OK);
	}
	
	private boolean urlIsValid(String url) {
		// As we accept an empty URL, which means it is not configured, length==0 is ok
		if (url.length() == 0)
			return true;
		try {
			new URL(url);
		} catch (MalformedURLException e) {<commit_msg>Current values of preferences are now shown in Settings menu of HABDroid<commit_after>/**
 * openHAB, the open Home Automation Bus.
 * Copyright (C) 2010-2012, openHAB.org <<EMAIL>>
 *
 * See the contributors.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Additional permission under GNU GPL version 3 section 7
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with Eclipse (or a modified version of that library),
 * containing parts covered by the terms of the Eclipse Public License
 * (EPL), the licensors of this Program grant you additional permission
 * to convey the resulting work.
 */

package org.openhab.habdroid.ui;

import java.net.MalformedURLException;
import java.net.URL;

import org.openhab.habdroid.R;

import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

/**
 * This is a class to provide preferences activity for application.
 * 
 * @author <NAME>
 *
 */

public class OpenHABPreferencesActivity extends PreferenceActivity {
	@SuppressWarnings("deprecation")

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    addPreferencesFromResource(R.xml.preferences);
	    Preference urlPreference = getPreferenceScreen().findPreference("default_openhab_url");
	    Preference altUrlPreference = getPreferenceScreen().findPreference("default_openhab_alturl");
	    Preference usernamePreference = getPreferenceScreen().findPreference("default_openhab_username");
	    Preference passwordPreference = getPreferenceScreen().findPreference("default_openhab_password");
	    urlPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				Log.d("OpenHABPreferencesActivity", "Validating new url = " + (String)newValue);
				String newUrl = (String)newValue;
				if (newUrl.length() == 0 || urlIsValid(newUrl)) {
					updateTextPreferenceSummary(preference, (String)newValue);
					return true;
				}
				showAlertDialog(getString(R.string.erorr_invalid_url));
				return false;
			}
	    });
	    updateTextPreferenceSummary(urlPreference, null);
	    altUrlPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				String newUrl = (String)newValue;
				if (newUrl.length() == 0 || urlIsValid(newUrl)) {
					updateTextPreferenceSummary(preference, (String)newValue);
					return true;
				}
				showAlertDialog(getString(R.string.erorr_invalid_url));
				return false;
			}
	    });
	    updateTextPreferenceSummary(altUrlPreference, null);
	    usernamePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				updateTextPreferenceSummary(preference, (String)newValue);
				return true;
			}
	    });
	    updateTextPreferenceSummary(usernamePreference, null);
	    passwordPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				updatePasswordPreferenceSummary(preference, (String)newValue);
				return true;
			}
	    });
	    updatePasswordPreferenceSummary(passwordPreference, null);
	    setResult(RESULT_OK);
	}
	
	private void updateTextPreferenceSummary(Preference textPreference, String newValue) {
		if (newValue == null) {
			if (textPreference.getSharedPreferences().getString(textPreference.getKey(), "").length() > 0)
		    	textPreference.setSummary(textPreference.getSharedPreferences().getString(textPreference.getKey(), ""));
		    else
		    	textPreference.setSummary("Not set");
		} else {
			if (newValue.length() > 0)
				textPreference.setSummary(newValue);
			else
				textPreference.setSummary("Not set");
		}
	}
	
	private void updatePasswordPreferenceSummary(Preference passwordPreference, String newValue) {
		if (newValue == null) {
			if (passwordPreference.getSharedPreferences().getString(passwordPreference.getKey(), "").length() > 0)
				passwordPreference.setSummary("******");
			else
				passwordPreference.setSummary("Not set");
		} else {
			if (newValue.length() > 0)
				passwordPreference.setSummary("******");
			else
				passwordPreference.setSummary("Not set");
		}
	}
	
	private boolean urlIsValid(String url) {
		// As we accept an empty URL, which means it is not configured, length==0 is ok
		if (url.length() == 0)
			return true;
		try {
			new URL(url);
		} catch (MalformedURLException e) {
{'fraction_non_alphanumeric': 0.09056093714800631, 'fraction_numerical': 0.003829691371930615, 'mean_word_length': 4.556320400500626, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '255105', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2401, 'n_words': 739}