Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Highlighted
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Simple Trial Manager

Here is a simple trial manager class for apps that want to post a simple way to manage a limited time the app works before they want them to purchase it.  The stress is on simple, since it does not do anything with remote activation and it can easily be fooled by changing the system clock.  It is meant for low priced applications since a more elaborate manager to deal with activation did not seem warranted if the app is < $2.  Also, a person will only change their system clock so often before it is not worth it each time for a < $2 app and how it will probably mess with other apps.  Also, uninstalling and reinstalling will reset the trial period, but again, a user will only do that so many times before they will give up and just buy the app.

 

So with those assumptions, to use the code in your app, after you set the native window to true:

var trial :AppWorldTryBuy = new AppWorldTryBuy( this.stage );
trial.trial_period = 14; // default is 7
trial.url_to_buy = 'http://appworld.blackberry.com/webstore/content/00000?lang=en';
trial.CheckTrialPeriod();

 The following code uses code from the playbook AS3 library that I posted some weeks back (http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Community-Library-Released-for-cl...).  You can modify this code to use native alert system and swap out how the preference file is saved.

 

package 
{
	import com.lib.playbook.dialog.AlertDialog;
	import com.lib.playbook.events.DialogEvent;
	import com.lib.playbook.file.PreferenceFile;
	
	import flash.desktop.NativeApplication;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.Timer;

  public class AppWorldTryBuy
  {
	private var _stage       : Stage;
	public  var installed_on : Date = new Date();
	public  var nag_prior    : int = 2;
	public  var trial_period : int = 7; // days
	public  var url_to_buy   : String = '';
		
		////////////////////////////////////////////////////////////////
	public function AppWorldTryBuy( stage :Stage )
	{
		this._stage = stage;
	}
		
		////////////////////////////////////////////////////////////////
	public function CheckTrialPeriod() : Boolean
	{
		var now   : Date = new Date();
		var alert : AlertDialog;

		// check trial start file
		if( PreferenceFile.Exists( 'trial.xml' ) )
		{
			var config : XML = PreferenceFile.Obtain( 'trial.xml' );
				
			this.installed_on.setTime( PreferenceFile.GetInteger( config, 'firstrun', 0 ) * 1000 );
				
			var days_from_install : Number = ( now.getTime() - this.installed_on.getTime() ) / ( 1000 * 60 * 60 * 24 );
				
			//
			// time expired or user changed the start date or current clock
			//
			if( days_from_install > this.trial_period || this.installed_on.getTime() > now.getTime() )
			{
		              alert = new AlertDialog();
			     alert.title = 'Trial Period Expired';
			     alert.message =   'Your trial period of ' + this.trial_period
					              + ' days has expired.\n\nYou may continue to use the application by purchasing a license from App World and download an un-restricted version.';
		              alert.addButton( 'Quit'    , {data:'quit'} );
				alert.addButton( 'Purchase', {data:'buy'} );
				alert.addEventListener( DialogEvent.DIALOG_BUTTON_CLICKED, TimeoutReply );
				alert.show();

				return false;
			}
			//
			// show nag screen
			//
			else if( days_from_install > ( this.trial_period - this.nag_prior ) )
			{
				// int( Math.ceil( days_diff ) )
				var days_left : int = this.trial_period - int( Math.ceil( days_from_install ) );
				alert = new AlertDialog();
				alert.title = 'Trial Period';
				alert.message =   'Your trial period of ' + this.trial_period
						+ ' days is about to expire in ' + days_left + ' day(s).\n\nThe trail version will continue to work until then.\n\nYou may purchase a license from App World and download an un-restricted version.';
				alert.addButton( 'Continue', {data:'continue'} );
				alert.addButton( 'Purchase', {data:'buy'} );
				alert.addEventListener( DialogEvent.DIALOG_BUTTON_CLICKED, NagReply );
				alert.show();
					
				return true;
			}
			else
			{
				return true;
			}
		
		//
		// first time
		//
		else
		{
			var init : String = '<trybuy>\n';
			init += PreferenceFile.TagInteger( 'firstrun', ( now.getTime() / 1000 ) );
			init += '</trybuy>';
			PreferenceFile.Store( 'trial.xml', init );
				
			// add EULA here:
				
			alert = new AlertDialog();
			alert.title = 'Trial Software';
			alert.message = 'This is trial software which you will be allowed to use for ' + this.trial_period
					+ ' days.\n\nNear the end of the trial period, you will be prompted to purchase the un-restricted version.';
			alert.addButton( 'OK' );
			alert.show();
				
			return true;
		}
	}
		
		////////////////////////////////////////////////////////////////
	private function NagReply( event : DialogEvent ) : void
	{
		if( event.responseBtnContext.data == 'buy' )
		{
			navigateToURL( new URLRequest( this.url_to_buy ) );
		}
	}
		
		///////////////////////////////////////////////////////////////
	private function TimeoutReply( event : DialogEvent ) : void
	{
		if( event.responseBtnContext.data == 'quit' )
		{
				//NativeApplication.nativeApplication.activeWindow.close();
			this._stage.nativeWindow.close();
		}
		else // purchase
		{
			navigateToURL( new URLRequest( this.url_to_buy ) );
			var delay :Timer = new Timer( 3000, 1 );
			delay.addEventListener(TimerEvent.TIMER_COMPLETE, CloseApp );
			delay.start();
		}
	}
		
		////////////////////////////////////////////////////////////////
	private function CloseApp( event : Event ) : void
	{
			//NativeApplication.nativeApplication.activeWindow.close();
			this._stage.nativeWindow.close();
	}
  }
}