public void loadSettings() {
    // if does not exist load default
    SharedPreferences settings = getActivity().getSharedPreferences(PREF_TITLE, 0);
    StringBuilder sb = new StringBuilder();
    et_serv.setText(settings.getString(PREF_URL, DEFAULT_URL));
    et_user.setText(settings.getString(PREF_USER, DEFAULT_USER));
    et_pass.setText(settings.getString(PREF_PASS, DEFAULT_PASS));
    et_thread.setText(sb.append(settings.getInt(PREF_THREAD, DEFAULT_THREAD)).toString());
    sb.setLength(0);
    sb_throttle.setProgress((int) (settings.getFloat(PREF_THROTTLE, DEFAULT_THROTTLE) * 100));
    et_scanTime.setText(sb.append(settings.getLong(PREF_SCANTIME, DEFAULT_SCANTIME)).toString());
    sb.setLength(0);
    et_retryPause.setText(
        sb.append(settings.getLong(PREF_RETRYPAUSE, DEFAULT_RETRYPAUSE)).toString());
    cb_service.setChecked(settings.getBoolean(PREF_BACKGROUND, DEFAULT_BACKGROUND));
    cb_donate.setChecked(settings.getBoolean(PREF_DONATE, DEFAULT_DONATE));

    if (settings.getInt(PREF_PRIORITY, DEFAULT_PRIORITY) == Thread.MIN_PRIORITY) {
      spn_priority.setSelection(0);
    }
    if (settings.getInt(PREF_PRIORITY, DEFAULT_PRIORITY) == Thread.NORM_PRIORITY) {
      spn_priority.setSelection(1);
    }
    if (settings.getInt(PREF_PRIORITY, DEFAULT_PRIORITY) == Thread.MAX_PRIORITY) {
      spn_priority.setSelection(2);
    }

    Toast.makeText(getActivity(), "Settings Loaded", Toast.LENGTH_SHORT).show();
  }
  public void saveSettings() {
    SharedPreferences settings = getActivity().getSharedPreferences(PREF_TITLE, 0);
    SharedPreferences.Editor editor = settings.edit();
    StringBuilder sb = new StringBuilder();

    if (verify() == true) {
      sb = new StringBuilder();

      String url = sb.append(et_serv.getText()).toString();
      sb.setLength(0);
      String user = sb.append(et_user.getText()).toString();
      sb.setLength(0);
      String pass = sb.append(et_pass.getText()).toString();
      sb.setLength(0);
      int threads = Integer.parseInt(sb.append(et_thread.getText()).toString());
      sb.setLength(0);
      float throttle = (float) sb_throttle.getProgress() / 100;
      sb.setLength(0);
      long scantime = Long.parseLong(sb.append(et_scanTime.getText()).toString());
      sb.setLength(0);
      long retrypause = Long.parseLong(sb.append(et_retryPause.getText()).toString());

      settings = getActivity().getSharedPreferences(PREF_TITLE, 0);
      editor = settings.edit();
      editor.putString(PREF_URL, url);
      editor.putString(PREF_USER, user);
      editor.putString(PREF_PASS, pass);
      editor.putInt(PREF_THREAD, threads);
      Log.i("LC", "Settings: Throttle: " + throttle);
      editor.putFloat(PREF_THROTTLE, throttle);
      editor.putLong(PREF_SCANTIME, scantime);
      editor.putLong(PREF_RETRYPAUSE, retrypause);
      editor.putBoolean(PREF_BACKGROUND, cb_service.isChecked());
      editor.putBoolean(PREF_DONATE, cb_donate.isChecked());

      Log.i("LC", "Settings: Pri " + (String) spn_priority.getSelectedItem());
      if (spn_priority.getSelectedItemPosition() == 0) {
        editor.putInt(PREF_PRIORITY, Thread.MIN_PRIORITY);
      }
      if (spn_priority.getSelectedItemPosition() == 1) {
        editor.putInt(PREF_PRIORITY, Thread.NORM_PRIORITY);
      }
      if (spn_priority.getSelectedItemPosition() == 2) {
        editor.putInt(PREF_PRIORITY, Thread.MAX_PRIORITY);
      }
      Log.i("LC", "Settings: Settings saved");
      editor.commit();
      Toast.makeText(getActivity(), "Settings Saved", Toast.LENGTH_SHORT).show();
    } else {
      Log.i("LC", "Settings: Invalid Input");
      Toast.makeText(getActivity(), "Settings: Errors changed to red", Toast.LENGTH_SHORT).show();
    }
    editor.commit();
  }
  public void loadDefault() {
    StringBuilder sb = new StringBuilder();
    et_serv.setText(sb.append(DEFAULT_URL).toString());
    sb.setLength(0);
    et_user.setText(sb.append(DEFAULT_USER).toString());
    sb.setLength(0);
    et_pass.setText(sb.append(DEFAULT_PASS).toString());
    sb.setLength(0);
    et_thread.setText(sb.append(DEFAULT_THREAD).toString());
    sb.setLength(0);
    et_scanTime.setText(sb.append(DEFAULT_SCANTIME).toString());
    sb.setLength(0);
    et_retryPause.setText(sb.append(DEFAULT_RETRYPAUSE).toString());
    sb.setLength(0);
    sb_throttle.setProgress((int) (DEFAULT_THROTTLE * 100));
    spn_priority.setSelection(0); // min priority

    cb_service.setChecked(DEFAULT_BACKGROUND);
    cb_donate.setChecked(DEFAULT_DONATE);
    Log.i("LC", "Settings: defaults loaded");

    Toast.makeText(getActivity(), "Defaults Loaded", Toast.LENGTH_SHORT).show();
  }
  public boolean verify() {
    boolean success = true;
    StringBuilder sb = new StringBuilder();
    // Attempt to verify url
    if (et_serv.getText() != null) {
      sb.append(et_serv.getText());
      try {
        URL url = new URL(sb.toString());
        et_serv.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
      } catch (MalformedURLException e) {
        Log.i("LC", "Settings Error: Invalid URL");
        et_serv.setShadowLayer(1, 1, 1, Color.RED);
        success = false;
      }
    }

    // Verify user
    if (et_user.getText() == null) {
      Log.i("LC", "Settings Error: no username");
      et_user.setShadowLayer(1, 1, 1, Color.RED);
      success = false;
    } else {
      et_user.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
    }

    // Verify password
    if (et_pass.getText() == null) {
      Log.i("LC", "Settings Error: no password");
      et_pass.setShadowLayer(1, 1, 1, Color.RED);
      success = false;
    } else {
      et_user.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
    }

    // Verify thread field
    if (et_thread.getText() == null) {
      Log.i("LC", "Settings Error: no threads");
      et_thread.setShadowLayer(1, 1, 1, Color.RED);
      success = false;
    } else {
      sb = new StringBuilder();
      sb.append(et_thread.getText());
      int thread = Integer.parseInt(sb.toString());
      if (thread < 1) {
        Log.i("LC", "Settings Error: must have atleast 1 thread");
        et_thread.setShadowLayer(1, 1, 1, Color.RED);
        success = false;
      } else {
        et_thread.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
      }
    }

    // Verify non-0 throttle
    if (sb_throttle.getProgress() == 0) {
      sb_throttle.setProgress(1);
    }

    // Verify scantime
    if (et_scanTime.getText() == null) {
      Log.i("LC", "Settings Error: no scan time");
      et_scanTime.setShadowLayer(1, 1, 1, Color.RED);
      success = false;
    } else {
      sb = new StringBuilder();
      sb.append(et_scanTime.getText());
      int scanTime = Integer.parseInt(sb.toString());
      if (scanTime < 1) {
        Log.i("LC", "Settings Error: scantime must be greater than 1ms");
        et_scanTime.setShadowLayer(1, 1, 1, Color.RED);
        success = false;
      } else {
        et_scanTime.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
      }
    }

    // Verify retry pause
    if (et_retryPause.getText() == null) {
      Log.i("LC", "Settings Error: no retry pause");
      et_retryPause.setShadowLayer(1, 1, 1, Color.RED);
      success = false;
    } else {
      sb = new StringBuilder();
      sb.append(et_retryPause.getText());
      int scanTime = Integer.parseInt(sb.toString());
      if (scanTime < 1) {
        Log.i("LC", "Settings Error: retry pause must be greater than 1ms");
        et_retryPause.setShadowLayer(1, 1, 1, Color.RED);
        success = false;
      } else {
        et_retryPause.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
      }
    }
    Log.i("LC", "Settings: Settings Verified");

    // warn about max priority threads
    if (spn_priority.getSelectedItemPosition() == 2) {
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setTitle("Warning");
      builder.setMessage("Setting to Thread.MAX_PRIORITY may cause instability");
      builder.setCancelable(false);
      builder.setPositiveButton(
          "OK",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              // do nothing
            }
          });
      builder.show();
    }

    return success;
  }