Question

Basicly I'm trying to cancel method execution in an aspect. So, here's my program: I have

  • a Sender Application

  • a receiver Application (let's call it the Central Monitor)

In the Sender App : I have,

  • An Activity (ın this activity, I have a method called callMethodA() )
  • An Aspect (In this aspect, I'm catching before the callMethodA() execution and in this before structure, I'm starting the Service )
  • A Service (When this service is started, it basicly sends a String to the Receiver App by Broadcast)

In the Receiver App: I have :

  • An Activity ( When a string is broadcasted by sender app, then it receives the broadcast by broadcastreceiver and puts the string into an automaton which checks some condition and when automaton completes, then Result string is broadcasted back to the sender App)

So here's my actual question :

When the Sender App receives the Result string (In the sender's Activity), if the Result is Success, then I want to allow callMethodA() to be executed( this can be done by doing simply nothing, because we caught the callMethodA() execution with before and if we do nothing, then this method will be executed. ) But if the Result is FAIL then I want this method not to be executed and also I want whole program to keep running (I mean for this broadcast result, callMethodA() doesn't have to be executed, It can be executed on the next broadcast result according to the automaton result), also I want to cancel it in the aspect.

Simply, please teach me how can I cancel a method execution in an aspect.

Here's the Sender App codes:

Sender1Activity.java

 package com.example.sender;

import java.util.Random;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Sender1Activity extends Activity {
    Button btn_send;
    public Intent serviceIntent;
    public Context context;

    //The initial state for automaton Result is Success, nothing fancy. 
    static String string_AutomatonResult = "Success";

    public int id;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // context = getApplicationContext();
        setContentView(R.layout.activity_sender1);
        // serviceIntent = new Intent(context, senderService.class);
        btn_send = (Button) findViewById(R.id.buttonSend);

        methodCallerHandler.removeCallbacks(hMyValueTask);
        methodCallerHandler.post(hMyValueTask);

        registerReceiver(broadcastReceiver_AutomatonResult, new IntentFilter(
                "intent_AutomatonResult"));

    }

    // callMethodA()  is called in the random time. It's just provides randomness
    public Handler methodCallerHandler = new Handler();
    public Runnable hMyValueTask = new Runnable() {
        public void run() {
            int n = new Random().nextInt(3000);
            System.out.println("A random delay : " + (float) n / 1000
                    + " seconds");
            callMethodA(new View(getApplicationContext()));
            methodCallerHandler.postDelayed(hMyValueTask, (long) n);

        }
    };


    // The actual method who starts everything, it does simply nothing for now.
    public void callMethodA(View v) {

        System.out.println("MethodA called");
    }


    // Receives Automaton result from the receiver via BroadcastReceiver 
    public BroadcastReceiver broadcastReceiver_AutomatonResult = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                string_AutomatonResult = bundle
                        .getString("automatonResult_Put_String");
                System.out
                        .println("***************************************************************");
                System.out.println("** Automaton Result returned to Sender1 : "
                        + string_AutomatonResult + "**");
                System.out
                        .println("***************************************************************");
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(broadcastReceiver_AutomatonResult);
        stopService(serviceIntent);
    }

}

senderService.java

package com.example.sender;

import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;



//Sends "a" string to the receiver via Broadcast
public class senderService extends Service {

    String value = String.valueOf("a");

    @Override
    public void onCreate() {
        super.onCreate();

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        mSendValue.removeCallbacks(hMyValueTask);
        mSendValue.post(hMyValueTask);
        return startId;
    }

    public Handler mSendValue = new Handler();
    public Runnable hMyValueTask = new Runnable() {
        public void run() {
            publishBuiltinAccelResults(value);

        }
    };

    @Override
    public void onDestroy() {
    }

