1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-07 16:06:18 +00:00

OCHttpClient::getPhoneList now used GoLang aar

* add handleEarlyHTTPStatus to properly handle http return codes in java part
* cleanup dead code
This commit is contained in:
Loic Blot 2018-02-10 10:44:53 +01:00
parent 1f26787983
commit db2cc05a3a
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
4 changed files with 32 additions and 34 deletions

Binary file not shown.

View File

@ -12,15 +12,13 @@ import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Collections;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.adapters.ContactListAdapter;
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import ncsmsgo.SmsPhoneListResponse;
public interface ASyncContactLoad {
class ContactLoadTask extends AsyncTask<Void, Void, Boolean> {
@ -68,10 +66,15 @@ public interface ASyncContactLoad {
ArrayList<String> serverPhoneList = new ArrayList<>();
JSONArray phoneNumbers = _client.getServerPhoneNumbers();
for (int i = 0; i < phoneNumbers.length(); i++) {
String phone = phoneNumbers.getString(i);
serverPhoneList.add(phone);
SmsPhoneListResponse splr = _client.getServerPhoneNumbers();
if (splr == null) {
_objects.add(_context.getString(R.string.err_fetch_phonelist));
return false;
}
String phoneNumber;
while (!(phoneNumber = splr.getNextEntry()).equals("")) {
serverPhoneList.add(phoneNumber);
}
// Read all contacts
@ -81,9 +84,6 @@ public interface ASyncContactLoad {
// Sort phone numbers
Collections.sort(_objects);
} catch (JSONException e) {
_objects.add(_context.getString(R.string.err_fetch_phonelist));
return false;
} catch (OCSyncException e) {
_objects.add(_context.getString(e.getErrorId()));
return false;

View File

@ -47,6 +47,7 @@ import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import fr.unix_experience.owncloud_sms.providers.AndroidVersionProvider;
import ncsmsgo.SmsBuffer;
import ncsmsgo.SmsHTTPClient;
import ncsmsgo.SmsPhoneListResponse;
import ncsmsgo.SmsPushResponse;
public class OCHttpClient {
@ -108,17 +109,13 @@ public class OCHttpClient {
return new Pair<>(0, null);
}
private Pair<Integer, JSONObject> post(String oc_call, String data) throws OCSyncException {
Log.i(OCHttpClient.TAG, "Perform POST " + _url + oc_call);
try {
return execute("POST",
new URL(_url.toString() + oc_call), data, false);
} catch (MalformedURLException e) {
Log.e(OCHttpClient.TAG, "Malformed URL provided, aborting. URL was: "
+ _url.toExternalForm() + oc_call);
private void handleEarlyHTTPStatus(int httpStatus) throws OCSyncException {
switch (httpStatus) {
case 403: {
// Authentication failed
throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH);
}
}
return new Pair<>(0, null);
}
Pair<Integer, JSONObject> getAllSmsIds() throws OCSyncException {
@ -130,6 +127,8 @@ public class OCHttpClient {
Integer serverAPIVersion = (int) _smsHttpClient.doVersionCall();
int httpStatus = (int) _smsHttpClient.getLastHTTPStatus();
handleEarlyHTTPStatus(httpStatus);
// If last status is not 200, send the wrong status now
if (httpStatus != 200) {
return new Pair<>(httpStatus, 0);
@ -153,11 +152,16 @@ public class OCHttpClient {
Pair<Integer, SmsPushResponse> pushSms(SmsBuffer smsBuf) throws OCSyncException {
SmsPushResponse spr = _smsHttpClient.doPushCall(smsBuf);
return new Pair<>((int) _smsHttpClient.getLastHTTPStatus(), spr);
int httpStatus = (int) _smsHttpClient.getLastHTTPStatus();
handleEarlyHTTPStatus(httpStatus);
return new Pair<>(httpStatus, spr);
}
Pair<Integer, JSONObject> getPhoneList() throws OCSyncException {
return get(_smsHttpClient.getPhoneListCall(), true);
Pair<Integer, SmsPhoneListResponse> getPhoneList() throws OCSyncException {
SmsPhoneListResponse splr = _smsHttpClient.doGetPhoneList();
int httpStatus = (int) _smsHttpClient.getLastHTTPStatus();
handleEarlyHTTPStatus(httpStatus);
return new Pair<>(httpStatus, splr);
}
Pair<Integer, JSONObject> getMessages(Long start, Integer limit) throws OCSyncException {

View File

@ -23,8 +23,6 @@ import android.content.Context;
import android.util.Log;
import android.util.Pair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.MalformedURLException;
@ -35,6 +33,7 @@ import fr.unix_experience.owncloud_sms.enums.OCSyncErrorType;
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
import ncsmsgo.SmsBuffer;
import ncsmsgo.SmsPhoneListResponse;
import ncsmsgo.SmsPushResponse;
@SuppressWarnings("deprecation")
@ -74,18 +73,13 @@ public class OCSMSOwnCloudClient {
return 0;
}
JSONArray getServerPhoneNumbers() throws OCSyncException {
Pair<Integer, JSONObject> response = _http.getPhoneList();
if (response.second == null) {
SmsPhoneListResponse getServerPhoneNumbers() throws OCSyncException {
Pair<Integer, SmsPhoneListResponse> response = _http.getPhoneList();
if (response.second == null || response.first != 200) {
return null;
}
try {
return response.second.getJSONArray("phoneList");
} catch (JSONException e) {
Log.e(OCSMSOwnCloudClient.TAG, "No phonelist received from server, empty it", e);
return null;
}
return response.second;
}
public void doPushRequest(SmsBuffer smsBuffer) throws OCSyncException {