/**
   * Changes current layout to another one. New layout is provided with special actions
   * substitutions. E.g. for secondary layout special action LAYOUT_BACK_TO_PRIMARY is substituted
   * to the particular primary layout. All layouts (primary and secondary) are provided with next
   * and prev primary layouts.
   */
  public void changeLayout(String name) {
    mCurrentLayout = bank.get(name);
    if (mCurrentLayout.type == TYPE_PRIMARY) {
      mCurrentPrimaryLayout = mCurrentLayout;
    } else {
      mCurrentLayout.registerLayoutActionKey(
          LAYOUT_BACK_TO_PRIMARY, backToPrimaryKeys.get(mCurrentPrimaryLayout.name));
    }

    // if there is not any "prev" or "next" layouts, set them to null and return.
    if (primaryLayouts.size() < 2) {
      mCurrentLayout.registerLayoutActionKey(LAYOUT_PREV_PRIMARY, null);
      mCurrentLayout.registerLayoutActionKey(LAYOUT_NEXT_PRIMARY, null);
      return;
    }

    // current primary layout's index
    int i = primaryLayouts.indexOf(mCurrentPrimaryLayout);

    // find and pass next primary layout name
    int next_i = (i == primaryLayouts.size() - 1) ? 0 : (i + 1);
    mCurrentLayout.registerLayoutActionKey(
        LAYOUT_NEXT_PRIMARY, primaryLayoutKeys.get(primaryLayouts.get(next_i).name));

    // don't display previous primary layout button if it is equal to next primary layout
    if (primaryLayouts.size() == 2) {
      mCurrentLayout.registerLayoutActionKey(LAYOUT_PREV_PRIMARY, null);
      return;
    }

    // find and pass prev primary layout name
    int prev_i = (i == 0) ? (primaryLayouts.size() - 1) : (i - 1);
    mCurrentLayout.registerLayoutActionKey(
        LAYOUT_PREV_PRIMARY, primaryLayoutKeys.get(primaryLayouts.get(prev_i).name));
  }
  public LayoutSwitcher() {
    // TODO: load layouts automatically using reflection
    // TODO: load primary layouts from settings.
    // TODO: if no primary layouts are selected in settings explicitely, select it by locale

    Layout tmp;

    tmp = new LayoutEn();
    bank.put(tmp.name, tmp);
    defaultLayout = tmp;

    tmp = new LayoutRu();
    bank.put(tmp.name, tmp);

    tmp = new LayoutNum();
    bank.put(tmp.name, tmp);

    tmp = new LayoutDiacritic();
    bank.put(tmp.name, tmp);

    // Preparing layouts to be able to switch between each other
    for (Layout l : bank.values()) {
      if (l.type == TYPE_PRIMARY) {
        backToPrimaryKeys.put(l.name, new Key(Action.createLayoutAction(l.name), l.labelSecondary));
        primaryLayoutKeys.put(l.name, new Key(Action.createLayoutAction(l.name), l.labelPrimary));
        primaryLayouts.add(l);
      }

      for (Layout l2 : bank.values()) {
        if (!l2.name.equals(l.name))
          l.registerLayoutActionKey(
              l2.name, new Key(Action.createLayoutAction(l2.name), l2.labelPrimary));
      }
    }

    changeLayout(defaultLayout.name);
  }