    public void publishBuiltinAccelResults(String value) {

        Intent intent = new Intent("ResultsA");
        intent.putExtra("resultA", value);
        sendBroadcast(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

Test.aj (Sender's aspect)

package com.example.sender;

import android.app.Activity;
import android.content.Intent;

public aspect Test {
    int i = 1;

    // pointcut for the callMethodA() execution.
    pointcut pointcutCatchMethod(Activity activity) :  execution(* callMethodA(*))
     && target(activity);

    // a hopeless try for me to cancel the method execution, see below please
    pointcut pointcutMethodAExecution() : execution(* callMethodA(*));

    // before callMethodA() execution, start sendService which sends string "a"
    // to the Receiver
    before(Activity activity) : pointcutCatchMethod(activity) {

        System.out.println("******" + "Beginning of " + i
                + "th Aspect *************************");
        Intent sendIntent = new Intent(activity.getApplicationContext(),
                senderService.class);

        System.out.println("SenderService is starting");
        activity.startService(sendIntent);

        System.out
                .println(" callMethodA() cought by before aspect , aspect No: "
                        + i);
        i++;

    }

    // a hopeless try, for example, here, when string_AutomatonResult is FAIL,
    // then I want to cancel callMethodA() execution, then I want also whole
    // program to keep running.
    Object around() : pointcutMethodAExecution(){
        Object result = proceed();
        System.out.println("aut res : "
                + Sender1Activity.string_AutomatonResult);
        System.out.println("******" + "End of " + i
                + "th Aspect ******************");
        return result;
    }

}

Receiver Application codes below:

ReceiverActivity.java

package com.example.receiver;

import android.app.Activity;

public class ReceiverActivity extends Activity {

    TextView txt_recA;
    TextView txt_recB;
    TextView txt_packageNumber;
    String returningStringInput;
    TextView txt_nowReceived;

    int state = 1;
    String automatonResult = "Init";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Integer pkg_number_int = 0;

        setContentView(R.layout.activity_receiver);
        txt_recA = (TextView) findViewById(R.id.txt_recA);
        txt_recB = (TextView) findViewById(R.id.txt_recB);
        txt_nowReceived = (TextView) findViewById(R.id.txt_nowReceived);
        txt_packageNumber = (TextView) findViewById(R.id.txt_packageNumber);

        registerReceiver(receiverResultsA, new IntentFilter("ResultsA"));
        registerReceiver(receiverResultsB, new IntentFilter("ResultsB"));

    }

    // Broadcast Receiver for the string "a" coming from Sender1.
    public BroadcastReceiver receiverResultsA = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                String sentStringA = bundle.getString("resultA");
                returningStringInput = sentStringA;
                AutomatonAB(sentStringA);
                txt_recA.setText(sentStringA);
                txt_nowReceived.setText("Now Received String : " + sentStringA);

            }
        }
    };

    // Ignore this BroadcastReceiver, because I have 2 Senders actually. This is
    // for other sender
    public BroadcastReceiver receiverResultsB = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                String sentStringB = bundle.getString("resultB");
                returningStringInput = sentStringB;

                AutomatonAB(sentStringB);

                txt_nowReceived.setText("Now Received String : " + sentStringB);

                txt_recB.setText(sentStringB);

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // unregisterReceiver(receiverResultsPackageNumber);
        unregisterReceiver(receiverResultsA);
        unregisterReceiver(receiverResultsB);

    }

    // Automaton for checking the strings coming from the senders. In the end,
    // it broadcasts the result to the senders (FAIL or SUCCESS)
    public String AutomatonAB(String returningString) {
        int stringIntValue = 0;

        // to use Java version below than 1.7, 'cause string value
        // cannot be used on switch...
        if (returningString.equals("a")) {
            stringIntValue = 1;
        } else if (returningString.equals("b")) {
            stringIntValue = 2;

        } else {
            System.out.println("No input");
        }

        switch (stringIntValue) {

        case 1:
            switch (state) {
            case 1:
                System.out.println("Status : Passing from State 1 to State 2");
                state = 2;
                System.out.println(" Success ");
                // Status : Passing from State 1 to State 2 :
                automatonResult = "Success2";

                break;
            case 2:
                System.out
                        .println("Status : Passing from State2 to Failure State");
                state = 3;
                System.out.println(" Failure ");
                // Status : Passing from State2 to Failure State :
                automatonResult = "Failure";

                break;

            default:
                break;

            }

            break;

        case 2:
            switch (state) {
            case 1:
                System.out
                        .println("Status : Passing from State 1 to Failure State");
                state = 3;
                System.out.println(" Failure ");
                // Status : Passing from State 1 to Failure State :
                automatonResult = "Failure";

                break;

            case 2:
                System.out.println("Status : Passing from State 2 to State 1");
                state = 1;
                System.out.println(" Success ");
                // Status : Passing from State 2 to State 1 :
                automatonResult = "Success1";
                break;
            default:
                break;
            }

            break;

        default:
            break;
        }

        // to make automaton keep going on the next turns.
        if (state == 3) {
            state = 1;
        }

        System.out.println("automata result : " + automatonResult);
        txt_packageNumber.setText(automatonResult);

        //Broadcast the automaton result to the senders
        Intent intent = new Intent("intent_AutomatonResult");
        intent.putExtra("automatonResult_Put_String", automatonResult);
        sendBroadcast(intent);

        return automatonResult;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Was it helpful?

Solution

I've resolved my problem, using Around advice is the solution,

  1. if we use proceed() then method is going to be executed, if we don't then it is not going to.

  2. if method returns a value, you can manipulate the methods return value in around advice like: return null. or if the method is void , then simply don't call the proceed().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top