Check if Geolocation is off in AS3 - actionscript-3

I've got this code in my AIR app:
if (Geolocation.isSupported){
var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}else{
trace("Geolocation is not supported");
}
But how to check if the GPS is simply off ?
I would like something like :
if (Geolocation.isSupported){
var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}
if Geolocation.isOff){
trace("Your GPS is off");
}
Thank you for your help
else{
trace("Geolocation is not supported");
}

You can use "Geolocation.muted".
if (Geolocation.isSupported){
var my_geo:Geolocation = new Geolocation();
if (my_geo.muted){
trace("Your GPS is off");
}
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}else{
trace("Geolocation is not supported");
}

First check if the Geolocation muted property is true or false to determine if access to the geolocation services are available (could be turned off or the user could have denied access to the application)
i.e:
if (my_geo.muted) { }
If muted is false (thus it is on), then add an event handler to track if it gets turned off (and thus muted becomes true) during your application's use of the geo services so your application can notify user, or whatever you need to do if geo services are not available.
i.e.
if (!my_geo.muted) {
// start listening for geo updates
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}
// Listen for changes in the status of the geo service
my_geo.addEventListener(StatusEvent.STATUS, yourGEOServiceUpdateHandler);
public function yourGEOServiceUpdateHandler(event:StatusEvent):void
{
if (my_geo.muted)
my_geo.removeEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
else
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
}

Related

Notify new application version with browser service workers

I build an html/js application (a progressive web app) with Polymer and polymer-cli and the well generated service-worker for caching and offline.
I wonder how to notify the user when a new version of the application is available and invite him to restart browser.
any ideas ?
Edit
a talk at IO2016 where Eric Bidel talk about service worker and notify user about new version of an application :
https://youtu.be/__KvYxcIIm8?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC&t=1510
Need to check the google IO Web source code
References:
https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle
https://classroom.udacity.com/courses/ud899
// page script
document.addEventListener('DOMContentLoaded', function(){
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/sw.js')
.then(function(registration) {
console.info('ServiceWorker registration successful with scope:', registration.scope);
// if there's no controller, this page wasn't loaded
// via a service worker, so they're looking at the latest version.
// In that case, exit early
if (!navigator.serviceWorker.controller) return;
// if there's an updated worker already waiting, update
if (registration.waiting) {
console.info('show toast and upon click update...');
registration.waiting.postMessage({ updateSw: true });
return;
}
// if there's an updated worker installing, track its
// progress. If it becomes "installed", update
if (registration.installing) {
registration.addEventListener('statechange', function(){
if (registration.installing.state == 'installed'){
console.info('show toast and upon click update...');
registration.installing.postMessage({ updateSw: true });
return;
}
});
}
// otherwise, listen for new installing workers arriving.
// If one arrives, track its progress.
// If it becomes "installed", update
registration.addEventListener('updatefound', function(){
let newServiceWorker = registration.installing;
newServiceWorker.addEventListener('statechange', function() {
if (newServiceWorker.state == 'installed') {
console.info('show toast and upon click update...');
newServiceWorker.postMessage({ updateSw: true });
}
});
});
})
.catch(function(error) {
console.info('ServiceWorker registration failed:', error);
});
navigator.serviceWorker.addEventListener('controllerchange', function() {
window.location.reload();
});
}
});
// sw script
self.addEventListener('message', function(e) {
if (e.data.updateSw){
self.skipWaiting();
}
});
Thanks to IO team .. we need to check if the current service-worker becomes redundant
// Check to see if the service worker controlling the page at initial load
// has become redundant, since this implies there's a new service worker with fresh content.
if (navigator.serviceWorker && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.onstatechange = function(event) {
if (event.target.state === 'redundant') {
// Define a handler that will be used for the next io-toast tap, at which point it
// be automatically removed.
const tapHandler = function() {
window.location.reload();
};
if (IOWA.Elements && IOWA.Elements.Toast &&
IOWA.Elements.Toast.showMessage) {
IOWA.Elements.Toast.showMessage(
'A new version of this app is available.', tapHandler, 'Refresh',
null, 0); // duration 0 indications shows the toast indefinitely.
} else {
tapHandler(); // Force reload if user never was shown the toast.
}
}
};
}

How to simulate Geolocation service AS3

I'm creating an app that is using Geolocation servioce on phone o send location/time data over the internet. And that is working just fine. Problem is that I cannot test it in Flashdevelop, cause Geolocation is not supported, so I have to upload every time new code and test it on phone. Is there any way to simulate Geolocation service in Flashdevelop? Or generally, on desktop PC?
Sorry for bothering ... I found out... quite simple answer was..
private function startLocating():void {
var myLat:Number=44.2343;
var myLong:Number=20.9432;
_geo = new Geolocation();
_geo.addEventListener(GeolocationEvent.UPDATE, Handler1 );
if (Geolocation.isSupported) {
if (!_geo.muted) {
_geo.setRequestedUpdateInterval(1000);
} else {
trace ("Location service not turned on.");
}
} else {
/* this code is working when Geolocation is not supported */
_geo.dispatchEvent(new GeolocationEvent(GeolocationEvent.UPDATE,false,false,myLat,myLong));
}
}
private function Handler1(ev:GeolocationEvent):void {
var latitude:Number = ev.latitude;
var longitude:Number = ev.longitude;
.....
}

