|
| 1 | +/* |
| 2 | +Copyright (C) 2013 by Pierre-Yves Orban |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | +package org.apache.cordova.plugin; |
| 23 | + |
| 24 | +import org.apache.cordova.api.CallbackContext; |
| 25 | +import org.apache.cordova.api.CordovaPlugin; |
| 26 | +import org.apache.cordova.api.PluginResult; |
| 27 | +import org.json.JSONArray; |
| 28 | +import org.json.JSONException; |
| 29 | + |
| 30 | +import android.app.Activity; |
| 31 | +import android.app.PendingIntent; |
| 32 | +import android.content.Intent; |
| 33 | +import android.content.pm.PackageManager; |
| 34 | +import android.telephony.SmsManager; |
| 35 | + |
| 36 | +public class SmsSendingPlugin extends CordovaPlugin { |
| 37 | + public final String ACTION_HAS_SMS_POSSIBILITY = "HasSMSPossibility"; |
| 38 | + public final String ACTION_SEND_SMS = "SendSMS"; |
| 39 | + |
| 40 | + private SmsManager smsManager = null; |
| 41 | + |
| 42 | + public SmsSendingPlugin() { |
| 43 | + super(); |
| 44 | + smsManager = SmsManager.getDefault(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean execute(String action, JSONArray args, |
| 49 | + final CallbackContext callbackContext) throws JSONException { |
| 50 | + |
| 51 | + if (action.equals(ACTION_HAS_SMS_POSSIBILITY)) { |
| 52 | + |
| 53 | + Activity ctx = this.cordova.getActivity(); |
| 54 | + if(ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)){ |
| 55 | + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true)); |
| 56 | + } else { |
| 57 | + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, false)); |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + else if (action.equals(ACTION_SEND_SMS)) { |
| 62 | + try { |
| 63 | + String phoneNumber = args.getString(0); |
| 64 | + String message = args.getString(1); |
| 65 | + this.sendSMS(phoneNumber, message); |
| 66 | + |
| 67 | + callbackContext.sendPluginResult(new PluginResult( |
| 68 | + PluginResult.Status.OK)); |
| 69 | + } |
| 70 | + catch (JSONException ex) { |
| 71 | + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ex.getMessage())); |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + private void sendSMS(String phoneNumber, String message) { |
| 79 | + PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0); |
| 80 | + smsManager.sendTextMessage(phoneNumber, null, message, sentIntent, null); |
| 81 | + } |
| 82 | +} |
0 commit comments