|
| 1 | +/** |
| 2 | + * Created by yailanderson on 10/06/16. |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// install : cordova plugin add https://github.com/Wikitude/wikitude-cordova-plugin.git |
| 7 | + |
| 8 | +(function (angular) { |
| 9 | + |
| 10 | + angular.module('ngCordova.plugins.wikitudePlugin', []) |
| 11 | + .factory('$wikitudePlugin', ['$q', '$window', '$exceptionHandler', function ($q, $window, $exceptionHandler) { |
| 12 | + |
| 13 | + return { |
| 14 | + /** |
| 15 | + * internal listeners for success and error, they are invoked in cordova.exec() function |
| 16 | + */ |
| 17 | + setInternalListeners: function () { |
| 18 | + $window.plugins.wikitudePlugin.prototype.onWikitudeOK = function () { |
| 19 | + //success callback |
| 20 | + }; |
| 21 | + |
| 22 | + $window.plugins.wikitudePlugin.prototype.onWikitudeError = function (error) { |
| 23 | + // error callback |
| 24 | + throw (error); |
| 25 | + }; |
| 26 | + }, |
| 27 | + |
| 28 | + /** |
| 29 | + * array of requiredFeatures for instance [ "2d_tracking", "geo" ] |
| 30 | + * if no argument is passed the default value is 2d tracking |
| 31 | + * @param requiredFeatures |
| 32 | + * @returns {*} |
| 33 | + */ |
| 34 | + isDeviceSupported: function (requiredFeatures) { |
| 35 | + |
| 36 | + // set the internal listeners for success and error |
| 37 | + this.setInternalListeners(); |
| 38 | + |
| 39 | + var self = this; |
| 40 | + var q = $q.defer(); |
| 41 | + |
| 42 | + // store features in the $wikitudePlugin for accessing it in all methods |
| 43 | + self.features = requiredFeatures || [ '2d_tracking' ]; |
| 44 | + |
| 45 | + $window.plugins.wikitudePlugin.isDeviceSupported(function () { |
| 46 | + //device supported |
| 47 | + q.resolve(self); |
| 48 | + }, function () { |
| 49 | + //device not supported |
| 50 | + q.reject('device not supported!'); |
| 51 | + }, self.features); |
| 52 | + |
| 53 | + return q.promise; |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + * |
| 58 | + * @param worldPath |
| 59 | + * @param startupConfiguration |
| 60 | + */ |
| 61 | + loadARchitectWorld: function (worldPath, startupConfiguration) { |
| 62 | + var q = $q.defer(); |
| 63 | + |
| 64 | + // startup configuration converted to json |
| 65 | + var config = JSON.stringify( startupConfiguration || { 'camera_position': 'back' } ); |
| 66 | + |
| 67 | + if (typeof worldPath === 'string') { |
| 68 | + |
| 69 | + $window.plugins.wikitudePlugin.loadARchitectWorld(function (loadedURL) { |
| 70 | + // loadedSuccessful |
| 71 | + q.resolve(loadedURL); |
| 72 | + }, function (errorMessage) { |
| 73 | + // error local path is wrong or the remote url returned an error code |
| 74 | + q.reject(errorMessage); |
| 75 | + },worldPath, this.features, config); |
| 76 | + } |
| 77 | + |
| 78 | + return q.promise; |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * inject a location into the Wikitude SDK |
| 83 | + * @param latitude |
| 84 | + * @param longitude |
| 85 | + * @param altitude |
| 86 | + * @param accuracy |
| 87 | + */ |
| 88 | + setLocation: function (latitude, longitude, altitude, accuracy) { |
| 89 | + try { |
| 90 | + //inject a location into the Wikitude SDK |
| 91 | + $window.plugins.wikitudePlugin.setLocation(latitude, longitude, altitude, accuracy); |
| 92 | + } catch (e) { |
| 93 | + // handle execption |
| 94 | + $exceptionHandler(e.message); |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + /** |
| 99 | + * The first argument Indicates if the ARchitect |
| 100 | + * web view should be included in the generated screenshot or not. |
| 101 | + * If a file path or file name is given in the second argument, |
| 102 | + * the generated screenshot will be saved in the application bundle. |
| 103 | + * Passing null will save the photo in the device photo library |
| 104 | + * @param includeWebView |
| 105 | + * @param imagePath |
| 106 | + * @returns {*} |
| 107 | + */ |
| 108 | + captureScreen: function (includeWebView, imagePath) { |
| 109 | + var q = $q.defer(); |
| 110 | + |
| 111 | + $window.plugins.wikitudePlugin.captureScreen(includeWebView, imagePath, function (bundlePath) { |
| 112 | + //success |
| 113 | + q.resolve(bundlePath); |
| 114 | + }, function (error) { |
| 115 | + //error |
| 116 | + q.reject(error); |
| 117 | + }); |
| 118 | + |
| 119 | + return q.promise; |
| 120 | + }, |
| 121 | + |
| 122 | + callJavaScript: function (js) { |
| 123 | + try { |
| 124 | + $window.plugins.wikitudePlugin.callJavaScript(js); |
| 125 | + } catch (e) { |
| 126 | + $exceptionHandler(e); |
| 127 | + } |
| 128 | + }, |
| 129 | + |
| 130 | + setOnUrlInvokeCallback: function (onUrlInvokeCallback) { |
| 131 | + $window.plugins.wikitudePlugin.setOnUrlInvokeCallback(onUrlInvokeCallback); |
| 132 | + }, |
| 133 | + |
| 134 | + |
| 135 | + close: function () { |
| 136 | + $window.plugins.wikitudePlugin.close(); |
| 137 | + }, |
| 138 | + |
| 139 | + show: function () { |
| 140 | + $window.plugins.wikitudePlugin.show(); |
| 141 | + }, |
| 142 | + |
| 143 | + hide: function () { |
| 144 | + $window.plugins.wikitudePlugin.hide(); |
| 145 | + } |
| 146 | + |
| 147 | + };// end of wikitudePlugin factory |
| 148 | + |
| 149 | + }]); |
| 150 | + |
| 151 | +})(window.angular); |
0 commit comments