mobile_template 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +28 -0
  5. data/Rakefile +3 -0
  6. data/bin/mobile_template +71 -0
  7. data/lib/mobile_template/version.rb +4 -0
  8. data/lib/mobile_template.rb +5 -0
  9. data/mobile_template.gemspec +21 -0
  10. data/templates/assets/Gemfile +4 -0
  11. data/templates/assets/config.rb +96 -0
  12. data/templates/assets/config.ru +4 -0
  13. data/templates/assets/source/images/vendor/ajax-loader.gif +0 -0
  14. data/templates/assets/source/images/vendor/ajax-loader.png +0 -0
  15. data/templates/assets/source/images/vendor/icons-18-black.png +0 -0
  16. data/templates/assets/source/images/vendor/icons-18-white.png +0 -0
  17. data/templates/assets/source/images/vendor/icons-36-black.png +0 -0
  18. data/templates/assets/source/images/vendor/icons-36-white.png +0 -0
  19. data/templates/assets/source/index.html.erb +2 -0
  20. data/templates/assets/source/javascripts/app/index.js.coffee +2 -0
  21. data/templates/assets/source/javascripts/application.js.coffee +5 -0
  22. data/templates/assets/source/javascripts/vendor/cordova.js +4841 -0
  23. data/templates/assets/source/javascripts/vendor/jquery.js +9267 -0
  24. data/templates/assets/source/javascripts/vendor/jquery.mobile.js +7410 -0
  25. data/templates/assets/source/layout.erb +24 -0
  26. data/templates/assets/source/stylesheets/application.css.scss +2 -0
  27. data/templates/assets/source/stylesheets/vendor/jquery.mobile.css.scss +1872 -0
  28. data/templates/cordova_android/.gitignore +18 -0
  29. data/templates/cordova_android/LICENSE +202 -0
  30. data/templates/cordova_android/NOTICE +5 -0
  31. data/templates/cordova_android/README.md +95 -0
  32. data/templates/cordova_android/VERSION +1 -0
  33. data/templates/cordova_android/bin/BOOM +4 -0
  34. data/templates/cordova_android/bin/autotest +2 -0
  35. data/templates/cordova_android/bin/bench +29 -0
  36. data/templates/cordova_android/bin/create +46 -0
  37. data/templates/cordova_android/bin/create.bat +1 -0
  38. data/templates/cordova_android/bin/create.js +88 -0
  39. data/templates/cordova_android/bin/create.xml +79 -0
  40. data/templates/cordova_android/bin/node_modules/.bin/cake +7 -0
  41. data/templates/cordova_android/bin/node_modules/.bin/coffee +7 -0
  42. data/templates/cordova_android/bin/node_modules/.bin/nodeunit +120 -0
  43. data/templates/cordova_android/bin/node_modules/coffee-script/.npmignore +11 -0
  44. data/templates/cordova_android/bin/node_modules/coffee-script/LICENSE +22 -0
  45. data/templates/cordova_android/bin/node_modules/coffee-script/README +48 -0
  46. data/templates/cordova_android/bin/node_modules/coffee-script/Rakefile +78 -0
  47. data/templates/cordova_android/bin/node_modules/coffee-script/bin/cake +7 -0
  48. data/templates/cordova_android/bin/node_modules/coffee-script/bin/coffee +7 -0
  49. data/templates/cordova_android/bin/node_modules/coffee-script/extras/jsl.conf +44 -0
  50. data/templates/cordova_android/bin/node_modules/coffee-script/lib/browser.js +75 -0
  51. data/templates/cordova_android/bin/node_modules/coffee-script/lib/cake.js +76 -0
  52. data/templates/cordova_android/bin/node_modules/coffee-script/lib/coffee-script.js +135 -0
  53. data/templates/cordova_android/bin/node_modules/coffee-script/lib/command.js +301 -0
  54. data/templates/cordova_android/bin/node_modules/coffee-script/lib/grammar.js +591 -0
  55. data/templates/cordova_android/bin/node_modules/coffee-script/lib/helpers.js +66 -0
  56. data/templates/cordova_android/bin/node_modules/coffee-script/lib/index.js +8 -0
  57. data/templates/cordova_android/bin/node_modules/coffee-script/lib/lexer.js +656 -0
  58. data/templates/cordova_android/bin/node_modules/coffee-script/lib/nodes.js +2289 -0
  59. data/templates/cordova_android/bin/node_modules/coffee-script/lib/optparse.js +111 -0
  60. data/templates/cordova_android/bin/node_modules/coffee-script/lib/parser.js +676 -0
  61. data/templates/cordova_android/bin/node_modules/coffee-script/lib/repl.js +123 -0
  62. data/templates/cordova_android/bin/node_modules/coffee-script/lib/rewriter.js +363 -0
  63. data/templates/cordova_android/bin/node_modules/coffee-script/lib/scope.js +120 -0
  64. data/templates/cordova_android/bin/node_modules/coffee-script/package.json +27 -0
  65. data/templates/cordova_android/bin/node_modules/nodeunit/.gitignore +5 -0
  66. data/templates/cordova_android/bin/node_modules/nodeunit/.npmignore +3 -0
  67. data/templates/cordova_android/bin/node_modules/nodeunit/CONTRIBUTORS.md +60 -0
  68. data/templates/cordova_android/bin/node_modules/nodeunit/LICENSE +19 -0
  69. data/templates/cordova_android/bin/node_modules/nodeunit/Makefile +126 -0
  70. data/templates/cordova_android/bin/node_modules/nodeunit/README.md +432 -0
  71. data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit +120 -0
  72. data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit.json +10 -0
  73. data/templates/cordova_android/bin/node_modules/nodeunit/deps/async.js +623 -0
  74. data/templates/cordova_android/bin/node_modules/nodeunit/deps/console.log.js +55 -0
  75. data/templates/cordova_android/bin/node_modules/nodeunit/deps/ejs.js +125 -0
  76. data/templates/cordova_android/bin/node_modules/nodeunit/deps/json2.js +483 -0
  77. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/nodeunit.js +1757 -0
  78. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite1.js +12 -0
  79. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite2.js +13 -0
  80. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/test.html +16 -0
  81. data/templates/cordova_android/bin/node_modules/nodeunit/img/example_fail.png +0 -0
  82. data/templates/cordova_android/bin/node_modules/nodeunit/img/example_pass.png +0 -0
  83. data/templates/cordova_android/bin/node_modules/nodeunit/index.js +3 -0
  84. data/templates/cordova_android/bin/node_modules/nodeunit/lib/assert.js +316 -0
  85. data/templates/cordova_android/bin/node_modules/nodeunit/lib/core.js +260 -0
  86. data/templates/cordova_android/bin/node_modules/nodeunit/lib/nodeunit.js +82 -0
  87. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/browser.js +119 -0
  88. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/default.js +123 -0
  89. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/html.js +107 -0
  90. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/index.js +9 -0
  91. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/junit.js +183 -0
  92. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/minimal.js +112 -0
  93. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/skip_passed.js +105 -0
  94. data/templates/cordova_android/bin/node_modules/nodeunit/lib/track.js +48 -0
  95. data/templates/cordova_android/bin/node_modules/nodeunit/lib/types.js +187 -0
  96. data/templates/cordova_android/bin/node_modules/nodeunit/lib/utils.js +209 -0
  97. data/templates/cordova_android/bin/node_modules/nodeunit/man1/nodeunit.1 +95 -0
  98. data/templates/cordova_android/bin/node_modules/nodeunit/nodelint.cfg +4 -0
  99. data/templates/cordova_android/bin/node_modules/nodeunit/package.json +56 -0
  100. data/templates/cordova_android/bin/node_modules/nodeunit/share/junit.xml.ejs +19 -0
  101. data/templates/cordova_android/bin/node_modules/nodeunit/share/license.js +11 -0
  102. data/templates/cordova_android/bin/node_modules/nodeunit/share/nodeunit.css +70 -0
  103. data/templates/cordova_android/bin/templates/project/.cordova/android/readme.md +1 -0
  104. data/templates/cordova_android/bin/templates/project/.cordova/readme.md +3 -0
  105. data/templates/cordova_android/bin/templates/project/cordova/create +36 -0
  106. data/templates/cordova_android/bin/templates/project/cordova/debug +9 -0
  107. data/templates/cordova_android/bin/templates/project/cordova/emulate +12 -0
  108. data/templates/cordova_android/bin/templates/project/cordova/log +3 -0
  109. data/templates/cordova_android/bin/templates/project/cordova/templates/Activity.java +16 -0
  110. data/templates/cordova_android/bin/templates/project/cordova/templates/project/AndroidManifest.xml +50 -0
  111. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/index.html +42 -0
  112. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/main.js +146 -0
  113. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/master.css +96 -0
  114. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/drawable/icon.png +0 -0
  115. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/cordova.xml +5 -0
  116. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/plugins.xml +19 -0
  117. data/templates/cordova_android/bin/test +26 -0
  118. data/templates/cordova_android/bin/tests/autotest.coffee +4 -0
  119. data/templates/cordova_android/bin/tests/create.coffee +21 -0
  120. data/templates/cordova_android/bin/tests/debug.coffee +0 -0
  121. data/templates/cordova_android/bin/tests/test.coffee +0 -0
  122. data/templates/cordova_android/framework/.classpath +8 -0
  123. data/templates/cordova_android/framework/.project +33 -0
  124. data/templates/cordova_android/framework/AndroidManifest.xml +68 -0
  125. data/templates/cordova_android/framework/ant.properties +34 -0
  126. data/templates/cordova_android/framework/assets/js/accelerometer.js +137 -0
  127. data/templates/cordova_android/framework/assets/js/app.js +89 -0
  128. data/templates/cordova_android/framework/assets/js/battery.js +134 -0
  129. data/templates/cordova_android/framework/assets/js/camera.js +168 -0
  130. data/templates/cordova_android/framework/assets/js/capture.js +203 -0
  131. data/templates/cordova_android/framework/assets/js/compass.js +168 -0
  132. data/templates/cordova_android/framework/assets/js/contact.js +310 -0
  133. data/templates/cordova_android/framework/assets/js/cordova.android.js +4841 -0
  134. data/templates/cordova_android/framework/assets/js/cordova.js.base +924 -0
  135. data/templates/cordova_android/framework/assets/js/crypto.js +54 -0
  136. data/templates/cordova_android/framework/assets/js/device.js +83 -0
  137. data/templates/cordova_android/framework/assets/js/file.js +1082 -0
  138. data/templates/cordova_android/framework/assets/js/filetransfer.js +125 -0
  139. data/templates/cordova_android/framework/assets/js/geolocation.js +209 -0
  140. data/templates/cordova_android/framework/assets/js/header.txt +19 -0
  141. data/templates/cordova_android/framework/assets/js/media.js +233 -0
  142. data/templates/cordova_android/framework/assets/js/network.js +100 -0
  143. data/templates/cordova_android/framework/assets/js/notification.js +133 -0
  144. data/templates/cordova_android/framework/assets/js/position.js +100 -0
  145. data/templates/cordova_android/framework/assets/js/storage.js +439 -0
  146. data/templates/cordova_android/framework/assets/www/index.html +27 -0
  147. data/templates/cordova_android/framework/build.xml +216 -0
  148. data/templates/cordova_android/framework/libs/commons-codec-1.3.jar +0 -0
  149. data/templates/cordova_android/framework/proguard-project.txt +20 -0
  150. data/templates/cordova_android/framework/project.properties +14 -0
  151. data/templates/cordova_android/framework/res/drawable/icon.png +0 -0
  152. data/templates/cordova_android/framework/res/drawable/splash.png +0 -0
  153. data/templates/cordova_android/framework/res/layout/main.xml +29 -0
  154. data/templates/cordova_android/framework/res/values/strings.xml +23 -0
  155. data/templates/cordova_android/framework/res/xml/cordova.xml +37 -0
  156. data/templates/cordova_android/framework/res/xml/plugins.xml +37 -0
  157. data/templates/cordova_android/framework/src/com/phonegap/api/IPlugin.java +27 -0
  158. data/templates/cordova_android/framework/src/com/phonegap/api/LOG.java +28 -0
  159. data/templates/cordova_android/framework/src/com/phonegap/api/PhonegapActivity.java +28 -0
  160. data/templates/cordova_android/framework/src/com/phonegap/api/Plugin.java +27 -0
  161. data/templates/cordova_android/framework/src/com/phonegap/api/PluginManager.java +35 -0
  162. data/templates/cordova_android/framework/src/com/phonegap/api/PluginResult.java +53 -0
  163. data/templates/cordova_android/framework/src/org/apache/cordova/AccelListener.java +311 -0
  164. data/templates/cordova_android/framework/src/org/apache/cordova/App.java +198 -0
  165. data/templates/cordova_android/framework/src/org/apache/cordova/AudioHandler.java +364 -0
  166. data/templates/cordova_android/framework/src/org/apache/cordova/AudioPlayer.java +450 -0
  167. data/templates/cordova_android/framework/src/org/apache/cordova/AuthenticationToken.java +69 -0
  168. data/templates/cordova_android/framework/src/org/apache/cordova/BatteryListener.java +156 -0
  169. data/templates/cordova_android/framework/src/org/apache/cordova/CallbackServer.java +431 -0
  170. data/templates/cordova_android/framework/src/org/apache/cordova/CameraLauncher.java +500 -0
  171. data/templates/cordova_android/framework/src/org/apache/cordova/Capture.java +400 -0
  172. data/templates/cordova_android/framework/src/org/apache/cordova/CompassListener.java +308 -0
  173. data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessor.java +198 -0
  174. data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessorSdk5.java +1934 -0
  175. data/templates/cordova_android/framework/src/org/apache/cordova/ContactManager.java +113 -0
  176. data/templates/cordova_android/framework/src/org/apache/cordova/CordovaChromeClient.java +314 -0
  177. data/templates/cordova_android/framework/src/org/apache/cordova/CordovaWebViewClient.java +306 -0
  178. data/templates/cordova_android/framework/src/org/apache/cordova/Device.java +219 -0
  179. data/templates/cordova_android/framework/src/org/apache/cordova/DirectoryManager.java +161 -0
  180. data/templates/cordova_android/framework/src/org/apache/cordova/DroidGap.java +1417 -0
  181. data/templates/cordova_android/framework/src/org/apache/cordova/ExifHelper.java +165 -0
  182. data/templates/cordova_android/framework/src/org/apache/cordova/FileTransfer.java +458 -0
  183. data/templates/cordova_android/framework/src/org/apache/cordova/FileUploadResult.java +63 -0
  184. data/templates/cordova_android/framework/src/org/apache/cordova/FileUtils.java +1048 -0
  185. data/templates/cordova_android/framework/src/org/apache/cordova/GeoBroker.java +165 -0
  186. data/templates/cordova_android/framework/src/org/apache/cordova/GeoListener.java +133 -0
  187. data/templates/cordova_android/framework/src/org/apache/cordova/GpsListener.java +163 -0
  188. data/templates/cordova_android/framework/src/org/apache/cordova/HttpHandler.java +80 -0
  189. data/templates/cordova_android/framework/src/org/apache/cordova/LinearLayoutSoftKeyboardDetect.java +104 -0
  190. data/templates/cordova_android/framework/src/org/apache/cordova/NetworkListener.java +153 -0
  191. data/templates/cordova_android/framework/src/org/apache/cordova/NetworkManager.java +248 -0
  192. data/templates/cordova_android/framework/src/org/apache/cordova/Notification.java +366 -0
  193. data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceNode.java +34 -0
  194. data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceSet.java +62 -0
  195. data/templates/cordova_android/framework/src/org/apache/cordova/StandAlone.java +35 -0
  196. data/templates/cordova_android/framework/src/org/apache/cordova/Storage.java +239 -0
  197. data/templates/cordova_android/framework/src/org/apache/cordova/TempListener.java +112 -0
  198. data/templates/cordova_android/framework/src/org/apache/cordova/api/CordovaInterface.java +145 -0
  199. data/templates/cordova_android/framework/src/org/apache/cordova/api/IPlugin.java +116 -0
  200. data/templates/cordova_android/framework/src/org/apache/cordova/api/LOG.java +234 -0
  201. data/templates/cordova_android/framework/src/org/apache/cordova/api/Plugin.java +210 -0
  202. data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginManager.java +359 -0
  203. data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginResult.java +119 -0
  204. data/templates/cordova_android/framework/src/org/apache/cordova/file/EncodingException.java +28 -0
  205. data/templates/cordova_android/framework/src/org/apache/cordova/file/FileExistsException.java +28 -0
  206. data/templates/cordova_android/framework/src/org/apache/cordova/file/InvalidModificationException.java +29 -0
  207. data/templates/cordova_android/framework/src/org/apache/cordova/file/NoModificationAllowedException.java +28 -0
  208. data/templates/cordova_android/framework/src/org/apache/cordova/file/TypeMismatchException.java +29 -0
  209. data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceNodeTest.java +53 -0
  210. data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceSetTest.java +73 -0
  211. metadata +279 -0
