CB-8943 fix PickAndContinue issue on Win10Phone

This commit is contained in:
Murat Sutunc 2015-05-06 10:49:05 -07:00
parent b0ee9dd905
commit 69c687e0cf

View File

@ -289,7 +289,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
captureSettings = null, captureSettings = null,
CaptureNS = Windows.Media.Capture; CaptureNS = Windows.Media.Capture;
function cameraPreviewOrientation(arg) { function cameraPreviewOrientation() {
// Rotate the cam since WP8.1 MediaCapture outputs video stream rotated 90° CCW // Rotate the cam since WP8.1 MediaCapture outputs video stream rotated 90° CCW
if (screen.msOrientation === "portrait-primary" || screen.msOrientation === "portrait-secondary") { if (screen.msOrientation === "portrait-primary" || screen.msOrientation === "portrait-secondary") {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees); capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
@ -411,6 +411,7 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite); return tempCapturedFile.openAsync(Windows.Storage.FileAccessMode.readWrite);
}) })
.then(function(fileStream) { .then(function(fileStream) {
imgStream.seek(0); // required for win8.1 emulator
return Windows.Storage.Streams.RandomAccessStream.copyAsync(imgStream, fileStream); return Windows.Storage.Streams.RandomAccessStream.copyAsync(imgStream, fileStream);
}) })
.done(function() { .done(function() {
@ -420,83 +421,17 @@ function takePictureFromCameraWP(successCallback, errorCallback, args) {
imgStream.close(); imgStream.close();
throw new Error("An error has occured while capturing the photo."); throw new Error("An error has occured while capturing the photo.");
}); });
}) });
}) })
.done(function(capturedFile) { .done(function(capturedFile) {
destroyCameraPreview(); destroyCameraPreview();
savePhoto(capturedFile, {
// success callback for capture operation destinationType: destinationType,
var success = function(capturedPhoto) { targetHeight: targetHeight,
if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) { targetWidth: targetWidth,
if (targetHeight > 0 && targetWidth > 0) { encodingType: encodingType,
resizeImage(successCallback, errorCallback, capturedPhoto, targetWidth, targetHeight, encodingType); saveToPhotoAlbum: saveToPhotoAlbum
} else { }, successCallback, errorCallback);
capturedPhoto.copyAsync(Windows.Storage.ApplicationData.current.localFolder, capturedPhoto.name, generateUniqueCollisionOption).done(function(copiedfile) {
successCallback("ms-appdata:///local/" + copiedfile.name);
}, errorCallback);
}
} else {
if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, capturedPhoto, targetWidth, targetHeight);
} else {
Windows.Storage.FileIO.readBufferAsync(capturedPhoto).done(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
capturedPhoto.deleteAsync().done(function() {
successCallback(strBase64);
}, function(err) {
errorCallback(err);
});
}, errorCallback);
}
}
};
if (!saveToPhotoAlbum) {
success(capturedFile);
return;
}
/*
Need to add and remove an event listener to catch activation state
Using FileSavePicker will suspend the app and it's required to catch the pickSaveFileContinuation
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var cameraActivationHandler = function(eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation) {
var file = eventArgs.file;
if (file) {
// Prevent updates to the remote version of the file until we're done
Windows.Storage.CachedFileManager.deferUpdates(file);
capturedFile.moveAndReplaceAsync(file)
.then(function() {
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
})
.done(function(updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
success(capturedFile);
} else {
errorCallback();
}
}, errorCallback);
} else {
errorCallback();
}
Windows.UI.WebUI.WebUIApplication.removeEventListener("activated", cameraActivationHandler);
}
};
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
} else {
savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
}
savePicker.suggestedFileName = fileName;
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", cameraActivationHandler);
savePicker.pickSaveFileAndContinue();
}, function(err) { }, function(err) {
destroyCameraPreview(); destroyCameraPreview();
errorCallback(err); errorCallback(err);
@ -548,53 +483,52 @@ function takePictureFromCameraWindows(successCallback, errorCallback, args) {
return; return;
} }
// save to photo album successCallback savePhoto(picture, {
var success = function() { destinationType: destinationType,
if (destinationType == Camera.DestinationType.FILE_URI) { targetHeight: targetHeight,
if (targetHeight > 0 && targetWidth > 0) { targetWidth: targetWidth,
resizeImage(successCallback, errorCallback, picture, targetWidth, targetHeight, encodingType); encodingType: encodingType,
saveToPhotoAlbum: saveToPhotoAlbum
}, successCallback, errorCallback);
}, function() {
errorCallback("Fail to capture a photo.");
});
}
function savePhoto(picture, options, successCallback, errorCallback) {
// success callback for capture operation
var success = function(picture) {
var generateUniqueCollisionOption = Windows.Storage.CreationCollisionOption.generateUniqueName;
if (options.destinationType == Camera.DestinationType.FILE_URI || options.destinationType == Camera.DestinationType.NATIVE_URI) {
if (options.targetHeight > 0 && options.targetWidth > 0) {
resizeImage(successCallback, errorCallback, picture, options.targetWidth, options.targetHeight, options.encodingType);
} else { } else {
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; picture.copyAsync(Windows.Storage.ApplicationData.current.localFolder, picture.name, generateUniqueCollisionOption).done(function(copiedFile) {
picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function(storageFile) { successCallback("ms-appdata:///local/" + copiedFile.name);
successCallback("ms-appdata:///local/" + storageFile.name); },errorCallback);
}, errorCallback);
} }
} else { } else {
if (targetHeight > 0 && targetWidth > 0) { if (options.targetHeight > 0 && options.targetWidth > 0) {
resizeImageBase64(successCallback, errorCallback, picture, targetWidth, targetHeight); resizeImageBase64(successCallback, errorCallback, picture, options.targetWidth, options.targetHeight);
} else { } else {
Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer) { Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
picture.deleteAsync().done(function() {
successCallback(strBase64); successCallback(strBase64);
}, function(err) {
errorCallback(err);
});
}, errorCallback); }, errorCallback);
} }
} }
}; };
// save to photo album errorCallback if (!options.saveToPhotoAlbum) {
var fail = function() { success(picture);
//errorCallback("FileError, code:" + fileError.code);
errorCallback("Save fail.");
};
if (!saveToPhotoAlbum) {
success();
return; return;
}
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
savePicker.suggestedFileName = "photo.png";
} else { } else {
savePicker.fileTypeChoices.insert("JPEG", [".jpg"]); var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedFileName = "photo.jpg"; var saveFile = function(file) {
}
savePicker.pickSaveFileAsync()
.done(function(file) {
if (file) { if (file) {
// Prevent updates to the remote version of the file until we're done // Prevent updates to the remote version of the file until we're done
Windows.Storage.CachedFileManager.deferUpdates(file); Windows.Storage.CachedFileManager.deferUpdates(file);
@ -606,18 +540,46 @@ function takePictureFromCameraWindows(successCallback, errorCallback, args) {
}) })
.done(function(updateStatus) { .done(function(updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) { if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
success(); success(picture);
} else { } else {
fail(); errorCallback("File update status is not complete.");
} }
}, fail); }, errorCallback);
} else { } else {
fail(); errorCallback("Failed to select a file.");
}
};
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
if (options.encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
savePicker.suggestedFileName = "photo.png";
} else {
savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
savePicker.suggestedFileName = "photo.jpg";
}
// If Windows Phone 8.1 use pickSaveFileAndContinue()
if (navigator.appVersion.indexOf('Windows Phone 8.1') >= 0) {
/*
Need to add and remove an event listener to catch activation state
Using FileSavePicker will suspend the app and it's required to catch the pickSaveFileContinuation
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var fileSaveHandler = function(eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation) {
var file = eventArgs.file;
saveFile(file);
Windows.UI.WebUI.WebUIApplication.removeEventListener("activated", fileSaveHandler);
}
};
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", fileSaveHandler);
savePicker.pickSaveFileAndContinue();
} else {
savePicker.pickSaveFileAsync()
.done(saveFile, errorCallback);
}
} }
}, fail);
}, function() {
errorCallback("Fail to capture a photo.");
});
} }
require("cordova/exec/proxy").add("Camera",module.exports); require("cordova/exec/proxy").add("Camera",module.exports);