Sunday, 15 May 2011

Where Am I source code







Feel free to take this source away for your learning in webOS.


function WAIAssistant() {
/*
* Author : Peter Wong
* Website : petkatan.blogspot.com
*/
}


WAIAssistant.prototype.setup = function() {
// Set orientation
if (this.controller.stageController.setWindowOrientation) {
this.controller.stageController.setWindowOrientation("free")
};

// Setup Spinner while getting a GPS fix and set to false until got a GPS fix
this.controller.setupWidget("divSpinner", { spinnerSize : "large" }, { spinning : true });

// Latitude and Longitude (set to read-only)
this.attributes = { maxLength : 20 };
this.model = {'txtLatitudeModel' : '', disabled: true };
this.controller.setupWidget("txtLatitude", this.attributes, this.model);

this.attributes = { maxLength : 20 };
this.model = {'txtLongitudeModel' : '', disabled: true };
this.controller.setupWidget("txtLongitude", this.attributes, this.model);

// Address (set to multiline)
this.attributes = { maxLength : 50, holdToEdit : true };
this.model = {'txtAddressModel' : '' , disabled: false };
this.controller.setupWidget("txtAddress", this.attributes, this.model);

// Mobile Phone number (set to editable and set numlock on)
this.attributes = { modifierState : Mojo.Widget.numLock, maxLength : 20,
focusMode : Mojo.Widget.focusInsertMode, holdToEdit : true };
this.model = {'txtMobileModel' : '' };
this.controller.setupWidget("txtMobile", this.attributes, this.model);

// SMS button
this.controller.setupWidget("btnSMS", { }, { label : "SMS" } );
Mojo.Event.listen(this.controller.get("btnSMS"), Mojo.Event.tap, this.onSMSTap.bind(this));

// WAI button
this.controller.setupWidget("btnWAI", { }, { label : "WAI" } );
Mojo.Event.listen(this.controller.get("btnWAI"), Mojo.Event.tap, this.onWAITap.bind(this));

// Map button
this.controller.setupWidget("btnMap", { }, { label : "Map" } );
Mojo.Event.listen(this.controller.get("btnMap"), Mojo.Event.tap, this.onMapTap.bind(this));
};


WAIAssistant.prototype.getAddress = function(){
// Get latitude and longitude and store somewhere
this.gps_latitude = this.controller.get('txtLatitude').mojo.getValue();
this.gps_longitude = this.controller.get('txtLongitude').mojo.getValue();
/*if (this.gps_latitude == "" || this.gps_longitude == "") {
return;
}*/

// Run Spinner until GPS fix is completed
$("divScrim").show();

// Get Address based on latitude and longitude
this.controller.serviceRequest('palm://com.palm.location', {
method:"getReverseLocation",
parameters:{ latitude : this.gps_latitude, longitude : this.gps_longitude},
onSuccess: function(inResponse2){
// Hide Spinner once GPS fix is completed
$("divScrim").hide();

switch (inResponse2.errorCode) {
case 0:
this.address = inResponse2.address;
// Set the Address
this.controller.get('txtAddress').mojo.setValue (this.address);
break;

case 6:
Mojo.Controller.errorDialog("GPS fix cannot be activated - Location Service is off or not accepted the terms of use.");
break;

case 7: case 8:
Mojo.Controller.errorDialog("GPS fix is underway - pending request or temporarily blacklisted.");
break;
}
}.bind(this),
onFailure : function(inResponse2) {
// Hide Spinner once GPS fix is completed
$("divScrim").hide();

switch (inResponse2.errorCode) {
case 6:
Mojo.Controller.errorDialog("GPS fix cannot be activated - Location Service is off or not accepted the terms of use.");
break;

case 7: case 8:
Mojo.Controller.errorDialog("GPS fix is underway - pending request or temporarily blacklisted.");
break;
}
}
});
}


WAIAssistant.prototype.prepareMessage = function(){
// Get address to be passed to SMS
this.address = this.controller.get('txtAddress').mojo.getValue();
if (this.address == "") {
Mojo.Controller.errorDialog("Unable to pass Address to SMS, please retry.");
return;
}

// Call and prepare Messaging API with latitude and longitude
this.google_map_url = "http://maps.google.com/?q=" + this.gps_latitude + "," + this.gps_longitude;
this.gps_address = " Address:" + this.address;
this.message_text = this.google_map_url + this.gps_address;

// Prepare the SMS
this.controller.serviceRequest('palm://com.palm.applicationManager', {
method: 'launch',
parameters: {
id: 'com.palm.app.messaging',
params: {
composeRecipients: [
/*{
address: "email",
serviceName: "gmail"
},*/
{
address: this.mobile_no
}
],
messageText: this.message_text
}
},
onSuccess: {},
onFailure: {}
});
}

