blob: 3f2bebe60488d9e048458c26e991398e65541079 [file] [log] [blame]
Elliott Hughescc794902016-03-22 21:18:58 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.recovery_l10n;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.AssetManager;
23import android.content.res.Configuration;
24import android.content.res.Resources;
25import android.graphics.Bitmap;
26import android.os.Bundle;
27import android.os.RemoteException;
28import android.util.DisplayMetrics;
29import android.util.Log;
30import android.view.View;
31import android.widget.Button;
32import android.widget.TextView;
33import android.widget.Spinner;
34import android.widget.ArrayAdapter;
35import android.widget.AdapterView;
36
37import java.io.FileOutputStream;
38import java.io.IOException;
39import java.util.ArrayList;
40import java.util.Arrays;
41import java.util.HashMap;
42import java.util.Locale;
43
44/**
45 * This activity assists in generating the specially-formatted bitmaps
46 * of text needed for recovery's localized text display. Each image
47 * contains all the translations of a single string; above each
48 * translation is a "header row" that encodes that subimage's width,
49 * height, and locale using pixel values.
50 *
51 * To use this app to generate new translations:
52 *
53 * - Update the string resources in res/values-*
54 *
55 * - Build and run the app. Select the string you want to
56 * translate, and press the "Go" button.
57 *
58 * - Wait for it to finish cycling through all the strings, then
59 * pull /data/data/com.android.recovery_l10n/files/text-out.png
60 * from the device.
61 *
62 * - "pngcrush -c 0 text-out.png output.png"
63 *
64 * - Put output.png in bootable/recovery/res/images/ (renamed
65 * appropriately).
66 *
67 * Recovery expects 8-bit 1-channel images (white text on black
68 * background). pngcrush -c 0 will convert the output of this program
69 * to such an image. If you use any other image handling tools,
70 * remember that they must be lossless to preserve the exact values of
71 * pixels in the header rows; don't convert them to jpeg or anything.
72 */
73
74public class Main extends Activity {
75 private static final String TAG = "RecoveryL10N";
76
77 HashMap<Locale, Bitmap> savedBitmaps;
78 TextView mText;
79 int mStringId = R.string.recovery_installing;
80
81 public class TextCapture implements Runnable {
82 private Locale nextLocale;
83 private Locale thisLocale;
84 private Runnable next;
85
86 TextCapture(Locale thisLocale, Locale nextLocale, Runnable next) {
87 this.nextLocale = nextLocale;
88 this.thisLocale = thisLocale;
89 this.next = next;
90 }
91
92 public void run() {
93 Bitmap b = mText.getDrawingCache();
94 savedBitmaps.put(thisLocale, b.copy(Bitmap.Config.ARGB_8888, false));
95
96 if (nextLocale != null) {
97 switchTo(nextLocale);
98 }
99
100 if (next != null) {
101 mText.postDelayed(next, 200);
102 }
103 }
104 }
105
106 private void switchTo(Locale locale) {
107 Resources standardResources = getResources();
108 AssetManager assets = standardResources.getAssets();
109 DisplayMetrics metrics = standardResources.getDisplayMetrics();
110 Configuration config = new Configuration(standardResources.getConfiguration());
111 config.locale = locale;
112 Resources defaultResources = new Resources(assets, metrics, config);
113
114 mText.setText(mStringId);
115
116 mText.setDrawingCacheEnabled(false);
117 mText.setDrawingCacheEnabled(true);
118 mText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
119 }
120
121 @Override
122 public void onCreate(Bundle savedInstance) {
123 super.onCreate(savedInstance);
124 setContentView(R.layout.main);
125
126 savedBitmaps = new HashMap<Locale, Bitmap>();
127
128 Spinner spinner = (Spinner) findViewById(R.id.which);
129 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
130 this, R.array.string_options, android.R.layout.simple_spinner_item);
131 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
132 spinner.setAdapter(adapter);
133 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
134 @Override
135 public void onItemSelected(AdapterView parent, View view,
136 int pos, long id) {
137 switch (pos) {
138 case 0: mStringId = R.string.recovery_installing; break;
139 case 1: mStringId = R.string.recovery_erasing; break;
140 case 2: mStringId = R.string.recovery_no_command; break;
141 case 3: mStringId = R.string.recovery_error; break;
142 }
143 }
144 @Override public void onNothingSelected(AdapterView parent) { }
145 });
146
147 mText = (TextView) findViewById(R.id.text);
148
149 String[] localeNames = getAssets().getLocales();
150 Arrays.sort(localeNames);
151 ArrayList<Locale> locales = new ArrayList<Locale>();
152 for (String ln : localeNames) {
153 int u = ln.indexOf('_');
154 if (u >= 0) {
155 Log.i(TAG, "locale = " + ln);
156 locales.add(new Locale(ln.substring(0, u), ln.substring(u+1)));
157 }
158 }
159
160 final Runnable seq = buildSequence(locales.toArray(new Locale[0]));
161
162 Button b = (Button) findViewById(R.id.go);
163 b.setOnClickListener(new View.OnClickListener() {
164 @Override
165 public void onClick(View ignore) {
166 mText.post(seq);
167 }
168 });
169 }
170
171 private Runnable buildSequence(final Locale[] locales) {
172 Runnable head = new Runnable() { public void run() { mergeBitmaps(locales); } };
173 Locale prev = null;
174 for (Locale loc : locales) {
175 head = new TextCapture(loc, prev, head);
176 prev = loc;
177 }
178 final Runnable fhead = head;
179 final Locale floc = prev;
180 return new Runnable() { public void run() { startSequence(fhead, floc); } };
181 }
182
183 private void startSequence(Runnable firstRun, Locale firstLocale) {
184 savedBitmaps.clear();
185 switchTo(firstLocale);
186 mText.postDelayed(firstRun, 200);
187 }
188
189 private void saveBitmap(Bitmap b, String filename) {
190 try {
191 FileOutputStream fos = openFileOutput(filename, 0);
192 b.compress(Bitmap.CompressFormat.PNG, 100, fos);
193 fos.close();
194 } catch (IOException e) {
195 Log.i(TAG, "failed to write PNG", e);
196 }
197 }
198
199 private int colorFor(byte b) {
200 return 0xff000000 | (b<<16) | (b<<8) | b;
201 }
202
203 private int colorFor(int b) {
204 return 0xff000000 | (b<<16) | (b<<8) | b;
205 }
206
207 private void mergeBitmaps(final Locale[] locales) {
208 HashMap<String, Integer> countByLanguage = new HashMap<String, Integer>();
209
210 int height = 2;
211 int width = 10;
212 int maxHeight = 0;
213 for (Locale loc : locales) {
214 Bitmap b = savedBitmaps.get(loc);
215 int h = b.getHeight();
216 int w = b.getWidth();
217 height += h+1;
218 if (h > maxHeight) maxHeight = h;
219 if (w > width) width = w;
220
221 String lang = loc.getLanguage();
222 if (countByLanguage.containsKey(lang)) {
223 countByLanguage.put(lang, countByLanguage.get(lang)+1);
224 } else {
225 countByLanguage.put(lang, 1);
226 }
227 }
228
229 Log.i(TAG, "output bitmap is " + width + " x " + height);
230 Bitmap out = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
231 out.eraseColor(0xff000000);
232 int[] pixels = new int[maxHeight * width];
233
234 int p = 0;
235 for (Locale loc : locales) {
236 Bitmap bm = savedBitmaps.get(loc);
237 int h = bm.getHeight();
238 int w = bm.getWidth();
239
240 bm.getPixels(pixels, 0, w, 0, 0, w, h);
241
242 // Find the rightmost and leftmost columns with any
243 // nonblack pixels; we'll copy just that region to the
244 // output image.
245
246 int right = w;
247 while (right > 1) {
248 boolean all_black = true;
249 for (int j = 0; j < h; ++j) {
250 if (pixels[j*w+right-1] != 0xff000000) {
251 all_black = false;
252 break;
253 }
254 }
255 if (all_black) {
256 --right;
257 } else {
258 break;
259 }
260 }
261
262 int left = 0;
263 while (left < right-1) {
264 boolean all_black = true;
265 for (int j = 0; j < h; ++j) {
266 if (pixels[j*w+left] != 0xff000000) {
267 all_black = false;
268 break;
269 }
270 }
271 if (all_black) {
272 ++left;
273 } else {
274 break;
275 }
276 }
277
278 // Make the last country variant for a given language be
279 // the catch-all for that language (because recovery will
280 // take the first one that matches).
281 String lang = loc.getLanguage();
282 if (countByLanguage.get(lang) > 1) {
283 countByLanguage.put(lang, countByLanguage.get(lang)-1);
284 lang = loc.toString();
285 }
286 int tw = right - left;
287 Log.i(TAG, "encoding \"" + loc + "\" as \"" + lang + "\": " + tw + " x " + h);
288 byte[] langBytes = lang.getBytes();
289 out.setPixel(0, p, colorFor(tw & 0xff));
290 out.setPixel(1, p, colorFor(tw >>> 8));
291 out.setPixel(2, p, colorFor(h & 0xff));
292 out.setPixel(3, p, colorFor(h >>> 8));
293 out.setPixel(4, p, colorFor(langBytes.length));
294 int x = 5;
295 for (byte b : langBytes) {
296 out.setPixel(x, p, colorFor(b));
297 x++;
298 }
299 out.setPixel(x, p, colorFor(0));
300
301 p++;
302
303 out.setPixels(pixels, left, w, 0, p, tw, h);
304 p += h;
305 }
306
307 // if no languages match, suppress text display by using a
308 // single black pixel as the image.
309 out.setPixel(0, p, colorFor(1));
310 out.setPixel(1, p, colorFor(0));
311 out.setPixel(2, p, colorFor(1));
312 out.setPixel(3, p, colorFor(0));
313 out.setPixel(4, p, colorFor(0));
314 p++;
315
316 saveBitmap(out, "text-out.png");
317 Log.i(TAG, "wrote text-out.png");
318 }
319}