From 4307bce6cd10fe0e3ebb86118d2077fbd78ff1cb Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 26 Apr 2010 22:28:20 +0200 Subject: Show character details on long click. --- .../android/unicodeMap/UnicodeCharacter.java | 32 +++++++ .../android/unicodeMap/UnicodeMapActivity.java | 98 ++++++++++++++++------ 2 files changed, 103 insertions(+), 27 deletions(-) (limited to 'src/org') 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 positionYStack = new ArrayList(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 list + = new ArrayList(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 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 list - = new ArrayList(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 -- cgit v1.2.3