package smm.outsms;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import android.net.Uri;
import android.database.ContentObserver;
import android.os.Handler;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.content.ContentResolver;
@ShortName("SMSObserver")
@Events(values={"smssent(smsid as int)"})
@Permissions(values={"android.permission.READ_SMS"})
public class smmsms {
private BA ba;
private String eventName;
public void Initialize(BA ba,String EventName) {
_initialize(ba , EventName);
}
@Hide
public void _initialize(BA ba, String EventName) {
this.eventName = EventName.toLowerCase(BA.cul);
this.ba = ba;
}
public void ListenToOutgoingMessages() {
final Uri content = Uri.parse("content://sms");
ContentObserver co = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor cursor = BA.applicationContext.getContentResolver().query(
content,null, null, null, null);
if (cursor.moveToNext()) {
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
int type = cursor.getInt(cursor.getColumnIndex("type"));
String body =cursor.getString(cursor.getColumnIndex("body"));
if (protocol != null || type != 2) {
return;
}//
// ba.Log("OnChange"+body);
ba.raiseEvent(null, eventName + "_smssent",cursor.getInt(cursor.getColumnIndex(BaseColumns._ID)) );
cursor.close();
}
}
};
ContentResolver contentResolver = BA.applicationContext.getContentResolver();
contentResolver.registerContentObserver(content, true, co);
}
}