+ 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;
+ }
+
+ public synchronized File getDictDir() {
+ // This metaphor doesn't work, because we've already reset
+ // prefsMightHaveChanged.
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+ String dir = prefs.getString(appContext.getString(R.string.quickdicDirectoryKey), "");
+ if (dir.isEmpty()) {
+ dir = selectDefaultDir();
+ }
+ File dictDir = new File(dir);
+ dictDir.mkdirs();
+ if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ appContext.getExternalFilesDirs(null);
+ }
+ return dictDir;
+ }
+
+ static public boolean checkFileCreate(File dir) {
+ boolean res = false;
+ File testfile = new File(dir, "quickdic_writetest");
+ try {
+ testfile.delete();
+ res = testfile.createNewFile() & testfile.delete();
+ } catch (Exception e) {
+ }
+ return res;
+ }
+
+ public File getWordListFile() {
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+ String file = prefs.getString(appContext.getString(R.string.wordListFileKey), "");
+ if (file.isEmpty()) {
+ return new File(getDictDir(), "wordList.txt");
+ }
+ return new File(file);
+ }
+
+ public Theme getSelectedTheme() {
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+ final String theme = prefs.getString(appContext.getString(R.string.themeKey), "themeLight");
+ if (theme.equals("themeLight")) {
+ return Theme.LIGHT;
+ } else {
+ return Theme.DEFAULT;
+ }
+ }
+
+ public File getPath(String uncompressedFilename) {
+ return new File(getDictDir(), uncompressedFilename);
+ }
+
+ String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
+ String defaultLangName = null;
+ final Map<String, String> fileToNameCache = new HashMap<>();
+
+ public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
+ // Hack to put the default locale first in the name.
+ if (indexInfos.size() > 1 &&
+ indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
+ List<IndexInfo> result = new ArrayList<>(indexInfos);
+ ListUtil.swap(result, 0, 1);
+ return result;
+ }
+ return indexInfos;
+ }
+
+ public synchronized String getDictionaryName(final String uncompressedFilename) {
+ final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
+ if (!currentLocale.equals(defaultLangISO2)) {
+ defaultLangISO2 = currentLocale;
+ fileToNameCache.clear();
+ defaultLangName = null;
+ }
+ if (defaultLangName == null) {
+ defaultLangName = IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(appContext, defaultLangISO2);
+ }
+
+ String name = fileToNameCache.get(uncompressedFilename);
+ if (name != null) {
+ return name;
+ }
+
+ final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
+ .get(uncompressedFilename);
+ if (dictionaryInfo != null) {
+ final StringBuilder nameBuilder = new StringBuilder();
+
+ List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
+ for (int i = 0; i < sortedIndexInfos.size(); ++i) {
+ if (i > 0) {
+ nameBuilder.append("-");
+ }
+ nameBuilder
+ .append(IsoUtils.INSTANCE.isoCodeToLocalizedLanguageName(appContext, sortedIndexInfos.get(i).shortName));
+ }
+ name = nameBuilder.toString();
+ } else {
+ name = uncompressedFilename.replace(".quickdic", "");
+ }
+ fileToNameCache.put(uncompressedFilename, name);
+ return name;
+ }
+
+ public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
+ dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
+ dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
+ PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);