<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Ward &#187; Flex</title>
	<atom:link href="http://www.jamesward.com/category/ria/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamesward.com</link>
	<description>Heroku &#124; Java &#124; Scala &#124; Cloud &#124; Open Source &#124; Linux</description>
	<lastBuildDate>Fri, 03 Feb 2012 19:29:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Flex AOP and Puzzlers at Flash Camp Italy</title>
		<link>http://www.jamesward.com/2011/09/14/flex-aop-and-puzzlers-at-flash-camp-italy?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flex-aop-and-puzzlers-at-flash-camp-italy</link>
		<comments>http://www.jamesward.com/2011/09/14/flex-aop-and-puzzlers-at-flash-camp-italy#comments</comments>
		<pubDate>Wed, 14 Sep 2011 16:02:32 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2629</guid>
		<description><![CDATA[Next week I&#8217;ll be speaking at the Flex Camp in Rimini, Italy. On September 23, Mike Labriola and I will be doing two sessions. First is &#8220;Planet of the AOPs&#8221; where we will show how Aspect Oriented Programming can be implemented in Flex using runtime bytecode modification. Mike and I will also be doing a [...]]]></description>
			<content:encoded><![CDATA[<p>Next week I&#8217;ll be speaking at the <a href="http://www.flex-developers.org/2011/08/flash-camp-italy-23-sept-2011/">Flex Camp in Rimini, Italy</a>.  On September 23, Mike Labriola and I will be doing two sessions.  First is &#8220;Planet of the AOPs&#8221; where we will show how Aspect Oriented Programming can be implemented in Flex using runtime bytecode modification.  Mike and I will also be doing a little &#8220;Flex Puzzlers&#8221; session where you will discover some very peculiar things about Flex and Flash Player.  It&#8217;s going to be a blast and I hope to see you there!  Also, there will be a raffle for a copy of Creative Suite 5.5 and a Playbook.  So go <a href="http://flashcampitaly.eventbrite.com/">sign up NOW</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/09/14/flex-aop-and-puzzlers-at-flash-camp-italy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seattle Force.com Developer Meetup on June 22</title>
		<link>http://www.jamesward.com/2011/06/15/seattle-force-com-developer-meetup-on-june-22/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=seattle-force-com-developer-meetup-on-june-22</link>
		<comments>http://www.jamesward.com/2011/06/15/seattle-force-com-developer-meetup-on-june-22/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 15:58:34 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Force.com]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2359</guid>
		<description><![CDATA[UPDATE: This event has been canceled! Sorry! Next week I&#8217;ll be presenting in Seattle at my first Force.com Developer Meetup! It starts at 6pm on Wednesday, June 22. My session will be on Flex Mobile Development. Other sessions include Force.com Platform Basics, Native iOS Development using JavaScript, and a Force.com Hands-On Lab. It&#8217;s going to [...]]]></description>
			<content:encoded><![CDATA[<p><font color="#ff0000">UPDATE: This event has been canceled!  Sorry!</font></p>
<p>Next week I&#8217;ll be presenting in Seattle at my first <a href="http://www.developerforce.com/events/seattle_developer_meetup/registration.php?d=70130000000G4WO">Force.com Developer Meetup</a>!  It starts at 6pm on Wednesday, June 22.  My session will be on Flex Mobile Development. Other sessions include Force.com Platform Basics, Native iOS Development using JavaScript, and a Force.com Hands-On Lab.  It&#8217;s going to be a fun evening, so if you are in the Seattle area, then I hope to see you there!</p>
<p><a href="http://www.developerforce.com/events/seattle_developer_meetup/registration.php?d=70130000000G4WO">Get more details and register!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/06/15/seattle-force-com-developer-meetup-on-june-22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Adventures on The Cloud</title>
		<link>http://www.jamesward.com/2011/06/05/new-adventures-on-the-cloud/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=new-adventures-on-the-cloud</link>
		<comments>http://www.jamesward.com/2011/06/05/new-adventures-on-the-cloud/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 00:47:25 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2345</guid>
		<description><![CDATA[When I started doing professional software development almost 15 years ago I was focused on the server-side. I started with Perl / CGI web apps &#8211; some of which are still in production today. Then I dove into Java web development with Java Web Server 1.0, Struts, JBoss, Tomcat and many other game changing technologies. [...]]]></description>
			<content:encoded><![CDATA[<p>When I started doing professional software development almost 15 years ago I was focused on the server-side.  I started with Perl / CGI web apps &#8211; some of which are still in production today.  Then I dove into Java web development with Java Web Server 1.0, Struts, JBoss, Tomcat and many other game changing technologies.</p>
<p>In 2004 I started getting into Macromedia Flex.  I was amazed at how easy it was to retrieve and nicely render data from a Java back-end.  In 2005 I began evangelizing Flex + Java.  Following the acquisition of Macromedia by Adobe, Flex has really flourished.  Adobe Flex is now the dominant RIA technology and it has been so fun to be a part of that!</p>
<p>Over the past seven years I&#8217;ve had so many great adventures on the client-side, but when a new opportunity on the server-side came my way I couldn&#8217;t pass it up.  Starting June 6th I&#8217;ll be stepping back into the Java world to evangelize the Cloud for Salesforce.com.  I&#8217;m excited to dive into some of the emerging Java/JVM technologies like Scala, Play Framework, and Clojure!</p>
<p>This change is certainly bittersweet for me.  Flex continues to make app development easier.  With things like Android support in Flex 4.5 and iOS support coming soon, the future of Flex is bright.  I&#8217;ve been very privileged to be a part of the Flex community for the past seven years. This group of passionate and creative developers have taught me so many new things.  Learning how to do runtime bytecode modification and co-creating <a href="http://www.jamesward.com/2011/04/26/introducing-mixing-loom-runtime-actionscript-bytecode-modification/">Mixing Loom</a> has certainly been one of the highlights!</p>
<p>As I begin this new adventure on the Cloud I&#8217;m excited about what lies ahead for Flex and for the Cloud.  Both continue to help us developers build better software.  I&#8217;ve hopefully helped you learn how to build great UIs with Flex.  Now I will help you learn how to build solid and scalable back-ends on the Cloud!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/06/05/new-adventures-on-the-cloud/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>Extending AIR for Android</title>
		<link>http://www.jamesward.com/2011/05/11/extending-air-for-android/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=extending-air-for-android</link>
		<comments>http://www.jamesward.com/2011/05/11/extending-air-for-android/#comments</comments>
		<pubDate>Wed, 11 May 2011 15:06:36 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2325</guid>
		<description><![CDATA[*** The following is totally unsupported by Adobe *** *** UPDATE: Adobe has officially added native extensions to AIR. I highly recommend you use that approach instead of mine. *** Adobe AIR provides a consistent platform for desktop and mobile apps. While consistency is very important there are times when developers need to extend beyond [...]]]></description>
			<content:encoded><![CDATA[<p><font color="#ff0000">*** The following is totally unsupported by Adobe ***</font><br />
<font color="#ff0000">*** UPDATE: <a href="http://www.adobe.com/devnet/air/articles/extending-air.html">Adobe has officially added native extensions to AIR</a>.  I highly recommend you use that approach instead of mine. ***</font></p>
<p>Adobe AIR provides a consistent platform for desktop and mobile apps.  While consistency is very important there are times when developers need to extend beyond the common APIs.  This article will walk you through how to integrate AIR for Android applications with other native APIs and functionality in the Android SDK.  It covers three common use cases for native extensibility: System Notifications, Widgets, and Application Licensing.</p>
<p>If you’d like to follow along you will need the following prerequisites:</p>
<ul>
<li><a href="https://www.adobe.com/cfusion/tdrc/index.cfm?product=flash_builder">Adobe Flash Builder 4.5</a>(which includes the Flex 4.5 SDK and AIR 2.6 SDK)</li>
<li><a href="http://developer.android.com/sdk/index.html">Android SDK</a></li>
<li><a href="http://developer.android.com/sdk/eclipse-adt.html">Android Eclipse Plugin</a></li>
</ul>
<p>Before getting started, a little background will help.  Android applications are distributed as APK files.  An APK file contains the Dalvik executable (dex), which will run on an Android device inside the Dalvik VM.  The Android SDK compiles a Java-like language to dex.</p>
<p>AIR for Android applications are also distributed as APK files.  Inside of these APK files is a small bit of dex that bootstraps the AIR for Android runtime, which then loads and runs the SWF file that is also inside of the APK.  The actual dex class that bootstraps the AIR application is dynamically generated by the <em>adt</em> tool in the AIR SDK.  The class is named AppEntry and its package name depends on the AIR application ID, but it always begins with “air”.  The AppEntry class checks for the existence of the AIR runtime and then launches the AIR application.  The Android descriptor file in an AIR APK specifies that the main application class is the AppEntry class.</p>
<p>To extend AIR for Android applications to include native APIs and Android SDK functionality, you start by creating a SWF file using Flex and then copy that SWF file, the dex classes for AIR for Android, and the required resources into a standard Android project.  By using the original AppEntry class you can still bootstrap the AIR application in the Android project but you can extend that class to gain a startup hook.</p>
<ol>
<li>To get started, download a package with the required dependencies for extending AIR for Android:<br />
<a href="http://www.jamesward.com/downloads/extending_air_for_android-flex_4_5-air_2_6-v_1.zip">http://www.jamesward.com/downloads/extending_air_for_android-flex_4_5-air_2_6-v_1.zip</a></li>
<li>Next, create a regular Android project in Eclipse (do not create an Activity yet):<br />
<img src="http://www.jamesward.com/wp/uploads/2011/05/new_android_project.jpg" alt="" title="New Android Project" width="613" height="905" class="alignnone size-full wp-image-2326" /></li>
<li>Copy all of the files from the zip file you downloaded into the root directory of the newly created Android project.  You will need to overwrite the existing files and update the launch configuration (if Eclipse asks you to).</li>
<li>Delete the “res/layout” directory.</li>
<li>Add the airbootstrap.jar file to the project’s build path.  You can do that by right-clicking on the file, then select Build Path and then Add to Build Path.</li>
<li>Verify that the project runs.  You should see “hello, world” on your Android device.  If so, then the AIR application is properly being bootstrapped and the Flex application in assets/app.swf is correctly being run.
<p>At this point if you do not need any custom startup hooks then you can simply replace the assets/app.swf file with your own SWF file (but it must be named app.swf).  If you do need a custom startup hook then simply create a new Java class named “MainApp” that extends the air.app.AppEntry class.<br />
<img src="http://www.jamesward.com/wp/uploads/2011/05/new_android_class.png" alt="" title="New Android Class" width="652" height="720" class="alignnone size-full wp-image-2327" /></li>
<li>Override the onCreate() method and add your own startup logic before super.onCreate() is called (which loads the AIR app).  Here is an example:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">jamesward</span>;
&nbsp;
<span style="color: #0066CC;">import</span> air.<span style="color: #006600;">app</span>.<span style="color: #006600;">AppEntry</span>;
<span style="color: #0066CC;">import</span> android.<span style="color: #0066CC;">os</span>.<span style="color: #006600;">Bundle</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainApp <span style="color: #0066CC;">extends</span> AppEntry <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #66cc66;">@</span>Override
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">void</span> onCreate<span style="color: #66cc66;">&#40;</span>Bundle arg0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;test test&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">super</span>.<span style="color: #006600;">onCreate</span><span style="color: #66cc66;">&#40;</span>arg0<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</li>
<li>Open the AndroidManifest.xml descriptor file and tell it to use the new MainApp class instead of the original AppEntry class.  First change the package to be the same as your MainApp’s package:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest</span> <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;com.jamesward&quot;</span> <span style="color: #000066;">android:versionCode</span>=<span style="color: #ff0000;">&quot;1000000&quot;</span> <span style="color: #000066;">android:versionName</span>=<span style="color: #ff0000;">&quot;1.0.0&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>Also update the activity to use the MainApp class (make sure you have the period before the class name):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.MainApp&quot;</span></span></pre></div></div>

<p>You can also add any other permissions or settings you might need in the AndroidManifest.xml file.</li>
<li>Save the changes and, when Eclipse prompts you, update the launch configuration.</li>
<li>Run the application and you should again see “hello, world”. This time, however, in LogCat (command line tool or Eclipse view) you should see the “test test” output.  Now that you have a startup hook, you can do some fun stuff!</li>
</ol>
<p><strong>System Notifications and Services</strong></p>
<p>AIR for Android applications don’t yet have an API to do Android system notifications.  But you can add system notifications to your AIR for Android application through a startup hook.  In order for the AIR application to communicate with the native Android APIs you must provide a bridge for the communication.  The simplest way to create that bridge is using a network socket.  The Android application can listen for data on the socket and then read that data and determine if it needs to display a system notification.  Then the AIR application can connect to the socket and send the necessary data.  This is a pretty straightforward example but some security (for instance a key exchange) should be implemented to insure that malicious apps don’t discover and abuse the socket.  Also some logic to determine which socket should be used would likely be necessary.</p>
<ol>
<li>Inside the application section add a new Android Service:</li>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">android:enabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;TestService&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</li>
<li>Since this example uses a Socket you will also need to add the INTERNET permission:</li>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.INTERNET&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</li>
<li>You might also want to enable the phone to vibrate when there is a new notification.  If so add that permission as well:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.VIBRATE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</li>
<li>Save your changes to AndroidManifest.xml.</li>
<li>Next, create the background Java Service class, called TestService.  This service will listen on a socket and when necessary, display an Android Notification:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jamesward</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.DataInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.ServerSocket</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.Socket</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Notification</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.NotificationManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.PendingIntent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Service</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.IBinder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Looper</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestService <span style="color: #000000; font-weight: bold;">extends</span> Service
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> stopped<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Thread</span> serverThread<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ServerSocket</span> ss<span style="color: #339933;">;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> IBinder onBind<span style="color: #009900;">&#40;</span>Intent intent<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;onCreate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      serverThread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">try</span>
                <span style="color: #009900;">&#123;</span>
                        Looper.<span style="color: #006633;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        ss <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ServerSocket</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12345</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        ss.<span style="color: #006633;">setReuseAddress</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        ss.<span style="color: #006633;">setPerformancePreferences</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>stopped<span style="color: #009900;">&#41;</span>
                        <span style="color: #009900;">&#123;</span>
                                <span style="color: #003399;">Socket</span> accept <span style="color: #339933;">=</span> ss.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                accept.<span style="color: #006633;">setPerformancePreferences</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                accept.<span style="color: #006633;">setKeepAlive</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                                <span style="color: #003399;">DataInputStream</span> _in <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                                <span style="color: #000000; font-weight: bold;">try</span>
                                <span style="color: #009900;">&#123;</span>
                                        _in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DataInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedInputStream</span><span style="color: #009900;">&#40;</span>accept.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #009900;">&#125;</span>
                                <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e2<span style="color: #009900;">&#41;</span>
                                <span style="color: #009900;">&#123;</span>
                                  e2.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #009900;">&#125;</span>
&nbsp;
                                <span style="color: #000066; font-weight: bold;">int</span> method <span style="color: #339933;">=</span>_in.<span style="color: #006633;">readInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                                <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span>
                                <span style="color: #009900;">&#123;</span>
                                  <span style="color: #666666; font-style: italic;">// notification</span>
                                  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
                                        doNotification<span style="color: #009900;">&#40;</span>_in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                                <span style="color: #009900;">&#125;</span>
                        <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Error in Listener&quot;</span>,e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">try</span>
                <span style="color: #009900;">&#123;</span>
                  ss.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                  Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;keep it simple&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>,<span style="color: #0000ff;">&quot;Server thread&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      serverThread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> doNotification<span style="color: #009900;">&#40;</span><span style="color: #003399;">DataInputStream</span> in<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> id <span style="color: #339933;">=</span> in.<span style="color: #006633;">readUTF</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    displayNotification<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          stopped<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                  ss.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
          serverThread.<span style="color: #006633;">interrupt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                  serverThread.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> displayNotification<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> notificationString<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> icon <span style="color: #339933;">=</span> R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">mp_warning_32x32_n</span><span style="color: #339933;">;</span>
    CharSequence tickerText <span style="color: #339933;">=</span> notificationString<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">long</span> when <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Context</span> context <span style="color: #339933;">=</span> getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CharSequence contentTitle <span style="color: #339933;">=</span> notificationString<span style="color: #339933;">;</span>
    CharSequence contentText <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    Intent notificationIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, MainApp.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PendingIntent contentIntent <span style="color: #339933;">=</span> PendingIntent.<span style="color: #006633;">getActivity</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, notificationIntent, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Notification notification <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Notification<span style="color: #009900;">&#40;</span>icon, tickerText, when<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    notification.<span style="color: #006633;">vibrate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">300</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    notification.<span style="color: #006633;">setLatestEventInfo</span><span style="color: #009900;">&#40;</span>context, contentTitle, contentText, contentIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">String</span> ns <span style="color: #339933;">=</span> <span style="color: #003399;">Context</span>.<span style="color: #006633;">NOTIFICATION_SERVICE</span><span style="color: #339933;">;</span>
    NotificationManager mNotificationManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NotificationManager<span style="color: #009900;">&#41;</span> getSystemService<span style="color: #009900;">&#40;</span>ns<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    mNotificationManager.<span style="color: #006633;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, notification<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This service listens on port 12345. When it receives some data it checks if the first “int” sent is “1”. If so, it then creates a new notification using the next piece of data (a string) that is received over the socket.</li>
<li>Modify the MainApp Java class to start the service when the onCreate() method is called:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			Intent srv <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, TestService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			startService<span style="color: #009900;">&#40;</span>srv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// service could not be started</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That is all you need to do in the Android application.</li>
<li>Next, create a Flex application that will connect to the socket and send the right data.  Here is some sample code for my Notifier.mxml class, which I used to test the Android service:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span> <span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #009900;">                       <span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    @namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
&nbsp;
    global {
      fontSize: 32;      
    }
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:VerticalLayout</span> <span style="color: #000066;">horizontalAlign</span>=<span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">paddingTop</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:TextInput</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;t&quot;</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;test test&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Button</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;create notification&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:click<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">        var s:Socket = new Socket();</span>
<span style="color: #339933;">        s.connect(&quot;localhost&quot;, 12345);</span>
<span style="color: #339933;">        s.addEventListener(Event.CONNECT, function(event:Event):void {</span>
<span style="color: #339933;">          trace('connected!');</span>
<span style="color: #339933;">          (event.currentTarget as Socket).writeInt(1);</span>
<span style="color: #339933;">          (event.currentTarget as Socket).writeUTF(t.text);</span>
<span style="color: #339933;">          (event.currentTarget as Socket).flush();</span>
<span style="color: #339933;">          (event.currentTarget as Socket).close();</span>
<span style="color: #339933;">        });</span>
<span style="color: #339933;">        s.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {</span>
<span style="color: #339933;">          trace('error! ' + event.errorID);</span>
<span style="color: #339933;">        });</span>
<span style="color: #339933;">        s.addEventListener(ProgressEvent.SOCKET_DATA, function(event:ProgressEvent):void {</span>
<span style="color: #339933;">          trace('progress ');</span>
<span style="color: #339933;">        });</span>
<span style="color: #339933;">      ]]&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:click<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As you can see there is just a TextInput control that allows the user to enter some text.  Then when the user clicks the Button the AIR for Android application connects to a local socket on port 12345, writes an int with the value of 1, writes the string that the user typed into the TextInput control, and finally flushes and closes the connection.  This causes the notification to be displayed.</li>
<li>Now simply compile the Flex app and overwrite the assets/app.swf file with the new Flex application.  Check out a <a href="http://www.youtube.com/watch?v=HjDu66NOpuA">video demonstration</a> of this code.</li>
</ol>
<p><strong>Widgets</strong></p>
<p>Widgets in Android are the mini apps that can be displayed on the home screen of the device.  There is a fairly limited amount of things that can be displayed in Widgets.  So unfortunately Widgets can’t be built with AIR for Android.  However a custom application Widget can be packaged with an AIR for Android application.  To add a Widget to an AIR for Android application you can use the default AppEntry class instead of wrapping it with another class (MainApp in my example).  (It doesn’t, however, do any harm to keep the MainApp class there.)  To add a Widget simply add its definition to the AndroidManifest.xml file, create the Widget with Java, and create a corresponding layout resource.  </p>
<ol>
<li>First define the Widget in the application section of the AndroidManifest.xml file:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.AndroidWidget&quot;</span> <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;app&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.appwidget.action.APPWIDGET_UPDATE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta-data</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.appwidget.provider&quot;</span> <span style="color: #000066;">android:resource</span>=<span style="color: #ff0000;">&quot;@xml/airandroidwidget&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
<li>You need an XML resource that provides metadata about the widget.  Simply create a new file named airandroidwidget.xml in a new res/xml directory with the following contents:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appwidget-provider</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:minWidth</span>=<span style="color: #ff0000;">&quot;294dp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:minHeight</span>=<span style="color: #ff0000;">&quot;72dp&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:updatePeriodMillis</span>=<span style="color: #ff0000;">&quot;86400000&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:initialLayout</span>=<span style="color: #ff0000;">&quot;@layout/main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appwidget-provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This tells the widget to use the main layout resource as the initial layout for the widget.</li>
<li>Create a res/layout/main.xml file that contains a simple text display:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/widget&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;#ffffffff&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span>  </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;hello&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next, you’ll need to create the AppWidgetProvider class specified in the AndroidManifest.xml file.</li>
<li>Create a new Java class named AndroidWidget with the following contents:

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jamesward</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.PendingIntent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.appwidget.AppWidgetManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.appwidget.AppWidgetProvider</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.RemoteViews</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.jamesward.MainApp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AndroidWidget <span style="color: #000000; font-weight: bold;">extends</span> AppWidgetProvider
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onUpdate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, AppWidgetManager appWidgetManager, <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> appWidgetIds<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> N <span style="color: #339933;">=</span> appWidgetIds.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Perform this loop procedure for each App Widget that belongs to this provider</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">int</span> appWidgetId <span style="color: #339933;">=</span> appWidgetIds<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            Intent intent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, MainApp.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            intent.<span style="color: #006633;">setAction</span><span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_MAIN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            PendingIntent pendingIntent <span style="color: #339933;">=</span> PendingIntent.<span style="color: #006633;">getActivity</span><span style="color: #009900;">&#40;</span>context, <span style="color: #cc66cc;">0</span>, intent, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            RemoteViews views <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RemoteViews<span style="color: #009900;">&#40;</span>context.<span style="color: #006633;">getPackageName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            views.<span style="color: #006633;">setOnClickPendingIntent</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">widget</span>, pendingIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            appWidgetManager.<span style="color: #006633;">updateAppWidget</span><span style="color: #009900;">&#40;</span>appWidgetId, views<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class will display the Widget when necessary and register a click handler that will open the MainApp application when the user taps on the Widget.</li>
<li>Run the application to verify that it works.</li>
<li>Now you can add the widget to the home screen by holding down on the home screen and following the Widget wizard.</li>
<li>Verify that tapping the widget launches the AIR application.</li>
</ol>
<p><strong>Application Licensing</strong></p>
<p>Android provides APIs to help you enforce licensing policies for non-free apps in the Android Market.  You might want to go <a href="http://developer.android.com/guide/publishing/licensing.html">read up on Android Licensing</a> before you give this one a try.</p>
<p>To add Application Licensing to you AIR for Android application you first need to follow the steps outlined in the Android documentation. The broad steps are as follows:</p>
<ol>
<li>Set up an Android Market publisher account</li>
<li>Install the Market Licensing Package in the Android SDK</li>
<li>Create a new LVL Android Library Project in Eclipse</li>
<li>Add a Library reference in the Android project to the LVL Android Library</li>
<li>Add the CHECK_LICENSE permission to your Android project’s manifest file:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.android.vending.CHECK_LICENSE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

</li>
</ol>
<p>After completing these set up steps, you are ready to update the MainApp Java class to handle validating the license:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jamesward</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.android.vending.licensing.AESObfuscator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.android.vending.licensing.LicenseChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.android.vending.licensing.LicenseCheckerCallback</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.android.vending.licensing.ServerManagedPolicy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">air.Foo.AppEntry</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Handler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.provider.Settings.Secure</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainApp <span style="color: #000000; font-weight: bold;">extends</span> AppEntry <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> BASE64_PUBLIC_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;REPLACE WITH KEY FROM ANDROID MARKET PROFILE&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Generate your own 20 random bytes, and put them here.</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> SALT <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">-</span><span style="color: #cc66cc;">45</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">72</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">31</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">8</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">122</span>, <span style="color: #cc66cc;">98</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">86</span>, <span style="color: #cc66cc;">47</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">65</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">47</span>, <span style="color: #cc66cc;">33</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">99</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">55</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">64</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">114</span>, <span style="color: #cc66cc;">39</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">71</span>, <span style="color: #cc66cc;">47</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> LicenseCheckerCallback mLicenseCheckerCallback<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> LicenseChecker mChecker<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Handler mHandler<span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> deviceId <span style="color: #339933;">=</span> Secure.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>getContentResolver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, Secure.<span style="color: #006633;">ANDROID_ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mLicenseCheckerCallback <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLicenseCheckerCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mChecker <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LicenseChecker<span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> ServerManagedPolicy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>,
                <span style="color: #000000; font-weight: bold;">new</span> AESObfuscator<span style="color: #009900;">&#40;</span>SALT, getPackageName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, deviceId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,
            BASE64_PUBLIC_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mChecker.<span style="color: #006633;">checkAccess</span><span style="color: #009900;">&#40;</span>mLicenseCheckerCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> displayFault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mHandler.<span style="color: #006633;">post</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Cover the screen with a messaging indicating there was a licensing problem</span>
                setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> MyLicenseCheckerCallback <span style="color: #000000; font-weight: bold;">implements</span> LicenseCheckerCallback <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> allow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isFinishing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Don't update UI if Activity is finishing.</span>
                <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// Should allow user access.</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dontAllow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isFinishing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Don't update UI if Activity is finishing.</span>
                <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            displayFault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> applicationError<span style="color: #009900;">&#40;</span>ApplicationErrorCode errorCode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isFinishing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Don't update UI if Activity is finishing.</span>
                <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mChecker.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Also add the following to a new res/layout/main.xml file in order to display an error when the license is denied:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span>  </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/license_problem&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The text to display uses a string resource named “license_problem”, which must be added to the res/values/strings.xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;license_problem&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>THERE WAS A PROBLEM LICENSING YOUR APPLICATION!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When the application runs it will check for a valid license.  If the license comes back as valid then the AIR application will start and run as usual.  However, if there is an invalid license then the application will set the ContentView to the R.layout.main resource, which displays the error message defined in the “license_problem” resource.  To simulate different responses you can change the “Test Response” in your Android Market profile.</p>
<p><strong>The Gory Details</strong><br />
I’ve wrapped up a generated AppEntry class and its resources to make the process of extending AIR for Android fairly easy.  If you are interested in seeing how that is done, I’ve posted all of the <a href="https://github.com/jamesward/extending_air_for_android">source code on github</a>.</p>
<p>Here is an overview of the procedure:</p>
<ol>
<li>Use the AIR SDK to create an AIR for Android APK file.</li>
<li>Use the dex2jar utility to convert the AppEntry dex classes into a JAR file.</li>
<li>Pull the resource classes out of the JAR file so that they don’t conflict with the new resources.</li>
<li>Use apktool to extract the original resources out of the AIR for Android APK file.</li>
<li>Create a single ZIP file containing the airbootstap.jar file, resources, AndroidManifest.xml file, and assets.</li>
</ol>
<p>Now you can simply copy and paste those dependencies into your Android project.</p>
<p><strong>Conclusion</strong><br />
Hopefully this article has helped you to better understand how you can extend AIR for Android applications with Android APIs.  There are still a number of areas where this method can be improved.  For instance, I am currently working with the <a href="http://code.google.com/p/merapi/">Merapi Project</a> developers to get Merapi working with my method of extending AIR for Android.  That will provide a better bridging technique for communicating between the AIR application and Android APIs.  So stay tuned for more information about that.  And let me know if you have any questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/05/11/extending-air-for-android/feed/</wfw:commentRss>
		<slash:comments>126</slash:comments>
		</item>
		<item>
		<title>Introducing Mixing Loom &#8211; Runtime ActionScript Bytecode Modification</title>
		<link>http://www.jamesward.com/2011/04/26/introducing-mixing-loom-runtime-actionscript-bytecode-modification/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introducing-mixing-loom-runtime-actionscript-bytecode-modification</link>
		<comments>http://www.jamesward.com/2011/04/26/introducing-mixing-loom-runtime-actionscript-bytecode-modification/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 15:54:03 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2278</guid>
		<description><![CDATA[At this year&#8217;s 360&#124;Flex conference in Denver, Mike Labriola and I unveiled a new project we&#8217;ve been working on called Mixing Loom. Our presentation was called &#8220;Planet of the AOPs&#8221; because Mixing Loom lays the foundation for true Aspect Oriented Programming (AOP) on the Flash Platform. Mixing Loom provides Flex and ActionScript applications the hooks [...]]]></description>
			<content:encoded><![CDATA[<p>At this year&#8217;s <a href="http://www.360flex.com/">360|Flex</a> conference in Denver, <a href="http://www.digitalprimates.net/author/codeslinger/">Mike Labriola</a> and I unveiled a new project we&#8217;ve been working on called <a href="http://github.com/MixingLoom">Mixing Loom</a>.  Our presentation was called &#8220;Planet of the AOPs&#8221; because Mixing Loom lays the foundation for true Aspect Oriented Programming (AOP) on the Flash Platform.  Mixing Loom provides Flex and ActionScript applications the hooks they need to do bytecode modification either before runtime or at runtime.  Through bytecode modification an application can apply a behavior across hierarchies of objects.  There are a number of behaviors in a typical Flex application (such as logging, security, application configuration, accessibility, and styling) that could be represented as Aspects.  Today these behaviors must either be included in every class that needs them or included way down the object hierarchy (i.e. UIComponent).  With Mixing Loom a compiled SWF can be modified (applying necessary behaviors) after it&#8217;s been compiled or as it&#8217;s starting up.</p>
<p>If you are building Flex apps and want to take advantage of AOP then Mixing Loom is probably a bit lower level than what you need.  Mixing Loom combined with <a href="http://www.as3commons.org/as3-commons-bytecode/index.html">AS3 Commons Bytecode</a> provides the foundation for AOP systems to be built on top of.  We hope that by providing developers the hooks to modify bytecode that frameworks will emerge that provide application developers higher level APIs based on AOP.  As Mike says, &#8220;Mixing Loom kicks off the Summer of AOP.&#8221;</p>
<p>If you are one of those developers who likes getting dirty with bytecode modification then you might want to check out the slides from the &#8220;Planet of the AOPs&#8221; session:<br />
<iframe src="http://www.slideshare.net/slideshow/embed_code/7667785" width="595" height="497" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>If you are still following along and looking for more details on how to use Mixing Loom, then keep reading.  Flex applications are broken into at least two pieces.  The first piece is the thing that displays the loading / progress bar.  That is located on the first &#8220;frame&#8221; of an application&#8217;s SWF file.  The rest of the application is on the second frame of the main SWF and possibly in other SWF files like Modules and/or Runtime Shared Libraries (RSLs).  Mixing Loom provides two ways to modify the bytecode of a running application.  First, using a custom preloader an application can modify its second frame and/or any Flex Modules before they are loaded into the VM.  The second way is to use LoomApplication and a custom preloader, which allows an application to modify its second frame, modules, and/or RSLs (even the signed Flex Framework RSLs).  Let&#8217;s walk through a simple example of an application that uses a custom preloader to modify a string that exists in its second frame.</p>
<p>Let&#8217;s take a simple object Foo that has a getBar method, which returns a string &#8220;a bar&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package blah
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getBar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
  <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;a bar&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And here is a simple application that just displays the results of calling an instance of Foo&#8217;s getBar method:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">               xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
    import blah.Foo;
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:applicationComplete</span><span style="color: #7400FF;">&gt;</span></span>
     var foo:Foo = new Foo();
     l.text = foo.getBar();
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:applicationComplete</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
  <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> id=<span style="color: #ff0000;">&quot;l&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>If you were to run this application as is then the Label would display &#8220;a bar&#8221; &#8211; as expected.  But to give you an idea of how to do runtime bytecode modification let&#8217;s change the &#8220;a bar&#8221; string to something else.  (BTW: If you are following along then you will need to pull down the <a href="https://github.com/MixingLoom/mixingloom-core">mixingloom-core</a> code from github and compile it on your own because we haven&#8217;t published a SWC for Mixing Loom yet.)  The thing in Mixing Loom that actually does the bytecode modification is called a &#8220;Patcher&#8221; so we will need to create one of those that searches the bytecode for a string and then replaces that string.  Before we do that, let me explain how a SWF file is structured.  Every SWF file is a series of &#8220;tags&#8221;.  There are many different tag types but the types we are interested in for bytecode modification are the ones that actually contain the ActionScript ByteCode (ABC).  This is the DoABC tag &#8211; type 82.  For a full list of SWF tags and their structures check out the <a href="http://www.adobe.com/content/dam/Adobe/en/devnet/swf/pdf/swf_file_format_spec_v10.pdf">SWF Spec</a>.  One of the tag types indicates an executable boundary called a Frame.  As a SWF file is being loaded by Flash Player it is parsing it.  When Flash Player parses a &#8220;ShowFrame&#8221; tag it knows it can load and run the preceding tags.  The code doing the bytecode modification will be running on the first frame, which means that all of the tags to do the modification and those to display the Flex preloader will have already been loaded.  That means we can&#8217;t modify those tags using this method at runtime.  But we can modify the tags on the second frame of the SWF, which will be passed to our Patcher before they have actually been loaded.</p>
<p>Here is the code for the StringModifierPatcher:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">patcher</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">tags</span>.<span style="color: #006600;">DoABCTag</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">AbcSpec</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">SwfContext</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">SwfTag</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">invocation</span>.<span style="color: #006600;">InvocationType</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArrayUtils</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringModifierPatcher <span style="color: #0066CC;">extends</span> AbstractPatcher
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> originalString:<span style="color: #0066CC;">String</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> replacementString:<span style="color: #0066CC;">String</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StringModifierPatcher<span style="color: #66cc66;">&#40;</span>originalString:<span style="color: #0066CC;">String</span>, replacementString:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">originalString</span> = originalString;
        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">replacementString</span> = replacementString;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span> invocationType:InvocationType, swfContext:SwfContext <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> searchByteArray:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        AbcSpec.<span style="color: #006600;">writeStringInfo</span><span style="color: #66cc66;">&#40;</span>originalString, searchByteArray<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> replacementByteArray:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        AbcSpec.<span style="color: #006600;">writeStringInfo</span><span style="color: #66cc66;">&#40;</span>replacementString, replacementByteArray<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> swfTag:SwfTag <span style="color: #b1b100;">in</span> swfContext.<span style="color: #006600;">swfTags</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #0066CC;">type</span> == DoABCTag.<span style="color: #006600;">TAG_ID</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                swfTag.<span style="color: #006600;">tagBody</span> = ByteArrayUtils.<span style="color: #006600;">findAndReplaceFirstOccurrence</span><span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #006600;">tagBody</span>, searchByteArray, replacementByteArray<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        invokeCallBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The StringModifierPatcher extends the Mixing Loom AbstractPatcher and takes two parameters, the originalString and the replacementString.  The StringModifierPatcher has an apply method, which will be called by Mixing Loom during application startup.  In the apply method the StringModifierPatcher creates a search ByteArray and a replacement ByteArray from the provided strings.  Then it loops through each tag from the second frame of the SWF being loaded (ignoring everything that is not a DoABC tag) and then uses Mixing Loom&#8217;s ByteArrayUtils.findAndReplaceFirstOccurrence utility to replace the first occurrence of the search ByteArray with replacement ByteArray.  Finally it notifies Mixing Loom that it is all done by calling the invokeCallBack method.  So that is the simple example of actually modifying the application, but we still need to set the hooks in the main application so that Mixing Loom can do its thing.</p>
<p>Since this example only modifies frame 2 tags (no RSLs), we can just use a custom preloader to set up the Mixing Loom hooks.  Here is the StringModifierPatcherPreloader:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package preloader <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">managers</span>.<span style="color: #006600;">IPatchManager</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">patcher</span>.<span style="color: #006600;">StringModifierPatcher</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">preloader</span>.<span style="color: #006600;">AbstractPreloader</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StringModifierPatcherPreloader <span style="color: #0066CC;">extends</span> AbstractPreloader <span style="color: #66cc66;">&#123;</span>
    override protected <span style="color: #000000; font-weight: bold;">function</span> setupPatchers<span style="color: #66cc66;">&#40;</span>manager:IPatchManager<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span>.<span style="color: #006600;">setupPatchers</span><span style="color: #66cc66;">&#40;</span>manager<span style="color: #66cc66;">&#41;</span>;
        manager.<span style="color: #006600;">registerPatcher</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> StringModifierPatcher<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a bar&quot;</span>, <span style="color: #ff0000;">&quot;not really a bar&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The StringModifierPatcherPreloader extends Mixing Loom&#8217;s AbstractPreloader and uses the setupPatchers method to register a new patcher.  In this case the only patcher is an instance of the StringModifierPatcher that will search for the default &#8220;a bar&#8221; string and replace it with the &#8220;not really a bar&#8221; string.</p>
<p>The last thing to make this all work is to tell the main application to use the new preloader:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">               xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">               preloader=<span style="color: #ff0000;">&quot;preloader.StringModifierPatcherPreloader&quot;</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Here is the result:<br />
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100" height="30"><param name="movie" value="/demos/MixingLoomTestApp/MixingLoomTestApp_StringModifierPatcher.swf" /><param name="quality" value="high" /><embed src="/demos/MixingLoomTestApp/MixingLoomTestApp_StringModifierPatcher.swf" quality="high" width="100" height="30" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object><br />
Exciting!  Our application code just modified itself at startup!  Now this is obviously a very trivial example but I hope it provides a basic understanding of how to use Mixing Loom as the foundation for AOP.  Let&#8217;s walk through some other examples that are more exciting (and complex).</p>
<p>For the next example let&#8217;s do something a little more AOP-ish.  There will be an XML configuration file that is loaded on startup that will specify some classes and methods to apply interceptors to.  An interceptor is simply a method call injected into the body of a method.  First, here is the FooInterceptor class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">FlexGlobals</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FooInterceptor
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> interceptAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
        FlexGlobals.<span style="color: #006600;">topLevelApplication</span>.<span style="color: #0066CC;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;backgroundColor&quot;</span>, <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> 0xffffff<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For demo purposes this interceptor is very simple &#8211; it just changes the application&#8217;s background color.  Here is the XML configuration file that the application will load on startup to determine where to apply the interceptor:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interceptor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;swfTag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>blah/Foo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/swfTag<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodEntryInvoker<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;className<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>FooInterceptor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/className<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>interceptAll<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodEntryInvoker<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interceptor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interceptors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In this case it is saying to only apply the interceptor to the SWF tag with the name &#8220;blah/Foo&#8221;.  In a debug version of the application the Foo class from above will be in its own SWF tag named &#8220;blah/Foo&#8221;.  The reason that the SWF tag is specified in this case is because by doing this the application won&#8217;t need to deserialize and reserialize every class.  The downside to doing things this way is that it won&#8217;t work if we create an optimized SWF where all of the frame 2 classes are contained in one SWF tag.  With some more work in AS3 Commons Bytecode we could optimize things for this kind of use case.  Volunteers?  :)  The methodEntryInvoker simply specifies the class and method name to call on method entry.  This interceptor will be added to every method, on every class in the SWF tag with the name &#8220;blah/Foo&#8221;.</p>
<p>Now for the fun part&#8230;  Here is the patcher that loads the XML config file, parses it, and then applies the interceptor:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package patcher <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoaderDataFormat</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">AbcFile</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">InstanceInfo</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">LNamespace</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">MethodInfo</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">Op</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">QualifiedName</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">abc</span>.<span style="color: #006600;">enum</span>.<span style="color: #006600;">Opcode</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">io</span>.<span style="color: #006600;">AbcSerializer</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">SwfContext</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">SwfTag</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">invocation</span>.<span style="color: #006600;">InvocationType</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">patcher</span>.<span style="color: #006600;">AbstractPatcher</span>;
&nbsp;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">as3commons</span>.<span style="color: #006600;">bytecode</span>.<span style="color: #006600;">io</span>.<span style="color: #006600;">AbcDeserializer</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleXMLPatcher <span style="color: #0066CC;">extends</span> AbstractPatcher <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span>;
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swfContext:SwfContext;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SampleXMLPatcher<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">url</span> = <span style="color: #0066CC;">url</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span> invocationType:InvocationType, swfContext:SwfContext <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>invocationType.<span style="color: #0066CC;">type</span> == InvocationType.<span style="color: #006600;">FRAME2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">swfContext</span> = swfContext;
&nbsp;
            <span style="color: #000000; font-weight: bold;">var</span> urlLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            urlLoader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #0066CC;">TEXT</span>;
            urlLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, handleXMLLoad<span style="color: #66cc66;">&#41;</span>;
            urlLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            invokeCallBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleXMLLoad<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">currentTarget</span> as URLLoader<span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> swfTagName:<span style="color: #0066CC;">String</span> = xmlData.<span style="color: #006600;">interceptor</span>.<span style="color: #006600;">swfTag</span>;
        <span style="color: #000000; font-weight: bold;">var</span> methodEntryInvokerClassName:<span style="color: #0066CC;">String</span> = xmlData.<span style="color: #006600;">interceptor</span>.<span style="color: #006600;">methodEntryInvoker</span>.<span style="color: #006600;">className</span>;
        <span style="color: #000000; font-weight: bold;">var</span> methodEntryInvokerMethodName:<span style="color: #0066CC;">String</span> = xmlData.<span style="color: #006600;">interceptor</span>.<span style="color: #006600;">methodEntryInvoker</span>.<span style="color: #006600;">methodName</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> methodEntryInvokerClassQName:QualifiedName = <span style="color: #000000; font-weight: bold;">new</span> QualifiedName<span style="color: #66cc66;">&#40;</span>methodEntryInvokerClassName, LNamespace.<span style="color: #0066CC;">PUBLIC</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">var</span> methodEntryInvokerMethodQName:QualifiedName = <span style="color: #000000; font-weight: bold;">new</span> QualifiedName<span style="color: #66cc66;">&#40;</span>methodEntryInvokerMethodName, LNamespace.<span style="color: #0066CC;">PUBLIC</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> swfTag:SwfTag <span style="color: #b1b100;">in</span> swfContext.<span style="color: #006600;">swfTags</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #0066CC;">name</span> == swfTagName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;">// skip the flags</span>
                swfTag.<span style="color: #006600;">tagBody</span>.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">4</span>;
&nbsp;
                <span style="color: #000000; font-weight: bold;">var</span> abcStartLocation:uint = <span style="color: #cc66cc;">4</span>;
                <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #006600;">tagBody</span>.<span style="color: #006600;">readByte</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    abcStartLocation++;
                <span style="color: #66cc66;">&#125;</span>
                abcStartLocation++; <span style="color: #808080; font-style: italic;">// skip the string byte terminator</span>
&nbsp;
                swfTag.<span style="color: #006600;">tagBody</span>.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
                <span style="color: #000000; font-weight: bold;">var</span> abcDeserializer:AbcDeserializer = <span style="color: #000000; font-weight: bold;">new</span> AbcDeserializer<span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #006600;">tagBody</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #000000; font-weight: bold;">var</span> abcFile:AbcFile = abcDeserializer.<span style="color: #006600;">deserialize</span><span style="color: #66cc66;">&#40;</span>abcStartLocation<span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> instanceInfo:InstanceInfo <span style="color: #b1b100;">in</span> abcFile.<span style="color: #006600;">instanceInfo</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
                    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> methodInfo:MethodInfo <span style="color: #b1b100;">in</span> instanceInfo.<span style="color: #006600;">methodInfo</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">var</span> startIndex:uint = <span style="color: #cc66cc;">0</span>;
                        <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> op:Op <span style="color: #b1b100;">in</span> methodInfo.<span style="color: #006600;">methodBody</span>.<span style="color: #006600;">opcodes</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                            startIndex++;
                            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>op.<span style="color: #006600;">opcode</span> === Opcode.<span style="color: #006600;">pushscope</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                                <span style="color: #b1b100;">break</span>;
                            <span style="color: #66cc66;">&#125;</span>
                        <span style="color: #66cc66;">&#125;</span>
&nbsp;
                        <span style="color: #000000; font-weight: bold;">var</span> findOp:Op = <span style="color: #000000; font-weight: bold;">new</span> Op<span style="color: #66cc66;">&#40;</span>Opcode.<span style="color: #006600;">findpropstrict</span>, <span style="color: #66cc66;">&#91;</span>methodEntryInvokerClassQName<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #000000; font-weight: bold;">var</span> getOp:Op = <span style="color: #000000; font-weight: bold;">new</span> Op<span style="color: #66cc66;">&#40;</span>Opcode.<span style="color: #0066CC;">getproperty</span>, <span style="color: #66cc66;">&#91;</span>methodEntryInvokerClassQName<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #000000; font-weight: bold;">var</span> callOp:Op = <span style="color: #000000; font-weight: bold;">new</span> Op<span style="color: #66cc66;">&#40;</span>Opcode.<span style="color: #006600;">callproperty</span>, <span style="color: #66cc66;">&#91;</span>methodEntryInvokerMethodQName, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                        methodInfo.<span style="color: #006600;">methodBody</span>.<span style="color: #006600;">opcodes</span>.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>startIndex, <span style="color: #cc66cc;">0</span>, findOp, getOp, callOp, <span style="color: #000000; font-weight: bold;">new</span> Op<span style="color: #66cc66;">&#40;</span>Opcode.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #66cc66;">&#125;</span>
                <span style="color: #66cc66;">&#125;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">var</span> abcSerializer:AbcSerializer = <span style="color: #000000; font-weight: bold;">new</span> AbcSerializer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #000000; font-weight: bold;">var</span> modifiedBytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                modifiedBytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>swfTag.<span style="color: #006600;">tagBody</span>, <span style="color: #cc66cc;">0</span>, abcStartLocation<span style="color: #66cc66;">&#41;</span>;
                modifiedBytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>abcSerializer.<span style="color: #006600;">serializeAbcFile</span><span style="color: #66cc66;">&#40;</span>abcFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                swfTag.<span style="color: #006600;">tagBody</span> = modifiedBytes;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        invokeCallBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The constructor for this patcher takes a URL, which is used to specify the XML config file.  Then in the apply method, if the invocation type is &#8220;FRAME2&#8243; (meaning the patcher is being applied to the SWF tags on the second frame of the loading SWF) then it uses URLLoader to load the XML config file.  Notice that URLLoader is used, not HTTPService.  That is because anything that goes into a patcher is put on the first frame of the SWF and if HTTPService was used here, then there would be a ton of additional classes (dependencies) that would need to also be moved to the first frame.  While technically this would work, it&#8217;s not a good practice because the more that is on the first frame, the longer the user has to wait before the preloader shows up (remember: all of the frame must be transferred across the network before the frame is loaded into the VM).  If the invocation type is not &#8220;FRAME2&#8243; then the invokeCallBack method is called to tell Mixing Loom that this patcher is done with the current invocation.  Side note: patchers can block for as long as they want.  Nothing moves forward in Mixing Loom until a patcher calls the invokeCallBack method.</p>
<p>When the data for the XML file arrives it is parsed using the E4X library.  Then new QualifiedName instances are created based on the interceptor&#8217;s class and method names.  Now the SWF tag with the name specified in the XML file is processed.  First it is deserialized by AS3 Commons Bytecode.  This provides an object representation of the underlying ABC code contained in the SWF tag.  Then for every class and method the interceptor is applied at the beginning of the method.  Kinda.  There are a few operations that must happen at the very beginning of the method.  For each method being intercepted we need to move past the &#8220;pushscope&#8221; opcode before we can insert new opcodes.  Then four new opcodes are spliced into the array of opcodes for the method: findpropstrict, getproperty, callproperty, and pop.  Those four opcodes are the ABC equivalent of calling the static method on the specified interceptor class.  In this case the rest of the opcodes in the method will be left alone.  Finally the ABC is recreated using AS3 Commons Bytecode and the original SWF tag is overwritten and the invokeCallBack method is called.</p>
<p>Just like before we need a custom preloader to register the patchers:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package preloader <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">managers</span>.<span style="color: #006600;">IPatchManager</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">preloader</span>.<span style="color: #006600;">AbstractPreloader</span>;
&nbsp;
<span style="color: #0066CC;">import</span> patcher.<span style="color: #006600;">SampleXMLPatcher</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleXMLPatcherPreloader <span style="color: #0066CC;">extends</span> AbstractPreloader <span style="color: #66cc66;">&#123;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setupPatchers<span style="color: #66cc66;">&#40;</span>manager:IPatchManager<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">setupPatchers</span><span style="color: #66cc66;">&#40;</span>manager<span style="color: #66cc66;">&#41;</span>;
			manager.<span style="color: #006600;">registerPatcher</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> SampleXMLPatcher<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;interceptors.xml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Notice that a new instance of the SampleXMLPatcher is created and given the URL to the interceptor XML file.  Here is a little test application containing a button that calls Foo&#8217;s getBar method every time it&#8217;s clicked:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">               xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">               preloader=<span style="color: #ff0000;">&quot;preloader.SampleXMLPatcherPreloader&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        import blah.Foo;
        import FooInterceptor; FooInterceptor;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;call foo.getBar()&quot;</span> fontSize=<span style="color: #ff0000;">&quot;32&quot;</span> top=<span style="color: #ff0000;">&quot;20&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:click</span><span style="color: #7400FF;">&gt;</span></span>
                var foo:Foo = new Foo();
                foo.getBar();
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:click</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Button</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Notice that since there wasn&#8217;t a reference anywhere else to the FooInterceptor we had to include one manually otherwise it will not exist in the compiled SWF.  Here is a demo of that application:<br />
<iframe src="http://www.jamesward.com/demos/MixingLoomTestApp/MixingLoomTestApp_SampleXMLPatcher.html" width="100%" height="80"></iframe></p>
<p>Well, that was fun!  And I hope you can see how Mixing Loom can be the foundation for doing AOP in Flex / ActionScript!  But before I let you go I want to show you one more crazy thing we can do with Mixing Loom.  Patchers can do just about anything they want since Mixing Loom provides hooks to modify the second frame, RSLs, and Modules.  For instance, say there is a private method or property in the Flex framework that you need access to.  One option is to use Monkey Patching to replace that class with one that you maintain.  This is not a very maintainable way to get access to something that is private.  Using Mixing Loom you can simply patch the class at runtime.  Here is a simple (but impractical) example&#8230;  The spark.components.Application class has a private method called &#8220;debugTickler&#8221; on it.  Using the <a href="https://github.com/MixingLoom/mixingloom-patchers/tree/master/RevealPrivatesPatcher">RevealPrivatesPatcher</a> from Mixing Loom we can make that method public at runtime.  First extend the base RevealPrivatesPatcher class and tell it only to apply the patcher on the &#8220;spark_&#8221; RSL:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">SwfContext</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">invocation</span>.<span style="color: #006600;">InvocationType</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">patcher</span>.<span style="color: #006600;">RevealPrivatesPatcher</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyRevealPrivatesPatcher <span style="color: #0066CC;">extends</span> RevealPrivatesPatcher <span style="color: #66cc66;">&#123;</span>
&nbsp;
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span> invocationType:InvocationType, swfContext:SwfContext <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>invocationType.<span style="color: #0066CC;">type</span> == InvocationType.<span style="color: #006600;">RSL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">&#40;</span>invocationType.<span style="color: #0066CC;">url</span>.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;spark_&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span>invocationType, swfContext<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            invokeCallBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Then create a custom preloader that registers a new instance of MyRevealPrivatesPatcher and tells it to reveal the &#8220;spark.components:Application&#8221; class and the &#8220;debugTickler&#8221; method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package preloader <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">managers</span>.<span style="color: #006600;">IPatchManager</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">mixingloom</span>.<span style="color: #006600;">preloader</span>.<span style="color: #006600;">AbstractPreloader</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RevealPrivatesPatcherPreloader <span style="color: #0066CC;">extends</span> AbstractPreloader <span style="color: #66cc66;">&#123;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setupPatchers<span style="color: #66cc66;">&#40;</span> manager:IPatchManager <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">setupPatchers</span><span style="color: #66cc66;">&#40;</span> manager <span style="color: #66cc66;">&#41;</span>;
			manager.<span style="color: #006600;">registerPatcher</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MyRevealPrivatesPatcher<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;spark.components:Application&quot;</span>, <span style="color: #ff0000;">&quot;debugTickler&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Finally, use the LoomApplication and the custom preloader in order to have the hooks to patch RSLs:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;ml:LoomApplication</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">                    xmlns:ml=<span style="color: #ff0000;">&quot;library://ns.mixingloom.org/flex/spark&quot;</span></span>
<span style="color: #000000;">                    xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">                    preloader=<span style="color: #ff0000;">&quot;preloader.RevealPrivatesPatcherPreloader&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;ml:applicationComplete</span><span style="color: #7400FF;">&gt;</span></span>
            try {
                this['debugTickler']();
                l.text = &quot;Yeah.  We just modified an RSL at runtime.&quot;;
            } catch (e:Error) {
                l.text = &quot;booo&quot;;
            }
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/ml:applicationComplete</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> id=<span style="color: #ff0000;">&quot;l&quot;</span> text=<span style="color: #ff0000;">&quot;nothing happened.&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/ml:LoomApplication</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Notice that we can&#8217;t use the dot syntax &#8220;this.debugTickler()&#8221; to call the method since the compiler won&#8217;t let us do that.  Instead we have to use the object key syntax &#8220;this['debugTickler']()&#8221; in order to make the method call.  Now watch as Mixing Loom&#8217;s magic wand modifies a signed Flex Framework RSL right before your very eyes:<br />
<iframe src="http://www.jamesward.com/demos/MixingLoomTestApp/MixingLoomTestAppWithFrameworkRSLs.html" width="300" height="30"></iframe></p>
<p>Fun stuff!!!  And there is more to come!  We are working on ways to also modify the first frame of the SWF and to modify a SWF pre-runtime.  But now it&#8217;s your turn!  All of the code for everything you&#8217;ve seen here, as well as some other demos, and goodies is <a href="https://github.com/MixingLoom">all on github</a>.  We&#8217;d love to see the community create some interesting and useful patchers!  So fork away and have fun!  Let me know if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/26/introducing-mixing-loom-runtime-actionscript-bytecode-modification/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Using an Embedded WSDL with Flex&#8217;s WebService API</title>
		<link>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-an-embedded-wsdl-with-flexs-webservice-api</link>
		<comments>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 14:42:53 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[flexorg]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2255</guid>
		<description><![CDATA[Recently I was helping a customer figure out how to use an embedded WSDL with Flex&#8217;s WebService API. One scenario in which this is needed is when the actual WSDL is not available at runtime. In this case the application must contain the WSDL instead of request it at runtime. The Flex WebService API today [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was helping a customer figure out how to use an embedded WSDL with Flex&#8217;s WebService API.  One scenario in which this is needed is when the actual WSDL is not available at runtime.  In this case the application must contain the WSDL instead of request it at runtime.  The Flex WebService API today only supports loading the WSDL over the network at runtime.  Beginning in Flash Builder 4 the Service wizard generate code that internally use the WebService API.  So no matter how you integrate with a SOAP Web Service in Flex, you need the WSDL accessible via a URL at runtime.  This wasn&#8217;t possible for the customer I was working with so we figured out a way to actually embed the WSDL into the application.  Here is what we did&#8230;</p>
<p>I used my <a href="http://www.jamesward.com/census2">Census</a> SOAP Service as a simple service to test this with.  In this case, my WSDL is publicly available at: <a href="http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl">http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl</a></p>
<p>First, I created a new Flex project and saved the WSDL into the src dir of the project.  I built a simple test program using the WebService API directly:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span> <span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #009900;">			   <span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:WebService</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ws&quot;</span> <span style="color: #000066;">wsdl</span>=<span style="color: #ff0000;">&quot;http://www.jamesward.com/census2-tests/services/CensusSOAPService?wsdl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		ws.getElements(0, 50);
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:applicationComplete<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:DataGrid</span> <span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{ws.getElements.lastResult}&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then I ran the application in Chrome with the Network view open in the Chrome Developer Tools panel.  This indicated that, just as expected, the WSDL is used at runtime:<br />
<img src="http://www.jamesward.com/wp/uploads/2011/04/flex_wsdl_webservice.png" title="WSDL Loaded with the Flex WebService API" width="806" height="511" class="alignnone size-full wp-image-2259" /></p>
<p>Next I had a look around the <em>WebService</em> source code (available in &lt;FLEX_SDK&gt;/frameworks/projects/rpc/src/mx/rpc/soap) and discovered there is a <em>loadWSDL</em> method that can be overwritten to handle the embedded loading of the WSDL instead of the default network loading of the WSDL.  So I created a new class that extends the base <em>WebService</em> class, embeds the WSDL file, and overrides the <em>loadWSDL</em> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">ByteArrayAsset</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">core</span>.<span style="color: #006600;">mx_internal</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">WSDLLoadEvent</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">soap</span>.<span style="color: #006600;">mxml</span>.<span style="color: #006600;">WebService</span>;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">wsdl</span>.<span style="color: #006600;">WSDL</span>;
&nbsp;
	use namespace mx_internal;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">dynamic</span> <span style="color: #000000; font-weight: bold;">class</span> MyWebService <span style="color: #0066CC;">extends</span> WebService
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;CensusSOAPService.xml&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const WSDL_CLASS:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyWebService<span style="color: #66cc66;">&#40;</span>destination:<span style="color: #0066CC;">String</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>destination<span style="color: #66cc66;">&#41;</span>;
			loadWSDL<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadWSDL<span style="color: #66cc66;">&#40;</span>uri:<span style="color: #0066CC;">String</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> thing:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> WSDL_CLASS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> baa:ByteArrayAsset = <span style="color: #66cc66;">&#40;</span>thing as ByteArrayAsset<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">xml</span>:<span style="color: #0066CC;">String</span> = baa.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#40;</span>baa.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			deriveHTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> wsdlLoadEvent:WSDLLoadEvent = WSDLLoadEvent.<span style="color: #006600;">createEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WSDL<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">xml</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			wsdlHandler<span style="color: #66cc66;">&#40;</span>wsdlLoadEvent<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In the regular <em>WebService</em> class, setting the <em>wsdl</em> property will trigger the <em>loadWSDL</em> method, but since we are embedding the WSDL, we must manually call the <em>loadWSDL</em> method.  I do that in the constructor.</p>
<p>Embedded assets become a class, so first an instance of that class must be instantiated as a <em>ByteArrayAsset</em> (the default type for files embedded with the application/octet-stream mimeType).  Then the instance is read into a string.  Then the <em>deriveHTTPService</em> method is called to set up the underlying <em>HTTPService</em>&#8216;s channel information.  Finally, we create a <em>WSDL</em> object from the <em>XML</em> that was read from the <em>ByteArrayAsset</em>, create a new <em>WSDLLoadEvent</em> with the <em>WSDL</em>, and call the <em>wsdlHandler</em> method, passing it the <em>wsdlLoadEvent</em>.  This essentially is doing the same thing as the original <em>WebService</em> class, but now doing it without the network request.</p>
<p>I can now switch my test application to use my extension to WebService instead of the original one:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:MyWebService</span> <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ws&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now when I run the application and monitor the network activity I no longer see the WSDL being requested at runtime!  So everything works when using the <em>WebService</em> API directly.  However, the customer I was working with was using the Service wizard in Flash Builder.  So we needed to figure out how to get the generated code to use the new <em>WebService</em> extension instead of the original <em>WebService</em> class.  I went through the Data Wizards and had it generate the client-side stubs for my Census SOAP Service.  This created a <em>CensusSOAPService</em> class that extends the generated <em>_Super_CensusSOAPService</em> class.  The <em>CensusSOAPService</em> is intended to give us a place to make modifications to the generated stuff, while the <em>_Super_CensusSOAPService</em> class is not supposed to be modified because it will be overwritten if we refresh the service.  Looking in the <em>_Super_CensusSOAPService</em> class I discovered that the <em>WebService</em> instance is being created directly in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _Super_CensusSOAPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// initialize service control</span>
        _serviceControl = <span style="color: #000000; font-weight: bold;">new</span> mx.<span style="color: #006600;">rpc</span>.<span style="color: #006600;">soap</span>.<span style="color: #006600;">mxml</span>.<span style="color: #006600;">WebService</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// rest of method omitted</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>These are the kinds of things that really make you wish the Flex framework used dependency injection because we need to set <em>_serviceControl</em> from the <em>CensusSOAPService</em> class.  So we thought&#8230;  Alright, this is not ideal but we can just copy the contents of the <em>_Super_CensusSOAPService</em>&#8216;s constructor into <em>CensusSOAPService</em>&#8216;s constructor, replace the line that instantiates the <em>WebService</em>, have it instantiate <em>MyWebService</em> instead, and then just not call <em>super()</em>.  We gave it a try and for some reason kept getting <em>_serviceControl</em> set as a <em>WebService</em> not <em>MyWebService</em>.  WTF?  It made no sense until we found this little gem in the <a href="http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf67eed-7fff.html">Flex docs</a>:<br />
<em>&#8220;If you define the constructor, but omit the call to super(), Flex automatically calls super() at the beginning of your constructor.&#8221;</em><br />
Now it all made sense!  Since we didn&#8217;t call <em>super()</em>, Flex conveniently inserted a <em>super()</em> call for us!  Fun.  So we had to figure out a way to convince the Flex compiler that we were going to call <em>super()</em>, but then not call it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Voila!  Now the <em>CensusSOAPService</em>&#8216;s constructor sets <em>_serviceControl</em> to a new instance of <em>MyWebService</em> and <em>_Super_CensusSOAPService</em> doesn&#8217;t get the chance to mess that up.</p>
<p>We tested the new <em>CensusSOAPService</em> and everything worked perfectly!</p>
<p>I hope that helps some of you who are using the Flex WebService API.  Let me know if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/15/using-an-embedded-wsdl-with-flexs-webservice-api/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Tour de Mobile Flex on iOS</title>
		<link>http://www.jamesward.com/2011/04/11/tour-de-mobile-flex-on-ios/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tour-de-mobile-flex-on-ios</link>
		<comments>http://www.jamesward.com/2011/04/11/tour-de-mobile-flex-on-ios/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 18:03:04 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[flexorg]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2234</guid>
		<description><![CDATA[Flex support for iOS apps is coming in June 2011!!! Here&#8217;s a little sneak peak of the Tour de Mobile Flex app running on iOS: These are exciting times for developers! With Flex we will be able to use one technology and one code base to build apps for iOS, Android, Playbook, Windows, Mac, Linux, [...]]]></description>
			<content:encoded><![CDATA[<p>Flex support for iOS apps is <a href="http://eon.businesswire.com/news/eon/20110410005103/en/Flash-Platform/Flex/Flash-Builder">coming in June 2011</a>!!!  Here&#8217;s a little sneak peak of the <a href="http://flex.org/tourmobile">Tour de Mobile Flex app</a> running on iOS:<br />
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/F3WvnRRFT5E?rel=0&amp;hd=1" frameborder="0" allowfullscreen></iframe><br />
These are exciting times for developers!  With Flex we will be able to use one technology and one code base to build apps for iOS, Android, Playbook, Windows, Mac, Linux, IE, Firefox, Chrome, Safari, etc!  Fun times!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/11/tour-de-mobile-flex-on-ios/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Building Mobile Apps with Flex 4.5</title>
		<link>http://www.jamesward.com/2011/04/11/building-mobile-apps-with-flex-4-5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-mobile-apps-with-flex-4-5</link>
		<comments>http://www.jamesward.com/2011/04/11/building-mobile-apps-with-flex-4-5/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 13:54:36 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2243</guid>
		<description><![CDATA[Now that Flex 4.5 has officially been announced I wanted to show just how easy it is to start building mobile apps with Flex: Very fun stuff! Let me know what you think.]]></description>
			<content:encoded><![CDATA[<p>Now that Flex 4.5 has <a href="http://www.adobe.com/products/flex/mobile/">officially been announced</a> I wanted to show just how easy it is to start building mobile apps with Flex:<br />
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/RAjbzIYlKGo?rel=0&amp;hd=1" frameborder="0" allowfullscreen></iframe></p>
<p>Very fun stuff!  Let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/11/building-mobile-apps-with-flex-4-5/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Next week in Denver: 360&#124;Flex and Flex Camp 2011</title>
		<link>http://www.jamesward.com/2011/04/04/next-week-in-denver-360flex-and-flex-camp-2011/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=next-week-in-denver-360flex-and-flex-camp-2011</link>
		<comments>http://www.jamesward.com/2011/04/04/next-week-in-denver-360flex-and-flex-camp-2011/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 15:06:37 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2224</guid>
		<description><![CDATA[Next week I&#8217;ll be presenting at two great Flex events here in Denver, Colorado. On Sunday April 10th, Greg Wilson and I will be presenting an &#8220;Intro to Building Mobile Apps&#8221; at 360&#124;Flex. Then on Tuesday April 12 Mike Labriola and I will leave the world of Flex bewildered and befuddled when we present &#8220;Planet [...]]]></description>
			<content:encoded><![CDATA[<p>Next week I&#8217;ll be presenting at two great Flex events here in Denver, Colorado.  On Sunday April 10th, <a href="http://gregsramblings.com/">Greg Wilson</a> and I will be presenting an &#8220;Intro to Building Mobile Apps&#8221; at <a href="http://www.360flex.com/">360|Flex</a>.  Then on Tuesday April 12 <a href="http://www.digitalprimates.net/author/codeslinger/">Mike Labriola</a> and I will leave the world of Flex bewildered and befuddled when we present &#8220;Planet of the AOPs&#8221; (also at 360|Flex).  And on Thursday April 14 I&#8217;ll be presenting &#8220;Building Cross-Device Apps with Flex&#8221; at the <a href="http://ria5280.org/calendar/event/2011/4/14/251740">Denver Flex Camp</a>.  It&#8217;s going to be a really fun week and I hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/04/04/next-week-in-denver-360flex-and-flex-camp-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Planet of the AOPs at 360&#124;Flex</title>
		<link>http://www.jamesward.com/2011/03/24/planet-of-the-aops-at-360flex/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=planet-of-the-aops-at-360flex</link>
		<comments>http://www.jamesward.com/2011/03/24/planet-of-the-aops-at-360flex/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 15:09:06 +0000</pubDate>
		<dc:creator>James Ward</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.jamesward.com/?p=2215</guid>
		<description><![CDATA[This year at 360&#124;Flex in Denver I&#8217;ll be co-presenting a session with Mike Labriola called &#8220;Planet of the AOPs&#8220;. What is the Planet of the AOPs? Let me give you a little hint&#8230; public class Foo &#123; private function getPrivateBar&#40;&#41;:String &#123; return &#34;private bar&#34;; &#125; &#125; var foo:Foo = new Foo&#40;&#41;; foo&#91;'getPrivateBar'&#93;&#40;&#41;; Yup! It&#8217;s gonna [...]]]></description>
			<content:encoded><![CDATA[<p>This year at <a href="http://www.360flex.com/">360|Flex</a> in Denver I&#8217;ll be co-presenting a session with <a href="http://www.digitalprimates.net/author/codeslinger/">Mike Labriola</a> called &#8220;<a href="http://www.360flex.com/schedule/">Planet of the AOPs</a>&#8220;.  What is the Planet of the AOPs?  Let me give you a little hint&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPrivateBar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;private bar&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> foo:Foo = <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
foo<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'getPrivateBar'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Yup!  It&#8217;s gonna be fun!  The session will be on Tuesday April 12th at 2:50pm.  Hope to see you there!</p>
<p>Also, Mike and I are doing a <a href="http://www.360flex.com/blog/2011/03/360flex-2011-speaker-chat-michael-labriola/">360|Flex speaker chat</a> today at 11am (MDT).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamesward.com/2011/03/24/planet-of-the-aops-at-360flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

