protected void InitExtensions(MoSync.Core core, MoSync.Runtime runtime) { try { MoSync.ExtensionsLoader.Load(); } catch (Exception e) { MoSync.Util.CriticalError("Couldn't load extension: " + e.ToString()); } MoSync.ExtensionModule extMod = runtime.GetModule <MoSync.ExtensionModule>(); System.Reflection.Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (System.Reflection.Assembly a in assemblies) { try { foreach (Type t in a.GetTypes()) { IExtensionModule extensionGroupInstance = null; if (t.GetInterface("MoSync.IExtensionModule", false) != null) { extensionGroupInstance = Activator.CreateInstance(t) as IExtensionModule; extMod.AddModule(extensionGroupInstance); extensionGroupInstance.Init(core, runtime); } } } catch { } } }
/* * Checks if a handle is a valid handler (a valid handle shouldn't be negative). * @param runtime The current runtime * @param handle The handle to be checked */ private bool isHandleValid(Runtime runtime, int handle) { if (runtime.GetModule <NativeUIModule>().GetWidget(handle).GetHandle() < 0) { return(false); } return(true); }
/** * Adds an operation to the operation queue. * @param op The operation to be added. */ public void AddOperation(WidgetOperation op) { MoSync.Util.RunActionOnMainThread(() => { // we need to check if the widget was already created or not when the AddOperation is called // if so, we'll run the operation on the widget instead of inserting it into the queue IWidget widget = mRuntime.GetModule <NativeUIModule>().GetWidget(this.GetHandle()); int handle = this.GetHandle(); if (widget is WidgetBaseMock) { (widget as WidgetBaseMock).OperationQueue.Enqueue(op); } else { // we need to check if the widget we got from the runtime can be cast to WidgetBaseWindowsPhone if (typeof(WidgetBaseWindowsPhone).IsAssignableFrom(widget.GetType())) { (widget as WidgetBaseWindowsPhone).RunOperation(op); } } }, true); }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { ioctls.maFontSetCurrent = delegate(int _font) { FontModule.FontInfo finfo = runtime.GetModule <FontModule>().GetFont(_font); MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.FontFamily = finfo.family; textBlock.FontStyle = finfo.style; textBlock.FontWeight = finfo.weight; textBlock.FontSize = finfo.size; }); return(0); }; ioctls.maFrameBufferInit = delegate(int frameBufferPointer) { Syscalls syscalls = runtime.GetSyscalls(); mOldUpdateScreenImplementation = syscalls.maUpdateScreen; syscalls.maUpdateScreen = delegate() { #if !LIB Memory mem = core.GetDataMemory(); #else SystemMemory mem = core.GetDataMemory(); #endif int[] dst = mFrontBuffer.Pixels; //mFrontBuffer.FromByteArray(mem.GetData(), frameBufferPointer, dst.Length * 4); #if !LIB System.Buffer.BlockCopy(mem.GetData(), frameBufferPointer, dst, 0, dst.Length * 4); #else byte[] bytes = new byte[dst.Length * 4]; mem.ReadBytes(bytes, frameBufferPointer, dst.Length * 4); System.Buffer.BlockCopy(bytes, 0, dst, 0, dst.Length * 4); //TO BE TESTED #endif const int opaque = (int)(0xff << 24); for (int i = 0; i < dst.Length; i++) { dst[i] |= opaque; } InvalidateWriteableBitmapBackBufferOnMainThread(mFrontBuffer); WriteableBitmap temp = mFrontBuffer; mFrontBuffer = mBackBuffer; mBackBuffer = temp; }; return(1); }; ioctls.maFrameBufferClose = delegate() { if (mOldUpdateScreenImplementation == null) { return(0); } Syscalls syscalls = runtime.GetSyscalls(); syscalls.maUpdateScreen = mOldUpdateScreenImplementation; mOldUpdateScreenImplementation = null; if (mCurrentDrawTarget == mFrontBuffer) { mCurrentDrawTarget = mBackBuffer; } System.Buffer.BlockCopy(mBackBuffer.Pixels, 0, mFrontBuffer.Pixels, 0, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight * 4); InvalidateWriteableBitmapBackBufferOnMainThread(mFrontBuffer); return(1); }; ioctls.maFrameBufferGetInfo = delegate(int info) { #if !LIB Memory mem = core.GetDataMemory(); #else SystemMemory mem = core.GetDataMemory(); #endif mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.sizeInBytes, mBackBuffer.PixelWidth * mBackBuffer.PixelHeight * 4); mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.bytesPerPixel, 4); mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.bitsPerPixel, 32); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.redMask, 0x00ff0000); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.redBits, 8); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.redShift, 16); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.greenMask, 0x0000ff00); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.greenBits, 8); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.greenShift, 8); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.blueMask, 0x000000ff); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.blueBits, 8); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.blueShift, 0); mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.width, mBackBuffer.PixelWidth); mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.height, mBackBuffer.PixelHeight); mem.WriteInt32(info + MoSync.Struct.MAFrameBufferInfo.pitch, mBackBuffer.PixelWidth * 4); mem.WriteUInt32(info + MoSync.Struct.MAFrameBufferInfo.supportsGfxSyscalls, 0); return(1); }; }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { /** * Shows a dialog widget. * \param _dialogHandle The handle of the dialog that will be shown. * * \returns Any of the following result codes: * - #MAW_RES_OK if the child could be removed from the parent. * - #MAW_RES_INVALID_HANDLE if the handle was invalid. * - #MAW_RES_ERROR otherwise. */ ioctls.maWidgetModalDialogShow = delegate(int _dialogHandle) { if (!isHandleValid(runtime, _dialogHandle)) { return(MoSync.Constants.MAW_RES_INVALID_HANDLE); } MoSync.Util.RunActionOnMainThreadSync(() => { ModalDialog modalDialog = ((ModalDialog)runtime.GetModule <NativeUIModule>().GetWidget(_dialogHandle)); if (modalDialog.Visible.Equals("false")) { // show the dialog modalDialog.ShowDialog(true); // set the dialog visibility modalDialog.Visible = "true"; } }); return(MoSync.Constants.MAW_RES_OK); }; /** * Hides/Dismisses a currently displayed dialog. * \param _dialogHandle The handle of the dialog that will be hidden. * * \returns Any of the following result codes: * - #MAW_RES_OK if the child could be removed from the parent. * - #MAW_RES_INVALID_HANDLE if the handle was invalid. * - #MAW_RES_ERROR otherwise. */ ioctls.maWidgetModalDialogHide = delegate(int _dialogHandle) { if (!isHandleValid(runtime, _dialogHandle)) { return(MoSync.Constants.MAW_RES_INVALID_HANDLE); } MoSync.Util.RunActionOnMainThreadSync(() => { ModalDialog modalDialog = ((ModalDialog)runtime.GetModule <NativeUIModule>().GetWidget(_dialogHandle)); if (modalDialog.Visible.Equals("true")) { // hide the dialog modalDialog.ShowDialog(false); // set the dialog visibility modalDialog.Visible = "false"; } }); return(MoSync.Constants.MAW_RES_OK); }; }
/** * Initializing the ioctls. */ public void Init(Ioctls ioctls, Core core, Runtime runtime) { mCamera = new PhotoCamera(mCameraType); mVideoBrush = new VideoBrush(); runtime.RegisterCleaner(delegate() { if (null != mCamera) { mCamera.Dispose(); mCamera = null; } }); mRuntime = runtime; PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage); // set the initial camera orientation in respect to the current page orientation SetInitialCameraOrientation(currentPage); // handle current page orientation and adjust the camera orientation accordingly HandleDeviceOrientation(currentPage); /** * Stores an output format in fmm parameter. * @param _index int the index of the required format. * @param _fmt int the momory address at which to write the output format dimensions. * * Note: the _index should be greater than 0 and smaller than the number of camera formats. */ ioctls.maCameraFormat = delegate(int _index, int _fmt) { System.Windows.Size dim; if (GetCameraFormat(_index, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.width, (int)dim.Width); core.GetDataMemory().WriteInt32(_fmt + MoSync.Struct.MA_CAMERA_FORMAT.height, (int)dim.Height); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Returns the number of different output formats supported by the current device's camera. * \< 0 if there is no camera support. * 0 if there is camera support, but the format is unknown. */ ioctls.maCameraFormatNumber = delegate() { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { // because the cammera is supported but not initialized, we return 0 return(0); } IEnumerable <System.Windows.Size> res = mCamera.AvailableResolutions; if (res == null) { return(0); } IEnumerator <System.Windows.Size> resolutions = res.GetEnumerator(); resolutions.MoveNext(); int number = 0; while (resolutions.Current != null) { number++; resolutions.MoveNext(); if (resolutions.Current == new System.Windows.Size(0, 0)) { break; } } return(number); }; /** * Starts the viewfinder and the camera */ ioctls.maCameraStart = delegate() { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StartViewFinder(); }); return(0); }; /** * stops the view finder and the camera. */ ioctls.maCameraStop = delegate() { if (isCameraSnapshotInProgress) { // We need to post snapshot failed if the camera was stopped during snapshot operation postSnapshotEvent(snapshotPlaceHolder, mCamera.Resolution, MoSync.Constants.MA_IMAGE_REPRESENTATION_UNKNOWN, MoSync.Constants.MA_CAMERA_RES_FAILED); isCameraSnapshotInProgress = false; } MoSync.Util.RunActionOnMainThreadSync(() => { mCameraPrev.StopViewFinder(); }); return(0); }; /** * Adds a previewWidget to the camera controller in devices that support native UI. */ ioctls.maCameraSetPreview = delegate(int _widgetHandle) { // if the camera is not initialized, we need to initialize it before // setting the preview if (!isCameraInitialized) { InitCamera(); } IWidget w = runtime.GetModule <NativeUIModule>().GetWidget(_widgetHandle); if (w.GetType() != typeof(MoSync.NativeUI.CameraPreview)) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCameraPrev = (NativeUI.CameraPreview)w; mCameraPrev.SetViewFinderContent(mVideoBrush); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Returns the number of available Camera on the device. */ ioctls.maCameraNumber = delegate() { if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) { return(2); } else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) || PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) { return(1); } return(0); }; /** * Captures an image and stores it as a new data object in the * supplied placeholder. * @param _formatIndex int the required format. * @param _placeHolder int the placeholder used for storing the image. */ ioctls.maCameraSnapshot = delegate(int _formatIndex, int _placeHolder) { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } // If MA_CAMERA_SNAPSHOT_MAX_SIZE is sent via _formatIndex then we // need to select the biggest available snapshot size/resolution. if (MoSync.Constants.MA_CAMERA_SNAPSHOT_MAX_SIZE == _formatIndex) { _formatIndex = (int)ioctls.maCameraFormatNumber() - 1; } AutoResetEvent are = new AutoResetEvent(false); System.Windows.Size dim; if (GetCameraFormat(_formatIndex, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) { mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; } mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { rotateAngle = 90; } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); }); are.Set(); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); are.WaitOne(); return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Captures an image and stores it as a new data object in new * placeholder that is sent via #EVENT_TYPE_CAMERA_SNAPSHOT event. * @param _placeHolder int the placeholder used for storing the image. * @param _sizeIndex int the required size index. */ ioctls.maCameraSnapshotAsync = delegate(int _placeHolder, int _sizeIndex) { if (isCameraSnapshotInProgress) { return(MoSync.Constants.MA_CAMERA_RES_SNAPSHOT_IN_PROGRESS); } // If MA_CAMERA_SNAPSHOT_MAX_SIZE is sent via _sizeIndex then we // need to select the biggest available snapshot size/resolution. if (MoSync.Constants.MA_CAMERA_SNAPSHOT_MAX_SIZE == _sizeIndex) { _sizeIndex = (int)ioctls.maCameraFormatNumber() - 1; } System.Windows.Size dim; if (GetCameraFormat(_sizeIndex, out dim) == false) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } mCamera.Resolution = dim; if (mCameraSnapshotDelegate != null) { mCamera.CaptureImageAvailable -= mCameraSnapshotDelegate; } mCameraSnapshotDelegate = delegate(object o, ContentReadyEventArgs args) { MoSync.Util.RunActionOnMainThreadSync(() => { // If the camera was stopped and this delegate was still called then we do nothing here // because in maCameraStop we already send the snapshot failed event. if (!isCameraSnapshotInProgress) { return; } Stream data = null; try { // as the camera always takes a snapshot in landscape left orientation, // we need to rotate the resulting image 90 degrees for a current PortraitUp orientation // and 180 degrees for a current LandscapeRight orientation int rotateAngle = 0; if (currentPage.Orientation == PageOrientation.PortraitUp) { // This is for the front camera. if (mCamera.CameraType != CameraType.Primary) { rotateAngle = 270; } else { rotateAngle = 90; } } else if (currentPage.Orientation == PageOrientation.LandscapeRight) { rotateAngle = 180; } // if the current page is in a LandscapeLeft orientation, the orientation angle will be 0 data = RotateImage(args.ImageStream, rotateAngle); } catch { // the orientation angle was not a multiple of 90 - we keep the original image data = args.ImageStream; } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeHolder); MemoryStream dataMem = new MemoryStream((int)data.Length); MoSync.Util.CopySeekableStreams(data, 0, dataMem, 0, (int)data.Length); res.SetInternalObject(dataMem); postSnapshotEvent(_placeHolder, mCamera.Resolution, MoSync.Constants.MA_IMAGE_REPRESENTATION_RAW, MoSync.Constants.MA_CAMERA_RES_OK); isCameraSnapshotInProgress = false; }); }; mCamera.CaptureImageAvailable += mCameraSnapshotDelegate; mCamera.CaptureImage(); snapshotPlaceHolder = _placeHolder; isCameraSnapshotInProgress = true; return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Sets the property represented by the string situated at the * _property address with the value situated at the _value address. * @param _property int the property name address * @param _value int the value address * * Note: the fallowing properties are not available on windows phone * MA_CAMERA_FOCUS_MODE, MA_CAMERA_IMAGE_FORMAT, MA_CAMERA_ZOOM, * MA_CAMERA_MAX_ZOOM. */ ioctls.maCameraSetProperty = delegate(int _property, int _value) { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } String property = core.GetDataMemory().ReadStringAtAddress(_property); String value = core.GetDataMemory().ReadStringAtAddress(_value); if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_MODE)) { if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_ON) && mCamera.IsFlashModeSupported(FlashMode.On)) { mCamera.FlashMode = FlashMode.On; mFlashMode = FlashMode.On; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_OFF) && mCamera.IsFlashModeSupported(FlashMode.Off)) { mCamera.FlashMode = FlashMode.Off; mFlashMode = FlashMode.Off; } else if (value.Equals(MoSync.Constants.MA_CAMERA_FLASH_AUTO) && mCamera.IsFlashModeSupported(FlashMode.Auto)) { mCamera.FlashMode = FlashMode.Auto; mFlashMode = FlashMode.Auto; } else { return(MoSync.Constants.MA_CAMERA_RES_INVALID_PROPERTY_VALUE); } return(MoSync.Constants.MA_CAMERA_RES_OK); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FOCUS_MODE)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_IMAGE_FORMAT)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } else { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } }; /** * Selects a camera from the avalable ones; * in this eigther the back or the front camera is * chosen */ ioctls.maCameraSelect = delegate(int _camera) { // if the camera is not initialized, we cannot access any of its properties if (!isCameraInitialized) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } if (MoSync.Constants.MA_CAMERA_CONST_BACK_CAMERA == _camera) { if (mCamera.CameraType != CameraType.Primary) { mCameraType = CameraType.Primary; InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { SetInitialCameraOrientation(currentPage); } ); } } else if (MoSync.Constants.MA_CAMERA_CONST_FRONT_CAMERA == _camera) { if (mCamera.CameraType != CameraType.FrontFacing) { mCameraType = CameraType.FrontFacing; InitCamera(); MoSync.Util.RunActionOnMainThreadSync(() => { SetInitialCameraOrientation(currentPage); } ); } } else { return(MoSync.Constants.MA_CAMERA_RES_FAILED); } return(MoSync.Constants.MA_CAMERA_RES_OK); }; /** * Retrieves the specified property value in the given buffer. * @param _property int the address for the property string * @param _value int the address for the property value string (the buffer) * @param _bufSize int the buffer size */ ioctls.maCameraGetProperty = delegate(int _property, int _value, int _bufSize) { String property = core.GetDataMemory().ReadStringAtAddress(_property); if (property.Equals(MoSync.Constants.MA_CAMERA_MAX_ZOOM)) { core.GetDataMemory().WriteStringAtAddress( _value, "0", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_ZOOM_SUPPORTED)) { core.GetDataMemory().WriteStringAtAddress( _value, "false", _bufSize); } else if (property.Equals(MoSync.Constants.MA_CAMERA_FLASH_SUPPORTED)) { /* * Since we cannot see if flash is supported because the camera may be not * fully initialized when this is called, we assume that each windows phone * has flash support for primary camera but not for the from camera. */ String result = "true"; if (mCamera.CameraType != CameraType.Primary) { result = "false"; } core.GetDataMemory().WriteStringAtAddress( _value, result, _bufSize); } else { return(MoSync.Constants.MA_CAMERA_RES_PROPERTY_NOTSUPPORTED); } return(0); }; ioctls.maCameraRecord = delegate(int _stopStartFlag) { return(MoSync.Constants.MA_CAMERA_RES_FAILED); }; }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { /** * @brief Creates a new banner. * @param bannerSize One of the MA_ADS_SIZE_ constants. Only for Android and WP7.1 platforms. * @param publisherID Only for Android and WP 7.1 platforms. * This param is ignored on iOS platform. * * @note A banner is a widget type object. * For more info see Widget API. * * @returns * - #MA_ADS_RES_UNSUPPORTED if ads are not supported on current system. * - #MA_ADS_RES_ERROR if a error occurred while creating the banner widget. * - a handle to a new banner widget(the handle value is >= 0). */ ioctls.maAdsBannerCreate = delegate(int _bannerSize, int _publisherID) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd = new NativeUI.Ad(); // If the banner size is a known windows phone 7 size, we set it. // The default value is 480*80 (XX-Large banner). switch (_bannerSize) { case MoSync.Constants.MA_ADS_SIZE_WP7_XLARGE: mAd.Width = 300; mAd.Height = 50; break; case MoSync.Constants.MA_ADS_SIZE_WP7_XXLARGE: mAd.Width = 480; mAd.Height = 80; break; default: mAd.Width = 480; mAd.Height = 80; break; } // the publisherID for windows phone contains two components separated by '|'. // The first one represents the application ID and the second one the ad unit ID. // The publisher ID structure(ex): f532778c-7db5-4a8b-a292-a45a684ed890 // The ad unit ID structure(ex): 81103 String publisherID = core.GetDataMemory().ReadStringAtAddress(_publisherID); string[] values = publisherID.Split('|'); // only if both values are present we set the properties if (2 == values.Length) { mAd.ApplicationID = values[0]; mAd.AdUnitID = values[1]; } } ); int handle = runtime.GetModule <NativeUIModule>().AddWidget(mAd); // if the handles is smaller than 0, the widget was not added to the layout if (handle < 0) { return(MoSync.Constants.MA_ADS_RES_ERROR); } mAd.SetHandle(handle); mAd.SetRuntime(runtime); return(handle); }; /** * @brief Destroy a banner. * * @param bannerHandle Handle to a banner. * * @returns One of the next constants: * - #MA_ADS_RES_OK if no error occurred. * - #MA_ADS_RES_INVALID_BANNER_HANDLE if the banner handle is invalid. */ ioctls.maAdsBannerDestroy = delegate(int _bannerHandler) { if (!isHandleValid(runtime, _bannerHandler)) { return(MoSync.Constants.MA_ADS_RES_INVALID_BANNER_HANDLE); } mAd = null; return(MoSync.Constants.MA_ADS_RES_OK); }; /** * @brief Add a banner to a layout widget. * * @param bannerHandle Handle to a banner. * @param layoutHandle Handle to a layout. * * @returns One of the next constants: * - #MA_ADS_RES_OK if no error occurred. * - #MA_ADS_RES_INVALID_BANNER_HANDLE if the banner handle is invalid. * - #MA_ADS_RES_INVALID_LAYOUT_HANDLE if the layout handle is invalid. */ ioctls.maAdsAddBannerToLayout = delegate(int _bannerHandle, int _layoutHandle) { // we first check if both the banner and the layout are widgets with a valid handle if (!isHandleValid(runtime, _bannerHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_BANNER_HANDLE); } if (!isHandleValid(runtime, _layoutHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_LAYOUT_HANDLE); } // add the banner to the parent widget runtime.GetModule <NativeUIModule>().GetWidget(_layoutHandle).AddChild( runtime.GetModule <NativeUIModule>().GetWidget(_bannerHandle)); // set the parent of the banner to be the layout on which is added mAd.SetParent(runtime.GetModule <NativeUIModule>().GetWidget(_layoutHandle)); return(MoSync.Constants.MA_ADS_RES_OK); }; /** * @brief Remove a banner from a layout widget. * * @param bannerHandle Handle to a banner. * @param layoutHandle Handle to a layout. * * @returns One of the next constants: * - #MA_ADS_RES_OK if no error occurred. * - #MA_ADS_RES_INVALID_BANNER_HANDLE if the banner handle is invalid. * - #MA_ADS_RES_INVALID_LAYOUT_HANDLE if the layout handle is invalid. */ ioctls.maAdsRemoveBannerFromLayout = delegate(int _bannerHandle, int _layoutHandle) { // we first check if both the banner and the layout are widgets with a valid handle if (!isHandleValid(runtime, _bannerHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_BANNER_HANDLE); } if (!isHandleValid(runtime, _layoutHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_LAYOUT_HANDLE); } runtime.GetModule <NativeUIModule>().GetWidget(_layoutHandle).RemoveChild( runtime.GetModule <NativeUIModule>().GetWidget(_bannerHandle)); return(MoSync.Constants.MA_ADS_RES_OK); }; /** * @brief Set a banner property. * * @param bannerHandle Handle to the banner. * @param property A string representing which property to set. * @param value The value that will be assigned to the property. * * @returns One of the next result codes: * - #MA_ADS_RES_OK if no error occurred. * - #MA_ADS_RES_INVALID_BANNER_HANDLE if the banner handle is invalid. * - #MA_ADS_RES_INVALID_PROPERTY_NAME if the property name is not valid. * - #MA_ADS_RES_INVALID_PROPERTY_VALUE if the property value is not valid. */ ioctls.maAdsBannerSetProperty = delegate(int _bannerHandle, int _property, int _value) { // check if the banner is a widget with a valid handle MoSync.NativeUI.Ad ad = (MoSync.NativeUI.Ad)runtime.GetModule <NativeUIModule>().GetWidget(_bannerHandle); if (!isHandleValid(runtime, _bannerHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_BANNER_HANDLE); } String property = core.GetDataMemory().ReadStringAtAddress(_property); // based on the string 'property' we set the ones that can be set on WP 7.1 // if a property is not available, we return MA_ADS_RES_INVALID_PROPERTY_NAME string value = ""; int intValue = -1; switch (property) { case MoSync.Constants.MA_ADS_HEIGHT: value = core.GetDataMemory().ReadStringAtAddress(_value); intValue = -1; int.TryParse(value, out intValue); if (intValue >= 0) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Height = intValue; } ); } else { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } break; case MoSync.Constants.MA_ADS_WIDTH: value = core.GetDataMemory().ReadStringAtAddress(_value); intValue = -1; int.TryParse(value, out intValue); if (intValue >= 0) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Width = intValue; } ); } else { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } break; case MoSync.Constants.MA_ADS_VISIBLE: value = core.GetDataMemory().ReadStringAtAddress(_value).ToLower(); if (value.Equals("true")) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Visible = "true"; } ); } else if (value.Equals("false")) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Visible = "false"; } ); } else { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } break; case MoSync.Constants.MA_ADS_ENABLED: value = core.GetDataMemory().ReadStringAtAddress(_value).ToLower(); if (value.Equals("true")) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Enabled = "true"; } ); } else if (value.Equals("false")) { MoSync.Util.RunActionOnMainThreadSync(() => { mAd.Enabled = "false"; } ); } else { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } break; case MoSync.Constants.MA_ADS_COLOR_BG: value = core.GetDataMemory().ReadStringAtAddress(_value); if (!IsHexColor(value)) { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } MoSync.Util.RunActionOnMainThreadSync(() => { mAd.BackgroundColor = value; } ); break; case MoSync.Constants.MA_ADS_COLOR_BORDER: value = core.GetDataMemory().ReadStringAtAddress(_value); if (!IsHexColor(value)) { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } MoSync.Util.RunActionOnMainThreadSync(() => { mAd.BorderColor = value; } ); break; case MoSync.Constants.MA_ADS_COLOR_TEXT: value = core.GetDataMemory().ReadStringAtAddress(_value); if (!IsHexColor(value)) { return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_VALUE); } MoSync.Util.RunActionOnMainThreadSync(() => { mAd.TextColor = value; } ); break; default: return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_NAME); } return(MoSync.Constants.MA_ADS_RES_OK); }; /** * @brief Retrieves a specified property from the given banner. * * @param bannerHandle Handle to the banner. * @param property A string representing for which property to get the value. * @param value A buffer that will hold the value of the property, represented as a string. * @param bufSize Size of the buffer. * * @returns One of the next result codes: * - #MA_ADS_RES_OK if no error occurred. * - #MA_ADS_RES_INVALID_BANNER_HANDLE if the banner handle is invalid. * - #MA_ADS_RES_INVALID_PROPERTY_NAME if the property name is not valid. * - #MA_ADS_RES_INVALID_STRING_BUFFER_SIZE if the buffer size was to small. */ ioctls.maAdsBannerGetProperty = delegate(int _bannerHandle, int _property, int _value, int _bufSize) { MoSync.NativeUI.Ad ad = (MoSync.NativeUI.Ad)runtime.GetModule <NativeUIModule>().GetWidget(_bannerHandle); if (!isHandleValid(runtime, _bannerHandle)) { return(MoSync.Constants.MA_ADS_RES_INVALID_BANNER_HANDLE); } String property = core.GetDataMemory().ReadStringAtAddress(_property); string stringvalue = ""; switch (property) { case MoSync.Constants.MA_ADS_HEIGHT: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = ((int)mAd.Height).ToString(); } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_WIDTH: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = ((int)mAd.Width).ToString(); } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_VISIBLE: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = mAd.Visible; } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_ENABLED: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = mAd.Enabled; } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_COLOR_BG: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = mAd.BackgroundColor.ToString(); } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_COLOR_BORDER: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = mAd.BorderColor.ToString(); } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; case MoSync.Constants.MA_ADS_COLOR_TEXT: stringvalue = ""; MoSync.Util.RunActionOnMainThreadSync(() => { stringvalue = mAd.TextColor.ToString(); } ); core.GetDataMemory().WriteStringAtAddress( _value, stringvalue, _bufSize); break; default: return(MoSync.Constants.MA_ADS_RES_INVALID_PROPERTY_NAME); } return(MoSync.Constants.MA_ADS_RES_OK); }; }