]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 10 Feb 2009 10:38:51 +0000 (02:38 -0800)
committerthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 10 Feb 2009 10:38:51 +0000 (02:38 -0800)
res/layout/main.xml
src/com/hughes/android/dictionary/Dictionary.java
src/com/hughes/android/dictionary/Entry.java
src/com/hughes/android/dictionary/Index.java
src/com/hughes/android/dictionary/R.java

index 4c08a5546d5da75f0112eebc2a961a42c80b3aee..80dee9162a584917af3d1e94f8ebfeea9227ccd1 100755 (executable)
@@ -1,11 +1,14 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
-    >
+       android:orientation="vertical" android:layout_width="fill_parent"
+       android:layout_height="fill_parent">
 
-<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/SearchText" android:hint="Search Text"></EditText>
+       <EditText android:layout_width="fill_parent"
+               android:layout_height="wrap_content" android:id="@+id/SearchText"
+               android:hint="Search Text"></EditText>
+
+       <ListView android:id="@id/android:list"
+               android:layout_width="fill_parent" android:layout_height="fill_parent"
+               android:choiceMode="singleChoice" android:clickable="true"></ListView>
 
-<ListView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/SearchResults" android:choiceMode="singleChoice"></ListView>
 </LinearLayout>
index 06779fabb6ea1c9bf18cf1b7f00d36c043cd6d79..9cd17ad40db0ad9f4b31e2df1a4ca40e36384970 100755 (executable)
@@ -12,22 +12,32 @@ import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import android.app.Activity;
+import android.app.ListActivity;
 import android.os.Bundle;
 import android.os.Handler;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.Log;
+import android.view.ContextMenu;
+import android.view.KeyEvent;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.Window;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.MenuItem.OnMenuItemClickListener;
+import android.view.View.OnLongClickListener;
+import android.widget.AdapterView;
 import android.widget.BaseAdapter;
 import android.widget.EditText;
 import android.widget.ListView;
 import android.widget.TextView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.AdapterView.OnItemLongClickListener;
 
 import com.hughes.util.FileUtil;
 
-public class Dictionary extends Activity {
+public class Dictionary extends ListActivity {
 
   public static final String INDEX_FORMAT = "%s_index_%d";
   private File dictionaryFile = new File("/sdcard/dict-de-en.txt");
@@ -35,7 +45,7 @@ public class Dictionary extends Activity {
   private RandomAccessFile dictionaryRaf;
   private final Index[] indexes = new Index[2];
   private final byte lang = Entry.LANG1;
-  
+
   final Handler uiHandler = new Handler();
 
   private final Object mutex = new Object();
@@ -43,6 +53,7 @@ public class Dictionary extends Activity {
   private SearchOperation searchOperation = null;
   private List<Entry> entries = Collections.emptyList();
   private DictionaryListAdapter dictionaryListAdapter = new DictionaryListAdapter();
+  private int selectedItem = -1;
 
   /** Called when the activity is first created. */
   @Override
@@ -54,14 +65,47 @@ public class Dictionary extends Activity {
     EditText searchText = (EditText) findViewById(R.id.SearchText);
     searchText.addTextChangedListener(new DictionaryTextWatcher());
 
-    ListView searchResults = (ListView) findViewById(R.id.SearchResults);
-    searchResults.setAdapter(dictionaryListAdapter);
+    setListAdapter(dictionaryListAdapter);
+
     try {
       loadIndex();
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
     onSearchTextChange("");
+
+    registerForContextMenu(getListView());
+    getListView().setOnItemLongClickListener((new OnItemLongClickListener() {
+      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
+          long arg3) {
+        selectedItem = arg2;
+        return false;
+      }
+    }));
+  }
+
+  @Override
+  public void onCreateContextMenu(ContextMenu menu, View v,
+      ContextMenuInfo menuInfo) {
+    if (selectedItem == -1) {
+      return;
+    }
+    final MenuItem addToWordlist = menu.add("Add to wordlist.");
+    addToWordlist.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+      public boolean onMenuItemClick(MenuItem item) {
+        Log
+            .d("THAD", "context menu: "
+                + entries.get(selectedItem).getRawText());
+        return false;
+      }
+    });
+  }
+
+  @Override
+  protected void onListItemClick(ListView l, View v, int position, long id) {
+    Log.d("THAD", "Clicked: " + entries.get(position).getRawText());
+    selectedItem = position;
+    openContextMenu(getListView());
   }
 
   private void loadIndex() throws IOException, ClassNotFoundException {
@@ -87,11 +131,11 @@ public class Dictionary extends Activity {
     final String searchText;
     final int count = 100;
     final AtomicBoolean interrupted = new AtomicBoolean(false);
-    
+
     public SearchOperation(final String searchText) {
-      this.searchText = searchText;
+      this.searchText = searchText.toLowerCase(); // TODO: better
     }
-    
+
     public void run() {
       Log.d("THAD", "SearchOperation: " + searchText);
       final List<Entry> newEntries = new ArrayList<Entry>(count);
@@ -118,13 +162,17 @@ public class Dictionary extends Activity {
         }
         entries = newEntries;
       }
-      
+
       uiHandler.post(new Runnable() {
         public void run() {
           synchronized (mutex) {
             dictionaryListAdapter.notifyDataSetChanged();
+            if (entries.size() > 0) {
+              setSelection(0);
+            }
           }
-        }});
+        }
+      });
     }
   }
 
