package com.openxc.enabler;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;

import android.os.Bundle;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

import android.widget.Toast;

import android.util.Log;

public class SettingsActivity extends PreferenceActivity {
    private static String TAG = "SettingsActivity";

    private PreferenceListener mPreferenceListener;
    private SharedPreferences mPreferences;

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

        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mPreferenceListener = new PreferenceListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPreferences.registerOnSharedPreferenceChangeListener(
                mPreferenceListener);
    }

    @Override
    public void onPause() {
        super.onPause();
        mPreferences.unregisterOnSharedPreferenceChangeListener(
                mPreferenceListener);
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    public static class RecordingPreferences extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.recording_preferences);
        }
    }

    public static class DataSourcePreferences extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.data_source_preferences);
        }
    }

    private class PreferenceListener implements
            SharedPreferences.OnSharedPreferenceChangeListener {
        public void onSharedPreferenceChanged(SharedPreferences preferences,
                String key) {
            if(key.equals(getString(R.string.uploading_path_key))) {
                try {
                    URI uri = new URI(getString(R.string.uploading_path_key));
                    if(!uri.isAbsolute()) {
                        Toast.makeText(getApplicationContext(), "Invalid URL",
                                Toast.LENGTH_SHORT).show();
                        Log.w(TAG, "Invalid target URL set");
                    }
                } catch(java.net.URISyntaxException e) {
                    Log.w(TAG, "Target URL in preferences not valid ", e);
                }
            }
        }
    }
}

@@ -2261,24 +2261,95 @@
 th_key))) %7B%0A
+                String uploadingPath = preferences.getString(key, %22%22);%0A
             
@@ -2396,35 +2396,16 @@
 new URI(
-getString(R.string.
 uploadin
@@ -2405,26 +2405,20 @@
 ploading
-_p
+P
 ath
-_key)
 );%0A     
@@ -2452,24 +2452,226 @@
 solute()) %7B%0A
+                        String errorMessage = %22Invalid target URL %5C%22%22 +%0A                            uploadingPath + %22%5C%22 -- must be an absolute URL %22 +%0A                            %22with http:// prefix%22;%0A
             
@@ -2726,21 +2726,20 @@
 (), 
-%22Invalid URL%22
+errorMessage
 ,%0A  
@@ -2835,32 +2835,20 @@
 AG, 
-%22Invalid target URL set%22
+errorMessage
 );%0A 
@@ -2957,17 +2957,25 @@
 w(TAG, %22
-T
+Invalid t
 arget UR
@@ -2980,35 +2980,63 @@
 URL 
-in preferences not valid %22,
+%5C%22%22 + uploadingPath + %22%5C%22%22,%0A                           
  e);

Verify the path stored in preferences, not the preference key.