@Override
  public void onSharedPreferenceChanged(
      final SharedPreferences sharedPreferences, final String key) {
    if (key.equals("pref_force_desktop")) {
      mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false);
      mNavigationHelper.onPrefChanged();
    } else if (key.equals("pref_user_location_mode")) {
      final int mode = Integer.parseInt(mSharedPrefs.getString("pref_user_location_mode", "0"));
      if (mUserLocation.setLocationMode(mode)) mReloadNeeded = true;
      return;
    } else if (key.equals("pref_persistent_zoom")) {
      mPersistentZoom = mSharedPrefs.getBoolean("pref_persistent_zoom", false);
      return;
    } else if (key.equals("pref_fullscreen")) {
      mIitcWebView.updateFullscreenStatus();
      mNavigationHelper.onPrefChanged();
      return;
    } else if (key.equals("pref_android_menu")) {
      final String[] menuDefaults =
          getResources().getStringArray(R.array.pref_android_menu_default);
      mAdvancedMenu =
          mSharedPrefs.getStringSet(
              "pref_android_menu", new HashSet<String>(Arrays.asList(menuDefaults)));
      mNavigationHelper.setDebugMode(mAdvancedMenu.contains(R.string.menu_debug));
      invalidateOptionsMenu();
      // no reload needed
      return;
    } else if (key.equals("pref_fake_user_agent")) {
      mIitcWebView.setUserAgent();
    } else if (key.equals("pref_last_plugin_update")) {
      final Long forceUpdate = sharedPreferences.getLong("pref_last_plugin_update", 0);
      if (forceUpdate == 0) mFileManager.updatePlugins(true);
      return;
    } else if (key.equals("pref_update_plugins_interval")) {
      final int interval =
          Integer.parseInt(mSharedPrefs.getString("pref_update_plugins_interval", "7"));
      mFileManager.setUpdateInterval(interval);
      return;
    } else if (key.equals("pref_press_twice_to_exit")
        || key.equals("pref_share_selected_tab")
        || key.equals("pref_messages")
        || key.equals("pref_secure_updates")
        || key.equals("pref_external_storage")) {
      // no reload needed
      return;
    }

    mReloadNeeded = true;
  }
  public void setLoadingState(final boolean isLoading) {
    mIsLoading = isLoading;
    mNavigationHelper.onLoadingStateChanged();
    invalidateOptionsMenu();
    updateViews();
    if (!isLoading) mFileManager.updatePlugins(false);

    if (mSearchTerm != null && !isLoading) {
      new Handler()
          .postDelayed(
              new Runnable() {
                @Override
                public void run() {
                  // switchToPane(Pane.MAP);
                  mIitcWebView.loadUrl("javascript:search('" + mSearchTerm + "');");
                  mSearchTerm = null;
                }
              },
              5000);
    }
  }
  @Override
  public boolean onOptionsItemSelected(final MenuItem item) {
    if (mNavigationHelper.onOptionsItemSelected(item)) return true;

    // Handle item selection
    final int itemId = item.getItemId();

    switch (itemId) {
      case android.R.id.home:
        switchToPane(Pane.MAP);
        return true;
      case R.id.reload_button:
        reloadIITC();
        return true;
      case R.id.toggle_fullscreen:
        mIitcWebView.toggleFullscreen();
        return true;
      case R.id.layer_chooser:
        mNavigationHelper.openRightDrawer();
        return true;
      case R.id.locate: // get the users current location and focus it on map
        switchToPane(Pane.MAP);

        if (mUserLocation.hasCurrentLocation()) {
          // if gps location is displayed we can use a better location without any costs
          mUserLocation.locate(mPersistentZoom);
        } else {
          // get location from network by default
          mIitcWebView.loadUrl(
              "javascript: window.map.locate({setView : true"
                  + (mPersistentZoom ? ", maxZoom : map.getZoom()" : "")
                  + "});");
        }
        return true;
      case R.id.action_settings: // start settings activity
        final Intent intent = new Intent(this, PreferenceActivity.class);
        try {
          intent.putExtra("iitc_version", mFileManager.getIITCVersion());
        } catch (final IOException e) {
          Log.w(e);
          return true;
        }
        startActivity(intent);
        return true;
      case R.id.menu_clear_cookies:
        final CookieManager cm = CookieManager.getInstance();
        cm.removeAllCookie();
        return true;
      case R.id.menu_send_screenshot:
        sendScreenshot();
        return true;
      case R.id.menu_debug:
        mDebugging = !mDebugging;
        updateViews();
        invalidateOptionsMenu();

        // TODO remove debugging stuff from JS?
        return true;
      default:
        return false;
    }
  }
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // enable progress bar above action bar
    requestWindowFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.activity_main);
    mImageLoading = findViewById(R.id.imageLoading);
    mIitcWebView = (IITC_WebView) findViewById(R.id.iitc_webview);
    mLvDebug = (ListView) findViewById(R.id.lvDebug);
    mViewDebug = findViewById(R.id.viewDebug);
    mBtnToggleMap = (ImageButton) findViewById(R.id.btnToggleMapVisibility);
    mEditCommand = (EditText) findViewById(R.id.editCommand);
    mEditCommand.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(
              final TextView v, final int actionId, final KeyEvent event) {
            if (EditorInfo.IME_ACTION_GO == actionId
                || EditorInfo.IME_ACTION_SEND == actionId
                || EditorInfo.IME_ACTION_DONE == actionId) {
              onBtnRunCodeClick(v);

              final InputMethodManager imm =
                  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

              return true;
            }
            return false;
          }
        });

    mLvDebug.setAdapter(new IITC_LogAdapter(this));
    mLvDebug.setOnItemLongClickListener(this);

    // do something if user changed something in the settings
    mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    mSharedPrefs.registerOnSharedPreferenceChangeListener(this);

    // enable/disable mDesktopMode mode on menu create and url load
    mDesktopMode = mSharedPrefs.getBoolean("pref_force_desktop", false);

    // enable/disable advance menu
    final String[] menuDefaults = getResources().getStringArray(R.array.pref_android_menu_default);
    mAdvancedMenu =
        mSharedPrefs.getStringSet(
            "pref_android_menu", new HashSet<String>(Arrays.asList(menuDefaults)));

    mPersistentZoom = mSharedPrefs.getBoolean("pref_persistent_zoom", false);

    // get fullscreen status from settings
    mIitcWebView.updateFullscreenStatus();

    mFileManager = new IITC_FileManager(this);
    mFileManager.setUpdateInterval(
        Integer.parseInt(mSharedPrefs.getString("pref_update_plugins_interval", "7")));

    mUserLocation = new IITC_UserLocation(this);
    mUserLocation.setLocationMode(
        Integer.parseInt(mSharedPrefs.getString("pref_user_location_mode", "0")));

    // pass ActionBar to helper because we deprecated getActionBar
    mNavigationHelper = new IITC_NavigationHelper(this, super.getActionBar());

    mMapSettings = new IITC_MapSettings(this);

    // Clear the back stack
    mBackStack.clear();

    // receive downloadManagers downloadComplete intent
    // afterwards install iitc update
    registerReceiver(
        mBroadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

    final NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
    if (nfc != null) nfc.setNdefPushMessageCallback(this, this);

    handleIntent(getIntent(), true);
  }