Save asynchrone local edition in a local db
This commit is contained in:
parent
032130721e
commit
83fd560681
5 changed files with 308 additions and 14 deletions
92
OsmAnd/src/net/osmand/OpenstreetmapLocalUtil.java
Normal file
92
OsmAnd/src/net/osmand/OpenstreetmapLocalUtil.java
Normal file
|
@ -0,0 +1,92 @@
|
|||
package net.osmand;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.osmand.OpenstreetmapPoint;
|
||||
import net.osmand.data.Amenity;
|
||||
import net.osmand.data.AmenityType;
|
||||
import net.osmand.osm.Entity;
|
||||
import net.osmand.osm.Entity.EntityId;
|
||||
import net.osmand.osm.Entity.EntityType;
|
||||
import net.osmand.osm.EntityInfo;
|
||||
import net.osmand.osm.MapRenderingTypes;
|
||||
import net.osmand.osm.MapUtils;
|
||||
import net.osmand.osm.Node;
|
||||
import net.osmand.osm.OSMSettings.OSMTagKey;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.OpenstreetmapsDbHelper;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import android.util.Xml;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class OpenstreetmapLocalUtil implements OpenstreetmapUtil {
|
||||
|
||||
private final MapActivity ctx;
|
||||
private final OpenstreetmapsDbHelper db;
|
||||
|
||||
public final static Log log = LogUtil.getLog(OpenstreetmapLocalUtil.class);
|
||||
|
||||
public OpenstreetmapLocalUtil(MapActivity uiContext){
|
||||
this.ctx = uiContext;
|
||||
this.db = new OpenstreetmapsDbHelper(ctx);
|
||||
}
|
||||
|
||||
public EntityInfo getEntityInfo() {
|
||||
return new EntityInfo();
|
||||
}
|
||||
|
||||
public boolean commitNodeImpl(Action action, Node n, EntityInfo info, String comment){
|
||||
OpenstreetmapPoint p = new OpenstreetmapPoint();
|
||||
p.setEntity(n);
|
||||
p.setAction(action);
|
||||
p.setComment(comment);
|
||||
return db.addOpenstreetmap(p);
|
||||
}
|
||||
|
||||
public Node loadNode(Amenity n) {
|
||||
if(n.getId() % 2 == 1){
|
||||
// that's way id
|
||||
return null;
|
||||
}
|
||||
long nodeId = n.getId() >> 1;
|
||||
|
||||
EntityId id = new Entity.EntityId(EntityType.NODE, nodeId);
|
||||
Node entity = new Node(n.getLocation().getLatitude(),
|
||||
n.getLocation().getLongitude(),
|
||||
nodeId);
|
||||
|
||||
Map<AmenityType, Map<String, String>> typeNameToTagVal = MapRenderingTypes.getDefault().getAmenityTypeNameToTagVal();
|
||||
AmenityType type = n.getType();
|
||||
String tag = type.getDefaultTag();
|
||||
String subType = n.getSubType();
|
||||
String val = subType;
|
||||
if (typeNameToTagVal.containsKey(type)) {
|
||||
Map<String, String> map = typeNameToTagVal.get(type);
|
||||
if (map.containsKey(subType)) {
|
||||
String res = map.get(subType);
|
||||
if (res != null) {
|
||||
int i = res.indexOf(' ');
|
||||
if (i != -1) {
|
||||
tag = res.substring(0, i);
|
||||
val = res.substring(i + 1);
|
||||
} else {
|
||||
tag = res;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
entity.putTag(tag, val);
|
||||
entity.putTag(OSMTagKey.NAME.getValue(), n.getName());
|
||||
entity.putTag(OSMTagKey.OPENING_HOURS.getValue(), n.getOpeningHours());
|
||||
|
||||
// check whether this is node (because id of node could be the same as relation)
|
||||
if(entity != null && MapUtils.getDistance(entity.getLatLon(), n.getLocation()) < 50){
|
||||
return entity;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
91
OsmAnd/src/net/osmand/OpenstreetmapPoint.java
Normal file
91
OsmAnd/src/net/osmand/OpenstreetmapPoint.java
Normal file
|
@ -0,0 +1,91 @@
|
|||
package net.osmand;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import net.osmand.OpenstreetmapRemoteUtil;
|
||||
import net.osmand.data.AmenityType;
|
||||
import net.osmand.osm.Node;
|
||||
import net.osmand.osm.OSMSettings.OSMTagKey;
|
||||
|
||||
public class OpenstreetmapPoint implements Serializable {
|
||||
private static final long serialVersionUID = 729654300829771467L;
|
||||
private Node entity;
|
||||
private OpenstreetmapUtil.Action action;
|
||||
private String comment;
|
||||
private boolean stored = false;
|
||||
|
||||
public OpenstreetmapPoint(){
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return entity.getTag(OSMTagKey.NAME.getValue());
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
String type = AmenityType.valueToString(AmenityType.OTHER);
|
||||
for(String k : entity.getTagKeySet()){
|
||||
if (!OSMTagKey.NAME.getValue().equals(k) &&
|
||||
!OSMTagKey.OPENING_HOURS.getValue().equals(k)) {
|
||||
type = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getSubtype() {
|
||||
return entity.getTag(this.getType());
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return entity.getLatitude();
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return entity.getLongitude();
|
||||
}
|
||||
|
||||
public String getOpeninghours() {
|
||||
return entity.getTag(OSMTagKey.OPENING_HOURS.getValue());
|
||||
}
|
||||
|
||||
public Node getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public OpenstreetmapUtil.Action getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public boolean isStored() {
|
||||
return stored;
|
||||
}
|
||||
|
||||
public void setEntity(Node entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = OpenstreetmapRemoteUtil.actionString.get(action);
|
||||
}
|
||||
|
||||
public void setAction(OpenstreetmapUtil.Action action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public void setStored(boolean stored) {
|
||||
this.stored = stored;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,9 @@ import java.net.MalformedURLException;
|
|||
import java.net.URL;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.osmand.data.Amenity;
|
||||
import net.osmand.osm.Entity;
|
||||
|
@ -52,7 +54,19 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
|
|||
|
||||
// private final static String SITE_API = "http://api06.dev.openstreetmap.org/";
|
||||
private final static String SITE_API = "http://api.openstreetmap.org/"; //$NON-NLS-1$
|
||||
|
||||
|
||||
public static final Map<Action, String> stringAction = new HashMap<Action, String>();
|
||||
public static final Map<String, Action> actionString = new HashMap<String, Action>();
|
||||
static {
|
||||
stringAction.put(Action.CREATE, "create");
|
||||
stringAction.put(Action.MODIFY, "modify");
|
||||
stringAction.put(Action.DELETE, "delete");
|
||||
|
||||
actionString.put("create", Action.CREATE);
|
||||
actionString.put("modify", Action.MODIFY);
|
||||
actionString.put("delete", Action.DELETE);
|
||||
};
|
||||
|
||||
private static final long NO_CHANGESET_ID = -1;
|
||||
|
||||
private final MapActivity ctx;
|
||||
|
@ -304,16 +318,6 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
final String a;
|
||||
if (Action.MODIFY == action) {
|
||||
a = "modify";
|
||||
} else if (Action.DELETE == action) {
|
||||
a = "delete";
|
||||
} else {
|
||||
a = "create";
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
StringWriter writer = new StringWriter(256);
|
||||
XmlSerializer ser = Xml.newSerializer();
|
||||
|
@ -323,11 +327,11 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil {
|
|||
ser.startTag(null, "osmChange"); //$NON-NLS-1$
|
||||
ser.attribute(null, "version", "0.6"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
ser.attribute(null, "generator", Version.APP_NAME); //$NON-NLS-1$
|
||||
ser.startTag(null, a);
|
||||
ser.startTag(null, stringAction.get(action));
|
||||
ser.attribute(null, "version", "0.6"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
ser.attribute(null, "generator", Version.APP_NAME); //$NON-NLS-1$
|
||||
writeNode(n, info, ser, changeSetId, OsmandSettings.getOsmandSettings(ctx).USER_NAME.get());
|
||||
ser.endTag(null, a);
|
||||
ser.endTag(null, stringAction.get(action));
|
||||
ser.endTag(null, "osmChange"); //$NON-NLS-1$
|
||||
ser.endDocument();
|
||||
} catch (IOException e) {
|
||||
|
|
100
OsmAnd/src/net/osmand/plus/OpenstreetmapsDbHelper.java
Normal file
100
OsmAnd/src/net/osmand/plus/OpenstreetmapsDbHelper.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
package net.osmand.plus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.osmand.OpenstreetmapPoint;
|
||||
import net.osmand.OpenstreetmapRemoteUtil;
|
||||
import net.osmand.osm.Node;
|
||||
import net.osmand.osm.OSMSettings.OSMTagKey;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
public class OpenstreetmapsDbHelper extends SQLiteOpenHelper {
|
||||
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
public static final String OPENSTREETMAP_DB_NAME = "openstreetmap"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_TABLE_NAME = "openstreetmap"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_ID = "id"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_NAME = "name"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_TYPE = "type"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_SUBTYPE = "subtype"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_LAT = "latitude"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_LON = "longitude"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_ACTION = "action"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_COMMENT = "comment"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_COL_OPENINGHOURS = "openinghours"; //$NON-NLS-1$
|
||||
private static final String OPENSTREETMAP_TABLE_CREATE = "CREATE TABLE " + OPENSTREETMAP_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
|
||||
OPENSTREETMAP_COL_ID + " INTEGER, " + OPENSTREETMAP_COL_NAME + " TEXT, " + OPENSTREETMAP_COL_TYPE + " TEXT, " + OPENSTREETMAP_COL_SUBTYPE + " TEXT, " + //$NON-NLS-1$ //$NON-NLS-2$
|
||||
OPENSTREETMAP_COL_LAT + " double, " + OPENSTREETMAP_COL_LON + " double, " + //$NON-NLS-1$ //$NON-NLS-2$
|
||||
OPENSTREETMAP_COL_ACTION + " TEXT, " + OPENSTREETMAP_COL_COMMENT + " TEXT, " + OPENSTREETMAP_COL_OPENINGHOURS + " TEXT);"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
private List<OpenstreetmapPoint> cachedOpenstreetmapPoints = new ArrayList<OpenstreetmapPoint>();
|
||||
private final Context context;
|
||||
|
||||
public OpenstreetmapsDbHelper(Context context) {
|
||||
super(context, OPENSTREETMAP_DB_NAME, null, DATABASE_VERSION);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL(OPENSTREETMAP_TABLE_CREATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
}
|
||||
|
||||
public List<OpenstreetmapPoint> getOpenstreetmapPoints() {
|
||||
checkOpenstreetmapPoints();
|
||||
return cachedOpenstreetmapPoints;
|
||||
}
|
||||
|
||||
public boolean addOpenstreetmap(OpenstreetmapPoint p) {
|
||||
checkOpenstreetmapPoints();
|
||||
SQLiteDatabase db = getWritableDatabase();
|
||||
if (db != null) {
|
||||
db.execSQL("INSERT INTO " + OPENSTREETMAP_TABLE_NAME +
|
||||
" (" + OPENSTREETMAP_COL_ID + ", " + OPENSTREETMAP_COL_NAME + ", " + OPENSTREETMAP_COL_TYPE + ", " + OPENSTREETMAP_COL_SUBTYPE + ", " + OPENSTREETMAP_COL_LAT + "," + OPENSTREETMAP_COL_LON + "," + OPENSTREETMAP_COL_ACTION + "," + OPENSTREETMAP_COL_COMMENT + "," + OPENSTREETMAP_COL_OPENINGHOURS + ")" +
|
||||
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] { p.getId(), p.getName(), p.getType(), p.getSubtype(), p.getLatitude(), p.getLongitude(), OpenstreetmapRemoteUtil.stringAction.get(p.getAction()), p.getComment(), p.getOpeninghours() }); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
cachedOpenstreetmapPoints.add(p);
|
||||
p.setStored(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void checkOpenstreetmapPoints(){
|
||||
SQLiteDatabase db = getWritableDatabase();
|
||||
if (db != null) {
|
||||
Cursor query = db.rawQuery("SELECT " + OPENSTREETMAP_COL_ID + ", " + OPENSTREETMAP_COL_NAME + ", " + OPENSTREETMAP_COL_TYPE + ", " + OPENSTREETMAP_COL_SUBTYPE + ", " + OPENSTREETMAP_COL_LAT + "," + OPENSTREETMAP_COL_LON + "," + OPENSTREETMAP_COL_ACTION + "," + OPENSTREETMAP_COL_COMMENT + "," + OPENSTREETMAP_COL_OPENINGHOURS + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
OPENSTREETMAP_TABLE_NAME, null);
|
||||
cachedOpenstreetmapPoints.clear();
|
||||
if (query.moveToFirst()) {
|
||||
do {
|
||||
String name = query.getString(1);
|
||||
OpenstreetmapPoint p = new OpenstreetmapPoint();
|
||||
Node entity = new Node(query.getDouble(4),
|
||||
query.getDouble(5),
|
||||
query.getLong(0));
|
||||
|
||||
entity.putTag(query.getString(2), query.getString(3));
|
||||
entity.putTag(OSMTagKey.NAME.getValue(), name);
|
||||
entity.putTag(OSMTagKey.OPENING_HOURS.getValue(), query.getString(8));
|
||||
p.setEntity(entity);
|
||||
p.setStored(true);
|
||||
p.setAction(query.getString(6));
|
||||
p.setComment(query.getString(7));
|
||||
} while (query.moveToNext());
|
||||
}
|
||||
query.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -7,6 +7,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.osmand.OpenstreetmapLocalUtil;
|
||||
import net.osmand.OpenstreetmapRemoteUtil;
|
||||
import net.osmand.OpenstreetmapUtil;
|
||||
import net.osmand.OsmAndFormatter;
|
||||
|
@ -69,7 +70,13 @@ public class EditingPOIActivity implements DialogProvider {
|
|||
|
||||
public EditingPOIActivity(MapActivity uiContext){
|
||||
this.ctx = uiContext;
|
||||
this.openstreetmapUtil = new OpenstreetmapRemoteUtil(uiContext);
|
||||
|
||||
OsmandSettings settings = OsmandSettings.getOsmandSettings(ctx);
|
||||
if(!settings.isInternetConnectionAvailable(true)){
|
||||
this.openstreetmapUtil = new OpenstreetmapLocalUtil(ctx);
|
||||
} else {
|
||||
this.openstreetmapUtil = new OpenstreetmapRemoteUtil(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
public void showEditDialog(Amenity editA){
|
||||
|
|
Loading…
Reference in a new issue