Speed up data transition from SearchByNameAbstractActivity to parent.

This commit is contained in:
GaidamakUA 2016-06-02 12:40:23 +03:00
parent ed558cd2b0
commit 21b73be257
2 changed files with 59 additions and 45 deletions

View file

@ -72,6 +72,7 @@ public abstract class SearchByNameAbstractActivity<T> extends OsmandListActivity
protected static final int MESSAGE_CLEAR_LIST = OsmAndConstants.UI_HANDLER_SEARCH + 2;
protected static final int MESSAGE_ADD_ENTITY = OsmAndConstants.UI_HANDLER_SEARCH + 3;
protected static final int MESSAGE_ADD_ENTITIES = OsmAndConstants.UI_HANDLER_SEARCH + 4;
protected static final String SELECT_ADDRESS = "SEQUENTIAL_SEARCH";
protected ProgressBar progress;
@ -402,29 +403,38 @@ public abstract class SearchByNameAbstractActivity<T> extends OsmandListActivity
}
updateTextBox(currentFilter, "", null, true);
} else if(msg.what == MESSAGE_ADD_ENTITY){
getListAdapter().add((T) msg.obj);
if (currentFilter.length() > 0) {
String shortText = getShortText((T) msg.obj);
int entries = !endingMap.containsKey(shortText) ? 0 : endingMap.get(shortText);
if (entries < minimalIndex) {
if(minimalText != null) {
endingMap.put(minimalText, endingMap.get(minimalText) - 1);
}
minimalIndex = entries;
minimalText = shortText;
endingMap.put(shortText, entries + 1);
String locEndingText;
if (shortText.toLowerCase().startsWith(currentFilter.toLowerCase())) {
locEndingText = shortText.substring(currentFilter.length());
} else {
locEndingText = " - " + shortText;
}
if (locEndingText.length() > MAX_VISIBLE_NAME) {
locEndingText = locEndingText.substring(0, MAX_VISIBLE_NAME) + "..";
}
updateTextBox(currentFilter, locEndingText, (T) msg.obj, true);
final Object obj = msg.obj;
addObjectToAdapter(currentFilter, (T) obj);
} else if (msg.what == MESSAGE_ADD_ENTITIES) {
final List<T> objects = (List<T>) msg.obj;
for (T object : objects) {
addObjectToAdapter(currentFilter, object);
}
}
}
private void addObjectToAdapter(String currentFilter, T obj) {
getListAdapter().add(obj);
if (currentFilter.length() > 0) {
String shortText = getShortText(obj);
int entries = !endingMap.containsKey(shortText) ? 0 : endingMap.get(shortText);
if (entries < minimalIndex) {
if(minimalText != null) {
endingMap.put(minimalText, endingMap.get(minimalText) - 1);
}
minimalIndex = entries;
minimalText = shortText;
endingMap.put(shortText, entries + 1);
String locEndingText;
if (shortText.toLowerCase().startsWith(currentFilter.toLowerCase())) {
locEndingText = shortText.substring(currentFilter.length());
} else {
locEndingText = " - " + shortText;
}
if (locEndingText.length() > MAX_VISIBLE_NAME) {
locEndingText = locEndingText.substring(0, MAX_VISIBLE_NAME) + "..";
}
updateTextBox(currentFilter, locEndingText, obj, true);
}
}
}

View file

@ -1,10 +1,13 @@
package net.osmand.plus.activities.search;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import android.os.AsyncTask;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
@ -21,13 +24,11 @@ import net.osmand.plus.activities.search.SearchAddressFragment.AddressInformatio
import net.osmand.plus.resources.RegionAddressRepository;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import android.os.AsyncTask;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
public class SearchStreetByNameActivity extends SearchByNameAbstractActivity<Street> {
private RegionAddressRepository region;
@ -172,27 +173,30 @@ public class SearchStreetByNameActivity extends SearchByNameAbstractActivity<Str
private void filter(String query, Collection<Street> list) {
boolean emptyQuery = query == null || query.length() == 0;
List<Street> streetsToSend = new ArrayList<>();
for (Street obj : list) {
if (namesFilter.isCancelled) {
break;
}
if (emptyQuery || CollatorStringMatcher.cmatches(collator, obj.getNameWithoutCityPart(settings.MAP_PREFERRED_LOCALE.get()),
query, StringMatcherMode.CHECK_ONLY_STARTS_WITH)) {
Message msg = uiHandler.obtainMessage(MESSAGE_ADD_ENTITY, obj);
msg.sendToTarget();
streetsToSend.add(obj);
}
if (!emptyQuery && CollatorStringMatcher.cmatches(collator, obj.getNameWithoutCityPart(settings.MAP_PREFERRED_LOCALE.get()),
query, StringMatcherMode.CHECK_STARTS_FROM_SPACE_NOT_BEGINNING)) {
streetsToSend.add(obj);
}
}
if (!emptyQuery) {
for (Street obj : list) {
if (namesFilter.isCancelled) {
break;
}
if (CollatorStringMatcher.cmatches(collator, obj.getNameWithoutCityPart(settings.MAP_PREFERRED_LOCALE.get()),
query, StringMatcherMode.CHECK_STARTS_FROM_SPACE_NOT_BEGINNING)) {
Message msg = uiHandler.obtainMessage(MESSAGE_ADD_ENTITY, obj);
msg.sendToTarget();
}
final int loopSize = 100;
for (int i = 0; i < streetsToSend.size(); i += loopSize) {
int endIndex = i + loopSize;
if (endIndex > streetsToSend.size()) {
endIndex = streetsToSend.size();
}
List<Street> listToSend = streetsToSend.subList(i, endIndex);
Message msg = uiHandler.obtainMessage(MESSAGE_ADD_ENTITIES, listToSend);
msg.sendToTarget();
}
}