Alexandre Baptiste
Alexandre Baptiste

Reputation: 287

Cordova #Intent-BroadcastReceiver

First of all, I'm working with some specific API ( Grand Stream GXV3275 phone ) which requires that Intent - BroadcastReceiver combo breaker.

When my device is on landscape orientation it works good so the problem came with Intent - BroadcastReceiver.

So I need that IntentFilter to know my HOOKEVENT ans then receive it with that BroadcastReceiver.

I just want to know why it doesn't even show the alert or don't work at all. Is that possible to deal with IntentFilter on CordovaPlugin? With BroadcastReceiver?

I made some test on my CordovaActivity and HOOKEVENT ; updating a text-view. So I assume that's a problem with CordovaPlugin.

I also tried to do:

CordovaActivity activity = (CordovaActivity) this.cordova.getActivity();
activity.getJs(); 

Which normally allow me to get string that works on my activity but gave me NPE..

public class Toast extends CordovaPlugin {

private String javascript = "";

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    initHookEvent();
    switch (action) {
        case "reversed":
            reversedTest();
            return true;
    }
    return false;
}

private Activity getActivity() { return this.cordova.getActivity();}

private void reversedTest(){
   Configuration configuration = getActivity().getResources().getConfiguration();
   if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
       webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"Landscape\";");
   }
   webView.sendJavascript(javascript);
}

public void initHookEvent() {
   IntentFilter filter = new IntentFilter("com.base.module.phone.HOOKEVENT");
   getActivity().registerReceiver(broadcastReceiver, filter);
}

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
      webView.sendJavascript("javascript:alert(\"test\");");
      if (intent.getBooleanExtra("hookoff", false)){
         javascript = "javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";";
      }
      else{
         javascript = "javascript:document.getElementById(\"combi\").innerHTML=\"raccroche\";";
      }
   }
};

Upvotes: 2

Views: 4323

Answers (1)

Alexandre Baptiste
Alexandre Baptiste

Reputation: 287

I found myself my problem.

I create a specific plugin only for that after. You just needed to :

webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";");

And

getActivity().getApplicationContext().registerReceiver(broadcastReceiver_hook, filter_hook);

Here's my final plugin :

public class Hook extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    initHookEvent();
    return false;
}


/**
 * Use to get the current Cordova Activity
 * @return your Cordova activity
 */
private Activity getActivity() { return this.cordova.getActivity();}

/**
 * Initializing GXV 3275 Hook Event
 * You ABSOLUTELY need to precise getActivity().getApplicationContext()
 * before registerReceiver() otherwise it won't get the good context.
 */
public void initHookEvent() {
    IntentFilter filter_hook = new IntentFilter("com.base.module.phone.HOOKEVENT");
    getActivity().getApplicationContext().registerReceiver(broadcastReceiver_hook, filter_hook);
}

/**
 * BroadcastReceiver is also needed with GXV 3275 Hook Event
 * Just sendJavascript for each cases
 *       /!\ webView /!\
 * Is natively created by extending CordovaPlugin
 */
public BroadcastReceiver broadcastReceiver_hook = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ( intent.getBooleanExtra("hookoff", false)){
            webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"decroche\";");
            webView.sendJavascript("javascript:document.getElementById(\"combi\").style.opacity = 1;");
        }
        else{
            webView.sendJavascript("javascript:document.getElementById(\"combi\").innerHTML=\"raccroche\";");
            webView.sendJavascript("javascript:document.getElementById(\"combi\").style.opacity = 1;");
        }
    }
};

}

Upvotes: 6

Related Questions