Skip to content

Instantly share code, notes, and snippets.

@riknoll
Created November 11, 2015 00:51
Show Gist options
  • Save riknoll/d0ab25583b1dc8ab22af to your computer and use it in GitHub Desktop.
Save riknoll/d0ab25583b1dc8ab22af to your computer and use it in GitHub Desktop.
Example application for save state V2

To test this app:

  1. Drop index.html and index.js into a new HelloWorld
  2. Pull this branch of cordova-android
  3. In cordova-js, compile cordova.js for Android and add it to the template in cordova-android
cd cordova-js
grunt compile:android
mv pkg/cordova.android.js ../cordova-android/bin/templates/project/assets/www/cordova.js
  1. Add local cordova-android as platform to project
  2. Add this branch of cordova-plugin-camera as a plugin
  3. On Android device, go into developer settings and check "Don't keep activities"
  4. In app, press take picture. The message at the bottom of the screen should say that the image source was obtained from plugin result
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<button id="take-picture">Take Picture</button>
<button id="pick-picture">Choose Picture</button>
<img id="camera-result"/>
<p id="message-log"/>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var logMessage = function(message) {
var messageLog = document.getElementById("message-log");
messageLog.textContent = message;
console.log("Cordova: " + message);
};
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('resume', this.onResume, false);
document.addEventListener('pause', this.onPause, false);
console.log("Cordova: Events bound!");
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
// Setup the picture buttons
var getPicture = function(source) {
navigator.camera.getPicture(
function(imageUri) {
var image = document.getElementById('camera-result');
image.src = imageUri;
}, function(error) {
logMessage(error);
}, {
sourceType: source,
destinationType: Camera.DestinationType.FILE_URI,
Quality: 50,
targetWidth: 225,
targetHeight: 225,
correctOrientation: true,
encodingType: Camera.EncodingType.JPEG,
saveToPhotoAlbum: false
}
);
};
document.getElementById("take-picture").addEventListener("click", function() {
getPicture(Camera.PictureSourceType.CAMERA);
});
document.getElementById("pick-picture").addEventListener("click", function() {
getPicture(Camera.PictureSourceType.PHOTOLIBRARY);
});
},
onPause: function() {
// Here is where we should save our state in local storage
},
onResume: function(event) {
// Here is where we should restore the state from onPause
// If the Activity was killed as part of a camera request, there will
// be a pending result as part of the object
if(event.pendingResult) {
// Status codes can be found in PluginResult.java
if(event.pendingResult.pluginStatus === "OK") {
var image = document.getElementById('camera-result');
image.src = event.pendingResult.result;
logMessage("Image source obtained from plugin result");
} else {
logMessage("Plugin result status: " + event.pendingResult.pluginStatus + ", result: " + event.pendingResult.result);
}
} else {
logMessage("No pending result in resume event");
}
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment