You would like to alert the user of something important that is occurring within the application by utilizing a flashing icon within the dock or system tray.
This is a custom solution that will utilize a filter to create a second icon that is colored in reverse of the original and a timer to swap the application icon from the original, to the reversed, and back.
The IconManager class below is a part of the everythingflexairlib.swc library which is hosted on Google Code. For more information, please visit http://blog.everythingflex.com/air-central/everythingflexair1swc/
The example below simply embeds a few pngs and sets their bitmapData into an Array named defaultSysDockIconBitmaps to be used as application icons. An instance of the IconManager is created and the defaultSysDockIconBitmaps are passed into the constructor. There is a second optional argument that allows you to pass in a second set of icons to be used as teh alternate icons that will sequence with the default set during an alert. If only the default icons are passed in, the second set is created in reverse color of the original by using a ColorMatrixFilter.
A basic example would then simply call the startAlert method on the IconManager. However, in this example, I have included a timer so that after you click on the startAlert method, you will have time to put focus onto a different window which will allow the bounce functionality to work if you are on a Mac. This example also passes in both optional arguments to the startAlert method. The first will be used on Windows machine's as the toolTip for the system tray icon. The second defines the Notification type that will be used when the operating system is a Mac.
Although the IconManager class is defined below, I would recommend that you use the compiled swc file available at http://blog.everythingflex.com/air-central/everythingflexair1swc/
Usage of IconManager:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
width="200" height="100">
<mx:Script>
<![CDATA[
import com.everythingflex.air.managers.IconManager;
[Embed(source="assets/icons/AIRApp_16.png")]
private var Icon16:Class;
private var bitmap16:Bitmap = new Icon16();
[Embed(source="assets/icons/AIRApp_32.png")]
private var Icon32:Class;
private var bitmap32:Bitmap = new Icon32();
[Embed(source="assets/icons/AIRApp_48.png")]
private var Icon48:Class;
private var bitmap48:Bitmap = new Icon48();
[Embed(source="assets/icons/AIRApp_128.png")]
private var Icon128:Class;
private var bitmap128:Bitmap = new Icon128();
private var defaultSysDockIconBitmaps:Array = [bitmap16.bitmapData,
bitmap32.bitmapData,
bitmap48.bitmapData,
bitmap128.bitmapData];
private var i:IconManager = new IconManager(defaultSysDockIconBitmaps);
private var timer:Timer;
private function testAlert():void{
timer = new Timer(3000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete);
timer.start();
}
private function timerComplete(event:TimerEvent):void{
/**
* set toolTip to 'ALERT! ALERT!' (Windows Only)
* set NotificationType to CRITICAL (Mac Only)
*/
i.startAlert('ALERT! ALERT!',NotificationType.CRITICAL)
}
]]>
</mx:Script>
<mx:Button click="testAlert()" label="Start Alert"
horizontalCenter="0" verticalCenter="0"/>
</mx:WindowedApplication>
IconManager Class
/*
Copyright (c) 2008 EverythingFlex.com.
http://code.google.com/p/everythingflexairlib/
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.everythingflex.air.managers
{
import flash.desktop.NativeApplication;
import flash.desktop.NotificationType;
import flash.desktop.SystemTrayIcon;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.TimerEvent;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Timer;
public class IconManager
{
/**
* @private
* stores the Array of bitmaps tobe used as icons
*/
private var sysDockIconBitmaps:Array = new Array();
/**
* @private
* stores the Array of altered bitmaps tobe used as icons
*/
private var alteredSysDockIconBitmaps:Array = new Array();
/**
* @private
* stores the alertType, defaults to NotificationType.CRITICAL
*/
private var alertType:String = NotificationType.CRITICAL;
/**
* @private
* used as an indicator to determine which state of icon to display
*/
private var _count:int;
/**
* @private
* timer used to alternate between sysDockIcon and alteredSysDockIcon
*/
private static var ALERT_TIMER:Timer;
/**
* Constructor.
*/
public function IconManager(sysDockIconBitmaps:Array,
alteredSysDockIconBitmaps:Array=null,
alertType:String="critical") {
this.sysDockIconBitmaps = sysDockIconBitmaps;
this.alteredSysDockIconBitmaps = alteredSysDockIconBitmaps;
this.alertType = alertType;
handleIcons();
}
/**
* @private
* called by constructor to initialize the icons sets
*/
private function handleIcons():void{
stopAlert();
if(NativeApplication.supportsDockIcon || NativeApplication.supportsSystemTrayIcon){
if(sysDockIconBitmaps.length > 0){
NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps;
if(alteredSysDockIconBitmaps == null){
alteredSysDockIconBitmaps = new Array();
for (var i:int=0; i<sysDockIconBitmaps.length;i++){
alteredSysDockIconBitmaps.push(applyAlertFilter(sysDockIconBitmaps[i].clone(),i));
}
}
}
}
}
/**
* @private
* alters icon to reverse of original using a filter
*/
private function applyAlertFilter(bitmapData:BitmapData,i:int):BitmapData {
var matrix:Array = new Array();
matrix = matrix.concat([-1, 0, 0, 0, 255]);
matrix = matrix.concat([0, -1, 0, 0, 255]);
matrix = matrix.concat([0, 0, -1, 0, 255]);
matrix = matrix.concat([0, 0, 0, 1, 0]);
var r:Rectangle;
if(i == 0)r= new Rectangle(0,0,16,16);
if(i == 1)r= new Rectangle(0,0,32,32);
if(i == 2)r= new Rectangle(0,0,48,48);
if(i == 3)r= new Rectangle(0,0,128,128);
bitmapData.applyFilter(bitmapData,r,new Point(),new ColorMatrixFilter(matrix));
return new Bitmap(bitmapData).bitmapData;
}
/**
* @public
* starts an alert and shows toolTip message (Windows Only)
*/
public function startAlert(message:String="Alert"):void{
IconManager.ALERT_TIMER = new Timer(500,0);
IconManager.ALERT_TIMER.addEventListener(TimerEvent.TIMER,changeIcon)
IconManager.ALERT_TIMER.start();
if(NativeApplication.supportsSystemTrayIcon){
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = message;
}
}
/**
* @public
* stops an alert
*/
public function stopAlert():void{
if(NativeApplication.supportsSystemTrayIcon){
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "";
}
try{
IconManager.ALERT_TIMER.stop();
} catch (e:Error){
}
}
/**
* @private
* timer handler, alters state of icon
*/
private function changeIcon(event:TimerEvent):void{
if(_count == 0){
if(sysDockIconBitmaps.length){
NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps;
}
_count = 1;
} else {
if(alteredSysDockIconBitmaps.length){
NativeApplication.nativeApplication.icon.bitmaps = alteredSysDockIconBitmaps;
}
_count = 0;
}
}
}
}
+