summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-26 20:28:20 (GMT)
committerDavid A. Madore <david+git@madore.org>2010-04-27 00:22:30 (GMT)
commit4307bce6cd10fe0e3ebb86118d2077fbd78ff1cb (patch)
tree9e884d1b3eb15c075ed6941d5ce1d0fdfa4ee55e
parenta179d593013b67e5ef9f7b74fe50547ddd7ae512 (diff)
downloadUnicodeMap-4307bce6cd10fe0e3ebb86118d2077fbd78ff1cb.zip
UnicodeMap-4307bce6cd10fe0e3ebb86118d2077fbd78ff1cb.tar.gz
UnicodeMap-4307bce6cd10fe0e3ebb86118d2077fbd78ff1cb.tar.bz2
Show character details on long click.
-rw-r--r--res/layout/char_details.xml18
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeCharacter.java32
-rw-r--r--src/org/madore/android/unicodeMap/UnicodeMapActivity.java98
3 files changed, 121 insertions, 27 deletions
diff --git a/res/layout/char_details.xml b/res/layout/char_details.xml
new file mode 100644
index 0000000..57a6b35
--- /dev/null
+++ b/res/layout/char_details.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:padding="10dp"
+ android:orientation="vertical"
+ android:background="#f0e0e0e0">
+ <TextView android:id="@+id/detailsLabel"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:textSize="20sp"
+ android:textColor="#ff000000" />
+ <TextView android:id="@+id/utf8Label"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:textSize="16sp"
+ android:textColor="#ff404040" />
+</LinearLayout>
diff --git a/src/org/madore/android/unicodeMap/UnicodeCharacter.java b/src/org/madore/android/unicodeMap/UnicodeCharacter.java
index 34bab47..646c696 100644
--- a/src/org/madore/android/unicodeMap/UnicodeCharacter.java
+++ b/src/org/madore/android/unicodeMap/UnicodeCharacter.java
@@ -6,6 +6,10 @@ import java.util.Map;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Formatter;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.IOException;
public class UnicodeCharacter implements UnicodeDisplayable {
@@ -320,4 +324,32 @@ public class UnicodeCharacter implements UnicodeDisplayable {
return this.getLabel();
}
+ static byte[] toUtf8(String s) {
+ try {
+ ByteArrayOutputStream buf = new ByteArrayOutputStream(8);
+ OutputStreamWriter writer = new OutputStreamWriter(buf, "UTF-8");
+ writer.write(s, 0, s.length());
+ writer.close();
+ return buf.toByteArray();
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError("UTF-8 encoding unsupported");
+ } catch (IOException e) {
+ throw new AssertionError("this is impossible");
+ }
+ }
+
+ static byte[] toUtf16(String s) {
+ try {
+ ByteArrayOutputStream buf = new ByteArrayOutputStream(8);
+ OutputStreamWriter writer = new OutputStreamWriter(buf, "UTF-16BE");
+ writer.write(s, 0, s.length());
+ writer.close();
+ return buf.toByteArray();
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError("UTF-16BE encoding unsupported");
+ } catch (IOException e) {
+ throw new AssertionError("this is impossible");
+ }
+ }
+
}
diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
index c352b58..2d8d61b 100644
--- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
+++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
@@ -3,16 +3,19 @@ package org.madore.android.unicodeMap;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
+import java.util.Formatter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
+import android.view.Window;
import android.view.KeyEvent;
import android.widget.*;
import android.text.ClipboardManager;
import android.app.ListActivity;
+import android.app.Dialog;
import android.app.ProgressDialog;
public final class UnicodeMapActivity extends ListActivity {
@@ -29,6 +32,8 @@ public final class UnicodeMapActivity extends ListActivity {
protected final List<Integer> positionYStack
= new ArrayList<Integer>(10);
+ EditText textForm;
+
protected void launchPopulation() {
final ProgressDialog progress = new ProgressDialog(this);
progress.setOwnerActivity(this);
@@ -57,6 +62,67 @@ public final class UnicodeMapActivity extends ListActivity {
thr.start();
}
+ protected class MapItemClickListener
+ implements AdapterView.OnItemClickListener,
+ AdapterView.OnItemLongClickListener {
+ public void onItemClick(AdapterView<?> parent, View view,
+ int position, long id) {
+ UnicodeDisplayable it
+ = (UnicodeDisplayable)parent.getItemAtPosition(position);
+ if ( it instanceof UnicodeCharacter )
+ textForm.append(((UnicodeCharacter)it).getChar());
+ else if ( it instanceof UnicodeCharacter.Range ) {
+ int from = ((UnicodeCharacter.Range)it).getFrom();
+ int to = ((UnicodeCharacter.Range)it).getTo();
+ List<UnicodeDisplayable> list
+ = new ArrayList<UnicodeDisplayable>(db.countRange(from,to));
+ for ( UnicodeCharacter ch : db.getRange(from,to) )
+ list.add(ch);
+ UnicodeArrayAdapter adapter
+ = new UnicodeArrayAdapter(UnicodeMapActivity.this,
+ list);
+ stack.add(adapter);
+ positionStack.add(position);
+ positionYStack.add(view.getTop());
+ setListAdapter(adapter);
+ } else
+ throw new AssertionError("unknown UnicodeDisplayable");
+ }
+ public boolean onItemLongClick(AdapterView<?> parent, View view,
+ int position, long id) {
+ UnicodeDisplayable it
+ = (UnicodeDisplayable)parent.getItemAtPosition(position);
+ if ( it instanceof UnicodeCharacter ) {
+ UnicodeCharacter itch = (UnicodeCharacter)it;
+ Dialog dialog = new Dialog(UnicodeMapActivity.this);
+ dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
+ dialog.setContentView(R.layout.char_details);
+ dialog.setCancelable(true);
+ TextView text;
+ text = (TextView) dialog.findViewById(R.id.detailsLabel);
+ text.setText(itch.getLabel());
+ text = (TextView) dialog.findViewById(R.id.utf8Label);
+ StringBuilder s = new StringBuilder();
+ Formatter fmt = new Formatter(s);
+ fmt.format("UTF-8:");
+ byte[] bytes;
+ bytes = UnicodeCharacter.toUtf8(itch.getChar());
+ for ( int i=0 ; i<bytes.length ; i++ )
+ fmt.format(" 0x%02x", bytes[i]);
+ fmt.format("\nUTF-16BE:");
+ bytes = UnicodeCharacter.toUtf16(itch.getChar());
+ for ( int i=0 ; i<bytes.length ; i++ )
+ fmt.format(" 0x%02x", bytes[i]);
+ text.setText(new String(s));
+ dialog.show();
+ return true;
+ } else if ( it instanceof UnicodeCharacter.Range ) {
+ return false;
+ } else
+ throw new AssertionError("unknown UnicodeDisplayable");
+ }
+ }
+
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -75,10 +141,10 @@ public final class UnicodeMapActivity extends ListActivity {
final ClipboardManager cmgr
= (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
final Button btn = (Button) findViewById(R.id.button);
- final EditText txt = (EditText) findViewById(R.id.edit);
+ textForm = (EditText) findViewById(R.id.edit);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
- cmgr.setText(txt.getText());
+ cmgr.setText(textForm.getText());
Toast.makeText(getApplicationContext(),
R.string.copied_toast,
Toast.LENGTH_SHORT).show();
@@ -86,31 +152,9 @@ public final class UnicodeMapActivity extends ListActivity {
});
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
- lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- UnicodeDisplayable it
- = (UnicodeDisplayable)parent.getItemAtPosition(position);
- if ( it instanceof UnicodeCharacter )
- txt.append(((UnicodeCharacter)it).getChar());
- else if ( it instanceof UnicodeCharacter.Range ) {
- int from = ((UnicodeCharacter.Range)it).getFrom();
- int to = ((UnicodeCharacter.Range)it).getTo();
- List<UnicodeDisplayable> list
- = new ArrayList<UnicodeDisplayable>(db.countRange(from,to));
- for ( UnicodeCharacter ch : db.getRange(from,to) )
- list.add(ch);
- UnicodeArrayAdapter adapter
- = new UnicodeArrayAdapter(UnicodeMapActivity.this,
- list);
- stack.add(adapter);
- positionStack.add(position);
- positionYStack.add(view.getTop());
- setListAdapter(adapter);
- } else
- throw new AssertionError("unknown UnicodeDisplayable");
- }
- });
+ MapItemClickListener listener = new MapItemClickListener();
+ lv.setOnItemClickListener(listener);
+ lv.setOnItemLongClickListener(listener);
}
@Override