mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-28 18:26:22 +00:00
Full sync is now fixed
Full sync was previously not really good, Sms were ordered by date DESC, then the last date was good and when multiple loop iterations occurs, only 1 iteration happened SmsBuffer now owns LastMessageDate instead of AndroidSmsFetcher Full sync is now owned by AsyncsmsSync instead of MainActivity Last message date is more accurate and less error prone
This commit is contained in:
parent
bd2e70c7e7
commit
3edd898d7d
@ -31,6 +31,7 @@ JNINativeMethod SmsBuffer::methods[] =
|
|||||||
DECL_JNIMETHOD(deleteNativeObject, "()V")
|
DECL_JNIMETHOD(deleteNativeObject, "()V")
|
||||||
DECL_JNIMETHOD(empty, "()Z")
|
DECL_JNIMETHOD(empty, "()Z")
|
||||||
DECL_JNIMETHOD(asRawJsonString, "()Ljava/lang/String;")
|
DECL_JNIMETHOD(asRawJsonString, "()Ljava/lang/String;")
|
||||||
|
DECL_JNIMETHOD(getLastMessageDate, "()J")
|
||||||
DECL_JNIMETHOD(push,
|
DECL_JNIMETHOD(push,
|
||||||
"(IIIJLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")
|
"(IIIJLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")
|
||||||
DECL_JNIMETHOD(print, "()V")
|
DECL_JNIMETHOD(print, "()V")
|
||||||
@ -69,6 +70,7 @@ void SmsBuffer::reset_buffer()
|
|||||||
m_buffer.clear();
|
m_buffer.clear();
|
||||||
m_buffer_empty = true;
|
m_buffer_empty = true;
|
||||||
m_sms_count = 0;
|
m_sms_count = 0;
|
||||||
|
m_last_message_date = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SmsBuffer::push(JNIEnv *env, jobject self, jint msg_id, jint mailbox_id, jint type,
|
void SmsBuffer::push(JNIEnv *env, jobject self, jint msg_id, jint mailbox_id, jint type,
|
||||||
@ -94,15 +96,19 @@ void SmsBuffer::_push(int msg_id, int mailbox_id, int type,
|
|||||||
m_buffer_empty = false;
|
m_buffer_empty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_buffer << "{\"_id\": " << msg_id << ", "
|
m_buffer << "{\"_id\":" << msg_id << ","
|
||||||
<< "\"mbox\": " << mailbox_id << ", "
|
<< "\"mbox\":" << mailbox_id << ","
|
||||||
<< "\"type\": " << type << ", "
|
<< "\"type\":" << type << ","
|
||||||
<< "\"date\": " << date << ", "
|
<< "\"date\":" << date << ","
|
||||||
<< "\"body\": " << json::escape_string(body) << ", "
|
<< "\"body\":" << json::escape_string(body) << ","
|
||||||
<< "\"address\": " << json::escape_string(address) << ", "
|
<< "\"address\":" << json::escape_string(address) << ","
|
||||||
<< "\"read\": " << json::escape_string(read) << ", "
|
<< "\"read\":" << json::escape_string(read) << ","
|
||||||
<< "\"seen\": " << json::escape_string(seen)
|
<< "\"seen\":" << json::escape_string(seen)
|
||||||
<< "}";
|
<< "}";
|
||||||
|
|
||||||
|
if (date > m_last_message_date) {
|
||||||
|
m_last_message_date = date;
|
||||||
|
}
|
||||||
m_sms_count++;
|
m_sms_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,3 +149,14 @@ void SmsBuffer::as_raw_json_string(std::string &result)
|
|||||||
ss << "{\"smsCount\": " << m_sms_count << ", \"smsDatas\": " << m_buffer.str() << "]}";
|
ss << "{\"smsCount\": " << m_sms_count << ", \"smsDatas\": " << m_buffer.str() << "]}";
|
||||||
result = ss.str();
|
result = ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jlong SmsBuffer::getLastMessageDate(JNIEnv *env, jobject self)
|
||||||
|
{
|
||||||
|
SMSBUFFER_CAST
|
||||||
|
return me->_get_last_message_date();
|
||||||
|
}
|
||||||
|
|
||||||
|
long long SmsBuffer::_get_last_message_date() const
|
||||||
|
{
|
||||||
|
return m_last_message_date;
|
||||||
|
}
|
@ -59,6 +59,13 @@ public:
|
|||||||
JNIEXPORT static jstring JNICALL asRawJsonString(JNIEnv *env, jobject self);
|
JNIEXPORT static jstring JNICALL asRawJsonString(JNIEnv *env, jobject self);
|
||||||
void as_raw_json_string(std::string &result);
|
void as_raw_json_string(std::string &result);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getLastMessageDate method
|
||||||
|
*/
|
||||||
|
|
||||||
|
JNIEXPORT static jlong JNICALL getLastMessageDate(JNIEnv *env, jobject self);
|
||||||
|
long long _get_last_message_date() const;
|
||||||
|
|
||||||
DECL_JNICLASSATTRS
|
DECL_JNICLASSATTRS
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -66,6 +73,7 @@ private:
|
|||||||
std::stringstream m_buffer;
|
std::stringstream m_buffer;
|
||||||
uint32_t m_sms_count{0};
|
uint32_t m_sms_count{0};
|
||||||
bool m_buffer_empty{true};
|
bool m_buffer_empty{true};
|
||||||
|
long long m_last_message_date{0};
|
||||||
|
|
||||||
static bool gJava_inited;
|
static bool gJava_inited;
|
||||||
static jfieldID gJava_mHandle;
|
static jfieldID gJava_mHandle;
|
||||||
|
@ -47,13 +47,8 @@ import android.widget.Toast;
|
|||||||
import fr.unix_experience.owncloud_sms.R;
|
import fr.unix_experience.owncloud_sms.R;
|
||||||
import fr.unix_experience.owncloud_sms.activities.remote_account.AccountListActivity;
|
import fr.unix_experience.owncloud_sms.activities.remote_account.AccountListActivity;
|
||||||
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync.SyncTask;
|
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync.SyncTask;
|
||||||
import fr.unix_experience.owncloud_sms.engine.AndroidSmsFetcher;
|
|
||||||
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
|
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.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;
|
import fr.unix_experience.owncloud_sms.prefs.PermissionChecker;
|
||||||
|
|
||||||
import static fr.unix_experience.owncloud_sms.enums.PermissionID.REQUEST_MAX;
|
import static fr.unix_experience.owncloud_sms.enums.PermissionID.REQUEST_MAX;
|
||||||
@ -210,21 +205,7 @@ public class MainActivity extends AppCompatActivity
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now fetch messages since last stored date
|
new SyncTask(getApplicationContext()).execute();
|
||||||
SmsBuffer smsBuffer = new SmsBuffer();
|
|
||||||
new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsBuffer, (long) 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new OCSMSSharedPrefs(this).showSyncNotifications()) {
|
|
||||||
OCSMSNotificationUI.notify(ctx, ctx.getString(R.string.sync_title),
|
|
||||||
ctx.getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal());
|
|
||||||
}
|
|
||||||
new SyncTask(getApplicationContext(), smsBuffer).execute();
|
|
||||||
Log.v(MainActivity.TAG, "Finish syncAllMessages()");
|
Log.v(MainActivity.TAG, "Finish syncAllMessages()");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,24 +22,72 @@ import android.accounts.AccountManager;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import fr.unix_experience.owncloud_sms.R;
|
import fr.unix_experience.owncloud_sms.R;
|
||||||
import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType;
|
import fr.unix_experience.owncloud_sms.enums.OCSMSNotificationType;
|
||||||
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
|
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
|
||||||
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
|
import fr.unix_experience.owncloud_sms.jni.SmsBuffer;
|
||||||
import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
|
import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
|
||||||
|
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
|
||||||
|
|
||||||
public interface ASyncSMSSync {
|
public interface ASyncSMSSync {
|
||||||
class SyncTask extends AsyncTask<Void, Void, Void> {
|
class SyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
public SyncTask(Context context, SmsBuffer smsBuffer) {
|
public SyncTask(Context context) {
|
||||||
_context = context;
|
_context = context;
|
||||||
_smsBuffer = smsBuffer;
|
_smsBuffer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SyncTask(Context context, SmsBuffer buffer) {
|
||||||
|
_context = context;
|
||||||
|
_smsBuffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
Log.i(ASyncSMSSync.TAG, "Starting background sync");
|
Log.i(ASyncSMSSync.TAG, "Starting background sync");
|
||||||
|
|
||||||
|
// If no smsBuffer given it's a full sync
|
||||||
|
if (_smsBuffer == null) {
|
||||||
|
doFullSync();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
performSync(_smsBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(ASyncSMSSync.TAG, "Stopping background sync");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doFullSync() {
|
||||||
|
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
|
||||||
|
long syncStartupDate = prefs.getLastMessageDate();
|
||||||
|
|
||||||
|
Log.i(ASyncSMSSync.TAG, "Current message date is " + syncStartupDate);
|
||||||
|
boolean shouldSync = true;
|
||||||
|
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
|
||||||
|
while (shouldSync) {
|
||||||
|
SmsBuffer smsBuffer = new SmsBuffer();
|
||||||
|
fetcher.bufferMessagesSinceDate(smsBuffer, syncStartupDate);
|
||||||
|
if (smsBuffer.empty()) {
|
||||||
|
Toast.makeText(_context, _context.getString(R.string.nothing_to_sync), Toast.LENGTH_SHORT).show();
|
||||||
|
Log.i(ASyncSMSSync.TAG, "Finish syncAllMessages(): no more sms");
|
||||||
|
smsBuffer.clear();
|
||||||
|
shouldSync = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prefs.showSyncNotifications()) {
|
||||||
|
OCSMSNotificationUI.notify(_context, _context.getString(R.string.sync_title),
|
||||||
|
_context.getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal());
|
||||||
|
}
|
||||||
|
|
||||||
|
syncStartupDate = smsBuffer.getLastMessageDate();
|
||||||
|
performSync(smsBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void performSync(SmsBuffer smsBuffer) {
|
||||||
// Get ownCloud SMS account list
|
// Get ownCloud SMS account list
|
||||||
AccountManager _accountMgr = AccountManager.get(_context);
|
AccountManager _accountMgr = AccountManager.get(_context);
|
||||||
Account[] myAccountList = _accountMgr.getAccountsByType(_context.getString(R.string.account_type));
|
Account[] myAccountList = _accountMgr.getAccountsByType(_context.getString(R.string.account_type));
|
||||||
@ -48,7 +96,7 @@ public interface ASyncSMSSync {
|
|||||||
for (Account element : myAccountList) {
|
for (Account element : myAccountList) {
|
||||||
try {
|
try {
|
||||||
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context, element);
|
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context, element);
|
||||||
_client.doPushRequest(_smsBuffer);
|
_client.doPushRequest(smsBuffer);
|
||||||
OCSMSNotificationUI.cancel(_context);
|
OCSMSNotificationUI.cancel(_context);
|
||||||
} catch (IllegalStateException e) { // Fail to read account data
|
} catch (IllegalStateException e) { // Fail to read account data
|
||||||
OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error),
|
OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error),
|
||||||
@ -60,13 +108,11 @@ public interface ASyncSMSSync {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
OCSMSNotificationUI.cancel(_context);
|
OCSMSNotificationUI.cancel(_context);
|
||||||
_smsBuffer.clear();
|
smsBuffer.clear();
|
||||||
Log.i(ASyncSMSSync.TAG, "Stopping background sync");
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Context _context;
|
|
||||||
private final SmsBuffer _smsBuffer;
|
private final SmsBuffer _smsBuffer;
|
||||||
|
private final Context _context;
|
||||||
}
|
}
|
||||||
|
|
||||||
String TAG = ASyncSMSSync.class.getSimpleName();
|
String TAG = ASyncSMSSync.class.getSimpleName();
|
||||||
|
@ -30,7 +30,6 @@ import fr.unix_experience.owncloud_sms.providers.SmsDataProvider;
|
|||||||
|
|
||||||
public class AndroidSmsFetcher {
|
public class AndroidSmsFetcher {
|
||||||
public AndroidSmsFetcher(Context ct) {
|
public AndroidSmsFetcher(Context ct) {
|
||||||
_lastMsgDate = (long) 0;
|
|
||||||
_context = ct;
|
_context = ct;
|
||||||
|
|
||||||
_existingInboxMessages = null;
|
_existingInboxMessages = null;
|
||||||
@ -174,11 +173,6 @@ public class AndroidSmsFetcher {
|
|||||||
entry.seen = (c.getInt(idx) > 0);
|
entry.seen = (c.getInt(idx) > 0);
|
||||||
break;
|
break;
|
||||||
case "date":
|
case "date":
|
||||||
// Special case for date, we need to record last without searching
|
|
||||||
Long tmpDate = c.getLong(idx);
|
|
||||||
if (tmpDate > _lastMsgDate) {
|
|
||||||
_lastMsgDate = tmpDate;
|
|
||||||
}
|
|
||||||
entry.date = c.getLong(idx);
|
entry.date = c.getLong(idx);
|
||||||
break;
|
break;
|
||||||
case "address":
|
case "address":
|
||||||
@ -237,16 +231,10 @@ public class AndroidSmsFetcher {
|
|||||||
_existingDraftsMessages = draftMessages;
|
_existingDraftsMessages = draftMessages;
|
||||||
}
|
}
|
||||||
|
|
||||||
Long getLastMessageDate() {
|
|
||||||
return _lastMsgDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Context _context;
|
private final Context _context;
|
||||||
private JSONArray _existingInboxMessages;
|
private JSONArray _existingInboxMessages;
|
||||||
private JSONArray _existingSentMessages;
|
private JSONArray _existingSentMessages;
|
||||||
private JSONArray _existingDraftsMessages;
|
private JSONArray _existingDraftsMessages;
|
||||||
|
|
||||||
private Long _lastMsgDate;
|
|
||||||
|
|
||||||
private static final String TAG = AndroidSmsFetcher.class.getSimpleName();
|
private static final String TAG = AndroidSmsFetcher.class.getSimpleName();
|
||||||
}
|
}
|
||||||
|
@ -148,9 +148,6 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void doPushRequestV1(SmsBuffer smsBuffer) 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 (smsBuffer == null) {
|
if (smsBuffer == null) {
|
||||||
doHttpRequest(_http.getAllSmsIds());
|
doHttpRequest(_http.getAllSmsIds());
|
||||||
if (_jsonQueryBuffer == null) {
|
if (_jsonQueryBuffer == null) {
|
||||||
@ -159,8 +156,6 @@ public class OCSMSOwnCloudClient {
|
|||||||
|
|
||||||
// Create new JSONArray to get results
|
// Create new JSONArray to get results
|
||||||
smsBuffer = new SmsBuffer();
|
smsBuffer = new SmsBuffer();
|
||||||
// Get maximum message date present in smsList to keep a step when connectivity changes
|
|
||||||
lastMsgDate = collectMessages(smsBuffer).getLastMessageDate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (smsBuffer.empty()) {
|
if (smsBuffer.empty()) {
|
||||||
@ -193,9 +188,10 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Push was OK, we can save the lastMessageDate which was saved to server
|
// Push was OK, we can save the lastMessageDate which was saved to server
|
||||||
(new OCSMSSharedPrefs(_context)).setLastMessageDate(lastMsgDate);
|
(new OCSMSSharedPrefs(_context)).setLastMessageDate(smsBuffer.getLastMessageDate());
|
||||||
|
|
||||||
Log.i(OCSMSOwnCloudClient.TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage);
|
Log.i(OCSMSOwnCloudClient.TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage);
|
||||||
|
Log.i(OCSMSOwnCloudClient.TAG, "LastMessageDate set to: " + smsBuffer.getLastMessageDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
private PostMethod createPushRequest(SmsBuffer smsBuffer) throws OCSyncException {
|
private PostMethod createPushRequest(SmsBuffer smsBuffer) throws OCSyncException {
|
||||||
|
@ -45,6 +45,7 @@ public class SmsBuffer {
|
|||||||
public native boolean empty();
|
public native boolean empty();
|
||||||
public native void print();
|
public native void print();
|
||||||
public native String asRawJsonString();
|
public native String asRawJsonString();
|
||||||
|
public native long getLastMessageDate();
|
||||||
|
|
||||||
public void push(MailboxID mbid, SmsEntry smsEntry) {
|
public void push(MailboxID mbid, SmsEntry smsEntry) {
|
||||||
push(smsEntry.id,
|
push(smsEntry.id,
|
||||||
@ -65,6 +66,4 @@ public class SmsBuffer {
|
|||||||
deleteNativeObject();
|
deleteNativeObject();
|
||||||
mHandle = 0;
|
mHandle = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -59,13 +59,13 @@ public class SmsObserver extends ContentObserver implements ASyncSMSSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
|
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
|
||||||
SmsBuffer smsList = fetcher.getLastMessage(MailboxID.ALL);
|
SmsBuffer smsBuffer = fetcher.getLastMessage(MailboxID.ALL);
|
||||||
|
|
||||||
ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
|
ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
|
||||||
|
|
||||||
// Synchronize if network is valid and there are SMS
|
// Synchronize if network is valid and there are SMS
|
||||||
if (cMon.isValid() && (smsList != null)) {
|
if (cMon.isValid() && (smsBuffer != null)) {
|
||||||
new SyncTask(_context, smsList).execute();
|
new SyncTask(_context, smsBuffer).execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ public class SmsDataProvider extends ContentProvider {
|
|||||||
|
|
||||||
public Cursor queryMessagesSinceDate(String mailBox, Long sinceDate) {
|
public Cursor queryMessagesSinceDate(String mailBox, Long sinceDate) {
|
||||||
return query(Uri.parse(mailBox), SmsDataProvider.messageFields,
|
return query(Uri.parse(mailBox), SmsDataProvider.messageFields,
|
||||||
"date > ?", new String[] { sinceDate.toString() }, null
|
"date > ?", new String[] { sinceDate.toString() }, "date ASC"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user