1 /***
2 * Aedict - an EDICT browser for Android
3 Copyright (C) 2009 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package sk.baka.aedict;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import sk.baka.aedict.KanjiDrawActivity.PainterView;
26 import sk.baka.aedict.util.SodLoader;
27 import sk.baka.autils.AndroidUtils;
28 import android.app.ListActivity;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.graphics.drawable.BitmapDrawable;
32 import android.graphics.drawable.ColorDrawable;
33 import android.os.Bundle;
34 import android.util.DisplayMetrics;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.ArrayAdapter;
38 import android.widget.ImageView;
39 import android.widget.LinearLayout;
40 import android.widget.TextView;
41
42 /***
43 * Shows a stroke order for given kanji list.
44 *
45 * @author Martin Vysny
46 */
47 public class StrokeOrderActivity extends ListActivity {
48 /***
49 * An intent string value: the list of kanji characters.
50 */
51 static final String INTENTKEY_KANJILIST = "kanjiList";
52
53 public static void launch(final Context activity, final String kanjiList) {
54 final Intent intent = new Intent(activity, StrokeOrderActivity.class);
55 intent.putExtra(INTENTKEY_KANJILIST, kanjiList);
56 activity.startActivity(intent);
57 }
58
59 /***
60 * Shows a list of matched entries. May contain an error message if the
61 * search failed.
62 */
63 private List<Character> model = new ArrayList<Character>();
64 /***
65 * Loads SOD images.
66 */
67 private SodLoader sodLoader = null;
68
69 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.sod);
73 if (AedictApp.getDownloader().checkSod(this)) {
74 final String kanjis = getIntent().getStringExtra(INTENTKEY_KANJILIST);
75 for (final char c : kanjis.toCharArray()) {
76 model.add(c);
77 }
78 try {
79 sodLoader = new SodLoader();
80 } catch (IOException e) {
81 throw new RuntimeException(e);
82 }
83 }
84 final DisplayMetrics dm = new DisplayMetrics();
85 getWindowManager().getDefaultDisplay().getMetrics(dm);
86 setListAdapter(new ArrayAdapter<Character>(this, R.layout.soddetail, model) {
87
88 @Override
89 public View getView(int position, View convertView, ViewGroup parent) {
90 View view = convertView;
91 if (view == null) {
92 view = getLayoutInflater().inflate(R.layout.soddetail, getListView(), false);
93 }
94 final TextView kanjiText = (TextView) view.findViewById(R.id.kanjiBig);
95 final Character kanji = model.get(position);
96 kanjiText.setText(kanji.toString());
97 final ImageView image = (ImageView) view.findViewById(R.id.sodImageView);
98 BitmapDrawable bd = null;
99 try {
100 bd = sodLoader.loadBitmap(kanji);
101 } catch (IOException e) {
102 AndroidUtils.handleError(e, StrokeOrderActivity.this, getClass(), null);
103 }
104 if (bd != null) {
105 image.setImageDrawable(bd);
106
107
108 int scaledWidth = (int)(bd.getIntrinsicWidth()*dm.density);
109 if (scaledWidth > dm.widthPixels) {
110 scaledWidth = dm.widthPixels;
111 }
112 image.setLayoutParams(new LinearLayout.LayoutParams(scaledWidth, (int) ((long) bd.getIntrinsicHeight() * scaledWidth / bd.getIntrinsicWidth())));
113 } else {
114 image.setImageDrawable(new ColorDrawable(0));
115 }
116 return view;
117 }
118
119 });
120 findViewById(R.id.kanjidrawRoot).setVisibility(model.size() == 1 ? View.VISIBLE : View.GONE);
121 if (model.size() == 1) {
122 addKanjiPad(model.get(0));
123 }
124 }
125
126 private void addKanjiPad(char kanji) {
127 final PainterView view = new PainterView(this, R.id.textStrokes);
128 ((ViewGroup) findViewById(R.id.kanjidrawRoot)).addView(view);
129 findViewById(R.id.undo).setOnClickListener(AndroidUtils.safe(this, new View.OnClickListener() {
130
131 public void onClick(View v) {
132 view.undoLastStroke();
133 }
134 }));
135 findViewById(R.id.btnKanjiClear).setOnClickListener(AndroidUtils.safe(this, new View.OnClickListener() {
136
137 public void onClick(View v) {
138 view.clear();
139 }
140 }));
141 findViewById(R.id.btnKanjiSearch).setOnClickListener(AndroidUtils.safe(this, new View.OnClickListener() {
142
143 public void onClick(View v) {
144 try {
145 final String kanjis = view.analyzeKanji();
146 KanjiAnalyzeActivity.launch(StrokeOrderActivity.this, kanjis, false);
147 } catch (IOException e) {
148 throw new RuntimeException(e);
149 }
150 }
151 }));
152 }
153
154 public static final int SOD_DPI = 120;
155 }