ytwater

example titanium geoloqi module

Aug 16th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is a test harness for your module
  2. // You should do something interesting in this harness
  3. // to test out the module and to provide instructions
  4. // to users on how to use it by example.
  5.  
  6. // open a single window
  7. var window = Ti.UI.createWindow({
  8.     backgroundColor : 'black',
  9.     layout : 'vertical'
  10. });
  11.  
  12.  
  13. // From code.google.com: API Key
  14. var google_api_key = 'XXX';
  15. // From URL of code.google.com
  16. var google_project_id = '123';
  17. // The e-mail that created the project on code.google.com
  18. var google_sender_id = 'xxx@gmail.com';
  19.  
  20. // From Appcelerator App Page
  21. var geoloqi_app_token = '17a6f-2294d65389369ec02c4529f7405f3923d9a199c5';
  22. var geoloqi_client_id = '116be7160ead35692dbf2842efc897f1';
  23. var geoloqi_secret = 'ff9cd1d688e81af374e29f0c4b4cbea4';
  24.  
  25.  
  26. var geoloqi = require('ti.geoloqi');
  27.  
  28. geoloqi.addEventListener(geoloqi.ON_VALIDATE, function(e) {
  29.     Ti.API.debug("Validation Error: error_code: " + e.error_code + ", error_description: " + e.error_description);
  30. });
  31. geoloqi.addEventListener(geoloqi.LOCATION_CHANGED, function(e) {
  32.     Ti.API.debug("LOCATION_CHANGED: Location: " + e.location);
  33. });
  34. geoloqi.addEventListener(geoloqi.LOCATION_UPLOADED, function(e) {
  35.     Ti.API.debug("LOCATION_UPLOADED: number: " + e);
  36. });
  37. geoloqi.addEventListener(geoloqi.TRACKER_PROFILE_CHANGED, function(e) {
  38.     Ti.API.debug("TRACKER_PROFILE_CHANGED: OLD_PROFILE: " + e.OLD_PROFILE + ", NEW_PROFILE: " + e.NEW_PROFILE);
  39. });
  40.  
  41. geoloqi.init({
  42.     clientId : geoloqi_client_id,
  43.     clientSecret : geoloqi_secret,
  44.     trackingProfile : 'PASSIVE',
  45.     lowBatteryTracking : true,
  46.     allowAnonymousUsers : false,
  47.     pushAccount : google_sender_id,
  48.     pushSender : google_project_id,
  49.     //account : 'perminder.singh@globallogic.com',   // I've never seen 'account' in api.
  50.     icon : 'push_icon'
  51. }, {
  52.     onSuccess : function() {
  53.         Ti.API.debug('init:onSuccess Called');
  54.         //Ti.API.debug('init:Session: ' + JSON.stringify(geoloqi.session));
  55.         //Ti.API.debug('init:Tracker: ' + JSON.stringify(geoloqi.tracker));
  56.         if (geoloqi.session != null) {
  57.             login.setEnabled('true');
  58.             create.setEnabled('true');
  59.         }
  60.  
  61.         if (Ti.Platform.osname !== "android") {
  62.             Ti.Network.registerForPushNotifications({
  63.                 types : [Titanium.Network.NOTIFICATION_TYPE_ALERT],
  64.                 callback : function(data) {
  65.                     geoloqi.iOS.handlePush(data);
  66.                 },
  67.                 success : function(data) {
  68.                     Ti.API.info("Push Registered for " + data.deviceToken);
  69.                     geoloqi.iOS.registerDeviceToken(data.deviceToken, "live");
  70.                 },
  71.                 error : function(data) {
  72.                     Ti.API.info("Could Not Register For Push" + data.error + data.type);
  73.                 }
  74.             });
  75.         }
  76.  
  77.     },
  78.     onFailure : function(e) {
  79.         Ti.API.debug('init:onFailure Called: error_code: ' + e.error_code + ', error_description: ' + e.error_description);
  80.     }
  81. });
  82.  
  83. var header = Titanium.UI.createLabel({
  84.     text : 'Geoloqi Android SDK',
  85.     height : '80',
  86.     width : '100%',
  87.     backgroundColor : '#444444',
  88.     color : 'white',
  89.     font : {
  90.         fontSize : 20,
  91.         fontWeight : 'bold'
  92.     },
  93.     textAlign : Titanium.UI.TEXT_ALIGNMENT_CENTER
  94. });
  95.  
  96. var onSuccess = Titanium.UI.createLabel({
  97.     text : 'onSuccess',
  98.     top : '10',
  99.     height : 'auto',
  100.     width : '100%',
  101.     color : 'white',
  102.     backgroundColor : '#444444',
  103.     textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT
  104. });
  105. var onComplete = Titanium.UI.createLabel({
  106.     text : 'onComplete',
  107.     top : '10',
  108.     height : 'auto',
  109.     width : '100%',
  110.     color : 'white',
  111.     backgroundColor : '#444444',
  112.     textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT
  113. });
  114. var onFailure = Titanium.UI.createLabel({
  115.     text : 'onFailure',
  116.     top : '10',
  117.     height : 'auto',
  118.     width : '100%',
  119.     color : 'white',
  120.     backgroundColor : '#444444',
  121.     textAlign : Titanium.UI.TEXT_ALIGNMENT_LEFT
  122. });
  123. var login = Titanium.UI.createButton({
  124.     title : 'login',
  125.     top : 10,
  126.     width : 100,
  127.     height : 50,
  128.     enabled : false
  129. });
  130. var create = Titanium.UI.createButton({
  131.     title : 'create user',
  132.     top : 10,
  133.     width : 100,
  134.     height : 50,
  135.     enabled : false
  136. });
  137. var update = Titanium.UI.createButton({
  138.     title : 'Update',
  139.     top : 10,
  140.     width : 100,
  141.     height : 50,
  142.     enabled : false
  143. });
  144.  
  145. create.addEventListener('click', function(e) {
  146.     geoloqi.session.postRequest("user/create", {
  147.         "client_id" : geoloqi_client_id,
  148.         "client_secret" : geoloqi_secret,
  149.         "username" : "bob2",
  150.         "password" : "bob2",
  151.         "device_id" : Ti.Platform.id,       // Never Saved
  152.         "mac" : Ti.Platform.macaddress      // Never Saved
  153.     }, {
  154.         onSuccess : function(e) {
  155.             update.setEnabled('true');
  156.             onSuccess.setText(e);
  157.         },
  158.         onFailure : function(e) {
  159.             onFailure.setText(e);
  160.         },
  161.         onComplete : function(e) {
  162.             onComplete.setText(e);
  163.         }
  164.     });
  165. });
  166.  
  167. /** Try and Update this user with device_id and mac.  Doesn't do anything. **/
  168.  
  169. update.addEventListener('click', function(e) {
  170.     geoloqi.session.postRequest("user/update/" + geoloqi.session.getUserId(), {
  171.         "client_id" : geoloqi_client_id,
  172.         "client_secret" : geoloqi_secret,
  173.         "device_id" : Ti.Platform.id,
  174.         "mac" : Ti.Platform.macaddress,
  175.     }, {
  176.         onSuccess : function(e) {
  177.             onSuccess.setText(e);
  178.         },
  179.         onFailure : function(e) {
  180.             onFailure.setText(e);
  181.         },
  182.         onComplete : function(e) {
  183.             onComplete.setText(e);
  184.         }
  185.     });
  186. });
  187.  
  188. window.add(header);
  189. window.add(onSuccess);
  190. window.add(onComplete);
  191. window.add(onFailure);
  192. window.add(create);
  193. window.add(update);
  194. window.open();
Add Comment
Please, Sign In to add comment