ActionScript / AIR - Determine Device Profile At Runtime? - actionscript-3

i'm developing an application for both desktop and mobile devices and would like to use the same code base for each build.
i want to employ cacheAsBitmapMatrix on some of my display objects, but cacheAsBitmapMatrix throws an error if it's included in an AIR application with a device profile other than mobileDevice or extendedMobileDevice.
something like the following would be ideal:
if (cacheAsBitmapMatrix.isSupported)
myDisplayObject.cacheAsBitmapMatrix = new Matrix();
update using try/catch:
try {myDisplayObject.cacheAsBitmapMatrix = new Matrix();}
catch(error:Error) {}
finally {myDisplayObject.cacheAsBitmap = true;}
update:
except for television profiles, this should work as well to distinguish between mobile and desktop:
//Resoslve Profile
if (Capabilities.os.indexOf("Windows") > -1 || Capabilities.os.indexOf("Mac") > -1 || Capabilities.os.indexOf("Linux") > -1)
trace("Desktop Profile");
else
trace("Mobile Profile");
update 2:
it seems the easiest way, and perhaps the most common way to determine the profile at runtime is to call:
NativeWindow.isSupported;
from the flash.display.NativeWindow documentation:
AIR profile support: This feature is
supported on all desktop operating
systems, but is not supported on
mobile devices or AIR for TV devices.
You can test for support at run time
on desktop devices using the
NativeWindow.isSupported property. See
AIR Profile Support for more
information regarding API support
across multiple profiles.
update 3:
while testing this on the BlackBerry PlayBook simulator, NativeWindow was supported. i haven't tested this on the device to know if it's was just supported on the simulator or not. i've since started using the following to determine the difference between mobile and desktop profiles:
if (
(Capabilities.os.toLowerCase().indexOf("mac") == -1) &&
(Capabilities.os.toLowerCase().indexOf("windows") == -1) &&
(Capabilities.os.toLowerCase().indexOf("linux") == -1)
)
deviceIsMobile = true;

This document specifies device capabilities for different profiles. Since cacheAsBitmapMatrix has no availability getter listed, you'll need to check it yourself once. It must be easy to do with try/catch block.
Edit: I'll try to illustrate what I meant under "check once":
public class Capabilities2
{
private static var cacheAsBitmapMatrixChecked:Boolean;
private static var cacheAsBitmapMatrixStatus:Boolean;
public static function get cacheAsBitmapMatrixIsSupported():Boolean
{
if (cacheAsBitmapMatrixChecked) return cacheAsBitmapMatrixStatus;
var test:Sprite = new Sprite();
try
{
text.cacheAsBitmapMatrix = new Matrix();
cacheAsBitmapMatrixStatus = true;
}
catch (error:Error)
{
cacheAsBitmapMatrixStatus = false;
}
cacheAsBitmapMatrixChecked = true;
return cacheAsBitmapMatrixStatus;
}
}
Get current profile might be cleaner solution, but I don't know how to do it. Another 'idea': using document above, test capabilities and deduce profile from results, like in Einstein riddle :)

For runtime checking if your application is on mobile or on web you can also use "Capabilities.playerType"
if (Capabilities.playerType == "Desktop") {
trace ("running on mobile");
}
else {
trace ("running on web");
}

Related

Content Settings in Chromium Browser

Is there a way to get/set the USB Hardware ID's for Mic/Camera in Chromium browser from the command line.
I could not able to find out. The requirement is to set a particular Camera/Mic during runtime without manually updating in chrome://settings/content in the chromium browser.
It will be great if I can get some link or some sample code. Thanks !!!
You can use the --use-fake-ui-for-media-stream CLI flag to suppress the permissions dialog from showing up when you have a media stream. It will allow by default.
In terms of selecting a particular microphone or camera with a CLI flag, I don't believe it's possible. However, you can access and select the device to be used in JavaScript.
The navigator.mediaDevices.enumerateDevices object returns the following information:
The device kind - audioinput or videoinput
The device label - e.g. Built-in Microphone, Logitech bla, FaceTime HD Camera, etc.
The device Id - e.g. bd9759740e6c29df7703d0bfg62vbbc726fcb9422fdb948a35550835a840b328
The device group Id - If 2+ devices share the same physical device, they can have group Id
The Id is generated per session, so it's not something we can hardcode, but the label is unlikely to change. This means you could traverse the list and get the device Id that matches the string. I did this with async/await but you can use Promises or transpile to ES5 if need be:
(async() => {
const getFaceTimeCamId = async() => {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const faceTimeCam = devices.find(device =>
device.kind === 'videoinput' && device.label === 'FaceTime HD Camera');
return (faceTimeCam && faceTimeCam.deviceId) || undefined;
} catch(err) {
console.log(`${err.name}: ${err.message}`);
}
}
console.log(await getFaceTimeCamId());
})();
You can then set the constraints object for your stream to include the deviceId you want, for example, when using MediaDevices.getUserMedia.
const constraints = {
video: { deviceId: { exact: faceTimeCamId } }
};
I've not fully tested this as I don't have another camera or mic at hand, but hopefully this is of some help. I don't know your exact use case so probably made some assumptions.