@@ -0,0 +1,500 @@
1
+ /*
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ */
19
+ package org.apache.cordova;
20
+
21
+ import java.io.ByteArrayOutputStream;
22
+ import java.io.File;
23
+ import java.io.FileNotFoundException;
24
+ import java.io.FileOutputStream;
25
+ import java.io.IOException;
26
+ import java.io.OutputStream;
27
+
28
+ import org.apache.commons.codec.binary.Base64;
29
+ import org.apache.cordova.api.LOG;
30
+ import org.apache.cordova.api.Plugin;
31
+ import org.apache.cordova.api.PluginResult;
32
+ import org.json.JSONArray;
33
+ import org.json.JSONException;
34
+ import org.json.JSONObject;
35
+
36
+
37
+ import android.app.Activity;
38
+ import android.content.ContentValues;
39
+ import android.content.Intent;
40
+ import android.database.Cursor;
41
+ import android.graphics.Bitmap;
42
+ import android.graphics.Bitmap.CompressFormat;
43
+ import android.net.Uri;
44
+ import android.provider.MediaStore;
45
+
46
+ /**
47
+ * This class launches the camera view, allows the user to take a picture, closes the camera view,
48
+ * and returns the captured image. When the camera view is closed, the screen displayed before
49
+ * the camera view was shown is redisplayed.
50
+ */
51
+ public class CameraLauncher extends Plugin {
52
+
53
+ private static final int DATA_URL = 0; // Return base64 encoded string
54
+ private static final int FILE_URI = 1; // Return file uri (content://media/external/images/media/2 for Android)
55
+
56
+ private static final int PHOTOLIBRARY = 0; // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
57
+ private static final int CAMERA = 1; // Take picture from camera
58
+ private static final int SAVEDPHOTOALBUM = 2; // Choose image from picture library (same as PHOTOLIBRARY for Android)
59
+
60
+ private static final int PICTURE = 0; // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
61
+ private static final int VIDEO = 1; // allow selection of video only, ONLY RETURNS URL
62
+ private static final int ALLMEDIA = 2; // allow selection from all media types
63
+
64
+ private static final int JPEG = 0; // Take a picture of type JPEG
65
+ private static final int PNG = 1; // Take a picture of type PNG
66
+ private static final String GET_PICTURE = "Get Picture";
67
+ private static final String GET_VIDEO = "Get Video";
68
+ private static final String GET_All = "Get All";
69
+
70
+ private static final String LOG_TAG = "CameraLauncher";
71
+
72
+ private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
73
+ private int targetWidth; // desired width of the image
74
+ private int targetHeight; // desired height of the image
75
+ private Uri imageUri; // Uri of captured image
76
+ private int encodingType; // Type of encoding to use
77
+ private int mediaType; // What type of media to retrieve
78
+
79
+ public String callbackId;
80
+ private int numPics;
81
+
82
+ /**
83
+ * Constructor.
84
+ */
85
+ public CameraLauncher() {
86
+ }
87
+
88
+ /**
89
+ * Executes the request and returns PluginResult.
90
+ *
91
+ * @param action The action to execute.
92
+ * @param args JSONArry of arguments for the plugin.
93
+ * @param callbackId The callback id used when calling back into JavaScript.
94
+ * @return A PluginResult object with a status and message.
95
+ */
96
+ public PluginResult execute(String action, JSONArray args, String callbackId) {
97
+ PluginResult.Status status = PluginResult.Status.OK;
98
+ String result = "";
99
+ this.callbackId = callbackId;
100
+
101
+ try {
102
+ if (action.equals("takePicture")) {
103
+ int srcType = CAMERA;
104
+ int destType = FILE_URI;
105
+ this.targetHeight = 0;
106
+ this.targetWidth = 0;
107
+ this.encodingType = JPEG;
108
+ this.mediaType = PICTURE;
109
+ this.mQuality = 80;
110
+
111
+ this.mQuality = args.getInt(0);
112
+ destType = args.getInt(1);
113
+ srcType = args.getInt(2);
114
+ this.targetWidth = args.getInt(3);
115
+ this.targetHeight = args.getInt(4);
116
+ this.encodingType = args.getInt(5);
117
+
118
+ if (srcType == CAMERA) {
119
+ this.takePicture(destType, encodingType);
120
+ }
121
+ else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
122
+ this.getImage(srcType, destType);
123
+ }
124
+ PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
125
+ r.setKeepCallback(true);
126
+ return r;
127
+ }
128
+ return new PluginResult(status, result);
129
+ } catch (JSONException e) {
130
+ e.printStackTrace();
131
+ return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
132
+ }
133
+ }
134
+
135
+ //--------------------------------------------------------------------------
136
+ // LOCAL METHODS
137
+ //--------------------------------------------------------------------------
138
+
139
+ /**
140
+ * Take a picture with the camera.
141
+ * When an image is captured or the camera view is cancelled, the result is returned
142
+ * in CordovaActivity.onActivityResult, which forwards the result to this.onActivityResult.
143
+ *
144
+ * The image can either be returned as a base64 string or a URI that points to the file.
145
+ * To display base64 string in an img tag, set the source to:
146
+ * img.src="data:image/jpeg;base64,"+result;
147
+ * or to display URI in an img tag
148
+ * img.src=result;
149
+ *
150
+ * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
151
+ * @param returnType Set the type of image to return.
152
+ */
153
+ public void takePicture(int returnType, int encodingType) {
154
+ // Save the number of images currently on disk for later
155
+ this.numPics = queryImgDB().getCount();
156
+
157
+ // Display camera
158
+ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
159
+
160
+ // Specify file so that large image is captured and returned
161
+ // TODO: What if there isn't any external storage?
162
+ File photo = createCaptureFile(encodingType);
163
+ intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
164
+ this.imageUri = Uri.fromFile(photo);
165
+
166
+ this.ctx.startActivityForResult((Plugin) this, intent, (CAMERA+1)*16 + returnType+1);
167
+ }
168
+
169
+ /**
170
+ * Create a file in the applications temporary directory based upon the supplied encoding.
171
+ *
172
+ * @param encodingType of the image to be taken
173
+ * @return a File object pointing to the temporary picture
174
+ */
175
+ private File createCaptureFile(int encodingType) {
176
+ File photo = null;
177
+ if (encodingType == JPEG) {
178
+ photo = new File(DirectoryManager.getTempDirectoryPath(ctx.getContext()), "Pic.jpg");
179
+ } else if (encodingType == PNG) {
180
+ photo = new File(DirectoryManager.getTempDirectoryPath(ctx.getContext()), "Pic.png");
181
+ } else {
182
+ throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
183
+ }
184
+ return photo;
185
+ }
186
+
187
+ /**
188
+ * Get image from photo library.
189
+ *
190
+ * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
191
+ * @param srcType The album to get image from.
192
+ * @param returnType Set the type of image to return.
193
+ */
194
+ // TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do!
195
+ public void getImage(int srcType, int returnType) {
196
+ Intent intent = new Intent();
197
+ String title = GET_PICTURE;
198
+ if (this.mediaType == PICTURE) {
199
+ intent.setType("image/*");
200
+ }
201
+ else if (this.mediaType == VIDEO) {
202
+ intent.setType("video/*");
203
+ title = GET_VIDEO;
204
+ }
205
+ else if (this.mediaType == ALLMEDIA) {
206
+ // I wanted to make the type 'image/*, video/*' but this does not work on all versions
207
+ // of android so I had to go with the wildcard search.
208
+ intent.setType("*/*");
209
+ title = GET_All;
210
+ }
211
+
212
+ intent.setAction(Intent.ACTION_GET_CONTENT);
213
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
214
+ this.ctx.startActivityForResult((Plugin) this, Intent.createChooser(intent,
215
+ new String(title)), (srcType+1)*16 + returnType + 1);
216
+ }
217
+
218
+ /**
219
+ * Scales the bitmap according to the requested size.
220
+ *
221
+ * @param bitmap The bitmap to scale.
222
+ * @return Bitmap A new Bitmap object of the same bitmap after scaling.
223
+ */
224
+ public Bitmap scaleBitmap(Bitmap bitmap) {
225
+ int newWidth = this.targetWidth;
226
+ int newHeight = this.targetHeight;
227
+ int origWidth = bitmap.getWidth();
228
+ int origHeight = bitmap.getHeight();
229
+
230
+ // If no new width or height were specified return the original bitmap
231
+ if (newWidth <= 0 && newHeight <= 0) {
232
+ return bitmap;
233
+ }
234
+ // Only the width was specified
235
+ else if (newWidth > 0 && newHeight <= 0) {
236
+ newHeight = (newWidth * origHeight) / origWidth;
237
+ }
238
+ // only the height was specified
239
+ else if (newWidth <= 0 && newHeight > 0) {
240
+ newWidth = (newHeight * origWidth) / origHeight;
241
+ }
242
+ // If the user specified both a positive width and height
243
+ // (potentially different aspect ratio) then the width or height is
244
+ // scaled so that the image fits while maintaining aspect ratio.
245
+ // Alternatively, the specified width and height could have been
246
+ // kept and Bitmap.SCALE_TO_FIT specified when scaling, but this
247
+ // would result in whitespace in the new image.
248
+ else {
249
+ double newRatio = newWidth / (double)newHeight;
250
+ double origRatio = origWidth / (double)origHeight;
251
+
252
+ if (origRatio > newRatio) {
253
+ newHeight = (newWidth * origHeight) / origWidth;
254
+ } else if (origRatio < newRatio) {
255
+ newWidth = (newHeight * origWidth) / origHeight;
256
+ }
257
+ }
258
+
259
+ return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
260
+ }
261
+
262
+ /**
263
+ * Called when the camera view exits.
264
+ *
265
+ * @param requestCode The request code originally supplied to startActivityForResult(),
266
+ * allowing you to identify who this result came from.
267
+ * @param resultCode The integer result code returned by the child activity through its setResult().
268
+ * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
269
+ */
270
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
271
+
272
+ // Get src and dest types from request code
273
+ int srcType = (requestCode/16) - 1;
274
+ int destType = (requestCode % 16) - 1;
275
+
276
+ // If CAMERA
277
+ if (srcType == CAMERA) {
278
+ // If image available
279
+ if (resultCode == Activity.RESULT_OK) {
280
+ try {
281
+ // Create an ExifHelper to save the exif data that is lost during compression
282
+ ExifHelper exif = new ExifHelper();
283
+ if (this.encodingType == JPEG) {
284
+ exif.createInFile(DirectoryManager.getTempDirectoryPath(ctx.getContext()) + "/Pic.jpg");
285
+ exif.readExifData();
286
+ }
287
+
288
+ // Read in bitmap of captured image
289
+ Bitmap bitmap;
290
+ try {
291
+ bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);
292
+ } catch (FileNotFoundException e) {
293
+ Uri uri = intent.getData();
294
+ android.content.ContentResolver resolver = this.ctx.getContentResolver();
295
+ bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
296
+ }
297
+
298
+ bitmap = scaleBitmap(bitmap);
299
+
300
+ // If sending base64 image back
301
+ if (destType == DATA_URL) {
302
+ this.processPicture(bitmap);
303
+ checkForDuplicateImage(DATA_URL);
304
+ }
305
+
306
+ // If sending filename back
307
+ else if (destType == FILE_URI){
308
+ // Create entry in media store for image
309
+ // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it)
310
+ ContentValues values = new ContentValues();
311
+ values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
312
+ Uri uri = null;
313
+ try {
314
+ uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
315
+ } catch (UnsupportedOperationException e) {
316
+ LOG.d(LOG_TAG, "Can't write to external media storage.");
317
+ try {
318
+ uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
319
+ } catch (UnsupportedOperationException ex) {
320
+ LOG.d(LOG_TAG, "Can't write to internal media storage.");
321
+ this.failPicture("Error capturing image - no media storage found.");
322
+ return;
323
+ }
324
+ }
325
+
326
+ // Add compressed version of captured image to returned media store Uri
327
+ OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);
328
+ bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
329
+ os.close();
330
+
331
+ // Restore exif data to file
332
+ if (this.encodingType == JPEG) {
333
+ exif.createOutFile(FileUtils.getRealPathFromURI(uri, this.ctx));
334
+ exif.writeExifData();
335
+ }
336
+
337
+ // Send Uri back to JavaScript for viewing image
338
+ this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
339
+ }
340
+ bitmap.recycle();
341
+ bitmap = null;
342
+ System.gc();
343
+
344
+ checkForDuplicateImage(FILE_URI);
345
+ } catch (IOException e) {
346
+ e.printStackTrace();
347
+ this.failPicture("Error capturing image.");
348
+ }
349
+ }
350
+
351
+ // If cancelled
352
+ else if (resultCode == Activity.RESULT_CANCELED) {
353
+ this.failPicture("Camera cancelled.");
354
+ }
355
+
356
+ // If something else
357
+ else {
358
+ this.failPicture("Did not complete!");
359
+ }
360
+ }
361
+
362
+ // If retrieving photo from library
363
+ else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
364
+ if (resultCode == Activity.RESULT_OK) {
365
+ Uri uri = intent.getData();
366
+ android.content.ContentResolver resolver = this.ctx.getContentResolver();
367
+
368
+ // If you ask for video or all media type you will automatically get back a file URI
369
+ // and there will be no attempt to resize any returned data
370
+ if (this.mediaType != PICTURE) {
371
+ this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
372
+ }
373
+ else {
374
+ // If sending base64 image back
375
+ if (destType == DATA_URL) {
376
+ try {
377
+ Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
378
+ bitmap = scaleBitmap(bitmap);
379
+ this.processPicture(bitmap);
380
+ bitmap.recycle();
381
+ bitmap = null;
382
+ System.gc();
383
+ } catch (FileNotFoundException e) {
384
+ e.printStackTrace();
385
+ this.failPicture("Error retrieving image.");
386
+ }
387
+ }
388
+
389
+ // If sending filename back
390
+ else if (destType == FILE_URI) {
391
+ // Do we need to scale the returned file
392
+ if (this.targetHeight > 0 && this.targetWidth > 0) {
393
+ try {
394
+ Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
395
+ bitmap = scaleBitmap(bitmap);
396
+
397
+ String fileName = DirectoryManager.getTempDirectoryPath(ctx.getContext()) + "/resize.jpg";
398
+ OutputStream os = new FileOutputStream(fileName);
399
+ bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
400
+ os.close();
401
+
402
+ bitmap.recycle();
403
+ bitmap = null;
404
+
405
+ // The resized image is cached by the app in order to get around this and not have to delete you
406
+ // application cache I'm adding the current system time to the end of the file url.
407
+ this.success(new PluginResult(PluginResult.Status.OK, ("file://" + fileName + "?" + System.currentTimeMillis())), this.callbackId);
408
+ System.gc();
409
+ } catch (Exception e) {
410
+ e.printStackTrace();
411
+ this.failPicture("Error retrieving image.");
412
+ }
413
+ }
414
+ else {
415
+ this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
416
+ }
417
+ }
418
+ }
419
+ }
420
+ else if (resultCode == Activity.RESULT_CANCELED) {
421
+ this.failPicture("Selection cancelled.");
422
+ }
423
+ else {
424
+ this.failPicture("Selection did not complete!");
425
+ }
426
+ }
427
+ }
428
+
429
+ /**
430
+ * Creates a cursor that can be used to determine how many images we have.
431
+ *
432
+ * @return a cursor
433
+ */
434
+ private Cursor queryImgDB() {
435
+ return this.ctx.getContentResolver().query(
436
+ android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
437
+ new String[] { MediaStore.Images.Media._ID },
438
+ null,
439
+ null,
440
+ null);
441
+ }
442
+
443
+ /**
444
+ * Used to find out if we are in a situation where the Camera Intent adds to images
445
+ * to the content store. If we are using a FILE_URI and the number of images in the DB
446
+ * increases by 2 we have a duplicate, when using a DATA_URL the number is 1.
447
+ *
448
+ * @param type FILE_URI or DATA_URL
449
+ */
450
+ private void checkForDuplicateImage(int type) {
451
+ int diff = 1;
452
+ Cursor cursor = queryImgDB();
453
+ int currentNumOfImages = cursor.getCount();
454
+
455
+ if (type == FILE_URI) {
456
+ diff = 2;
457
+ }
458
+
459
+ // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
460
+ if ((currentNumOfImages - numPics) == diff) {
461
+ cursor.moveToLast();
462
+ int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1;
463
+ Uri uri = Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + id);
464
+ this.ctx.getContentResolver().delete(uri, null, null);
465
+ }
466
+ }
467
+
468
+ /**
469
+ * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
470
+ *
471
+ * @param bitmap
472
+ */
473
+ public void processPicture(Bitmap bitmap) {
474
+ ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
475
+ try {
476
+ if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
477
+ byte[] code = jpeg_data.toByteArray();
478
+ byte[] output = Base64.encodeBase64(code);
479
+ String js_out = new String(output);
480
+ this.success(new PluginResult(PluginResult.Status.OK, js_out), this.callbackId);
481
+ js_out = null;
482
+ output = null;
483
+ code = null;
484
+ }
485
+ }
486
+ catch(Exception e) {
487
+ this.failPicture("Error compressing image.");
488
+ }
489
+ jpeg_data = null;
490
+ }
491
+
492
+ /**
493
+ * Send error message to JavaScript.
494
+ *
495
+ * @param err
496
+ */
497
+ public void failPicture(String err) {
498
+ this.error(new PluginResult(PluginResult.Status.ERROR, err), this.callbackId);
499
+ }
500
+ }