@Override
  protected final void loadAllResources() {
    mNextWordDictionary = new NextWordDictionary(mContext, mLocale);
    mNextWordDictionary.load();

    BTreeDictionary androidBuiltIn = null;
    try {
      // The only reason I see someone uses this, is for development or debugging.
      if (AnyApplication.getConfig().alwaysUseFallBackUserDictionary())
        throw new RuntimeException("User requested to always use fall-back user-dictionary.");

      androidBuiltIn = createAndroidUserDictionary(mContext, mLocale);
      androidBuiltIn.loadDictionary();
      mActualDictionary = androidBuiltIn;
    } catch (Exception e) {
      Logger.w(
          TAG,
          "Can not load Android's built-in user dictionary (since '%s'). FallbackUserDictionary to the rescue!",
          e.getMessage());
      if (androidBuiltIn != null) {
        try {
          androidBuiltIn.close();
        } catch (Exception buildInCloseException) {
          // it's an half-baked object, no need to worry about it
          buildInCloseException.printStackTrace();
          Logger.w(
              TAG, "Failed to close the build-in user dictionary properly, but it should be fine.");
        }
      }
      BTreeDictionary fallback = createFallbackUserDictionary(mContext, mLocale);
      fallback.loadDictionary();

      mActualDictionary = fallback;
    }
  }
예제 #2
0
 @Nullable
 public final Context getPackageContext() {
   Context c = mPackageContext.get();
   if (c == null) {
     try {
       c = mAskAppContext.createPackageContext(mPackageName, Context.CONTEXT_IGNORE_SECURITY);
       mPackageContext = new WeakReference<>(c);
     } catch (NameNotFoundException e) {
       Logger.w(TAG, "Failed to find package %s!", mPackageName);
       Logger.w(TAG, "Failed to find package! ", e);
     }
   }
   return c;
 }
 @Override
 public final boolean addWord(String word, int frequency) {
   if (mActualDictionary != null) {
     return mActualDictionary.addWord(word, frequency);
   } else {
     Logger.d(TAG, "There is no actual dictionary to use for adding word! How come?");
     return false;
   }
 }
  public static List<KeyboardAddOnAndBuilder> getEnabledKeyboards(Context askContext) {
    final List<KeyboardAddOnAndBuilder> allAddOns = msInstance.getAllAddOns(askContext);
    Logger.i(
        TAG, "Creating enabled addons list. I have a total of " + allAddOns.size() + " addons");

    // getting shared prefs to determine which to create.
    final SharedPreferences sharedPreferences =
        PreferenceManager.getDefaultSharedPreferences(askContext);

    final ArrayList<KeyboardAddOnAndBuilder> enabledAddOns = new ArrayList<>();
    for (int addOnIndex = 0; addOnIndex < allAddOns.size(); addOnIndex++) {
      final KeyboardAddOnAndBuilder addOn = allAddOns.get(addOnIndex);

      final boolean addOnEnabled =
          sharedPreferences.getBoolean(addOn.getId(), addOn.getKeyboardDefaultEnabled());

      if (addOnEnabled) {
        enabledAddOns.add(addOn);
      }
    }

    // Fix: issue 219
    // Check if there is any keyboards created if not, lets create a default english keyboard
    if (enabledAddOns.size() == 0) {
      final SharedPreferences.Editor editor = sharedPreferences.edit();
      final KeyboardAddOnAndBuilder addOn = allAddOns.get(0);
      editor.putBoolean(addOn.getId(), true);
      SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
      enabledAddOns.add(addOn);
    }

    if (BuildConfig.TESTING_BUILD) {
      for (final KeyboardAddOnAndBuilder addOn : enabledAddOns) {
        Logger.d(TAG, "Factory provided addon: %s", addOn.getId());
      }
    }

    return enabledAddOns;
  }
  @Override
  protected KeyboardAddOnAndBuilder createConcreteAddOn(
      Context askContext,
      Context context,
      String prefId,
      int nameId,
      String description,
      int sortIndex,
      AttributeSet attrs) {
    final int layoutResId =
        attrs.getAttributeResourceValue(null, XML_LAYOUT_RES_ID_ATTRIBUTE, AddOn.INVALID_RES_ID);
    final int landscapeLayoutResId =
        attrs.getAttributeResourceValue(
            null, XML_LANDSCAPE_LAYOUT_RES_ID_ATTRIBUTE, AddOn.INVALID_RES_ID);
    final int iconResId =
        attrs.getAttributeResourceValue(
            null, XML_ICON_RES_ID_ATTRIBUTE, R.drawable.sym_keyboard_notification_icon);
    final String defaultDictionary = attrs.getAttributeValue(null, XML_DICTIONARY_NAME_ATTRIBUTE);
    final String additionalIsLetterExceptions =
        attrs.getAttributeValue(null, XML_ADDITIONAL_IS_LETTER_EXCEPTIONS_ATTRIBUTE);
    String sentenceSeparators =
        attrs.getAttributeValue(null, XML_SENTENCE_SEPARATOR_CHARACTERS_ATTRIBUTE);
    if (TextUtils.isEmpty(sentenceSeparators)) sentenceSeparators = DEFAULT_SENTENCE_SEPARATORS;
    final int physicalTranslationResId =
        attrs.getAttributeResourceValue(
            null, XML_PHYSICAL_TRANSLATION_RES_ID_ATTRIBUTE, AddOn.INVALID_RES_ID);
    // A keyboard is enabled by default if it is the first one (index==1)
    final boolean keyboardDefault =
        attrs.getAttributeBooleanValue(null, XML_DEFAULT_ATTRIBUTE, sortIndex == 1);

    // asserting
    if ((prefId == null)
        || (nameId == AddOn.INVALID_RES_ID)
        || (layoutResId == AddOn.INVALID_RES_ID)) {
      Logger.e(
          TAG,
          "External Keyboard does not include all mandatory details! Will not create keyboard.");
      return null;
    } else {
      if (BuildConfig.DEBUG) {
        Logger.d(
            TAG,
            "External keyboard details: prefId:"
                + prefId
                + " nameId:"
                + nameId
                + " resId:"
                + layoutResId
                + " landscapeResId:"
                + landscapeLayoutResId
                + " iconResId:"
                + iconResId
                + " defaultDictionary:"
                + defaultDictionary);
      }
      return new KeyboardAddOnAndBuilder(
          askContext,
          context,
          prefId,
          nameId,
          layoutResId,
          landscapeLayoutResId,
          defaultDictionary,
          iconResId,
          physicalTranslationResId,
          additionalIsLetterExceptions,
          sentenceSeparators,
          description,
          sortIndex,
          keyboardDefault);
    }
  }