@Override
  public boolean onCreateOptionsMenu(
      final Menu pMenu, final int pMenuIdOffset, final MapView pMapView) {
    final SubMenu mapMenu =
        pMenu
            .addSubMenu(0, MENU_MAP_MODE + pMenuIdOffset, Menu.NONE, R.string.map_mode)
            .setIcon(R.drawable.ic_menu_mapmode);

    for (int a = 0; a < TileSourceFactory.getTileSources().size(); a++) {
      final ITileSource tileSource = TileSourceFactory.getTileSources().get(a);
      mapMenu.add(
          MENU_MAP_MODE + pMenuIdOffset,
          MENU_TILE_SOURCE_STARTING_ID + a + pMenuIdOffset,
          Menu.NONE,
          tileSource.name());
    }
    mapMenu.setGroupCheckable(MENU_MAP_MODE + pMenuIdOffset, true, true);

    if (ctx != null) {
      final String title =
          ctx.getString(
              pMapView.useDataConnection() ? R.string.set_mode_offline : R.string.set_mode_online);
      final Drawable icon = ctx.getResources().getDrawable(R.drawable.ic_menu_offline);
      pMenu.add(0, MENU_OFFLINE + pMenuIdOffset, Menu.NONE, title).setIcon(icon);
    }
    return true;
  }
  @Override
  public boolean onPrepareOptionsMenu(
      final Menu pMenu, final int pMenuIdOffset, final MapView pMapView) {
    final int index =
        TileSourceFactory.getTileSources().indexOf(pMapView.getTileProvider().getTileSource());
    if (index >= 0) {
      pMenu.findItem(MENU_TILE_SOURCE_STARTING_ID + index + pMenuIdOffset).setChecked(true);
    }

    pMenu
        .findItem(MENU_OFFLINE + pMenuIdOffset)
        .setTitle(
            pMapView.useDataConnection() ? R.string.set_mode_offline : R.string.set_mode_online);

    return true;
  }
  @Override
  public boolean onOptionsItemSelected(
      final MenuItem pItem, final int pMenuIdOffset, final MapView pMapView) {

    final int menuId = pItem.getItemId() - pMenuIdOffset;
    if ((menuId >= MENU_TILE_SOURCE_STARTING_ID)
        && (menuId < MENU_TILE_SOURCE_STARTING_ID + TileSourceFactory.getTileSources().size())) {
      pMapView.setTileSource(
          TileSourceFactory.getTileSources().get(menuId - MENU_TILE_SOURCE_STARTING_ID));
      return true;
    } else if (menuId == MENU_OFFLINE) {
      final boolean useDataConnection = !pMapView.useDataConnection();
      pMapView.setUseDataConnection(useDataConnection);
      return true;
    } else {
      return false;
    }
  }