Advertisement
Guest User

webintent.java

a guest
Jul 9th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.84 KB | None | 0 0
  1. package com.borismus.webintent;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.apache.cordova.DroidGap;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.content.Intent;
  12. import android.net.Uri;
  13. import android.util.Log;
  14. import android.text.Html;
  15.  
  16. import org.apache.cordova.api.CordovaPlugin;
  17. import org.apache.cordova.api.CallbackContext;
  18.  
  19. /**
  20.  * WebIntent is a PhoneGap plugin that bridges Android intents and web
  21.  * applications:
  22.  *
  23.  * 1. web apps can spawn intents that call native Android applications. 2.
  24.  * (after setting up correct intent filters for PhoneGap applications), Android
  25.  * intents can be handled by PhoneGap web applications.
  26.  *
  27.  * @author boris@borismus.com
  28.  *
  29.  */
  30. public class WebIntent extends Plugin {
  31.  
  32.     private String onNewIntentCallback = null;
  33.  
  34.     /**
  35.      * Executes the request and returns PluginResult.
  36.      *
  37.      * @param action
  38.      *            The action to execute.
  39.      * @param args
  40.      *            JSONArray of arguments for the plugin.
  41.      * @param callbackId
  42.      *            The callback id used when calling back into JavaScript.
  43.      * @return A PluginResult object with a status and message.
  44.      */
  45. // NEU
  46.     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
  47. //
  48.         try {
  49.             if (action.equals("startActivity")) {
  50.                 if (args.length() != 1) {
  51.                     //return new PluginResult(PluginResult.Status.INVALID_ACTION);
  52.                     callbackContext.error();
  53.                     return true;
  54.                 }
  55.  
  56.                 // Parse the arguments
  57.                 JSONObject obj = args.getJSONObject(0);
  58.                 String type = obj.has("type") ? obj.getString("type") : null;
  59.                 Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
  60.                 JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
  61.                 Map<String, String> extrasMap = new HashMap<String, String>();
  62.  
  63.                 // Populate the extras if any exist
  64.                 if (extras != null) {
  65.                     JSONArray extraNames = extras.names();
  66.                     for (int i = 0; i < extraNames.length(); i++) {
  67.                         String key = extraNames.getString(i);
  68.                         String value = extras.getString(key);
  69.                         extrasMap.put(key, value);
  70.                     }
  71.                 }
  72.  
  73.                 startActivity(obj.getString("action"), uri, type, extrasMap);
  74.                 //return new PluginResult(PluginResult.Status.OK);
  75.  
  76.                 callbackContext.success();
  77.                 return true;
  78.  
  79.             } else if (action.equals("hasExtra")) {
  80.                 if (args.length() != 1) {
  81.                     return new PluginResult(PluginResult.Status.INVALID_ACTION);
  82.                 }
  83.                 Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  84.                 String extraName = args.getString(0);
  85.                 return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
  86.  
  87.             } else if (action.equals("getExtra")) {
  88.                 if (args.length() != 1) {
  89.                     return new PluginResult(PluginResult.Status.INVALID_ACTION);
  90.                 }
  91.                 Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  92.                 String extraName = args.getString(0);
  93.                 if (i.hasExtra(extraName)) {
  94.                     return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
  95.                 } else {
  96.                     return new PluginResult(PluginResult.Status.ERROR);
  97.                 }
  98.             } else if (action.equals("getUri")) {
  99.                 if (args.length() != 0) {
  100.                     return new PluginResult(PluginResult.Status.INVALID_ACTION);
  101.                 }
  102.  
  103.                 Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
  104.                 String uri = i.getDataString();
  105.                 return new PluginResult(PluginResult.Status.OK, uri);
  106.             } else if (action.equals("onNewIntent")) {
  107.                 if (args.length() != 0) {
  108.                     return new PluginResult(PluginResult.Status.INVALID_ACTION);
  109.                 }
  110.  
  111.                 this.onNewIntentCallback = callbackId;
  112.                 PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
  113.                 result.setKeepCallback(true);
  114.                 return result;
  115.             } else if (action.equals("sendBroadcast"))
  116.             {
  117.                 if (args.length() != 1) {
  118.                     return new PluginResult(PluginResult.Status.INVALID_ACTION);
  119.                 }
  120.  
  121.                 // Parse the arguments
  122.                 JSONObject obj = args.getJSONObject(0);
  123.  
  124.                 JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
  125.                 Map<String, String> extrasMap = new HashMap<String, String>();
  126.  
  127.                 // Populate the extras if any exist
  128.                 if (extras != null) {
  129.                     JSONArray extraNames = extras.names();
  130.                     for (int i = 0; i < extraNames.length(); i++) {
  131.                         String key = extraNames.getString(i);
  132.                         String value = extras.getString(key);
  133.                         extrasMap.put(key, value);
  134.                     }
  135.                 }
  136.  
  137.                 sendBroadcast(obj.getString("action"), extrasMap);
  138.                 return new PluginResult(PluginResult.Status.OK);
  139.             }
  140.             return new PluginResult(PluginResult.Status.INVALID_ACTION);
  141.         } catch (JSONException e) {
  142.             e.printStackTrace();
  143.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
  144.         }
  145.     }
  146.  
  147.     @Override
  148.     public void onNewIntent(Intent intent) {
  149.         if (this.onNewIntentCallback != null) {
  150.             PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString());
  151.             result.setKeepCallback(true);
  152.  
  153.             callbackContext.sendPluginResult(result);
  154.             //this.success(result, this.onNewIntentCallback);
  155.         }
  156.     }
  157.  
  158.     void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
  159.         Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
  160.        
  161.         if (type != null && uri != null) {
  162.             i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
  163.         } else {
  164.             if (type != null) {
  165.                 i.setType(type);
  166.             }
  167.         }
  168.        
  169.         for (String key : extras.keySet()) {
  170.             String value = extras.get(key);
  171.             // If type is text html, the extra text must sent as HTML
  172.             if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
  173.                 i.putExtra(key, Html.fromHtml(value));
  174.             } else if (key.equals(Intent.EXTRA_STREAM)) {
  175.                 // allowes sharing of images as attachments.
  176.                 // value in this case should be a URI of a file
  177.                 i.putExtra(key, Uri.parse(value));
  178.             } else if (key.equals(Intent.EXTRA_EMAIL)) {
  179.                 // allows to add the email address of the receiver
  180.                 i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
  181.             } else {
  182.                 i.putExtra(key, value);
  183.             }
  184.         }
  185.         ((DroidGap)this.cordova.getActivity()).startActivity(i);
  186.     }
  187.  
  188.     void sendBroadcast(String action, Map<String, String> extras) {
  189.         Intent intent = new Intent();
  190.         intent.setAction(action);
  191.         for (String key : extras.keySet()) {
  192.             String value = extras.get(key);
  193.             intent.putExtra(key, value);
  194.         }
  195.  
  196.         ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement