Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new function count to provide count of SMS #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/android/SMSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SMSPlugin
private static final String ACTION_DELETE_SMS = "deleteSMS";
private static final String ACTION_RESTORE_SMS = "restoreSMS";
private static final String ACTION_SEND_SMS = "sendSMS";
private static final String ACTION_COUNT_SMS = "count";

public static final String OPT_LICENSE = "license";
private static final String SEND_SMS_ACTION = "SENT_SMS_ACTION";
Expand Down Expand Up @@ -114,6 +115,9 @@ public boolean execute(String action, JSONArray inputs, CallbackContext callback
JSONArray addressList = inputs.optJSONArray(0);
String message = inputs.optString(1);
result = this.sendSMS(addressList, message, callbackContext);
} else if (ACTION_COUNT_SMS.equals(action)) {
JSONObject filters = inputs.optJSONObject(0);
result = this.count(filters, callbackContext);
} else {
Log.d(LOGTAG, String.format("Invalid action passed: %s", action));
result = new PluginResult(PluginResult.Status.INVALID_ACTION);
Expand Down Expand Up @@ -229,6 +233,25 @@ private PluginResult sendSMS(JSONArray addressList, String text, CallbackContext
return null;
}

private PluginResult count(JSONObject filter, CallbackContext callbackContext) {
Log.i(LOGTAG, ACTION_LIST_SMS);
String uri_filter = filter.has(BOX) ? filter.optString(BOX) : "inbox";
int fread = filter.has(READ) ? filter.optInt(READ) : -1;
int fid = filter.has("_id") ? filter.optInt("_id") : -1;
String faddress = filter.optString(ADDRESS);
String fcontent = filter.optString(BODY);
int indexFrom = filter.has("indexFrom") ? filter.optInt("indexFrom") : 0;
// int maxCount = filter.has("maxCount") ? filter.optInt("maxCount") : 10;
JSONArray jsons = new JSONArray();
Activity ctx = this.cordova.getActivity();
Uri uri = Uri.parse((SMS_URI_ALL + uri_filter));
Cursor cur = ctx.getContentResolver().query(uri, (String[])null, "", (String[])null, null);

callbackContext.success(cur.getCount());

return null;
}

private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) {
Log.i(LOGTAG, ACTION_LIST_SMS);
String uri_filter = filter.has(BOX) ? filter.optString(BOX) : "inbox";
Expand Down Expand Up @@ -260,7 +283,7 @@ private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext)
if (! matchFilter) continue;

if (i < indexFrom) continue;
if (i >= indexFrom + maxCount) break;
if (maxCount > 0 && i >= indexFrom + maxCount) break;
++i;

if ((json = this.getJsonFromCursor(cur)) == null) {
Expand Down Expand Up @@ -476,6 +499,7 @@ private ContentValues getContentValuesFromJson(JSONObject json) {
values.put( SERVICE_CENTER, json.optString(SERVICE_CENTER));
return values;
}

private PluginResult restoreSMS(JSONArray array, CallbackContext callbackContext) {
ContentResolver resolver = this.cordova.getActivity().getContentResolver();
Uri uri = Uri.parse(SMS_URI_INBOX);
Expand Down
7 changes: 7 additions & 0 deletions www/SMS.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cordova.define("cordova-plugin-sms.SMS", function(require, exports, module) {

var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec');
Expand Down Expand Up @@ -61,6 +62,10 @@ safesmsExport.listSMS = function(filter, successCallback, failureCallback) {
cordova.exec( successCallback, failureCallback, 'SMS', 'listSMS', [ filter ] );
};

safesmsExport.count = function(filter, successCallback, failureCallback) {
cordova.exec( successCallback, failureCallback, 'SMS', 'count', [ filter ] );
};

safesmsExport.deleteSMS = function(filter, successCallback, failureCallback) {
cordova.exec( successCallback, failureCallback, 'SMS', 'deleteSMS', [ filter ] );
};
Expand All @@ -84,3 +89,5 @@ safesmsExport.restoreSMS = function(msg, successCallback, failureCallback) {

module.exports = safesmsExport;


});