Skip to content

Commit

Permalink
Added a helper for handling the "find me" functionality when theres a…
Browse files Browse the repository at this point in the history
… page reset due to the out of memory workaround.
  • Loading branch information
kzusy committed Feb 5, 2014
1 parent e27a615 commit 61f382e
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 54 deletions.
6 changes: 6 additions & 0 deletions Arbiter-Android/assets/www/js/Arbiter/Cordova/Cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ Arbiter.Cordova = (function() {

cordova.exec(null, null, "ArbiterCordova",
"dismissDownloadingSchemasProgress", []);
},

alertGeolocationError: function(msg){

cordova.exec(null, null, "ArbiterCordova",
"alertGeolocationError", [msg]);
}
};
})();
4 changes: 3 additions & 1 deletion Arbiter-Android/assets/www/js/Arbiter/Cordova/Project.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Arbiter.Cordova.Project = (function(){
var describeFeatureTypeReader = new OpenLayers.Format.WFSDescribeFeatureType();

var includeOOMWorkaround = true;

var syncInProgress = false;

var getSchemaHelper = function(specificSchemas, layerId){
Expand Down Expand Up @@ -239,7 +241,7 @@ Arbiter.Cordova.Project = (function(){
throw "AOI layer does not exist";
}

var findMe = new Arbiter.FindMe(map, aoiLayer);
var findMe = new Arbiter.FindMe(map, aoiLayer, includeOOMWorkaround);

findMe.findMe();
}catch(e){
Expand Down
98 changes: 84 additions & 14 deletions Arbiter-Android/assets/www/js/Arbiter/Geolocation/FindMe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Arbiter.FindMe = function(olMap, olLayer){
Arbiter.FindMe = function(olMap, olLayer, includeOOM){
this.olMap = olMap;
this.gotHighAccuracy = false;
this.olLayer = olLayer;

if(includeOOM){
this.oom = new Arbiter.FindMe_OOM();
}

this.minimumFindMeZoom = 18;
this.point = null;

Expand Down Expand Up @@ -29,6 +34,16 @@ Arbiter.FindMe = function(olMap, olLayer){
externalGraphic: this.smallBallHighAccuracy,
pointRadius: this.smallRadius
};

var context = this;

this.geolocation = new Arbiter.Geolocation(this, this.lowAccuracyCallback,
this.highAccuracyCallback, function(e){

if(Arbiter.Util.funcExists(context.onFailure)){
context.onFailure(e);
}
});
};

Arbiter.FindMe.prototype.zoom = function(position){
Expand Down Expand Up @@ -70,14 +85,20 @@ Arbiter.FindMe.prototype.addPoint = function(position, style){
var context = this;

this.removePointTimeoutId = window.setTimeout(function(){
console.log("remove point timeout");

context.removePoint();

if(Arbiter.Util.existsAndNotNull(context.oom)){

context.oom.clearSavedPoint(function(){
console.log("FindMe removed saved point");
}, function(e){
console.log("FindMe could not removed saved point: " + JSON.stringify(e));
});
}
}, this.removePointTimeout);

this.styleChangeIntervalId = window.setInterval(function(){

console.log("change style interval");

var ball = style.externalGraphic;
var radius = style.pointRadius;
Expand Down Expand Up @@ -132,15 +153,34 @@ Arbiter.FindMe.prototype.removePoint = function(){
this.point = null;
};

Arbiter.FindMe.prototype.savePosition = function(position){
var context = this;

this.oom.savePoint(this.gotHighAccuracy,
position, function(){

context.zoom(position);
}, function(e){

context.zoom(position);
});
};

Arbiter.FindMe.prototype.lowAccuracyCallback = function(position){

if(this.gotHighAccuracy){
return;
}

this.addPoint(position, this.lowAccuracyPointStyle);

this.zoom(position);

if(Arbiter.Util.existsAndNotNull(this.oom)){

this.savePosition(position);
}else{

this.zoom(position);
}
};

Arbiter.FindMe.prototype.highAccuracyCallback = function(position){
Expand All @@ -149,24 +189,54 @@ Arbiter.FindMe.prototype.highAccuracyCallback = function(position){

this.addPoint(position, this.highAccuracyPointStyle);

this.zoom(position);
if(Arbiter.Util.existsAndNotNull(this.oom)){

this.savePosition(position);
}else{

this.zoom(position);
}
};

Arbiter.FindMe.prototype.onFailure = function(e){

console.log("FindMe could not get location");
var msg = "FindMe could not get location: " + JSON.stringify(e);

console.log(msg);

Arbiter.Cordova.alertGeolocationError(msg);
};

Arbiter.FindMe.prototype.findMe = function(){
this.geolocation.getLocation();
};

Arbiter.FindMe.prototype.resume = function(){

if(!Arbiter.Util.existsAndNotNull(this.oom)){
return;
}

var context = this;

var geolocation = new Arbiter.Geolocation(this, this.lowAccuracyCallback,
this.highAccuracyCallback, function(e){
this.oom.getPoint(function(findme){

if(Arbiter.Util.funcExists(context.onFailure)){
context.onFailure(e);
if(Arbiter.Util.existsAndNotNull(findme)
&& Arbiter.Util.existsAndNotNull(findme.gotHighAccuracy)
&& Arbiter.Util.existsAndNotNull(findme.position)){


if(!findme.gotHighAccuracy){

context.addPoint(findme.position, context.lowAccuracyPointStyle);

context.geolocation.getCurrentLocationHighAccuracy();
}else{

context.addPoint(findme.position, context.highAccuracyPointStyle);
}
}
}, function(e){
console.log("FindMe error loading saved position: " + JSON.stringify(e));
});

geolocation.getLocation();
};
80 changes: 80 additions & 0 deletions Arbiter-Android/assets/www/js/Arbiter/Geolocation/FindMe_OOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Arbiter.SavedFindMe = function(gotHighAccuracy, position){
this.gotHighAccuracy = gotHighAccuracy;
this.position = position;
};

Arbiter.FindMe_OOM = function(){

};

Arbiter.FindMe_OOM.prototype.FINDME = "findme";

Arbiter.FindMe_OOM.prototype.savePoint = function(gotHighAccuracy, position, onSuccess, onFailure){
var obj = new Arbiter.SavedFindMe(gotHighAccuracy, position);

console.log("saving point: " + obj.gotHighAccuracy);

Arbiter.PreferencesHelper.put(this.FINDME,
JSON.stringify(obj), this, function(){

console.log("point saved successfully: " + obj.gotHighAccuracy);
if(Arbiter.Util.funcExists(onSuccess)){
onSuccess();
}
}, function(e){

console.log("could not save point: " + obj.gotHighAccuracy);
if(Arbiter.Util.funcExists(onFailure)){
onFailure(e);
}
});
};

Arbiter.FindMe_OOM.prototype.getPoint = function(onSuccess, onFailure){
var context = this;

var success = function(findme){
if(Arbiter.Util.funcExists(onSuccess)){

if(Arbiter.Util.existsAndNotNull(findme)){
onSuccess(JSON.parse(findme));
}else{
onSuccess(findme);
}
}
};

Arbiter.PreferencesHelper.get(this.FINDME, this, function(findme){

context.clearSavedPoint(function(){

success(findme);
}, function(e){

console.log("Could not remove findme from preferences table: " + JSON.stringify(e));

success(findme);
});

}, function(e){

if(Arbiter.Util.funcExists(onFailure)){
onFailure(e);
}
});
};

Arbiter.FindMe_OOM.prototype.clearSavedPoint = function(onSuccess, onFailure){

Arbiter.PreferencesHelper.remove(this.FINDME, this, function(){

if(Arbiter.Util.funcExists(onSuccess)){
onSuccess();
}
}, function(e){

if(Arbiter.Util.funcExists(onFailure)){
onFailure(e);
}
});
};
Loading

0 comments on commit 61f382e

Please sign in to comment.