mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-07 16:06:18 +00:00
Add a loop which retrieve all messages without doing anything
This commit is contained in:
parent
80a91635dd
commit
f05eabe94b
@ -61,11 +61,11 @@ import static fr.unix_experience.owncloud_sms.enums.PermissionID.REQUEST_SMS;
|
|||||||
public class MainActivity extends AppCompatActivity
|
public class MainActivity extends AppCompatActivity
|
||||||
implements NavigationView.OnNavigationItemSelectedListener{
|
implements NavigationView.OnNavigationItemSelectedListener{
|
||||||
|
|
||||||
private static ConnectivityMonitor mConnectivityMonitor = null;
|
private ConnectivityMonitor _ConnectivityMonitor = null;
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
if (MainActivity.mConnectivityMonitor == null) {
|
if (_ConnectivityMonitor == null) {
|
||||||
MainActivity.mConnectivityMonitor = new ConnectivityMonitor(getApplicationContext());
|
_ConnectivityMonitor = new ConnectivityMonitor(getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
@ -134,7 +134,7 @@ public class MainActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
Context ctx = getApplicationContext();
|
Context ctx = getApplicationContext();
|
||||||
if (MainActivity.mConnectivityMonitor.isValid()) {
|
if (_ConnectivityMonitor.isValid()) {
|
||||||
// Now fetch messages since last stored date
|
// Now fetch messages since last stored date
|
||||||
JSONArray smsList = new JSONArray();
|
JSONArray smsList = new JSONArray();
|
||||||
new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);
|
new AndroidSmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);
|
||||||
|
@ -1,5 +1,22 @@
|
|||||||
package fr.unix_experience.owncloud_sms.activities.remote_account;
|
package fr.unix_experience.owncloud_sms.activities.remote_account;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014-2016, Loic Blot <loic.blot@unix-experience.fr>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
@ -9,6 +26,7 @@ import android.support.v4.app.ActivityCompat;
|
|||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
|
||||||
import fr.unix_experience.owncloud_sms.R;
|
import fr.unix_experience.owncloud_sms.R;
|
||||||
|
import fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery;
|
||||||
|
|
||||||
public class RestoreMessagesActivity extends AppCompatActivity {
|
public class RestoreMessagesActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@ -45,6 +63,8 @@ public class RestoreMessagesActivity extends AppCompatActivity {
|
|||||||
if (_account == null) {
|
if (_account == null) {
|
||||||
throw new IllegalStateException(getString(R.string.err_didnt_find_account_restore));
|
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();
|
private static final String TAG = RestoreMessagesActivity.class.getSimpleName();
|
||||||
|
@ -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 <loic.blot@unix-experience.fr>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface ASyncSMSRecovery {
|
||||||
|
class SMSRecoveryTask extends AsyncTask<Void, Void, Void> {
|
||||||
|
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();
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package fr.unix_experience.owncloud_sms.engine;
|
package fr.unix_experience.owncloud_sms.engine;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2014-2015, Loic Blot <loic.blot@unix-experience.fr>
|
* Copyright (c) 2014-2016, Loic Blot <loic.blot@unix-experience.fr>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
@ -82,7 +82,7 @@ class HTTPRequestBuilder {
|
|||||||
return get(HTTPRequestBuilder.OC_V2_GET_PHONELIST);
|
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.
|
return get(HTTPRequestBuilder.OC_V2_GET_MESSAGES.
|
||||||
replace("[START]", start.toString()).replace("[LIMIT]", limit.toString()));
|
replace("[START]", start.toString()).replace("[LIMIT]", limit.toString()));
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
|
|||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public class OCSMSOwnCloudClient {
|
public class OCSMSOwnCloudClient {
|
||||||
|
|
||||||
|
private static final Integer SERVER_RECOVERY_MSG_LIMIT = 500;
|
||||||
|
|
||||||
public OCSMSOwnCloudClient(Context context, Account account) {
|
public OCSMSOwnCloudClient(Context context, Account account) {
|
||||||
_context = context;
|
_context = context;
|
||||||
_serverAPIVersion = 1;
|
_serverAPIVersion = 1;
|
||||||
@ -239,6 +241,26 @@ public class OCSMSOwnCloudClient {
|
|||||||
return requestEntity;
|
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 {
|
private void doHttpRequest(HttpMethod req) throws OCSyncException {
|
||||||
doHttpRequest(req, false);
|
doHttpRequest(req, false);
|
||||||
}
|
}
|
||||||
@ -303,7 +325,6 @@ public class OCSMSOwnCloudClient {
|
|||||||
Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e);
|
Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e);
|
||||||
throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO);
|
throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO);
|
||||||
}
|
}
|
||||||
//Log.i(TAG, "Successful response: " + response);
|
|
||||||
|
|
||||||
// Parse the response
|
// Parse the response
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user