private void updateTileSourceSummary() {
    fillTileSourcesToPreference(tileSourcePreference, osmandSettings.MAP_TILE_SOURCES.get(), false);
    fillTileSourcesToPreference(overlayPreference, osmandSettings.MAP_OVERLAY.get(), true);
    fillTileSourcesToPreference(underlayPreference, osmandSettings.MAP_UNDERLAY.get(), true);

    String mapName = " " + osmandSettings.MAP_TILE_SOURCES.get(); // $NON-NLS-1$
    String summary = tileSourcePreference.getSummary().toString();
    if (summary.lastIndexOf(':') != -1) {
      summary = summary.substring(0, summary.lastIndexOf(':') + 1);
    }
    tileSourcePreference.setSummary(summary + mapName);
  }
  @SuppressWarnings("unchecked")
  @Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    // handle boolean prefences
    OsmandPreference<Boolean> boolPref = booleanPreferences.get(preference.getKey());
    OsmandPreference<Integer> seekPref = seekBarPreferences.get(preference.getKey());
    OsmandPreference<Object> listPref =
        (OsmandPreference<Object>) listPreferences.get(preference.getKey());
    OsmandPreference<String> editPref = editTextPreferences.get(preference.getKey());
    if (boolPref != null) {
      boolPref.set((Boolean) newValue);
      if (boolPref.getId().equals(osmandSettings.MAP_VECTOR_DATA.getId())) {
        MapRenderRepositories r =
            ((OsmandApplication) getApplication()).getResourceManager().getRenderer();
        if (r.isEmpty()) {
          Toast.makeText(this, getString(R.string.no_vector_map_loaded), Toast.LENGTH_LONG).show();
          return false;
        }
      }
    } else if (seekPref != null) {
      seekPref.set((Integer) newValue);
    } else if (editPref != null) {
      editPref.set((String) newValue);
    } else if (listPref != null) {
      int ind = ((ListPreference) preference).findIndexOfValue((String) newValue);
      CharSequence entry = ((ListPreference) preference).getEntries()[ind];
      Map<String, ?> map = listPrefValues.get(preference.getKey());
      Object obj = map.get(entry);
      final Object oldValue = listPref.get();
      boolean changed = listPref.set(obj);

      // Specific actions after list preference changed
      if (changed) {
        if (listPref.getId().equals(osmandSettings.VOICE_PROVIDER.getId())) {
          if (MORE_VALUE.equals(newValue)) {
            listPref.set(oldValue); // revert the change..
            final Intent intent = new Intent(this, DownloadIndexActivity.class);
            intent.putExtra(DownloadIndexActivity.FILTER_KEY, "voice");
            startActivity(intent);
          } else {
            getMyApplication().showDialogInitializingCommandPlayer(this, false);
          }
        } else if (listPref.getId().equals(osmandSettings.APPLICATION_MODE.getId())) {
          updateAllSettings();
        } else if (listPref.getId().equals(osmandSettings.PREFERRED_LOCALE.getId())) {
          // restart application to update locale
          getMyApplication().checkPrefferedLocale();
          Intent intent = getIntent();
          finish();
          startActivity(intent);
        }
      }
      if (listPref.getId().equals(osmandSettings.RENDERER.getId())) {
        if (changed) {
          Toast.makeText(this, R.string.renderer_load_sucess, Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(this, R.string.renderer_load_exception, Toast.LENGTH_SHORT).show();
        }
      }
    } else if (preference == applicationDir) {
      warnAboutChangingStorage((String) newValue);
      return false;
    } else if (preference == routeServiceEnabled) {
      Intent serviceIntent = new Intent(this, NavigationService.class);
      if ((Boolean) newValue) {
        ComponentName name = startService(serviceIntent);
        if (name == null) {
          routeServiceEnabled.setChecked(getMyApplication().getNavigationService() != null);
        }
      } else {
        if (!stopService(serviceIntent)) {
          routeServiceEnabled.setChecked(getMyApplication().getNavigationService() != null);
        }
      }
    } else if (preference == tileSourcePreference
        || preference == overlayPreference
        || preference == underlayPreference) {
      if (MORE_VALUE.equals(newValue)) {
        SettingsActivity.installMapLayers(
            this,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                updateTileSourceSummary();
              }
            });
      } else if (preference == tileSourcePreference) {
        osmandSettings.MAP_TILE_SOURCES.set((String) newValue);
        updateTileSourceSummary();
      } else {
        if (((String) newValue).length() == 0) {
          newValue = null;
        }
        if (preference == underlayPreference) {
          osmandSettings.MAP_UNDERLAY.set(((String) newValue));
        } else if (preference == overlayPreference) {
          osmandSettings.MAP_OVERLAY.set(((String) newValue));
        }
      }
    }
    return true;
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings_pref);
    String[] entries;
    String[] entrieValues;
    PreferenceScreen screen = getPreferenceScreen();
    osmandSettings = OsmandSettings.getOsmandSettings(this);

    registerBooleanPreference(osmandSettings.SHOW_VIEW_ANGLE, screen);
    registerBooleanPreference(osmandSettings.USE_TRACKBALL_FOR_MOVEMENTS, screen);
    registerBooleanPreference(osmandSettings.USE_HIGH_RES_MAPS, screen);
    registerBooleanPreference(osmandSettings.USE_ENGLISH_NAMES, screen);
    registerBooleanPreference(osmandSettings.SHOW_MORE_MAP_DETAIL, screen);
    registerBooleanPreference(osmandSettings.AUTO_ZOOM_MAP, screen);
    registerBooleanPreference(osmandSettings.SAVE_TRACK_TO_GPX, screen);
    registerBooleanPreference(osmandSettings.DEBUG_RENDERING_INFO, screen);
    registerBooleanPreference(osmandSettings.USE_STEP_BY_STEP_RENDERING, screen);
    registerBooleanPreference(osmandSettings.FAST_ROUTE_MODE, screen);
    registerBooleanPreference(osmandSettings.USE_OSMAND_ROUTING_SERVICE_ALWAYS, screen);
    registerBooleanPreference(osmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, screen);
    registerBooleanPreference(osmandSettings.MAP_VECTOR_DATA, screen);

    registerEditTextPreference(osmandSettings.USER_NAME, screen);
    registerEditTextPreference(osmandSettings.USER_PASSWORD, screen);

    registerSeekBarPreference(osmandSettings.MAP_OVERLAY_TRANSPARENCY, screen);
    registerSeekBarPreference(osmandSettings.MAP_TRANSPARENCY, screen);

    // List preferences
    registerListPreference(
        osmandSettings.ROTATE_MAP,
        screen,
        new String[] {
          getString(R.string.rotate_map_none_opt),
          getString(R.string.rotate_map_bearing_opt),
          getString(R.string.rotate_map_compass_opt)
        },
        new Integer[] {
          OsmandSettings.ROTATE_MAP_NONE,
          OsmandSettings.ROTATE_MAP_BEARING,
          OsmandSettings.ROTATE_MAP_COMPASS
        });

    registerListPreference(
        osmandSettings.MAP_SCREEN_ORIENTATION,
        screen,
        new String[] {
          getString(R.string.map_orientation_portrait),
          getString(R.string.map_orientation_landscape),
          getString(R.string.map_orientation_default)
        },
        new Integer[] {
          ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
          ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
          ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
        });

    registerListPreference(
        osmandSettings.POSITION_ON_MAP,
        screen,
        new String[] {
          getString(R.string.position_on_map_center), getString(R.string.position_on_map_bottom)
        },
        new Integer[] {OsmandSettings.CENTER_CONSTANT, OsmandSettings.BOTTOM_CONSTANT});

    registerListPreference(
        osmandSettings.AUDIO_STREAM_GUIDANCE,
        screen,
        new String[] {
          getString(R.string.voice_stream_music),
          getString(R.string.voice_stream_notification),
          getString(R.string.voice_stream_voice_call)
        },
        new Integer[] {
          AudioManager.STREAM_MUSIC,
          AudioManager.STREAM_NOTIFICATION,
          AudioManager.STREAM_VOICE_CALL
        });

    entries = new String[DayNightMode.values().length];
    for (int i = 0; i < entries.length; i++) {
      entries[i] = DayNightMode.values()[i].toHumanString(this);
    }
    registerListPreference(osmandSettings.DAYNIGHT_MODE, screen, entries, DayNightMode.values());

    entries = new String[MetricsConstants.values().length];
    for (int i = 0; i < entries.length; i++) {
      entries[i] = MetricsConstants.values()[i].toHumanString(this);
    }
    registerListPreference(
        osmandSettings.METRIC_SYSTEM, screen, entries, MetricsConstants.values());

    // getResources().getAssets().getLocales();
    entrieValues = new String[] {"", "en", "cs", "de", "es", "fr", "hu", "it", "pt", "ru", "sk"};
    entries = new String[entrieValues.length];
    entries[0] = getString(R.string.system_locale);
    for (int i = 1; i < entries.length; i++) {
      entries[i] = entrieValues[i];
    }
    registerListPreference(osmandSettings.PREFERRED_LOCALE, screen, entries, entrieValues);

    int startZoom = 12;
    int endZoom = 19;
    entries = new String[endZoom - startZoom + 1];
    Integer[] intValues = new Integer[endZoom - startZoom + 1];
    for (int i = startZoom; i <= endZoom; i++) {
      entries[i - startZoom] = i + ""; // $NON-NLS-1$
      intValues[i - startZoom] = i;
    }
    registerListPreference(osmandSettings.MAX_LEVEL_TO_DOWNLOAD_TILE, screen, entries, intValues);

    Float[] floatValues = new Float[] {0.6f, 0.8f, 1.0f, 1.2f, 1.5f};
    entries = new String[floatValues.length];
    for (int i = 0; i < floatValues.length; i++) {
      entries[i] = (int) (floatValues[i] * 100) + " %";
    }
    registerListPreference(osmandSettings.MAP_TEXT_SIZE, screen, entries, floatValues);

    startZoom = 1;
    endZoom = 18;
    entries = new String[endZoom - startZoom + 1];
    intValues = new Integer[endZoom - startZoom + 1];
    for (int i = startZoom; i <= endZoom; i++) {
      entries[i - startZoom] = i + ""; // $NON-NLS-1$
      intValues[i - startZoom] = i;
    }
    registerListPreference(
        osmandSettings.LEVEL_TO_SWITCH_VECTOR_RASTER, screen, entries, intValues);

    entries = new String[RouteService.values().length];
    for (int i = 0; i < entries.length; i++) {
      entries[i] = RouteService.values()[i].getName();
    }
    registerListPreference(osmandSettings.ROUTER_SERVICE, screen, entries, RouteService.values());

    entries = new String[] {getString(R.string.gps_provider), getString(R.string.network_provider)};
    entrieValues = new String[] {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER};
    registerListPreference(osmandSettings.SERVICE_OFF_PROVIDER, screen, entries, entrieValues);

    registerTimeListPreference(
        osmandSettings.SAVE_TRACK_INTERVAL,
        screen,
        new int[] {1, 2, 3, 5, 10, 15, 20, 30},
        new int[] {1, 2, 3, 5},
        1);
    registerTimeListPreference(
        osmandSettings.SERVICE_OFF_INTERVAL,
        screen,
        new int[] {0, 30, 45, 60},
        new int[] {2, 3, 5, 8, 10, 15, 20, 30, 40, 50, 70, 90},
        1000);
    registerTimeListPreference(
        osmandSettings.SERVICE_OFF_WAIT_INTERVAL,
        screen,
        new int[] {15, 30, 45, 60, 90},
        new int[] {2, 3, 5, 10},
        1000);

    entries = new String[ApplicationMode.values().length];
    for (int i = 0; i < entries.length; i++) {
      entries[i] = ApplicationMode.toHumanString(ApplicationMode.values()[i], this);
    }
    registerListPreference(
        osmandSettings.APPLICATION_MODE, screen, entries, ApplicationMode.values());

    Collection<String> rendererNames = getMyApplication().getRendererRegistry().getRendererNames();
    entries = (String[]) rendererNames.toArray(new String[rendererNames.size()]);
    registerListPreference(osmandSettings.RENDERER, screen, entries, entries);

    tileSourcePreference =
        (ListPreference) screen.findPreference(osmandSettings.MAP_TILE_SOURCES.getId());
    tileSourcePreference.setOnPreferenceChangeListener(this);
    overlayPreference = (ListPreference) screen.findPreference(osmandSettings.MAP_OVERLAY.getId());
    overlayPreference.setOnPreferenceChangeListener(this);
    underlayPreference =
        (ListPreference) screen.findPreference(osmandSettings.MAP_UNDERLAY.getId());
    underlayPreference.setOnPreferenceChangeListener(this);

    Preference localIndexes = (Preference) screen.findPreference(OsmandSettings.LOCAL_INDEXES);
    localIndexes.setOnPreferenceClickListener(this);
    saveCurrentTrack = (Preference) screen.findPreference(OsmandSettings.SAVE_CURRENT_TRACK);
    saveCurrentTrack.setOnPreferenceClickListener(this);
    routeServiceEnabled =
        (CheckBoxPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_ENABLED);
    routeServiceEnabled.setOnPreferenceChangeListener(this);
    applicationDir =
        (EditTextPreference) screen.findPreference(OsmandSettings.EXTERNAL_STORAGE_DIR);
    applicationDir.setOnPreferenceChangeListener(this);

    broadcastReceiver =
        new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            routeServiceEnabled.setChecked(false);
          }
        };
    registerReceiver(
        broadcastReceiver, new IntentFilter(NavigationService.OSMAND_STOP_SERVICE_ACTION));

    Intent intent = getIntent();
    if (intent != null && intent.getIntExtra(INTENT_KEY_SETTINGS_SCREEN, 0) != 0) {
      int s = intent.getIntExtra(INTENT_KEY_SETTINGS_SCREEN, 0);
      String pref = null;
      if (s == SCREEN_GENERAL_SETTINGS) {
        pref = "general_settings";
      } else if (s == SCREEN_NAVIGATION_SETTINGS) {
        pref = "routing_settings";
      } else if (s == SCREEN_MONITORING_SETTINGS) {
        pref = "monitor_settings";
      }
      if (pref != null) {
        Preference toOpen = screen.findPreference(pref);
        if (toOpen instanceof PreferenceScreen) {
          setPreferenceScreen((PreferenceScreen) toOpen);
        }
      }
    }
  }