Does the MessageDialog class for UWP Apps support three buttons on Mobile?

I'm creating a simple program for reading text file on the Windows Phone. I decided to make it a Universal Windows Platform (UWP) App.
In the app, I have a very simple MessageDialog, with three options, Yes, No, Cancel. It works perfectly on the Desktop and in the Simulator. However, when testing with the actual device, the ShowAsync method fails with the message: "Value does not fall in the expected range".
This only happens if there are more than two commands registered in the dialog. Does the MessageDialog class really supports up to three commands - as the documentation suggests - or is this only applying for UWP Apps running on Desktop devices?
At the moment, there is a clear statement in the docs:
The dialog has a command bar that can support up to 3 commands in desktop apps, or 2 commands in mobile apps.
Sad but true: on mobiles, there are two commands only. Need more? Use ContentDialog instead.
It looks like the documentation is missing information about Mobile (and really the API should do a better job here).
For Mobile, if you hit the Back key you get a null return value, so you can do this (not recommended coding pattern, but best I can think of):
async Task Test()
{
const int YES = 1;
const int NO = 2;
const int CANCEL = 3;
var dialog = new MessageDialog("test");
dialog.Commands.Add(new UICommand { Label = "yes", Id = YES });
dialog.Commands.Add(new UICommand { Label = "no", Id = NO });
// Ugly hack; not really how it's supposed to be used.
// TODO: Revisit if MessageDialog API is updated in future release
var deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
if (deviceFamily.Contains("Desktop"))
{
dialog.Commands.Add(new UICommand { Label = "cancel", Id = CANCEL });
}
// Maybe Xbox 'B' button works, but I don't know so best to not do anything
else if (!deviceFamily.Contains("Mobile"))
{
throw new Exception("Don't know how to show dialog for device "
+ deviceFamily);
}
// Will return null if you press Back on Mobile
var result = await dialog.ShowAsync();
// C# 6 syntactic sugar to avoid some null checks
var id = (int)(result?.Id ?? CANCEL);
Debug.WriteLine("You chose {0}", id);
}

Application working on emulator but crashed on phone WP8

At start I'm sorry for my English is poor. And this is the only place where i solved the problem.
I have a problem with my application. I write and test it on emulator in VisualStudnio 2012 and It work fine. But when I add aplication in WindowsPhone store and I get to phone. It crashed. I think that problem is in geolocator or something with GPS, because when i use function where my it don't use gps it work. Everywhere where i use geolocator_geopositionchanged it break down and app is terminate. in one of application page i use map control but i gave token and application id but only in class where i use map.
private void maping_Loaded(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Maps.MapsSettings.ApplicationContext.ApplicationId = "id";
Microsoft.Phone.Maps.MapsSettings.ApplicationContext.AuthenticationToken = "token";
}
Do you have any sugestion or advices?
if you want watching app there is a link
http://www.windowsphone.com/pl-PL/store/app/opencaching/06bce1e1-16ef-4ebf-ac53-23b4c725f78b
I have geolocator in a few class it's one of them
Geolocator code
if (!tracking)
{
gps = new Geolocator();
gps.DesiredAccuracy = PositionAccuracy.High;
gps.ReportInterval = 100;
gps.PositionChanged += geolocator_PositionChanged;
}
else
{
gps.PositionChanged -= geolocator_PositionChanged;
gps = null;
}
tracking = !tracking;
Geoposition changed code
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
double distance = 0;
distance = point.GetDistanceTo(new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude));
string asa = Convert.ToInt64(distance).ToString();
if (asa != null)
{
Dispatcher.BeginInvoke(() =>
{
TBodleglosc.Text = asa +"m";
navi.Rotation = 180 + Kierunek(point.Latitude, point.Longitude, args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
});
}
}
Debug on your device. If that cannot repro, setup a Beta Test app and use that to distribute the app back to yourself for debugging. Sometimes signing breaks things.
I debug at lumia 920 and I have a problem with convert.toDouble
because there are was , i have . and vice versa
I think that it's connected with phone language
because in english emulator and Ertay Shashko phone who debug it yesterday it's working fine.
At now application work at phone but doesn't at emulator.
but if i change settings on location and language apps work but I can't debug because visual studio have error
It's weird .....

