/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/service/LanguageManager.java
Java | 180 lines | 97 code | 38 blank | 45 comment | 14 complexity | 20630df8bb304ca24e7a8ce5806c7c63 MD5 | raw file
1/* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.ocr.service; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.res.Resources; 22 23import com.googlecode.eyesfree.ocr.R; 24import com.googlecode.eyesfree.ocr.client.Intents; 25import com.googlecode.eyesfree.ocr.client.Language; 26 27import java.io.File; 28import java.io.FilenameFilter; 29import java.lang.ref.WeakReference; 30import java.util.ArrayList; 31import java.util.Collections; 32import java.util.List; 33import java.util.Map; 34import java.util.TreeMap; 35 36/** 37 * Provides a list of installed languages. 38 * 39 * @author alanv@google.com (Alan Viverette) 40 */ 41public class LanguageManager { 42 private static final String TESSDATA = "tessdata"; 43 private static final String EXTENSION = ".traineddata"; 44 private static final String ENGLISH_UNKNOWN = "Unknown (%s)"; 45 private static final String ISO6391_UNKNOWN = "??"; 46 47 private File mDatapath; 48 private File mTessdata; 49 50 private Map<String, Language> mSupported; 51 private List<Language> mAvailable; 52 private WeakReference<Context> mContext; 53 54 public LanguageManager(Context context) { 55 mContext = new WeakReference<Context>(context); 56 mSupported = new TreeMap<String, Language>(); 57 mAvailable = new ArrayList<Language>(); 58 } 59 60 public boolean loadLanguages() { 61 Context context = mContext.get(); 62 63 if (context == null) 64 return false; 65 66 mDatapath = context.getExternalFilesDir(null); 67 68 if (mDatapath == null) { 69 return false; 70 } 71 72 mTessdata = new File(mDatapath, TESSDATA); 73 74 loadSupported(); 75 loadAvailable(); 76 77 // Broadcast intent to let everyone know we've updated the language list 78 Intent intent = new Intent(Intents.Actions.LANGUAGES_UPDATED); 79 context.sendBroadcast(intent); 80 81 return true; 82 } 83 84 public Map<String, Language> getSupported() { 85 return mSupported; 86 } 87 88 /** 89 * Returns the list of available languages. You must call loadLanguages() 90 * prior to calling this method. 91 * 92 * @return the list of available languages 93 */ 94 public List<Language> getAvailable() { 95 return mAvailable; 96 } 97 98 /** 99 * Returns the directory containing tessdata. You must call loadLanguages() 100 * prior to calling this method. 101 * 102 * @return the directory containing tessdata 103 */ 104 public File getDatapath() { 105 return mDatapath; 106 } 107 108 /** 109 * Returns the tessdata directory. You must call loadLanguages() prior to 110 * calling this method. 111 * 112 * @return the tessdata directory 113 */ 114 public File getTessdata() { 115 return mTessdata; 116 } 117 118 // ******************* 119 // * Private methods * 120 // ******************* 121 122 private void loadSupported() { 123 mSupported.clear(); 124 125 Context context = mContext.get(); 126 127 if (context == null) 128 return; 129 130 Resources res = context.getResources(); 131 132 String[] english = res.getStringArray(R.array.english); 133 String[] iso6391 = res.getStringArray(R.array.iso_639_1); 134 String[] iso6392 = res.getStringArray(R.array.iso_639_2); 135 136 for (int i = 0; i < english.length; i++) { 137 Language lang = new Language(english[i], iso6391[i], iso6392[i]); 138 139 mSupported.put(iso6392[i], lang); 140 } 141 } 142 143 private void loadAvailable() { 144 mAvailable.clear(); 145 146 if (mTessdata == null) 147 return; 148 149 String[] files = mTessdata.list(trainedDataFilter); 150 151 if (files == null) 152 return; 153 154 for (String file : files) { 155 int lastIndex = file.lastIndexOf('.'); 156 String iso6392 = file.substring(0, lastIndex); 157 Language lang = mSupported.get(iso6392); 158 159 if (lang == null) { 160 String english = String.format(ENGLISH_UNKNOWN, iso6392); 161 lang = new Language(english, ISO6391_UNKNOWN, iso6392); 162 } 163 164 mAvailable.add(lang); 165 } 166 167 Collections.sort(mAvailable); 168 } 169 170 // ******************* 171 // * Private classes * 172 // ******************* 173 174 private FilenameFilter trainedDataFilter = new FilenameFilter() { 175 @Override 176 public boolean accept(File dir, String filename) { 177 return filename.endsWith(EXTENSION); 178 } 179 }; 180}