private List<Server> getServersFromSQLite() {
    List<Server> servers = new ArrayList<Server>();

    dataSource.open();
    List<Server> values = dataSource.getMostRecentServers();
    String shown = "";
    for (Server s : values) {
      shown += s.getRegion() + s.getDate().toString() + "\n";
      servers.add(new Server(s));
    }
    Log.d(OTPApp.TAG, shown);
    dataSource.close();

    dataSource.open();
    Calendar someDaysBefore = Calendar.getInstance();
    someDaysBefore.add(Calendar.DAY_OF_MONTH, -OTPApp.EXPIRATION_DAYS_FOR_SERVER_LIST);
    Long serversUpdateDate = dataSource.getMostRecentDate();
    if ((serversUpdateDate != null) && (someDaysBefore.getTime().getTime() > serversUpdateDate)) {
      servers = null;
    }
    dataSource.close();
    //		Toast.makeText(activity.getApplicationContext(), shown, Toast.LENGTH_SHORT).show();

    return servers;
  }
 @Override
 public void onServerCheckerComplete(
     String result, boolean isCustomServer, boolean isAutoDetected, boolean isWorking) {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
   SharedPreferences.Editor prefsEditor = prefs.edit();
   if (isCustomServer) {
     if (isWorking) {
       prefsEditor.putBoolean(PREFERENCE_KEY_AUTO_DETECT_SERVER, false);
       prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, true);
       prefsEditor.putBoolean(PREFERENCE_KEY_CUSTOM_SERVER_URL_IS_VALID, true);
       prefsEditor.commit();
       if (selectedCustomServer) {
         String baseURL = prefs.getString(PREFERENCE_KEY_CUSTOM_SERVER_URL, "");
         selectedServer = new Server(baseURL, context);
         callback.onServerSelectorComplete(selectedServer);
       }
     } else {
       prefsEditor.putBoolean(PREFERENCE_KEY_CUSTOM_SERVER_URL_IS_VALID, false);
       prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, false);
       prefsEditor.commit();
       Toast.makeText(
               context,
               context.getResources().getString(R.string.toast_server_checker_error_bad_url),
               Toast.LENGTH_SHORT)
           .show();
     }
   } else {
     if (isWorking) {
       if (isAutoDetected) {
         long serverId = prefs.getLong(OTPApp.PREFERENCE_KEY_SELECTED_SERVER, 0);
         Server s = null;
         boolean serverIsChanged = true;
         if (serverId != 0) {
           dataSource.open();
           s = dataSource.getServer(prefs.getLong(OTPApp.PREFERENCE_KEY_SELECTED_SERVER, 0));
           dataSource.close();
         }
         if (s != null) {
           serverIsChanged = !(s.getRegion().equals(selectedServer.getRegion()));
         }
         if (showDialog || serverIsChanged) {
           Toast.makeText(
                   context,
                   context.getResources().getString(R.string.toast_server_selector_detected)
                       + " "
                       + selectedServer.getRegion()
                       + ". "
                       + context
                           .getResources()
                           .getString(R.string.toast_server_selector_server_change_info),
                   Toast.LENGTH_SHORT)
               .show();
         }
       }
       prefsEditor.putLong(PREFERENCE_KEY_SELECTED_SERVER, selectedServer.getId());
       prefsEditor.putBoolean(PREFERENCE_KEY_SELECTED_CUSTOM_SERVER, false);
       prefsEditor.commit();
       callback.onServerSelectorComplete(selectedServer);
     } else {
       Toast.makeText(
               context,
               context
                   .getResources()
                   .getString(R.string.toast_server_checker_error_unreachable_detected_server),
               Toast.LENGTH_SHORT)
           .show();
     }
   }
 }
  protected void onPostExecute(Integer result) {
    if ((activity.get() != null) && showDialog) {
      try {
        if (progressDialog != null && progressDialog.isShowing()) {
          progressDialog.dismiss();
        }
      } catch (Exception e) {
        Log.e(OTPApp.TAG, "Error in Server Selector PostExecute dismissing dialog: " + e);
      }
    }

    if (selectedServer != null) {
      // We've already auto-selected a server
      ServerChecker serverChecker =
          new ServerChecker(activity, context, ServerSelector.this, false, false, true);
      serverChecker.execute(selectedServer);
    } else if (knownServers != null && !knownServers.isEmpty()) {
      Log.d(
          OTPApp.TAG,
          "No server automatically selected.  User will need to choose the OTP server.");

      // Create dialog for user to choose
      List<String> serverNames = new ArrayList<String>();
      for (Server server : knownServers) {
        serverNames.add(server.getRegion());
      }

      Collections.sort(serverNames);

      serverNames.add(
          0, context.getResources().getString(R.string.server_checker_info_custom_server_name));

      final CharSequence[] items = serverNames.toArray(new CharSequence[serverNames.size()]);

      Activity activityRetrieved = activity.get();

      if (activityRetrieved != null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activityRetrieved);
        builder.setTitle(context.getResources().getString(R.string.server_checker_info_title));
        builder.setItems(
            items,
            new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int item) {

                // If the user selected to enter a custom URL, they are shown this EditText box to
                // enter it
                if (items[item].equals(
                    context
                        .getResources()
                        .getString(R.string.server_checker_info_custom_server_name))) {
                  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

                  Activity activityRetrieved = activity.get();

                  if (activityRetrieved != null) {
                    final EditText tbBaseURL = new EditText(activityRetrieved);
                    String actualCustomServer =
                        prefs.getString(PREFERENCE_KEY_CUSTOM_SERVER_URL, "");
                    tbBaseURL.setText(actualCustomServer);

                    AlertDialog.Builder urlAlert = new AlertDialog.Builder(activityRetrieved);
                    urlAlert.setTitle(
                        context
                            .getResources()
                            .getString(R.string.server_selector_custom_server_alert_title));
                    urlAlert.setView(tbBaseURL);
                    urlAlert.setPositiveButton(
                        context.getResources().getString(android.R.string.ok),
                        new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int whichButton) {
                            Editable tbEditable = tbBaseURL.getText();
                            if (tbEditable != null) {
                              String value = tbEditable.toString().trim();
                              if (URLUtil.isValidUrl(value)) {
                                SharedPreferences.Editor prefsEditor =
                                    PreferenceManager.getDefaultSharedPreferences(context).edit();
                                prefsEditor.putString(PREFERENCE_KEY_CUSTOM_SERVER_URL, value);

                                ServerChecker serverChecker =
                                    new ServerChecker(
                                        activity, context, ServerSelector.this, true, true, false);
                                serverChecker.execute(new Server(value, context));
                                prefsEditor.commit();
                              } else {
                                Toast.makeText(
                                        context,
                                        context
                                            .getResources()
                                            .getString(
                                                R.string
                                                    .settings_menu_custom_server_url_description_error_url),
                                        Toast.LENGTH_SHORT)
                                    .show();
                              }
                            }
                          }
                        });
                    selectedCustomServer = true;
                    urlAlert.create().show();
                  }
                } else {
                  // User picked server from the list
                  for (Server server : knownServers) {
                    // If this server region matches what the user picked, then set the server as
                    // the selected server
                    if (server.getRegion().equals(items[item])) {
                      selectedServer = server;
                      ServerChecker serverChecker =
                          new ServerChecker(
                              activity, context, ServerSelector.this, false, false, false);
                      serverChecker.execute(selectedServer);
                      break;
                    }
                  }
                }
                Log.d(OTPApp.TAG, "Chosen: " + items[item]);
              }
            });
        builder.show();
      }
    } else {
      Log.e(OTPApp.TAG, "Server list could not be downloaded!!");
      Toast.makeText(
              context,
              context
                  .getResources()
                  .getString(R.string.toast_server_selector_refresh_server_list_error),
              Toast.LENGTH_SHORT)
          .show();
    }
  }