diff --git a/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java b/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java index 750c766..d06f6eb 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/activities/MainActivity.java @@ -61,11 +61,11 @@ import static fr.unix_experience.owncloud_sms.enums.PermissionID.REQUEST_SMS; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ - private static ConnectivityMonitor mConnectivityMonitor = null; + private ConnectivityMonitor _ConnectivityMonitor = null; @Override protected void onCreate(Bundle savedInstanceState) { - if (MainActivity.mConnectivityMonitor == null) { - MainActivity.mConnectivityMonitor = new ConnectivityMonitor(getApplicationContext()); + if (_ConnectivityMonitor == null) { + _ConnectivityMonitor = new ConnectivityMonitor(getApplicationContext()); } requestWindowFeature(Window.FEATURE_NO_TITLE); @@ -134,7 +134,7 @@ public class MainActivity extends AppCompatActivity } Context ctx = getApplicationContext(); - if (MainActivity.mConnectivityMonitor.isValid()) { + if (_ConnectivityMonitor.isValid()) { // Now fetch messages since last stored date JSONArray smsList = new JSONArray(); new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0); diff --git a/src/main/java/fr/unix_experience/owncloud_sms/activities/remote_account/RestoreMessagesActivity.java b/src/main/java/fr/unix_experience/owncloud_sms/activities/remote_account/RestoreMessagesActivity.java index 3a67225..8bb3009 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/activities/remote_account/RestoreMessagesActivity.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/activities/remote_account/RestoreMessagesActivity.java @@ -1,5 +1,22 @@ package fr.unix_experience.owncloud_sms.activities.remote_account; +/* + * Copyright (c) 2014-2016, Loic Blot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + import android.Manifest; import android.accounts.Account; import android.accounts.AccountManager; @@ -9,6 +26,7 @@ import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import fr.unix_experience.owncloud_sms.R; +import fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery; public class RestoreMessagesActivity extends AppCompatActivity { @@ -45,6 +63,8 @@ public class RestoreMessagesActivity extends AppCompatActivity { if (_account == null) { throw new IllegalStateException(getString(R.string.err_didnt_find_account_restore)); } + + new ASyncSMSRecovery.SMSRecoveryTask(getApplicationContext(), _account).execute(); } private static final String TAG = RestoreMessagesActivity.class.getSimpleName(); diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSRecovery.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSRecovery.java new file mode 100644 index 0000000..b195bc6 --- /dev/null +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSRecovery.java @@ -0,0 +1,57 @@ +package fr.unix_experience.owncloud_sms.engine; + +import android.accounts.Account; +import android.content.Context; +import android.os.AsyncTask; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +/* + * Copyright (c) 2014-2016, Loic Blot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +public interface ASyncSMSRecovery { + class SMSRecoveryTask extends AsyncTask { + private final Context _context; + private final Account _account; + + public SMSRecoveryTask(Context context, Account account) { + _context = context; + _account = account; + } + + @Override + protected Void doInBackground(Void... params) { + Log.i(ASyncSMSRecovery.TAG, "Starting background recovery"); + Long start = (long) 0; + JSONObject obj = new OCSMSOwnCloudClient(_context, _account).retrieveSomeMessages(start, 500); + try { + while (obj.getLong("last_id") != start) { + start = obj.getLong("last_id"); + obj = new OCSMSOwnCloudClient(_context, _account).retrieveSomeMessages(start, 500); + } + } catch (JSONException e) { + Log.e(ASyncSMSRecovery.TAG, "Missing last_id field!"); + } + Log.i(ASyncSMSRecovery.TAG, "Finishing background recovery"); + return null; + } + } + + String TAG = ASyncSMSRecovery.class.getSimpleName(); +} diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java index 04ca2c6..d2fb3d1 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/ASyncSMSSync.java @@ -1,7 +1,7 @@ package fr.unix_experience.owncloud_sms.engine; /* - * Copyright (c) 2014-2015, Loic Blot + * Copyright (c) 2014-2016, Loic Blot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/HTTPRequestBuilder.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/HTTPRequestBuilder.java index bf881e3..51ae112 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/HTTPRequestBuilder.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/HTTPRequestBuilder.java @@ -82,7 +82,7 @@ class HTTPRequestBuilder { return get(HTTPRequestBuilder.OC_V2_GET_PHONELIST); } - GetMethod getMessages(Integer start, Integer limit) { + GetMethod getMessages(Long start, Integer limit) { return get(HTTPRequestBuilder.OC_V2_GET_MESSAGES. replace("[START]", start.toString()).replace("[LIMIT]", limit.toString())); } diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java index e990db7..07d27d7 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java @@ -44,6 +44,8 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs; @SuppressWarnings("deprecation") public class OCSMSOwnCloudClient { + private static final Integer SERVER_RECOVERY_MSG_LIMIT = 500; + public OCSMSOwnCloudClient(Context context, Account account) { _context = context; _serverAPIVersion = 1; @@ -239,6 +241,26 @@ public class OCSMSOwnCloudClient { return requestEntity; } + JSONObject retrieveSomeMessages(Long start, Integer limit) { + // This is not allowed by server + if (limit > OCSMSOwnCloudClient.SERVER_RECOVERY_MSG_LIMIT) { + return null; + } + + try { + doHttpRequest(_http.getMessages(start, limit)); + } catch (OCSyncException e) { + _jsonQueryBuffer = null; + return null; + } + + if (!_jsonQueryBuffer.has("messages") || !_jsonQueryBuffer.has("last_id")) { + return null; + } + + return _jsonQueryBuffer; + } + private void doHttpRequest(HttpMethod req) throws OCSyncException { doHttpRequest(req, false); } @@ -303,7 +325,6 @@ public class OCSMSOwnCloudClient { Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e); throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO); } - //Log.i(TAG, "Successful response: " + response); // Parse the response try {