Java Code Examples for com.android.inputmethod.latin.utils.DictionaryInfoUtils#getMainDictionaryResourceId()

The following examples show how to use com.android.inputmethod.latin.utils.DictionaryInfoUtils#getMainDictionaryResourceId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryDictionaryGetter.java    From Android-Keyboard with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of file addresses for a given locale, trying relevant methods in order.
 *
 * Tries to get binary dictionaries from various sources, in order:
 * - Uses a content provider to get a public dictionary set, as per the protocol described
 *   in BinaryDictionaryFileDumper.
 * If that fails:
 * - Gets a file name from the built-in dictionary for this locale, if any.
 * If that fails:
 * - Returns null.
 * @return The list of addresses of valid dictionary files, or null.
 */
public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
        final Context context, boolean notifyDictionaryPackForUpdates) {
    if (notifyDictionaryPackForUpdates) {
        final boolean hasDefaultWordList = DictionaryInfoUtils.isDictionaryAvailable(
                context, locale);
        // It makes sure that the first time keyboard comes up and the dictionaries are reset,
        // the DB is populated with the appropriate values for each locale. Helps in downloading
        // the dictionaries when the user enables and switches new languages before the
        // DictionaryService runs.
        BinaryDictionaryFileDumper.downloadDictIfNeverRequested(
                locale, context, hasDefaultWordList);

        // Move a staging files to the cache ddirectories if any.
        DictionaryInfoUtils.moveStagingFilesIfExists(context);
    }
    final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
    final String mainDictId = DictionaryInfoUtils.getMainDictId(locale);
    final DictPackSettings dictPackSettings = new DictPackSettings(context);

    boolean foundMainDict = false;
    final ArrayList<AssetFileAddress> fileList = new ArrayList<>();
    // cachedWordLists may not be null, see doc for getCachedDictionaryList
    for (final File f : cachedWordLists) {
        final String wordListId = DictionaryInfoUtils.getWordListIdFromFileName(f.getName());
        final boolean canUse = f.canRead() && hackCanUseDictionaryFile(f);
        if (canUse && DictionaryInfoUtils.isMainWordListId(wordListId)) {
            foundMainDict = true;
        }
        if (!dictPackSettings.isWordListActive(wordListId)) continue;
        if (canUse) {
            final AssetFileAddress afa = AssetFileAddress.makeFromFileName(f.getPath());
            if (null != afa) fileList.add(afa);
        } else {
            Log.e(TAG, "Found a cached dictionary file for " + locale.toString()
                    + " but cannot read or use it");
        }
    }

    if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
        final int fallbackResId =
                DictionaryInfoUtils.getMainDictionaryResourceId(context.getResources(), locale);
        final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
        if (null != fallbackAsset) {
            fileList.add(fallbackAsset);
        }
    }

    return fileList;
}
 
Example 2
Source File: BinaryDictionaryGetter.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of file addresses for a given locale, trying relevant methods in order.
 *
 * Tries to get binary dictionaries from various sources, in order:
 * - Uses a content provider to get a public dictionary set, as per the protocol described
 *   in BinaryDictionaryFileDumper.
 * If that fails:
 * - Gets a file name from the built-in dictionary for this locale, if any.
 * If that fails:
 * - Returns null.
 * @return The list of addresses of valid dictionary files, or null.
 */