WAIAssistant.prototype.prepareMap = function(){
// Call Google Map API with latitude and longitude
this.controller.serviceRequest("palm://com.palm.applicationManager", {
method: "open",
parameters: {
id: "com.palm.app.maps",
params: {
location: {lat: this.gps_latitude, lng: this.gps_longitude},
type: "m"
}
}
});
}


WAIAssistant.prototype.gpsLocation = function(param){
// Run Spinner until GPS fix is completed
$("divScrim").show();

// Call GPS API to get latitude and longitude
this.controller.serviceRequest("palm://com.palm.location", {
method : "getCurrentPosition",
parameters : { maximumAge : 60 },
onSuccess : function(inResponse) {
// Hide Spinner once GPS fix is completed
$("divScrim").hide();

switch (inResponse.errorCode) {
case 0:
// Get the latitude and longitude
this.gps_latitude = inResponse.latitude;
this.gps_longitude = inResponse.longitude;

// Set the texfields of latitude and longitude
this.controller.get('txtLatitude').mojo.setValue (this.gps_latitude);
this.controller.get('txtLongitude').mojo.setValue (this.gps_longitude);

// Convert to numeric and check to ensure it is still on earth
// Waiting for HP to produce a fix for Southern Hemisphere
this.ngps_latitude = Number(this.gps_latitude);
if (this.ngps_latitude < -90.0 || this.ngps_latitude > 90.0) {
Mojo.Controller.errorDialog("GPS Latitude fix timeout - you will need a clear sky for GPS fix.");
break;
}
this.ngps_longitude = Number(this.gps_longitude);
if (this.ngps_longitude < -180.0 || this.ngps_longitude > 180.0) {
Mojo.Controller.errorDialog("GPS Longitude fix timeout - you will need a clear sky for GPS fix.");
break;
}

// WAI, SMS and MAP
switch (param) {
case "WAI":
this.getAddress();
break;

case "SMS":
this.getAddress();
this.prepareMessage();
break;

case "MAP":
this.getAddress();
this.prepareMap();
break;
}
break;

case 1: case 2: case 3:
Mojo.Controller.errorDialog("GPS fix timeout - you will need a clear sky for GPS fix.");
break;

case 5: case 6:
Mojo.Controller.errorDialog("GPS fix cannot be activated - Location Service is off or not accepted the terms of use.");
break;

case 7: case 8:
Mojo.Controller.errorDialog("GPS fix is underway - pending request or temporarily blacklisted.");
break;
}
}.bind(this),
onFailure : function(inResponse) {
$("divScrim").hide();
switch (inResponse.errorCode) {
case 1: case 2: case 3:
Mojo.Controller.errorDialog("GPS fix timeout - you will need a clear sky for GPS fix.");
break;

case 5: case 6:
Mojo.Controller.errorDialog("GPS fix cannot be activated - Location Service is off or not accepted the terms of use.");
break;

case 7: case 8:
Mojo.Controller.errorDialog("GPS fix is underway - pending request or temporarily blacklisted.");
break;
}
}
});
}

WAIAssistant.prototype.onSMSTap = function() {
// If Mobile number is blank, exit
this.mobile_no = this.controller.get('txtMobile').mojo.getValue();
if (this.mobile_no == "") {
Mojo.Controller.errorDialog("You must enter a Mobile before sending SMS.");
return;
}

// Check and make sure Mobile does not contain decimal value
this.nmobile_no = this.mobile_no.indexOf('.');
if (this.nmobile_no > 0) {
Mojo.Controller.errorDialog("You must enter numeric for Mobile.");
break;
}

// Check and make sure Mobile is less than 0 and more the length of Mobile
this.nmobile_no = Number(this.mobile_no);
if (this.nmobile_no <= 0 || this.nmobile_no > 99999999999999999999) {
Mojo.Controller.errorDialog("You must enter valid numeric for Mobile.");
break;
}

// Call GPS API and pass SMS (Short Messaging System) in for further action
this.gpsLocation('SMS');
};

WAIAssistant.prototype.onWAITap = function() {
// Call GPS API and pass WAI (Where Am I) in for further action
this.gpsLocation('WAI');
};


WAIAssistant.prototype.onMapTap = function() {
// Call GPS API and pass Map (Map) in for further action
this.gpsLocation('MAP');
};


WAIAssistant.prototype.clearValues = function(){
// Clear all the values
this.controller.get('txtLatitude').mojo.setValue ('');
this.controller.get('txtLongitude').mojo.setValue ('');
this.controller.get('txtAddress').mojo.setValue ('');
this.controller.get('txtMobile').mojo.setValue ('');

}


WAIAssistant.prototype.handleShaking = function(event) {
this.clearValues();
}


WAIAssistant.prototype.activate = function(event) {
this.controller.listen(document, 'shaking', this.handleShaking.bindAsEventListener(this));
};

WAIAssistant.prototype.deactivate = function(event) {
};

WAIAssistant.prototype.cleanup = function(event) {
};

No comments:

Post a Comment