Permalink
Browse files

Revert "Some cleanup on EnhancedTextView"

This reverts commit d606a6d.

Conflicts:

	src/net/lutzky/transportdroidil/EnhancedTextView.java
  • Loading branch information...
lutzky committed Oct 7, 2011
1 parent 4b1aa3e commit 0e194de5b8f24f85a3b4931963a06743525dacf8
@@ -1,5 +1,8 @@
package net.lutzky.transportdroidil;
import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
@@ -18,6 +21,7 @@
TAG = "EnhancedTextView",
XMLNS = "http://transportdroidil.lutzky.net/apk/res/custom";
List<String> completionOptions = new LinkedList<String>();
final String preferencesFieldName;
public EnhancedTextView(Context context, AttributeSet attrs) {
@@ -44,23 +48,43 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return connection;
}
/**
* Set the list used to save completion options.
* Notice that the list is not copied but used as reference!
*
* @param completionOptions the list to hold the options.
*/
public void setCompletionOptions(List<String> completionOptions) {
this.completionOptions = completionOptions;
}
public List<String> getCompletionOptions() {
return completionOptions;
}
public void addCurrentValueAsCompletion() {
String s = getText().toString();
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>) getAdapter();
// If the completion was already there, remove and re-add it. This way,
// it gets moved to the front.
int previousLocation = completionOptions.indexOf(s);
if (previousLocation != -1) {
completionOptions.remove(previousLocation);
}
Log.d(TAG, "Adding completion: " + s);
if (s.equals(getHint()))
return;
// Remove the string so there are no duplicates.
arrayAdapter.remove(s);
// Add our completion to the actual active completions database
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>) getAdapter();
arrayAdapter.add(s);
// Insert at beginning of the list - this is a recent entry, we want
// it to show up at the top.
arrayAdapter.insert(s, 0);
// Add our completion to our non-persistent storage
completionOptions.add(0, s);
}
public void clearCompletionOptions(SharedPreferences settings) {
completionOptions.clear();
savePersistentState(settings);
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>)getAdapter();
@@ -71,14 +95,11 @@ public void savePersistentState(SharedPreferences settings) {
if (preferencesFieldName == null)
return;
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>)getAdapter();
SharedPreferences.Editor editor = settings.edit();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < Math.min(arrayAdapter.getCount(), MAX_HISTORY); ++i) {
buffer.append(arrayAdapter.getItem(i));
StringBuilder buffer = new StringBuilder(completionOptions.size());
List<String> toSave = uniqueBoundedList(completionOptions, MAX_HISTORY);
for (String s : toSave) {
buffer.append(s);
buffer.append(SEPARATOR);
}
try {
@@ -89,18 +110,46 @@ public void savePersistentState(SharedPreferences settings) {
editor.commit();
}
static <E> List<E> uniqueBoundedList(List<E> l, int bound) {
List<E> result = new LinkedList<E>();
int count = 0;
for (E item : l) {
if (!result.contains(item)) {
result.add(item);
count += 1;
}
if (count == bound) {
return result;
}
}
return result;
}
public void loadPersistentState(SharedPreferences settings) {
if (preferencesFieldName == null)
return;
String allQueries = settings.getString(preferencesFieldName, "");
for (String query : allQueries.split(SEPARATOR)) {
completionOptions.add(query);
}
@SuppressWarnings("unchecked")
ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>) getAdapter();
arrayAdapter.clear();
// no addAll until API 11
for (String s : completionOptions)
arrayAdapter.add(s);
}
String allQueries = settings.getString(preferencesFieldName, "");
for (String query : allQueries.split(SEPARATOR)) {
// No addAll until API 11
arrayAdapter.add(query);
}
public String getString() {
String text = getText().toString();
if (text.equals(getHint()))
return "";
else
return text;
}
}
@@ -33,6 +33,9 @@ public QueryView(final Context context, AttributeSet attrs) {
getToTextView().setAdapter(placesAdapter);
getTimeTextView().setAdapter(timeAdapter);
// Share the list of completion options.
getToTextView().setCompletionOptions(getFromTextView().getCompletionOptions());
final Button submit_egged = (Button) findViewById(R.id.submit_egged);
final Button submit_busgovil = (Button) findViewById(R.id.submit_busgovil);
@@ -97,9 +100,9 @@ public void clearCompletionOptions(SharedPreferences settings) {
}
public String getQueryString() {
String from = getFromTextView().getText().toString();
String to = getToTextView().getText().toString();
String time = getTimeTextView().getText().toString();
String from = getFromTextView().getString();
String to = getToTextView().getString();
String time = getTimeTextView().getString();
return from + " ל" + to + " " + time;
}

0 comments on commit 0e194de

Please sign in to comment.