mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
Added sms inbox_sent plugin
This commit is contained in:
127
Android/SMS inbox_sent access/SMSReader.java
Normal file
127
Android/SMS inbox_sent access/SMSReader.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package com.karq.gbackup;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.util.Log;
|
||||
import com.phonegap.api.Plugin;
|
||||
import com.phonegap.api.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* Author: kaarel Kargu(karq)
|
||||
* Date: 8/24/11
|
||||
* PhoneGAP sms inbox/sent reading plugin
|
||||
*/
|
||||
|
||||
public class SMSReader extends Plugin {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public PluginResult execute(String action, JSONArray data, String callbackId) {
|
||||
Log.d("SMSReadPlugin", "Plugin Called");
|
||||
PluginResult result = null;
|
||||
JSONObject messages = new JSONObject();
|
||||
if (action.equals("inbox")) {
|
||||
try {
|
||||
messages = readSMS("inbox");
|
||||
Log.d("SMSReadPlugin", "Returning " + messages.toString());
|
||||
result = new PluginResult(PluginResult.Status.OK, messages);
|
||||
} catch (JSONException jsonEx) {
|
||||
Log.d("SMSReadPlugin", "Got JSON Exception "+ jsonEx.getMessage());
|
||||
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||
}
|
||||
}
|
||||
else if(action.equals("sent")){
|
||||
try {
|
||||
messages = readSMS("sent");
|
||||
Log.d("SMSReadPlugin", "Returning " + messages.toString());
|
||||
result = new PluginResult(PluginResult.Status.OK, messages);
|
||||
} catch (JSONException jsonEx) {
|
||||
Log.d("SMSReadPlugin", "Got JSON Exception "+ jsonEx.getMessage());
|
||||
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
Log.d("SMSReadPlugin", "Invalid action : "+action+" passed");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
//Read messages from inbox/or sent box.
|
||||
private JSONObject readSMS(String folder) throws JSONException {
|
||||
JSONObject data = new JSONObject();
|
||||
Uri uriSMSURI = Uri.parse("");
|
||||
if(folder.equals("inbox")){
|
||||
uriSMSURI = Uri.parse("content://sms/inbox");
|
||||
}
|
||||
else if(folder.equals("sent")){
|
||||
uriSMSURI = Uri.parse("content://sms/sent");
|
||||
}
|
||||
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
|
||||
JSONArray smsList = new JSONArray();
|
||||
data.put("messages", smsList);
|
||||
while (cur.moveToNext()) {
|
||||
JSONObject sms = new JSONObject();
|
||||
sms.put("number",cur.getString(2));
|
||||
sms.put("text",cur.getString(11));
|
||||
|
||||
String name = getContact(cur.getString(2));
|
||||
if(!name.equals("")){
|
||||
sms.put("name",name);
|
||||
}
|
||||
smsList.put(sms);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private String getContact(String number){
|
||||
Cursor cur = this.ctx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
|
||||
String returnName= "";
|
||||
if(cur.getCount() > 0){
|
||||
while(cur.moveToNext()){
|
||||
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
|
||||
Log.d("Kontakt","ID:" + id );
|
||||
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||
Log.d("Kontakt","name:" + name);
|
||||
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
|
||||
Cursor pcur = this.ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
|
||||
ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",new String[]{number},null);
|
||||
int numindex = pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
|
||||
if(pcur.moveToFirst()){
|
||||
String ml = pcur.getString(numindex);
|
||||
if(ml.equals(number)){
|
||||
returnName = name;
|
||||
Log.d("number","number:" + ml);
|
||||
}
|
||||
else {
|
||||
Log.d("number","numbers dont match!");
|
||||
}
|
||||
}
|
||||
else {
|
||||
Log.d("number", "no result");
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
Log.d("number", "No Number");
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnName;
|
||||
}
|
||||
|
||||
private ContentResolver getContentResolver(){
|
||||
return this.ctx.getContentResolver();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
75
Android/SMS inbox_sent access/readme.txt
Normal file
75
Android/SMS inbox_sent access/readme.txt
Normal file
@@ -0,0 +1,75 @@
|
||||
-Author: Kaarel Kargu(karq)
|
||||
-Phonegap Android SMS inbox/sent retrieve plugin
|
||||
-With this plugin you can retrieve messages from androids inbox and sent folder.
|
||||
|
||||
RETRIEVES:
|
||||
-Message body
|
||||
-Number
|
||||
-contact name if exists.
|
||||
|
||||
|
||||
INSTALLATION
|
||||
1)Move smsread.js to your assets/www folder.
|
||||
2)Move SMSReader.java to some package in src
|
||||
3)Open res/xml/plugins.xml and add this line
|
||||
- <plugin name="SMSReader" value="com.example.app.SMSReader" /> <--Change the package name to where SMSReader is located.
|
||||
4)Open smsread.js and changes this line
|
||||
- PluginManager.addService("SMSReader", "com.example.app.SMSReader"); <--- Change the package name again to the location of SMSReader
|
||||
5)Open androidManifest file and add these lines
|
||||
- <uses-permission android:name="android.permission.READ_SMS" />
|
||||
- <uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||
6)Open your HTML file and add
|
||||
- <script type="text/javascript" charset="utf-8" src="smsread.js"></script>
|
||||
|
||||
|
||||
HOW TO USE THE PLUGIN:
|
||||
1)Access inbox
|
||||
window.plugins.SMSReader.getInbox("",function(data){
|
||||
//Some code here to handle data
|
||||
|
||||
},function(){
|
||||
//Handle failure here
|
||||
});
|
||||
|
||||
2)Access sent
|
||||
window.plugins.SMSReader.getSent("",function(data){
|
||||
//Handle data here
|
||||
},function(){
|
||||
//Handle failure here
|
||||
});
|
||||
|
||||
|
||||
|
||||
AND HERE'S AN EXAMPLE OF HOW TO READ/DISPLAY MESSAGES FROM INBOX
|
||||
html file:
|
||||
<body>
|
||||
<h1>INBOX</h1><br />
|
||||
<div id="inbox">inbox</div><br />
|
||||
</body>
|
||||
|
||||
js script:
|
||||
document.addEventListener('deviceready',main, false);
|
||||
function main(){
|
||||
window.plugins.SMSReader.getInbox("",function(data){
|
||||
var text = getData(data);
|
||||
$("#inbox").html(text);
|
||||
},function(){
|
||||
alert("Something went wrong!");
|
||||
});
|
||||
}
|
||||
|
||||
function getData(data){
|
||||
var txt = "";
|
||||
for(var i = 0; i < data.messages.length; i++){
|
||||
txt += "<b>Number:</b>" + data.messages[i].number + " ";
|
||||
if(data.messages[i].name != ""){
|
||||
txt += "<b>Name:</b>" + data.messages[i].name + " ";
|
||||
}
|
||||
txt += "<b> Message:</b>" + data.messages[i].text + "!<br />";
|
||||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
29
Android/SMS inbox_sent access/smsread.js
Normal file
29
Android/SMS inbox_sent access/smsread.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* Author: kaarel Kargu(karq)
|
||||
* Date: 8/24/11
|
||||
* PhoneGAP sms inbox/sent reading plugin(js part)
|
||||
*/
|
||||
|
||||
var SMSReader = function () {};
|
||||
|
||||
SMSReader.prototype.getInbox = function(params, success, fail) {
|
||||
return PhoneGap.exec(function(args) {
|
||||
success(args);
|
||||
}, function(args) {
|
||||
fail(args);
|
||||
}, 'SMSReader', 'inbox', [params]);
|
||||
};
|
||||
|
||||
SMSReader.prototype.getSent = function(params, success, fail) {
|
||||
return PhoneGap.exec(function(args) {
|
||||
success(args);
|
||||
}, function(args) {
|
||||
fail(args);
|
||||
}, 'SMSReader', 'sent', [params]);
|
||||
};
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
PhoneGap.addPlugin("SMSReader", new SMSReader());
|
||||
PluginManager.addService("SMSReader", "com.karq.gbackup.SMSReader");
|
||||
});
|
||||
Reference in New Issue
Block a user