1 /*
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 
17 package com.android.recovery_l10n;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.AssetManager;
23 import android.content.res.Configuration;
24 import android.content.res.Resources;
25 import android.graphics.Bitmap;
26 import android.os.Bundle;
27 import android.os.RemoteException;
28 import android.util.DisplayMetrics;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.Button;
32 import android.widget.TextView;
33 import android.widget.Spinner;
34 import android.widget.ArrayAdapter;
35 import android.widget.AdapterView;
36 
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import 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 
74 public 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 
TextCapture(Locale thisLocale, Locale nextLocale, Runnable next)86         TextCapture(Locale thisLocale, Locale nextLocale, Runnable next) {
87             this.nextLocale = nextLocale;
88             this.thisLocale = thisLocale;
89             this.next = next;
90         }
91 
run()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 
switchTo(Locale locale)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
onCreate(Bundle savedInstance)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 localeName : localeNames) {
153             Log.i(TAG, "locale = " + localeName);
154             locales.add(Locale.forLanguageTag(localeName));
155         }
156 
157         final Runnable seq = buildSequence(locales.toArray(new Locale[0]));
158 
159         Button b = (Button) findViewById(R.id.go);
160         b.setOnClickListener(new View.OnClickListener() {
161             @Override
162             public void onClick(View ignore) {
163                 mText.post(seq);
164             }
165             });
166     }
167 
buildSequence(final Locale[] locales)168     private Runnable buildSequence(final Locale[] locales) {
169         Runnable head = new Runnable() { public void run() { mergeBitmaps(locales); } };
170         Locale prev = null;
171         for (Locale loc : locales) {
172             head = new TextCapture(loc, prev, head);
173             prev = loc;
174         }
175         final Runnable fhead = head;
176         final Locale floc = prev;
177         return new Runnable() { public void run() { startSequence(fhead, floc); } };
178     }
179 
180     private void startSequence(Runnable firstRun, Locale firstLocale) {
181         savedBitmaps.clear();
182         switchTo(firstLocale);
183         mText.postDelayed(firstRun, 200);
184     }
185 
186     private void saveBitmap(Bitmap b, String filename) {
187         try {
188             FileOutputStream fos = openFileOutput(filename, 0);
189             b.compress(Bitmap.CompressFormat.PNG, 100, fos);
190             fos.close();
191         } catch (IOException e) {
192             Log.i(TAG, "failed to write PNG", e);
193         }
194     }
195 
196     private int colorFor(byte b) {
197         return 0xff000000 | (b<<16) | (b<<8) | b;
198     }
199 
200     private int colorFor(int b) {
201         return 0xff000000 | (b<<16) | (b<<8) | b;
202     }
203 
204     private void mergeBitmaps(final Locale[] locales) {
205         HashMap<String, Integer> countByLanguage = new HashMap<String, Integer>();
206 
207         int height = 2;
208         int width = 10;
209         int maxHeight = 0;
210         for (Locale loc : locales) {
211             Bitmap b = savedBitmaps.get(loc);
212             int h = b.getHeight();
213             int w = b.getWidth();
214             height += h+1;
215             if (h > maxHeight) maxHeight = h;
216             if (w > width) width = w;
217 
218             String lang = loc.getLanguage();
219             if (countByLanguage.containsKey(lang)) {
220                 countByLanguage.put(lang, countByLanguage.get(lang)+1);
221             } else {
222                 countByLanguage.put(lang, 1);
223             }
224         }
225 
226         Log.i(TAG, "output bitmap is " + width + " x " + height);
227         Bitmap out = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
228         out.eraseColor(0xff000000);
229         int[] pixels = new int[maxHeight * width];
230 
231         int p = 0;
232         for (Locale loc : locales) {
233             Bitmap bm = savedBitmaps.get(loc);
234             int h = bm.getHeight();
235             int w = bm.getWidth();
236 
237             bm.getPixels(pixels, 0, w, 0, 0, w, h);
238 
239             // Find the rightmost and leftmost columns with any
240             // nonblack pixels; we'll copy just that region to the
241             // output image.
242 
243             int right = w;
244             while (right > 1) {
245                 boolean all_black = true;
246                 for (int j = 0; j < h; ++j) {
247                     if (pixels[j*w+right-1] != 0xff000000) {
248                         all_black = false;
249                         break;
250                     }
251                 }
252                 if (all_black) {
253                     --right;
254                 } else {
255                     break;
256                 }
257             }
258 
259             int left = 0;
260             while (left < right-1) {
261                 boolean all_black = true;
262                 for (int j = 0; j < h; ++j) {
263                     if (pixels[j*w+left] != 0xff000000) {
264                         all_black = false;
265                         break;
266                     }
267                 }
268                 if (all_black) {
269                     ++left;
270                 } else {
271                     break;
272                 }
273             }
274 
275             // Make the last country variant for a given language be
276             // the catch-all for that language (because recovery will
277             // take the first one that matches).
278             String lang = loc.getLanguage();
279             if (countByLanguage.get(lang) > 1) {
280                 countByLanguage.put(lang, countByLanguage.get(lang)-1);
281                 lang = loc.toString();
282             }
283             int tw = right - left;
284             Log.i(TAG, "encoding \"" + loc + "\" as \"" + lang + "\": " + tw + " x " + h);
285             byte[] langBytes = lang.getBytes();
286             out.setPixel(0, p, colorFor(tw & 0xff));
287             out.setPixel(1, p, colorFor(tw >>> 8));
288             out.setPixel(2, p, colorFor(h & 0xff));
289             out.setPixel(3, p, colorFor(h >>> 8));
290             out.setPixel(4, p, colorFor(langBytes.length));
291             int x = 5;
292             for (byte b : langBytes) {
293                 out.setPixel(x, p, colorFor(b));
294                 x++;
295             }
296             out.setPixel(x, p, colorFor(0));
297 
298             p++;
299 
300             out.setPixels(pixels, left, w, 0, p, tw, h);
301             p += h;
302         }
303 
304         // if no languages match, suppress text display by using a
305         // single black pixel as the image.
306         out.setPixel(0, p, colorFor(1));
307         out.setPixel(1, p, colorFor(0));
308         out.setPixel(2, p, colorFor(1));
309         out.setPixel(3, p, colorFor(0));
310         out.setPixel(4, p, colorFor(0));
311         p++;
312 
313         saveBitmap(out, "text-out.png");
314         Log.i(TAG, "wrote text-out.png");
315     }
316 }
317