summaryrefslogtreecommitdiffstats
path: root/src/org/madore/android/unicodeMap/UnicodeMapActivity.java
blob: c352b58db9f67a3d6babb12e83c352f9573b139b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.madore.android.unicodeMap;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
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.KeyEvent;
import android.widget.*;
import android.text.ClipboardManager;
import android.app.ListActivity;
import android.app.ProgressDialog;

public final class UnicodeMapActivity extends ListActivity {

    protected UnicodeDatabase db;

    protected final List<UnicodeDisplayable> rootList
	= new ArrayList<UnicodeDisplayable>(200);

    protected final List<UnicodeArrayAdapter> stack
	= new ArrayList<UnicodeArrayAdapter>(10);
    protected final List<Integer> positionStack
	= new ArrayList<Integer>(10);
    protected final List<Integer> positionYStack
	= new ArrayList<Integer>(10);

    protected void launchPopulation() {
	final ProgressDialog progress = new ProgressDialog(this);
	progress.setOwnerActivity(this);
	progress.setTitle(R.string.loading_database_title);
	progress.setMessage(getResources().getText(R.string.loading_database_msg));
	progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	progress.setCancelable(false);
	progress.show();
	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
		    int done = msg.getData().getInt("done");
		    int total = msg.getData().getInt("total");
		    progress.setMax(total);
		    progress.setProgress(done);
		    if ( done >= total )
			progress.dismiss();
		}
	    };
	final Thread thr = new Thread() {
		public void run() {
		    final UnicodeDatabase writeDb
			= new UnicodeDatabase(UnicodeMapActivity.this);
		    writeDb.populate(handler);
		}
	    };
	thr.start();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	setContentView(R.layout.main_layout);
	db = new UnicodeDatabase(this);
	final boolean needPopulate = db.needPopulate();
	if ( needPopulate )
	    launchPopulation();
	for ( UnicodeCharacter.Range rng : UnicodeCharacter.Range.values() )
	    rootList.add(rng);
	UnicodeArrayAdapter rootAdapter
	    = new UnicodeArrayAdapter(this, rootList);
	stack.add(rootAdapter);
	setListAdapter(rootAdapter);
	final ClipboardManager cmgr
	    = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
	final Button btn = (Button) findViewById(R.id.button);
	final EditText txt = (EditText) findViewById(R.id.edit);
	btn.setOnClickListener(new View.OnClickListener() {
		public void onClick(View view) {
		    cmgr.setText(txt.getText());
		    Toast.makeText(getApplicationContext(),
				   R.string.copied_toast,
				   Toast.LENGTH_SHORT).show();
		}
	    });
	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");
		}
	    });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if ( ( keyCode == KeyEvent.KEYCODE_BACK) && stack.size() > 1 ) {
	    final ListView lv = getListView();
	    stack.remove(stack.size()-1);
	    setListAdapter(stack.get(stack.size()-1));
	    lv.setSelectionFromTop(positionStack.remove(positionStack.size()-1),
				   positionYStack.remove(positionYStack.size()-1));
	    return true;
	}
	return super.onKeyDown(keyCode, event);
    }

}