@@ -147,7 +195,8 @@ public class Dictionary extends Activity {
       return position;
     }
 
-    public View getView(int position, View convertView, ViewGroup parent) {
+    public View getView(final int position, final View convertView,
+        final ViewGroup parent) {
       TextView result = null;
       if (convertView instanceof TextView) {
         result = (TextView) convertView;
index 6187ab8c4c600ffa72e9f5f9d9377e97ec6fbfd8..32739b364b33713423d4e55f16929d2dd4a124c7 100755 (executable)
@@ -60,4 +60,8 @@ public final class Entry {
     return lang == LANG1 ? LANG2 : LANG1;\r
   }\r
 \r
+  public String getRawText() {\r
+    return getAllText(LANG1) + " :: " + getAllText(LANG2);\r
+  }\r
+\r
 }\r
index 5fd2dff084844190e49accd102fd633332afe13a..39ab36ea68fb1a542d283fb9fa47036a09e5a4f5 100755 (executable)
@@ -28,14 +28,14 @@ public final class Index {
     return lookup(text, 0, root);\r
   }\r
   \r
-  private Node lookup(final String text, final int pos, final Node n) throws IOException {\r
+  private Node lookup(final String text, final int pos, final Node node) throws IOException {\r
     if (pos == text.length()) {\r
-      return n;\r
+      return node;\r
     }\r
     \r
     // Check whether any prefix of text is a child.\r
     for (int i = pos + 1; i <= text.length(); ++i) {\r
-      final Integer child = n.children.get(text.substring(pos, i));\r
+      final Integer child = node.children.get(text.substring(pos, i));\r
       if (child != null) {\r
         return lookup(text, i, getNode(text.substring(0, i), child));\r
       }\r
@@ -43,13 +43,13 @@ public final class Index {
     \r
     // Check whether any child starts with what's left of text.\r
     final String remainder = text.substring(pos);\r
-    for (final Map.Entry<String, Integer> childEntry : n.children.entrySet()) {\r
+    for (final Map.Entry<String, Integer> childEntry : node.children.entrySet()) {\r
       if (childEntry.getKey().startsWith(remainder)) {\r
-        return getNode(n.text + childEntry.getKey(), childEntry.getValue());\r
+        return getNode(node.text + childEntry.getKey(), childEntry.getValue());\r
       }\r
     }\r
     \r
-    return n;\r
+    return node;\r
   }\r
   \r
   private Node getNode(final String text, final int indexOffset) throws IOException {\r
index 88153f53c657e653845cf58c4fcda0e0e2c0852f..a74f9490e2332335619467d8dee3e83a0bcfd6e0 100755 (executable)
@@ -14,7 +14,6 @@ public final class R {
         public static final int icon=0x7f020000;\r
     }\r
     public static final class id {\r
-        public static final int SearchResults=0x7f050001;\r
         public static final int SearchText=0x7f050000;\r
     }\r
     public static final class layout {\r