Using the STF Class - Asynchronous Command Model

Add the ScreentimeScreensaver component (Flash, or swc for Flex )to your FLA (Flash, or project Flex) to add STF methods to ActionScript. The STF methods become effective once the SWF is compiled and run as a screensaver.


STF.setActiveWallpaper("cool_image.jpg");

If you are using a STF method with a return value, you will notice that these functions are asynchronous and return values return to "callback" event handlers.

To utilize a return value, register a callback function with an argument matching the data type of the STF function result. This function will be called by Screentime upon the completion of the command and will be passed the command result. The code calls STF.getPlayMode() to determine where the screensaver is playing: in the preview monitor, the settings window or as a screensaver.


STF.registerCallBack("getPlayMode", onGetPlayModeReturn, this);
function onGetPlayModeReturn(playMode:Number) {
	STF.trace(playMode);
	//1=Screensaver, 2=Preview, 3=Settings Wnd 
	switch (playMode) {
	case 1:
		saver_mc.loadMovie("screen_saver.swf");
		break;
	case 2:
		saver_mc.loadMovie("preview_monitor.swf");
		break;
	case 3:
		saver_mc.loadMovie("settings_window.swf");
		break;
	}
}
//register callback
STF.registerCallBack("getPlayMode", onGetPlayModeReturn, this);
STF.getPlayMode();

If you need results from two or more STF methods you will end up linking the callbacks together - the first method's callback calls the second STF method; the second method's callback calls the third STF method; and so on.

Dynamic Slide Show Example:

To download an image you will want to check for a net connection, download a series of images, and finally load the images. You will also want to have two display modes: 1) a placeholder animation played when images have not yet been downloaded and 2) a display image animation. To determine the mode you will want to read/write an images are ready flag to the local hard drive.

The step-by-step pseudo code is as follows:

STEP (1) - call STF.checkNetConnection.
STEP (2) - from checkNetConnection's callback call STF.downloadFile.
STEP (3) - from downloadFile's callback call downloadFile for the second time.
STEP (4) - from downloadFile's callback call displayImages (the basic screensaver animation).

STEP (*) - if connection is not availalble or STF.downloadFile fails, check if
images exist from previous download and set animation.

STEP (5) - do animation.

The ActionScript for this example is as follows:


// define variables
var isConnected:Boolean = false;
var imagesAreReady:Boolean = false;
var currentImageIndex:int = -1;
var ttlNumberOfImages:uint = 15;

// STEP (2)
// define STF.checkNetConnection callback
function onCheckNetConnectionReturn (returnValue:Boolean) {
	isConnected = returnValue;
	status_txt._visible = false;
	if (isConnected ) {
		downloadImage(isConnected);
	} else {
		setStatusText("no internet connection");
		startAnimation();
	}
};
// register STF.checkNetConnection callback
STF.registerCallBack("checkNetConnection",onCheckNetConnectionReturn,this);


// STEP (3, 4)
// define STF.downloadFile callback
function downloadImage(success:Boolean):void  {
	setStatusText("downloadImage");
	currentImageIndex ++;
	if (!success) {
		getImagesAreReadyFlag();
	} else if (currentImageIndex < ttlNumberOfImages) {
		setStatusText("downloading image: " + currentImageIndex);
		trace(imageArray[currentImageIndex]);
		localFileName_str = currentImageIndex +".jpg";
		remoteUrl_str = "http://www.screentime.com/temp/"+localFileName_str;
		STF.downloadFile(imageArray[currentImageIndex],remoteUrl_str);
	} else {
		setStatusText("downloading images complete");
		imagesAreReady = true;
		setImagesAreReadyFlag();
		startAnimation();
	}
}
// register STF.downloadFile callback
STF.registerCallBack("downloadFile", downloadImage, this);

// STEP (*)
// define STF.readString callback
// reads flag written when checkNetConnection and downloadFile succeed
function onGetImagesAreReadyFlag (returnValue:String):void  {
	if (returnValue == "imagesAreReady") {
		imagesAreReady = true;
	}
	startAnimation();
}
// register STF.readString callback
STF.registerCallBack("readString", onGetImagesAreReadyFlag, this);

function getImagesAreReadyFlag():void {
	// look to see if checkNetConnection and downloadFile have ever worked.
	STF.readString("saverInitialized.txt");
}
function setImagesAreReadyFlag():void {
	// set imagesAreReady = true
	writeString("saverInitialized.txt", "imagesAreReady");
}

// STEP (5)
// do animation.
function doImageAnimation():void  {
	// do something to display iamges
}
function doPlaceholderAnimation():void  {
	// do something when images have not been downloaded.
}
function startAnimation():void {
	if (imagesAreReady) {
		doImageAnimation();
	} else {
		doPlaceholderAnimation();
	}
}

function setStatusText():void  {
	// do something to display status text
}

// STEP (1)
// get things started by calling checkNetConnection
STF.checkNetConnection();
setStatusText("checking net connection...");