Is there any way to detect that the Microphone is disabled on a Windows Store App?

I want to detect if the user turned off the Microphone on a Windows Store app on the fly. I know that it is possible because Audio Recorder uses it. But how?
UPDATE: I want to be notified by an event that the microphone was disabled.
Is there anything to do with this? http://msdn.microsoft.com/en-us/library/windows/desktop/dd370810(v=vs.85).aspx
You can check it this way.
bool IsMicAvailable = true;
try
{
var MyMediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
await MyMediaCapture.InitializeAsync(settings);
}
catch (Exception)
{
IsMicAvailable = false;
}
if(IsMicAvailable)
//TODO : The logic of recording audio
else
//TODO : Display error message
Basically if microphone permission is off then System.UnauthorizedAccessException will occur and if microphone is not attached then System.Exception will occur & it contains message Exception from HRESULT: 0xC00DABE0
var deviceAccessInfo = DeviceAccessInformation.CreateFromDeviceClass(DeviceClass.AudioCapture);
deviceAccessInfo.AccessChanged += OnDeviceAccessChanged;
void OnDeviceAccessChangedDeviceAccessInformation sender, DeviceAccessChangedEventArgs args)
{
if (args.Status == DeviceAccessStatus.DeniedByUser)
; // handle
else if (args.Status == DeviceAccessStatus.Allowed)
; // handle
}
The only way is to do initalizeAsync and watch for error. Read http://msdn.microsoft.com/en-us/library/windows/apps/hh768223.aspx for more information.
On your latter question. No, there's no way to learn that permission status has changed.

How can i get AS3 facebook API to init on a self signed https site

I am setting up a facebook app - with the AS3 facebook API. The first thing i do is call Facebook.init(). When this is done on our normal development setup (a normal http site) - this works perfectly and i am able to access the graph. However when i switch to a https site (with a self signed certificate) Facebook.init() never fires the callback. I have crossdomain.xml set up correctly to allow all and security false.
Anybody know how i could set this up so that it will work.
Indeed the Facebook connection with the FB API AS3 doesn't work anymore since august...
With the FlashWebExemple.fla from http://code.google.com/p/facebook-actionscript-api/downloads/detail?name=GraphAPI_Examples_1_8_1.zip
Facebook.init(APP_ID, onInit);
protected function onInit(result:Object, fail:Object):void {
trace("on INIT");
if (result) { //already logged in because of existing session
outputTxt.text = "onInit, Logged In\n";
loginToggleBtn.label = "Log Out";
} else {
outputTxt.text = "onInit, Not Logged In\n";
}
}
No "on INIT" message...
Anyone ?
Get accessToken from FB.init in JS
Pass accessToken to Flash (swf params or ExternalInterface)
Call Facebook.init() with accessToken and set options.status = true
var options:Object = {};
var accessToken:String = null;
if(!StringValidator._isNullOrEmpty(m_swfParameters._accessToken))
{
options.status = true;
accessToken = m_swfParameters._accessToken;
}
Facebook.init(m_facebookProvider._appId, _onInitedCallback, options, accessToken);
if(options.status === true)
{
_onInitedCallback(options, null);
}

Adobe Air and Dropbox

I'm trying to integrate dropbox into my BB Playbook app using adobe air in flashbuilder 4.6. I got the API from http://code.google.com/p/dropbox-as3/wiki/EXAMPLES and I'm also using that example.
public function getRequestToken():void
{
dropAPI.requestToken();
var handler:Function = function (evt:DropboxEvent):void
{
dropAPI.removeEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, handler);
var obj:Object = evt.resultObject;
reqTokenKeyLabel.text = obj.key;
reqTokenSecretLabel.text = obj.secret;
// goto authorization web page to authorize, after that, call get access token
if (oauthRadioBtn.selected) {
Alert.show(dropAPI.authorizationUrl);
}
};
dropAPI.addEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, handler);
if (!dropAPI.hasEventListener(DropboxEvent.REQUEST_TOKEN_FAULT)) {
dropAPI.addEventListener(DropboxEvent.REQUEST_TOKEN_FAULT, faultHandler);
}
}
This executes as expected but I don't know how to go further, I tried sending the user to the link generated and I allow the application but the get access token still fails. I feel like there is missing code, how does my application know what the access token is? should I not be getting something back from dropbox when the user allows the application?
Once the user has accepted the app in the web browser you should call this function in order to get the access token and secret:
public function getAccessToken():void{
dropAPI.accessToken();
var handler:Function = function (evt:DropboxEvent):void{
dropAPI.removeEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, handler);
var obj:Object = evt.resultObject;
myAccessToken = obj.key;
myAccessSecret = obj.secret;
};
dropAPI.addEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, handler);
if (!dropAPI.hasEventListener(DropboxEvent.ACCESS_TOKEN_FAULT)) {
dropAPI.addEventListener(DropboxEvent.ACCESS_TOKEN_FAULT, faultHandler);
}
}
Once you have them you can save them for future use. After that you have establish connection with Dropbox.
I hope this will help you