1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2026-08-20 12:52:59 +00:00

SmsBuffer is now working, replace json objects with the SmsBuffer on sync

This commit is contained in:
Loic Blot
2017-08-23 00:28:34 +02:00
committed by Loïc Blot
parent 47c2923d0e
commit 5e6a1fc28e
13 changed files with 313 additions and 142 deletions
@@ -44,8 +44,6 @@ import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
import org.json.JSONArray;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.activities.remote_account.AccountListActivity;
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync.SyncTask;
@@ -53,6 +51,7 @@ import fr.unix_experience.owncloud_sms.engine.AndroidSmsFetcher;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType;
import fr.unix_experience.owncloud_sms.enums.PermissionID;
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
import fr.unix_experience.owncloud_sms.prefs.PermissionChecker;
@@ -212,10 +211,10 @@ public class MainActivity extends AppCompatActivity
}
// Now fetch messages since last stored date
JSONArray smsList = new JSONArray();
new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);
SmsBuffer smsBuffer = new SmsBuffer();
new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsBuffer, (long) 0);
if (smsList.length() == 0) {
if (smsBuffer.empty()) {
Toast.makeText(ctx, ctx.getString(R.string.nothing_to_sync), Toast.LENGTH_SHORT).show();
Log.v(MainActivity.TAG, "Finish syncAllMessages(): no sms");
return;
@@ -225,7 +224,7 @@ public class MainActivity extends AppCompatActivity
OCSMSNotificationUI.notify(ctx, ctx.getString(R.string.sync_title),
ctx.getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal());
}
new SyncTask(getApplicationContext(), smsList).execute();
new SyncTask(getApplicationContext(), smsBuffer).execute();
Log.v(MainActivity.TAG, "Finish syncAllMessages()");
}
@@ -25,8 +25,6 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.json.JSONArray;
import java.util.concurrent.atomic.AtomicReference;
import fr.unix_experience.owncloud_sms.R;
@@ -34,6 +32,7 @@ import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync;
import fr.unix_experience.owncloud_sms.engine.AndroidSmsFetcher;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
import fr.unix_experience.owncloud_sms.enums.PermissionID;
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
import fr.unix_experience.owncloud_sms.prefs.PermissionChecker;
@@ -82,14 +81,14 @@ public class ConnectivityChanged extends BroadcastReceiver implements ASyncSMSSy
Log.i(ConnectivityChanged.TAG,"Synced Last:" + lastMessageSynced);
// Now fetch messages since last stored date
JSONArray smsList = new JSONArray();
new AndroidSmsFetcher(context).bufferMessagesSinceDate(smsList, lastMessageSynced);
SmsBuffer smsBuffer = new SmsBuffer();
new AndroidSmsFetcher(context).bufferMessagesSinceDate(smsBuffer, lastMessageSynced);
AtomicReference<ConnectivityMonitor> cMon = new AtomicReference<>(new ConnectivityMonitor(context));
// Synchronize if network is valid and there are SMS
if (cMon.get().isValid() && (smsList.length() > 0)) {
new SyncTask(context, smsList).execute();
if (cMon.get().isValid() && !smsBuffer.empty()) {
new SyncTask(context, smsBuffer).execute();
}
}
@@ -23,18 +23,17 @@ import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType;
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
public interface ASyncSMSSync {
class SyncTask extends AsyncTask<Void, Void, Void> {
public SyncTask(Context context, JSONArray smsList) {
public SyncTask(Context context, SmsBuffer smsBuffer) {
_context = context;
_smsList = smsList;
_smsBuffer = smsBuffer;
}
@Override
@@ -49,7 +48,7 @@ public interface ASyncSMSSync {
for (Account element : myAccountList) {
try {
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context, element);
_client.doPushRequest(_smsList);
_client.doPushRequest(_smsBuffer);
OCSMSNotificationUI.cancel(_context);
} catch (IllegalStateException e) { // Fail to read account data
OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error),
@@ -66,7 +65,7 @@ public interface ASyncSMSSync {
}
private final Context _context;
private final JSONArray _smsList;
private final SmsBuffer _smsBuffer;
}
String TAG = ASyncSMSSync.class.getSimpleName();
@@ -39,13 +39,13 @@ public class AndroidSmsFetcher {
_existingDraftsMessages = null;
}
void fetchAllMessages(JSONArray result) {
void fetchAllMessages(SmsBuffer result) {
bufferMailboxMessages(result, MailboxID.INBOX);
bufferMailboxMessages(result, MailboxID.SENT);
bufferMailboxMessages(result, MailboxID.DRAFTS);
}
private void readMailBox(Cursor c, JSONArray result, MailboxID mbID) {
private void readMailBox(Cursor c, SmsBuffer smsBuffer, MailboxID mbID) {
do {
JSONObject entry = new JSONObject();
@@ -56,10 +56,15 @@ public class AndroidSmsFetcher {
// Mailbox ID is required by server
entry.put("mbox", mbID.ordinal());
result.put(entry);
SmsBuffer buf = new SmsBuffer();
buf.push(mbID.ordinal());
buf.print();
smsBuffer.push(entry.getInt("_id"),
mbID.ordinal(),
entry.getInt("type"),
entry.getLong("date"),
entry.getString("address"),
entry.getString("body"),
entry.getString("read"),
entry.getString("seen"));
} catch (JSONException e) {
Log.e(AndroidSmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e);
@@ -68,7 +73,7 @@ public class AndroidSmsFetcher {
while (c.moveToNext());
}
private void bufferMailboxMessages(JSONArray result, MailboxID mbID) {
private void bufferMailboxMessages(SmsBuffer smsBuffer, MailboxID mbID) {
if ((_context == null)) {
return;
}
@@ -88,14 +93,14 @@ public class AndroidSmsFetcher {
}
// Reading mailbox
readMailBox(c, result, mbID);
readMailBox(c, smsBuffer, mbID);
Log.i(AndroidSmsFetcher.TAG, c.getCount() + " messages read from " + mbID.getURI());
c.close();
}
// Used by Content Observer
public JSONArray getLastMessage(MailboxID mbID) {
public SmsBuffer getLastMessage(MailboxID mbID) {
if ((_context == null)) {
return null;
}
@@ -107,8 +112,8 @@ public class AndroidSmsFetcher {
}
// We create a list of strings to store results
JSONArray results = new JSONArray();
JSONObject entry = new JSONObject();
SmsBuffer results = new SmsBuffer();
try {
Integer mboxId = -1;
@@ -126,7 +131,14 @@ public class AndroidSmsFetcher {
*/
entry.put("mbox", (mboxId - 1));
results.put(entry);
results.push(entry.getInt("_id"),
mbID.ordinal(),
entry.getInt("type"),
entry.getLong("date"),
entry.getString("address"),
entry.getString("body"),
entry.getString("read"),
entry.getString("seen"));
} catch (JSONException e) {
Log.e(AndroidSmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e);
}
@@ -137,14 +149,14 @@ public class AndroidSmsFetcher {
}
// Used by ConnectivityChanged Event
public void bufferMessagesSinceDate(JSONArray result, Long sinceDate) {
bufferMessagesSinceDate(result, MailboxID.INBOX, sinceDate);
bufferMessagesSinceDate(result, MailboxID.SENT, sinceDate);
bufferMessagesSinceDate(result, MailboxID.DRAFTS, sinceDate);
public void bufferMessagesSinceDate(SmsBuffer smsBuffer, Long sinceDate) {
bufferMessagesSinceDate(smsBuffer, MailboxID.INBOX, sinceDate);
bufferMessagesSinceDate(smsBuffer, MailboxID.SENT, sinceDate);
bufferMessagesSinceDate(smsBuffer, MailboxID.DRAFTS, sinceDate);
}
// Used by ConnectivityChanged Event
private void bufferMessagesSinceDate(JSONArray result, MailboxID mbID, Long sinceDate) {
private void bufferMessagesSinceDate(SmsBuffer smsBuffer, MailboxID mbID, Long sinceDate) {
Log.i(AndroidSmsFetcher.TAG, "bufferMessagesSinceDate for " + mbID.toString() + " sinceDate " + sinceDate.toString());
if ((_context == null)) {
return;
@@ -159,7 +171,7 @@ public class AndroidSmsFetcher {
}
// Read Mailbox
readMailBox(c, result, mbID);
readMailBox(c, smsBuffer, mbID);
Log.i(AndroidSmsFetcher.TAG, c.getCount() + " messages read from " + mbID.getURI());
c.close();
@@ -191,7 +203,7 @@ public class AndroidSmsFetcher {
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
entry.put(colName, c.getString(idx));
entry.put(colName, c.getLong(idx));
break;
default:
entry.put(colName, c.getString(idx));
@@ -38,6 +38,7 @@ import java.net.ConnectException;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.enums.OCSyncErrorType;
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
@SuppressWarnings("deprecation")
@@ -94,17 +95,17 @@ public class OCSMSOwnCloudClient {
}
}
public void doPushRequest(JSONArray smsList) throws OCSyncException {
public void doPushRequest(SmsBuffer smsBuffer) throws OCSyncException {
/*
* If we need other API push, set it here
*/
switch (_serverAPIVersion) {
case 1:
default: doPushRequestV1(smsList); break;
default: doPushRequestV1(smsBuffer); break;
}
}
private AndroidSmsFetcher collectMessages(JSONArray smsList) throws OCSyncException {
private AndroidSmsFetcher collectMessages(SmsBuffer smsBuffer) throws OCSyncException {
JSONObject smsBoxes = new JSONObject();
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
try {
@@ -141,33 +142,33 @@ public class OCSMSOwnCloudClient {
fetcher.setExistingSentMessages(sentSmsList);
fetcher.setExistingDraftsMessages(draftsSmsList);
fetcher.fetchAllMessages(smsList);
fetcher.fetchAllMessages(smsBuffer);
return fetcher;
}
private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
private void doPushRequestV1(SmsBuffer smsBuffer) throws OCSyncException {
// We need to save this date as a step for connectivity change
Long lastMsgDate = (long) 0;
if (smsList == null) {
if (smsBuffer == null) {
doHttpRequest(_http.getAllSmsIds());
if (_jsonQueryBuffer == null) {
return;
}
// Create new JSONArray to get results
smsList = new JSONArray();
smsBuffer = new SmsBuffer();
// Get maximum message date present in smsList to keep a step when connectivity changes
lastMsgDate = collectMessages(smsList).getLastMessageDate();
lastMsgDate = collectMessages(smsBuffer).getLastMessageDate();
}
if (smsList.length() == 0) {
if (smsBuffer.empty()) {
Log.i(OCSMSOwnCloudClient.TAG, "No new SMS to sync, sync done");
return;
}
PostMethod post = createPushRequest(smsList);
PostMethod post = createPushRequest(smsBuffer);
if (post == null) {
Log.e(OCSMSOwnCloudClient.TAG,"Push request for POST is null");
throw new OCSyncException(R.string.err_sync_craft_http_request, OCSyncErrorType.IO);
@@ -196,44 +197,15 @@ public class OCSMSOwnCloudClient {
Log.i(OCSMSOwnCloudClient.TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage);
}
private PostMethod createPushRequest(JSONArray smsList) throws OCSyncException {
JSONObject obj = createPushJSONObject(smsList);
if (obj == null) {
return null;
}
StringRequestEntity ent = createJSONRequestEntity(obj);
if (ent == null) {
return null;
}
return _http.pushSms(ent);
private PostMethod createPushRequest(SmsBuffer smsBuffer) throws OCSyncException {
return _http.pushSms(createJSONRequestEntity(smsBuffer));
}
private JSONObject createPushJSONObject(JSONArray smsList) throws OCSyncException {
if (smsList == null) {
Log.e(OCSMSOwnCloudClient.TAG,"NULL SMS List");
throw new OCSyncException(R.string.err_sync_create_json_null_smslist, OCSyncErrorType.IO);
}
JSONObject reqJSON = new JSONObject();
try {
reqJSON.put("smsDatas", smsList);
reqJSON.put("smsCount", smsList.length());
} catch (JSONException e) {
Log.e(OCSMSOwnCloudClient.TAG,"JSON Exception when creating JSON request object");
throw new OCSyncException(R.string.err_sync_create_json_put_smslist, OCSyncErrorType.PARSE);
}
return reqJSON;
}
private StringRequestEntity createJSONRequestEntity(JSONObject obj) throws OCSyncException {
private StringRequestEntity createJSONRequestEntity(SmsBuffer smsBuffer) throws OCSyncException {
StringRequestEntity requestEntity;
try {
requestEntity = new StringRequestEntity(
obj.toString(),
smsBuffer.asRawJsonString(),
"application/json",
"UTF-8");
@@ -37,13 +37,39 @@ public class SmsBuffer {
private static native long createNativeObject();
private static native void deleteNativeObject(long handle);
public static native void push(long handle, int mbid);
public void push(int mbid) {
SmsBuffer.push(mHandle, mbid);
/*
JNI: push method
*/
public static native void push(long handle, int id, int mbid, int type, long date,
String address, String body, String read, String seen);
public void push(int id, int mbid, int type, long date, String address, String body,
String read, String seen) {
SmsBuffer.push(mHandle, id, mbid, type, date, address, body, read, seen);
}
/*
JNI: Eepty method
*/
public static native boolean empty(long handle);
public boolean empty() {
return SmsBuffer.empty(mHandle);
}
/*
JNI: print method
*/
public static native void print(long handle);
public void print() {
SmsBuffer.print(mHandle);
}
/*
JNI: asRawJsonString method
*/
public static native String asRawJsonString(long handle);
public String asRawJsonString() {
return SmsBuffer.asRawJsonString(mHandle);
}
}
@@ -25,8 +25,6 @@ import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;
import org.json.JSONArray;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync;
import fr.unix_experience.owncloud_sms.engine.AndroidSmsFetcher;
@@ -34,6 +32,7 @@ import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
import fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient;
import fr.unix_experience.owncloud_sms.enums.MailboxID;
import fr.unix_experience.owncloud_sms.enums.PermissionID;
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
import fr.unix_experience.owncloud_sms.prefs.PermissionChecker;
public class SmsObserver extends ContentObserver implements ASyncSMSSync {
@@ -60,7 +59,7 @@ public class SmsObserver extends ContentObserver implements ASyncSMSSync {
}
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
JSONArray smsList = fetcher.getLastMessage(MailboxID.ALL);
SmsBuffer smsList = fetcher.getLastMessage(MailboxID.ALL);
ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
@@ -29,15 +29,15 @@ import fr.unix_experience.owncloud_sms.enums.MailboxID;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class SmsDataProvider extends ContentProvider {
static String messageFields[] = {
"read",
"date",
"address",
"seen",
"body",
"_id",
"type",
//"length(address)" // For debug purposes
static String[] messageFields = {
"read",
"date",
"address",
"seen",
"body",
"_id",
"type",
//"length(address)" // For debug purposes
};
// WARNING: mandatory
@@ -54,7 +54,7 @@ public class SmsDataProvider extends ContentProvider {
public Cursor query(String mailBox) {
return query(Uri.parse(mailBox),
new String[] { "read", "date", "address", "seen", "body", "_id", "type", },
SmsDataProvider.messageFields,
null, null, null
);
}