How to browse mobile directory in flex?

I have captured 3 videos on my mobile which is by default stored on the phone gallery (Gallery/videos/). I have to play these 3 videos in one of my flex mobile application. How can I get the videos to the flex project? if I need to browse the mobile directory means kindly help me with some code to do so.
I too am looking for an answer to this question. Right now, based on other Stackoverflow discussions, exhaustive perusal of tutorials and Adobe documentation, and comments to both (often the more useful resource), I'm coming to the conclusion that it's not possible.
you can use CameraRoll.browseForImage() and open the iOS gallery of photos to see all entities of MediaType.IMAGE, but it will not show you MediaType.VIDEO
you can use CameraUI to launch the system camera by delegation and that returns a MediaPromise, but as far as I can tell, it does not save the video you capture anywhere, and I cannot find a way to access the captured video using the MediaPromise (at least using the Loader class)
Here's my code as a hint in that direction. The second code block is using the CameraRoll to browseForImage() but there is no browseForVideo() in the API.
if(CameraUI.isSupported)
{
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE, videoMediaEventComplete);
camera.addEventListener(Event.CANCEL, cameraCanceled);
camera.addEventListener(ErrorEvent.ERROR, cameraError);
camera.launch(MediaType.VIDEO);
}
else
{
statusText.text = "Camera not supported on this device.";
startTimer();
}
if (CameraRoll.supportsBrowseForImage)
{
roll = new CameraRoll();
roll.addEventListener(MediaEvent.SELECT, cameraRollEventComplete);
roll.addEventListener(Event.CANCEL, cameraCanceled);
roll.addEventListener(ErrorEvent.ERROR, cameraError);
roll.browseForImage();
}
else
{
statusText.text = "Camera roll not supported on this device.";
startTimer();
}
I've since found that Videos captured using the delegated system camera are stored in a temporary storage location that iOS -DOES!- allow access to. (I was pleasantly shocked.)
The Captured video is not added to the device's Camera Roll as other videos captured using the iOS System Camera app, so it's not enough to capture video and expect to be able to access it later (if, for instance, CameraRoll.browseForVideo() is ever added to the API.
Therefore, you have to 'get while the getting is good' and move the file from the temporary storage location to some non-volatile location such as ApplicationStorageDirectory or the user's Documents directory (The only options in iOS I think).
The MediaPromise... I think... is completely useless for accessing the video via any direct progressive loader/streamer method, but still provides the location/url/path/filename of the temporary file so you can perform File operations on it.
Ironic that there are tutorials for getting around the lack of a file location/url/path/filename in the MediaPromise when using CameraRoll.browseForImage()... and that method is to use a loader class to load the image content (which you can then write out to a file), but when taking video, the video content is not accessible, and instead a file location/url/path/filename is provided. Ironic that there are nearly no resources I was able to find to help with this also. grumble
I'm going to include some code chunks w/o really editing them to strip out extraneous bits because it's way past when I need to be in bed, but I wanted you to have this. I may come clean it up later.
This section is in a Spark SkinnablePopUpContainer and I use the same click event for several buttons, thus the below 'case' is in the switch-case in that event handler function.
In case you are not familiar, the 'close(true, data)' is the method to close the SkinnablePopUpContainer, tell the parent/owner that the container was closed purposefully and that it should look for the data object being shared back (i.e., there are changes to be 'commit'ed).
case "cameraVideo":
{
if(CameraUI.isSupported)
{
camera = new CameraUI();
camera.addEventListener(MediaEvent.COMPLETE, videoMediaEventComplete);
camera.addEventListener(Event.CANCEL, cameraCanceled);
camera.addEventListener(ErrorEvent.ERROR, cameraError);
camera.launch(MediaType.VIDEO);
}
else
{
statusText.text = "Camera not supported on this device.";
startTimer();
}
break;
}
protected function cameraCanceled(event:Event):void
{
statusText.text = "Camera access canceled by user.";
startTimer();
}
protected function cameraError(event:ErrorEvent):void
{
statusText.text = "There was an error while trying to use the camera.";
startTimer();
}
protected function videoMediaEventComplete(event:MediaEvent):void
{
statusText.text="Preparing captured video...";
camera.removeEventListener(MediaEvent.COMPLETE, videoMediaEventComplete);
camera.removeEventListener(Event.CANCEL, cameraCanceled);
camera.removeEventListener(ErrorEvent.ERROR, cameraError);
var media:MediaPromise = event.data;
data.MediaType = MediaType.VIDEO;
data.MediaPromise = media;
data.source = "camera video";
close(true,data)
}
This section is the Actionscript in the close handler of the parent/owner of the SkinnablePopUpContainer (truncated once the useful code is included)
private function choosePictureLightboxClosed(event:PopUpEvent):void
{
imageButtonsActive = false;
if(event.commit)
{
this.data = event.data as Object;
filters = new Array();
selection = true;
switch(data.MediaType)
{
case MediaType.VIDEO:
{
mediaType = "video";
trace(data.MediaPromise.file.url + " - " + data.MediaPromise.relativePath + " - " +data.MediaPromise.mediaType);
var sourceFile:File = new File(data.MediaPromise.file.url);
var destinationFile:File = File.applicationStorageDirectory.resolvePath("User" +parentApplication.userid);
if(destinationFile.exists && !destinationFile.isDirectory)
{
destinationFile.deleteFile();
}
destinationFile.createDirectory();
destinationFile = destinationFile.resolvePath("Videos");
if(destinationFile.exists && !destinationFile.isDirectory)
{
destinationFile.deleteFile();
}
destinationFile.createDirectory();
destinationFile = destinationFile.resolvePath(parentApplication.userid+"Video"+new Date().getTime()+".mov");
trace(destinationFile.nativePath);
sourceFile.moveTo(destinationFile,true);
break;
}
I sure do hope this helps. This has been a very frustrating (and costly in terms of our project being government grant funded and having deadlines we utterly failed to meet), and I very much hope that these hard-won solutions might help others avoid the same experience.

Can I turn on WiFi-Direct from code? on Android API-14 (ICS)

I'm using the new Wi-Fi Direct API from google on Android 4.0
and in Sample code they send the User to Settings, to activate WiFi -Direct Mode.
Is there a way to Start it by code???
all they offer is to listen to WIFI_P2P_STATE_CHANGED_ACTION intent, and then use this code
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
} else {
// Wifi Direct mode is disabled
}
Yes there is a way using reflection. Works on my GSII (and fails gracefully on non Wifi Direct HTC Sensation) but as this is reflection it may not work on all phones.
p2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
channel = p2pManager.initialize(getApplicationContext(),
getMainLooper(), null);
try {
Class<?> wifiManager = Class
.forName("android.net.wifi.p2p.WifiP2pManager");
Method method = wifiManager
.getMethod(
"enableP2p",
new Class[] { android.net.wifi.p2p.WifiP2pManager.Channel.class });
method.invoke(p2pManager, channel);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Please note:
On Jelly Bean and above, when you try to use the WifiP2pManager API, WiFi-Direct is automatically enabled (as long as WiFi is on), so there is no need to use this hack.
No, all you could do is notify the user to turn on WiFi.
On some devices, even though Wi-Fi direct is supported, it's not enabled due to some system bugs. Here's a more reliable way to check whether it's enabled (unfortunately it requires root) using Kotlin.
val matcher = "^mNetworkInfo .* (isA|a)vailable: (true|false)"
.toPattern(Pattern.MULTILINE)
.matcher(su("dumpsys ${Context.WIFI_P2P_SERVICE}"))
if (!matcher.find()) return "Root unavailable"
if (matcher.group(2) != "true") return "Wi-Fi Direct unavailable"
return "Wi-Fi Direct available"
This should work for Android 4.3 - 8.1. Check source code below:
https://android.googlesource.com/platform/frameworks/base/+/f0afe4144d09aa9b980cffd444911ab118fa9cbe%5E%21/wifi/java/android/net/wifi/p2p/WifiP2pService.java
https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/a8d5e40/service/java/com/android/server/wifi/p2p/WifiP2pServiceImpl.java#639
https://android.googlesource.com/platform/frameworks/base.git/+/f0afe4144d09aa9b980cffd444911ab118fa9cbe/core/java/android/net/NetworkInfo.java#433
https://android.googlesource.com/platform/frameworks/base.git/+/220871a/core/java/android/net/NetworkInfo.java#415