public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
        final Context context, boolean notifyDictionaryPackForUpdates) {
    if (notifyDictionaryPackForUpdates) {
        final boolean hasDefaultWordList = DictionaryInfoUtils.isDictionaryAvailable(
                context, locale);
        // It makes sure that the first time keyboard comes up and the dictionaries are reset,
        // the DB is populated with the appropriate values for each locale. Helps in downloading
        // the dictionaries when the user enables and switches new languages before the
        // DictionaryService runs.
        BinaryDictionaryFileDumper.downloadDictIfNeverRequested(
                locale, context, hasDefaultWordList);

        // Move a staging files to the cache ddirectories if any.
        DictionaryInfoUtils.moveStagingFilesIfExists(context);
    }
    final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
    final String mainDictId = DictionaryInfoUtils.getMainDictId(locale);
    final DictPackSettings dictPackSettings = new DictPackSettings(context);

    boolean foundMainDict = false;
    final ArrayList<AssetFileAddress> fileList = new ArrayList<>();
    // cachedWordLists may not be null, see doc for getCachedDictionaryList
    for (final File f : cachedWordLists) {
        final String wordListId = DictionaryInfoUtils.getWordListIdFromFileName(f.getName());
        final boolean canUse = f.canRead() && hackCanUseDictionaryFile(f);
        if (canUse && DictionaryInfoUtils.isMainWordListId(wordListId)) {
            foundMainDict = true;
        }
        if (!dictPackSettings.isWordListActive(wordListId)) continue;
        if (canUse) {
            final AssetFileAddress afa = AssetFileAddress.makeFromFileName(f.getPath());
            if (null != afa) fileList.add(afa);
        } else {
            Log.e(TAG, "Found a cached dictionary file for " + locale.toString()
                    + " but cannot read or use it");
        }
    }

    if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
        final int fallbackResId =
                DictionaryInfoUtils.getMainDictionaryResourceId(context.getResources(), locale);
        final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
        if (null != fallbackAsset) {
            fileList.add(fallbackAsset);
        }
    }

    return fileList;
}
 
Example 3
Source File: BinaryDictionaryGetter.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a list of file addresses for a given locale, trying relevant methods in order.
 *
 * Tries to get binary dictionaries from various sources, in order:
 * - Uses a content provider to get a public dictionary set, as per the protocol described
 *   in BinaryDictionaryFileDumper.
 * If that fails:
 * - Gets a file name from the built-in dictionary for this locale, if any.
 * If that fails:
 * - Returns null.
 * @return The list of addresses of valid dictionary files, or null.
 */
public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
        final Context context, boolean notifyDictionaryPackForUpdates) {
    if (notifyDictionaryPackForUpdates) {
        final boolean hasDefaultWordList = DictionaryInfoUtils.isDictionaryAvailable(
                context, locale);
        // It makes sure that the first time keyboard comes up and the dictionaries are reset,
        // the DB is populated with the appropriate values for each locale. Helps in downloading
        // the dictionaries when the user enables and switches new languages before the
        // DictionaryService runs.
        BinaryDictionaryFileDumper.downloadDictIfNeverRequested(
                locale, context, hasDefaultWordList);

        // Move a staging files to the cache ddirectories if any.
        DictionaryInfoUtils.moveStagingFilesIfExists(context);
    }
    final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
    final String mainDictId = DictionaryInfoUtils.getMainDictId(locale);
    final DictPackSettings dictPackSettings = new DictPackSettings(context);

    boolean foundMainDict = false;
    final ArrayList<AssetFileAddress> fileList = new ArrayList<>();
    // cachedWordLists may not be null, see doc for getCachedDictionaryList
    for (final File f : cachedWordLists) {
        final String wordListId = DictionaryInfoUtils.getWordListIdFromFileName(f.getName());
        final boolean canUse = f.canRead() && hackCanUseDictionaryFile(f);
        if (canUse && DictionaryInfoUtils.isMainWordListId(wordListId)) {
            foundMainDict = true;
        }
        if (!dictPackSettings.isWordListActive(wordListId)) continue;
        if (canUse) {
            final AssetFileAddress afa = AssetFileAddress.makeFromFileName(f.getPath());
            if (null != afa) fileList.add(afa);
        } else {
            Log.e(TAG, "Found a cached dictionary file for " + locale.toString()
                    + " but cannot read or use it");
        }
    }

    if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
        final int fallbackResId =
                DictionaryInfoUtils.getMainDictionaryResourceId(context.getResources(), locale);
        final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
        if (null != fallbackAsset) {
            fileList.add(fallbackAsset);
        }
    }

    return fileList;
}