Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *
  3.  * Phonegap print plugin for Android
  4.  * Luis Balam 2012
  5.  * @version 0.15.1
  6.  *
  7.  */
  8.  
  9. package org.opencorebanking.plugins;
  10.  
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.content.pm.PackageManager;
  18. import android.content.pm.ResolveInfo;
  19. //import android.content.pm.*;
  20. //import android.content.*;
  21. import android.net.*;
  22. import android.util.Log;
  23.  
  24. import java.util.*;
  25.  
  26. import org.apache.cordova.api.Plugin;
  27. import org.apache.cordova.api.PluginResult;
  28.  
  29. public class LocalPrint extends Plugin {
  30.     private static final String ADR_PRINT   = "org.androidprinting.intent.action.PRINT";
  31.     private static final String ADR_SHARE   = "com.dynamixsoftware.printershare";
  32.     private static final String ADR_CLOUD   = "com.sec.android.app.mobileprint.PRINT";
  33.    
  34.    
  35.     @Override
  36.     public PluginResult execute(String action, JSONArray args, String callbackId) {
  37.         Context ctx2                    = this.cordova.getActivity().getApplicationContext();
  38.         try {
  39.             JSONObject jo   = args.getJSONObject(0);
  40.             Uri uri         = Uri.parse(jo.getString("fileURI"));
  41.             doPrintIntent(uri, jo.getString("mimeType"), ctx2);
  42.             //Log.e("org.opencorebanking", "get a intent action");
  43.             return new PluginResult(PluginResult.Status.OK);
  44.         } catch (JSONException e) {
  45.             //Log.e("org.opencorebanking", "Error in Intent action");
  46.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
  47.         }
  48.     }
  49.    
  50.     /*@SuppressWarnings("deprecation")*/
  51.     private void doPrintIntent(Uri fileURI, String mimeType, Context ctx) {
  52.        
  53.  
  54.         if( ctx.getPackageManager().getLaunchIntentForPackage(ADR_SHARE) != null ){
  55.             Intent i = new Intent(android.content.Intent.ACTION_VIEW);
  56.             i.setPackage("com.dynamixsoftware.printershare");
  57.             i.setDataAndType(fileURI, mimeType);
  58.             this.cordova.startActivityForResult(this, i, 0);
  59.             //Log.e("org.opencorebanking", ADR_SHARE);     
  60.  
  61.         } else {
  62.             if( this.isIntentAvailable(ADR_CLOUD, ctx) != false ){
  63.                 Intent i = new Intent(android.content.Intent.ACTION_SEND);
  64.                 i.setPackage("com.pauloslf.cloudprint");
  65.                 i.setDataAndType(fileURI, mimeType);
  66.                 this.cordova.startActivityForResult(this, i, 0);
  67.                 //Log.e("org.opencorebanking", ADR_CLOUD);
  68.             } else {
  69.                 if( this.isIntentAvailable(ADR_PRINT, ctx) != false) {
  70.                     Intent sendIntent = new Intent(ADR_PRINT);
  71.                     sendIntent.addCategory(Intent.CATEGORY_DEFAULT);
  72.                     sendIntent.setDataAndType(fileURI, mimeType);
  73.                     this.cordova.startActivityForResult(this, sendIntent, 0);
  74.                     //Log.e("org.opencorebanking", ADR_PRINT);
  75.                 } else {
  76.                     Intent sendIntent = new Intent(android.content.Intent.ACTION_VIEW);
  77.                     sendIntent.setDataAndType(fileURI, mimeType);
  78.                     this.cordova.startActivityForResult(this, sendIntent, 0);
  79.                     Log.e("org.opencorebanking", "No Intent available");
  80.                 }
  81.             }
  82.         }
  83.     }
  84.     //http://android-developers.blogspot.mx/2009/01/can-i-use-this-intent.html
  85.     /**
  86.      * Indicates whether the specified action can be used as an intent. This
  87.      * method queries the package manager for installed packages that can
  88.      * respond to an intent with the specified action. If no suitable package is
  89.      * found, this method returns false.
  90.      *
  91.      * @param action The Intent action to check for availability.
  92.      *
  93.      * @return True if an Intent with the specified action can be sent and
  94.      *         responded to, false otherwise.
  95.      */
  96.     private boolean isIntentAvailable(String action, Context ctxX) {
  97.        
  98.         final PackageManager packageManager = ctxX.getPackageManager();
  99.  
  100.         final Intent intent = new Intent(action);
  101.         List<ResolveInfo> list =
  102.                 packageManager.queryIntentActivities(intent,
  103.                         PackageManager.MATCH_DEFAULT_ONLY);
  104.         return list.size() > 0;
  105.     }
  106.  
  107. }