+
+ DictionaryConfig dictionaryConfig = null;
+
+ public int languageButtonPixels = -1;
+
+ static synchronized void staticInit(final Context context) {
+ if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
+ return;
+ }
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new HashMap<String, DictionaryInfo>();
+ final BufferedReader reader = new BufferedReader(
+ new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
+ try {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ if (line.length() == 0 || line.charAt(0) == '#') {
+ continue;
+ }
+ final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
+ dictionaryInfo.uncompressedFilename, dictionaryInfo);
+ }
+ } catch (IOException e) {
+ Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
+ }
+ try {
+ reader.close();
+ } catch (IOException e) {}
+ }
+
+ private File dictDir;
+
+ public synchronized void init(Context c) {
+ if (appContext != null) {
+ assert c == appContext;
+ return;
+ }
+ appContext = c;
+ Log.d("QuickDic", "Application: onCreate");
+ TransliteratorManager.init(null, threadBackground);
+ staticInit(appContext);
+
+ languageButtonPixels = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_DIP, 60, appContext.getResources().getDisplayMetrics());
+
+ // Load the dictionaries we know about.
+ dictionaryConfig = PersistentObjectCache.init(appContext).read(
+ C.DICTIONARY_CONFIGS, DictionaryConfig.class);
+ if (dictionaryConfig == null) {
+ dictionaryConfig = new DictionaryConfig();
+ }
+ if (!dictionaryConfig.isValid()) {
+ dictionaryConfig = new DictionaryConfig();
+ }
+
+ // Theme stuff.
+ appContext.setTheme(getSelectedTheme().themeId);
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+ prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
+ String key) {
+ Log.d("QuickDic", "prefs changed: " + key);
+ if (key.equals(appContext.getString(R.string.themeKey))) {
+ appContext.setTheme(getSelectedTheme().themeId);
+ }
+ }
+ });
+ }
+
+ public static void onCreateGlobalOptionsMenu(
+ final Context context, final Menu menu) {
+ final Context c = context.getApplicationContext();
+ final MenuItem about = menu.add(c.getString(R.string.about));
+ MenuItemCompat.setShowAsAction(about, MenuItem.SHOW_AS_ACTION_NEVER);
+ about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ final Intent intent = new Intent(c, AboutActivity.class);
+ context.startActivity(intent);
+ return false;
+ }
+ });
+
+ final MenuItem help = menu.add(c.getString(R.string.help));
+ MenuItemCompat.setShowAsAction(help, MenuItem.SHOW_AS_ACTION_NEVER);
+ help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent(c));
+ return false;
+ }
+ });
+
+ final MenuItem preferences = menu.add(c.getString(R.string.settings));
+ MenuItemCompat.setShowAsAction(preferences, MenuItem.SHOW_AS_ACTION_NEVER);
+ preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ PreferenceActivity.prefsMightHaveChanged = true;
+ final Intent intent = new Intent(c, PreferenceActivity.class);
+ context.startActivity(intent);
+ return false;
+ }
+ });
+
+ final MenuItem reportIssue = menu.add(c.getString(R.string.reportIssue));
+ MenuItemCompat.setShowAsAction(reportIssue, MenuItem.SHOW_AS_ACTION_NEVER);
+ reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ final Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setData(Uri
+ .parse("http://github.com/rdoeffinger/Dictionary/issues"));
+ context.startActivity(intent);
+ return false;
+ }
+ });
+ }
+
+ private String selectDefaultDir() {
+ final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
+ String dir = defaultDictDir.getAbsolutePath();
+ File dictDir = new File(dir);
+ String[] fileList = dictDir.isDirectory() ? dictDir.list() : null;
+ if (fileList != null && fileList.length > 0) {
+ return dir;
+ }
+ File efd = null;
+ try {
+ efd = appContext.getExternalFilesDir(null);
+ } catch (Exception e) {
+ }
+ if (efd != null) {
+ efd.mkdirs();
+ if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ appContext.getExternalFilesDirs(null);
+ }
+ if (efd.isDirectory() && efd.canWrite() && checkFileCreate(efd)) {
+ return efd.getAbsolutePath();
+ }
+ }
+ if (!dictDir.isDirectory() && !dictDir.mkdirs()) {
+ return appContext.getFilesDir().getAbsolutePath();
+ }
+ return dir;