Feed on
Posts
Comments

Implementation

In the following are reported the guidelines for realise step by step the implementation of the TestSmsViewer example. If you do not want to code by hand you can find the Eclipse project related to this example inside the utils-projects folder of the standard JaCa-Android distribution.

SmsViewer

Modify the Application main activty as showed above. This activity must extends the JaCaActivity class (a JaCa-Android extension of the standard Android activity class).

package jaca.android.example;

import jaca.android.dev.JaCaActivity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SmsViewer extends JaCaActivity {

    private Handler mHandler;
    private ArrayAdapter mConversationArrayAdapter;
    private ListView mConversationView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_viewer);
	    mConversationArrayAdapter = new ArrayAdapter(this, R.layout.message);
        mConversationView = (ListView) findViewById(R.id.in);
        mConversationView.setAdapter(mConversationArrayAdapter);
        mHandler = new Handler();
        runJaCaService(R.string.mas2j);
	    createArtifact("viewer", ViewerArtifact.class.getCanonicalName());
	}

	public void append(final String source, final String msg){
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				mConversationArrayAdapter.add(source+":  " + msg);
			}
		});
	}
}

What it is worth of particular attention here is:

  • The runJaCaService method: we use this method to run our JaCa MAS. This method need the string resources that identify the mas2j file path.
  • The append method: we use this method for adding a new SMS to the GUI. Note this method is always called by the ViewerArtifact wrapping the activity (see bellow).


XML files used for realising the GUI

We report here the xml file used fore defining the GUI of the sample application.

The main GUI: sms_viewer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
    />
</LinearLayout>



The GUI used for displaying each message on screen: message.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:padding="5dp"
/>


ViewerArtifact

We will use this artifact for managing the application GUI (i.e. if you take a look at the init of the artifact you will see that the ViewerArtifact wrap the real application GUI, the SmsViewer JaCaActivity). What is worth of particular attention in this class is the append operation. This operation allows us to add a new message to the list of the received messages of the wrapped SmsViewer. For more details about realising GUI in JaCa-Android see here.

package jaca.android.example;

import jaca.android.dev.GUIArtifact;
import jaca.android.dev.IJaCaActivity;
import android.os.Bundle;
import cartago.INTERNAL_OPERATION;
import cartago.OPERATION;
import cartago.ObsProperty;

public class ViewerArtifact extends GUIArtifact{

	private SmsViewer mViewer;

	protected void init(IJaCaActivity activity, Bundle savedInstanceState) {
		super.init(activity, savedInstanceState);
		mViewer = (SmsViewer) activity;
		defineObsProperty("viewer_state", "not_displayed");
	}

	@OPERATION public void addSMSToList(String source, String msg){
    	mViewer.append(source, msg);
    }

    @INTERNAL_OPERATION void onStart(){
    	ObsProperty obsProp = getObsProperty("viewer_state");
		obsProp.updateValue("displayed");
    }

	@INTERNAL_OPERATION void onStop(){
		ObsProperty obsProp = getObsProperty("viewer_state");
		obsProp.updateValue("not_displayed");
    }
}


Agent

Add a new agent called agent creating a file agent.asl with the following content in the jaca.android.example package. This is the agent we were refering in the example.mas2j file. The agent source code is quite simple. We skip for now deep explanations of the init plan, will be deeply discussed in the JaCa-Android by example tutorial, for now is sufficient to know that this plan takes care of the join to the MiddlewareServices shared workspace. Once the agent has joined this workspace it can start its job (plan do_job). The agent focus the sms-manager artifact for waiting events related to SMS reception. As soon an event indicating a new SMS is perceived (+sms_received(Source, Message)) the addSMSToList operation of the ViewerArtifact operation is called for appending the new message to the list.

!start.

+!start
  <-  !init;
       !do_job.

/*
 * Initialisation plan for joining the MiddlewareServices workspace
 */
+!init
  <-  lookupArtifact("console", LocalConsoleID);
       focusWhenAvailable("viewer");
       lookupArtifact("viewer", ViewerID);
       +art_id(viewer, ViewerID);
       +art_id(console,LocalConsoleID);
       +join_attempts(0);
       !try_join.

+!try_join : art_id(console,LocalConsoleID) & join_attempts(N) & N < 3
  <-  -+join_attempts(N+1);
       joinRemoteWorkspace("MiddlewareServices", "jaca.android.jacaservice", WspID);
       +wsp_id(jaca_services,WspID);
       println("Join to MiddlewareServices wsp done.")[artifact_id(LocalConsoleID)].

-!try_join : join_attempts(N) & N >= 3
  <-  println("Failure, cannot join MiddlewareServices wsp.")[artifact_id(LocalConsoleID)].

-!try_join
  <-  println("Failure,
           trying join MiddlewareServices again in one second.")[artifact_id(LocalConsoleID)];
       .wait(1000);
       !try_join.

/******************** Main agent plan ********************/		

+!do_job: true
  <-  focusWhenAvailable("sms-manager");
       .println("init done").

+sms_received(A, B) : art_id(viewer, ViewerID)
  <-  addSMSToList(A, B)[artifact_id(ViewerID)].



Below is reported a screenshot of the running application.

Comments are closed.

-->