Example #1
0
public class City extends MapObject {
  public enum CityType {
    // that's tricky way to play with that numbers (to avoid including suburbs in city & vice verse)
    // district special type and it is not registered as a city
    CITY(10000),
    TOWN(5000),
    VILLAGE(1300),
    HAMLET(1000),
    SUBURB(400),
    DISTRICT(400),
    NEIGHBOURHOOD(300);

    private double radius;

    private CityType(double radius) {
      this.radius = radius;
    }

    public double getRadius() {
      return radius;
    }

    public static String valueToString(CityType t) {
      return t.toString().toLowerCase();
    }

    public static CityType valueFromString(String place) {
      if (place == null) {
        return null;
      }
      for (CityType t : CityType.values()) {
        if (t.name().equalsIgnoreCase(place)) {
          return t;
        }
      }
      return null;
    }
  }

  private CityType type = null;
  // Be attentive ! Working with street names ignoring case
  private Map<String, Street> streets =
      new TreeMap<String, Street>(OsmAndCollator.primaryCollator());
  private String isin = null;
  private String postcode = null;

  private static long POSTCODE_INTERNAL_ID = -1000;

  public static City createPostcode(String postcode) {
    return new City(postcode, POSTCODE_INTERNAL_ID--);
  }

  public City(CityType type) {
    if (type == null) {
      throw new NullPointerException();
    }
    this.type = type;
  }

  public City(String postcode, long id) {
    this.type = null;
    this.name = this.enName = postcode;
    this.id = id;
  }

  public String getIsInValue() {
    return isin;
  }

  public boolean isPostcode() {
    return type == null;
  }

  public boolean isEmptyWithStreets() {
    return streets.isEmpty();
  }

  public Street unregisterStreet(String name) {
    return streets.remove(name.toLowerCase());
  }

  public void removeAllStreets() {
    streets.clear();
  }

  public String getPostcode() {
    return postcode;
  }

  public void setPostcode(String postcode) {
    this.postcode = postcode;
  }

  protected Street registerStreet(Street street, boolean en) {
    String name = en ? street.getEnName() : street.getName();
    name = name.toLowerCase();
    if (!Algorithms.isEmpty(name)) {
      if (!streets.containsKey(name)) {
        return streets.put(name, street);
      } else {
        // try to merge streets
        Street prev = streets.get(name);
        prev.mergeWith(street);
        return prev;
      }
    }
    return null;
  }

  public Street registerStreet(Street street) {
    return registerStreet(street, false);
  }

  public CityType getType() {
    return type;
  }

  public Collection<Street> getStreets() {
    return streets.values();
  }

  public Street getStreet(String name) {
    return streets.get(name.toLowerCase());
  }

  @Override
  public String toString() {
    if (isPostcode()) {
      return "Postcode : " + getName(); // $NON-NLS-1$ //$NON-NLS-2$
    }
    return "City [" + type + "] " + getName(); // $NON-NLS-1$ //$NON-NLS-2$
  }

  public void setIsin(String isin) {
    this.isin = isin;
  }
}
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = ((OsmandApplication) getApplication()).getSettings();
    setContentView(R.layout.search_by_name);

    initializeTask = getInitializeTask();
    uiHandler = new UIUpdateHandler();
    namesFilter = new NamesFilter();
    addFooterViews();
    final NamesAdapter namesAdapter =
        new NamesAdapter(new ArrayList<T>(), createComparator()); // $NON-NLS-1$
    setListAdapter(namesAdapter);

    collator = OsmAndCollator.primaryCollator();

    progress = (ProgressBar) findViewById(R.id.ProgressBar);

    searchText = (EditText) findViewById(R.id.SearchText);
    searchText.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void afterTextChanged(Editable s) {
            String newFilter = s.toString();
            String newEndingText = endingText;
            if (newEndingText.length() > 0) {
              while (!newFilter.endsWith(newEndingText) && newEndingText.length() > 0) {
                newEndingText = newEndingText.substring(1);
              }
              newFilter = newFilter.substring(0, newFilter.length() - newEndingText.length());
            }
            updateTextBox(newFilter, newEndingText, endingObject, false);
            querySearch(newFilter);
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });
    // Not perfect
    //		filter.setOnClickListener(new OnClickListener() {
    //			}
    //		});
    searchText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    searchText.requestFocus();
    searchText.setOnEditorActionListener(
        new OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                || (actionId == EditorInfo.IME_ACTION_DONE)) {
              if (endingObject != null) {
                itemSelectedBase(endingObject, v);
              }
              return true;
            }
            return false;
          }
        });

    progress.setVisibility(View.INVISIBLE);
    findViewById(R.id.ResetButton)
        .setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                reset();
              }
            });
    selectAddress = getIntent() != null && getIntent().hasExtra(SELECT_ADDRESS);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    if (initializeTask != null) {
      initializeTask.execute();
    }
  }