package interdroid.contextdroid.sensors.impl;

import interdroid.contextdroid.sensors.AbstractConfigurationActivity;
import interdroid.contextdroid.sensors.AbstractMemorySensor;
import interdroid.contextdroid.ContextDroidException;
import interdroid.contextdroid.ContextTypedValueListener;
import interdroid.contextdroid.R;
import interdroid.contextdroid.contextexpressions.ContextTypedValue;
import interdroid.contextdroid.contextexpressions.TimestampedValue;

import java.util.List;

import android.os.Bundle;

public class IntentSensor extends AbstractMemorySensor {

	public static final String TAG = "Intent";

	/**
	 * The configuration activity for this sensor.
	 * @author nick &lt;palmer@cs.vu.nl&gt;
	 *
	 */
	public static class ConfigurationActivity
	extends AbstractConfigurationActivity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setResult(
					RESULT_OK,
					getIntent().putExtra("configuration",
							IntentSensor.STARTED_FIELD));
			finish();
		}

		@Override
		public final int getPreferencesXML() {
			return R.xml.intent_preferences;
		}

	}

	public static final String STARTED_FIELD = "started";

	protected static final int HISTORY_SIZE = 10;
	public static final long EXPIRE_TIME = 5 * 60 * 1000;

	private static final String MAGIC_RELAY = "MAGIC_RELAY";

	@Override
	public String[] getValuePaths() {
		return new String[] { STARTED_FIELD };
	}

	@Override
	public void initDefaultConfiguration(Bundle DEFAULT_CONFIGURATION) {
	}

	@Override
	public String getScheme() {
		return "{'type': 'record', 'name': 'train', 'namespace': 'context.sensor',"
				+ " 'fields': ["
				+ "            {'name': '"
				+ STARTED_FIELD
				+ "', 'type': 'string'}"
				+ "           ]"
				+ "}".replace('\'', '"');
	}

	@Override
	public void onConnected() {
	}

	@Override
	public void register(final String id, final String valuePath,
			final Bundle configuration) {
		try {
			contextServiceConnector.registerContextTypedValue(id + "."
					+ MAGIC_RELAY, new ContextTypedValue(
					"logcat/log?logcat_parameters=ActivityManager:I *:S"),
					new ContextTypedValueListener() {

						@Override
						public void onReading(String relayedId,
								TimestampedValue[] newValues) {
							// values is always of length 1
							if (newValues[0].getValue().toString().contains(
									"Starting: Intent {")) {
								if (getValues().size() >= HISTORY_SIZE) {
									getValues().remove(0);
								}
								getValues().get(valuePath)
										.add(new TimestampedValue(
												getIntentFrom(
														newValues[0]
																.getValue()),
												newValues[0].getTimestamp(),
												newValues[0].getExpireTime()));
								notifyDataChangedForId(id);
							}

						}

					});
		} catch (ContextDroidException e) {
			e.printStackTrace();
		}
	}

	private String getIntentFrom(final Object value) {
		String string = value.toString();
		string = string.substring(string.indexOf("cmp=") + 4);
		string = string.substring(0, string.indexOf(" "));
		return string;
	}

	@Override
	public void unregister(final String id) {
		try {
			contextServiceConnector.unregisterContextTypedValue(id + "."
					+ MAGIC_RELAY);
		} catch (ContextDroidException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDestroySensor() {
		// Nothing to do
	}
}

@@ -459,32 +459,8 @@
 e;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -1581,16 +1581,26 @@
 ': '
-train', 
+intent', %22 +%0A%09%09%09%09%22
 'nam
@@ -1623,16 +1623,23 @@
 t.sensor
+.intent
 ',%22%0A%09%09%09%09
@@ -2066,17 +2066,74 @@
 %09%22logcat
-/
+%22 + ContextTypedValue.ENTITY_VALUE_PATH_SEPARATOR%0A%09%09%09%09%09+ %22
 log?logc

Fix obvious copy paste error in IntentSensor.