1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-27 09:46:23 +00:00

Add getServerPhoneNumbers call and fix file coding style

This commit is contained in:
Loic Blot 2015-04-28 17:50:00 +02:00
parent 9d3c9e93a0
commit a22b5c3318

View File

@ -46,7 +46,7 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class OCSMSOwnCloudClient { public class OCSMSOwnCloudClient {
public OCSMSOwnCloudClient(Context context, Uri serverURI, String accountName, String accountPassword) { public OCSMSOwnCloudClient(final Context context, final Uri serverURI, final String accountName, final String accountPassword) {
_context = context; _context = context;
_ocClient = OwnCloudClientFactory.createOwnCloudClient( _ocClient = OwnCloudClientFactory.createOwnCloudClient(
@ -61,8 +61,8 @@ public class OCSMSOwnCloudClient {
} }
public Integer getServerAPIVersion() throws OCSyncException { public Integer getServerAPIVersion() throws OCSyncException {
GetMethod get = createGetVersionRequest(); final GetMethod get = createGetVersionRequest();
JSONObject obj = doHttpRequest(get, true); final JSONObject obj = doHttpRequest(get, true);
if (obj == null) { if (obj == null) {
// Return default version // Return default version
return 1; return 1;
@ -71,7 +71,7 @@ public class OCSMSOwnCloudClient {
try { try {
_serverAPIVersion = obj.getInt("version"); _serverAPIVersion = obj.getInt("version");
} }
catch (JSONException e) { catch (final JSONException e) {
Log.e(TAG, "No version received from server, assuming version 1", e); Log.e(TAG, "No version received from server, assuming version 1", e);
_serverAPIVersion = 1; _serverAPIVersion = 1;
} }
@ -79,7 +79,22 @@ public class OCSMSOwnCloudClient {
return _serverAPIVersion; return _serverAPIVersion;
} }
public void doPushRequest(JSONArray smsList) throws OCSyncException { public JSONArray getServerPhoneNumbers() throws OCSyncException {
final GetMethod get = createGetPhoneListRequest();
final JSONObject obj = doHttpRequest(get, true);
if (obj == null) {
return null;
}
try {
return obj.getJSONArray("phonelist");
} catch (final JSONException e) {
Log.e(TAG, "No phonelist received from server, empty it", e);
return null;
}
}
public void doPushRequest(final JSONArray smsList) throws OCSyncException {
/** /**
* If we need other API push, set it here * If we need other API push, set it here
*/ */
@ -95,8 +110,8 @@ public class OCSMSOwnCloudClient {
Long lastMsgDate = (long) 0; Long lastMsgDate = (long) 0;
if (smsList == null) { if (smsList == null) {
GetMethod get = createGetSmsIdListRequest(); final GetMethod get = createGetSmsIdListRequest();
JSONObject smsGetObj = doHttpRequest(get); final JSONObject smsGetObj = doHttpRequest(get);
if (smsGetObj == null) { if (smsGetObj == null) {
return; return;
} }
@ -105,10 +120,10 @@ public class OCSMSOwnCloudClient {
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null; JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
try { try {
smsBoxes = smsGetObj.getJSONObject("smslist"); smsBoxes = smsGetObj.getJSONObject("smslist");
} catch (JSONException e) { } catch (final JSONException e) {
try { try {
smsGetObj.getJSONArray("smslist"); smsGetObj.getJSONArray("smslist");
} catch (JSONException e2) { } catch (final JSONException e2) {
Log.e(TAG, "Invalid datas received from server (doPushRequest, get SMS list)", e); Log.e(TAG, "Invalid datas received from server (doPushRequest, get SMS list)", e);
throw new OCSyncException(R.string.err_sync_get_smslist, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_get_smslist, OCSyncErrorType.PARSE);
} }
@ -116,23 +131,23 @@ public class OCSMSOwnCloudClient {
try { try {
inboxSmsList = smsBoxes.getJSONArray("inbox"); inboxSmsList = smsBoxes.getJSONArray("inbox");
} catch (JSONException e) { } catch (final JSONException e) {
Log.d(TAG, "No inbox Sms received from server (doPushRequest, get SMS list)"); Log.d(TAG, "No inbox Sms received from server (doPushRequest, get SMS list)");
} }
try { try {
sentSmsList = smsBoxes.getJSONArray("sent"); sentSmsList = smsBoxes.getJSONArray("sent");
} catch (JSONException e) { } catch (final JSONException e) {
Log.d(TAG, "No sent Sms received from server (doPushRequest, get SMS list)"); Log.d(TAG, "No sent Sms received from server (doPushRequest, get SMS list)");
} }
try { try {
draftsSmsList = smsBoxes.getJSONArray("drafts"); draftsSmsList = smsBoxes.getJSONArray("drafts");
} catch (JSONException e) { } catch (final JSONException e) {
Log.d(TAG, "No drafts Sms received from server (doPushRequest, get SMS list)"); Log.d(TAG, "No drafts Sms received from server (doPushRequest, get SMS list)");
} }
SmsFetcher fetcher = new SmsFetcher(_context); final SmsFetcher fetcher = new SmsFetcher(_context);
fetcher.setExistingInboxMessages(inboxSmsList); fetcher.setExistingInboxMessages(inboxSmsList);
fetcher.setExistingSentMessages(sentSmsList); fetcher.setExistingSentMessages(sentSmsList);
fetcher.setExistingDraftsMessages(draftsSmsList); fetcher.setExistingDraftsMessages(draftsSmsList);
@ -148,13 +163,13 @@ public class OCSMSOwnCloudClient {
return; return;
} }
PostMethod post = createPushRequest(smsList); final PostMethod post = createPushRequest(smsList);
if (post == null) { if (post == null) {
Log.e(TAG,"Push request for POST is null"); Log.e(TAG,"Push request for POST is null");
throw new OCSyncException(R.string.err_sync_craft_http_request, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_craft_http_request, OCSyncErrorType.IO);
} }
JSONObject obj = doHttpRequest(post); final JSONObject obj = doHttpRequest(post);
if (obj == null) { if (obj == null) {
Log.e(TAG,"Request failed. It doesn't return a valid JSON Object"); Log.e(TAG,"Request failed. It doesn't return a valid JSON Object");
throw new OCSyncException(R.string.err_sync_push_request, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_push_request, OCSyncErrorType.IO);
@ -166,7 +181,7 @@ public class OCSMSOwnCloudClient {
pushStatus = obj.getBoolean("status"); pushStatus = obj.getBoolean("status");
pushMessage = obj.getString("msg"); pushMessage = obj.getString("msg");
} }
catch (JSONException e) { catch (final JSONException e) {
Log.e(TAG, "Invalid datas received from server", e); Log.e(TAG, "Invalid datas received from server", e);
throw new OCSyncException(R.string.err_sync_push_request_resp, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_push_request_resp, OCSyncErrorType.PARSE);
} }
@ -177,7 +192,7 @@ public class OCSMSOwnCloudClient {
Log.d(TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage); Log.d(TAG, "SMS Push request said: status " + pushStatus + " - " + pushMessage);
} }
public void doPushRequestV2(JSONArray smsList) throws OCSyncException { public void doPushRequestV2(final JSONArray smsList) throws OCSyncException {
} }
@ -185,6 +200,10 @@ public class OCSMSOwnCloudClient {
return createGetRequest(OC_GET_VERSION); return createGetRequest(OC_GET_VERSION);
} }
public GetMethod createGetPhoneListRequest() {
return createGetRequest(OC_GET_PHONELIST);
}
public GetMethod createGetSmsIdListRequest() { public GetMethod createGetSmsIdListRequest() {
return createGetRequest(OC_GET_ALL_SMS_IDS); return createGetRequest(OC_GET_ALL_SMS_IDS);
} }
@ -197,48 +216,48 @@ public class OCSMSOwnCloudClient {
return createGetRequest(OC_GET_LAST_MSG_TIMESTAMP); return createGetRequest(OC_GET_LAST_MSG_TIMESTAMP);
} }
private GetMethod createGetRequest(String oc_call) { private GetMethod createGetRequest(final String oc_call) {
GetMethod get = new GetMethod(_ocClient.getBaseUri() + oc_call); final GetMethod get = new GetMethod(_ocClient.getBaseUri() + oc_call);
get.addRequestHeader("OCS-APIREQUEST", "true"); get.addRequestHeader("OCS-APIREQUEST", "true");
return get; return get;
} }
public PostMethod createPushRequest() throws OCSyncException { public PostMethod createPushRequest() throws OCSyncException {
SmsFetcher fetcher = new SmsFetcher(_context); final SmsFetcher fetcher = new SmsFetcher(_context);
JSONArray smsList = fetcher.fetchAllMessages(); final JSONArray smsList = fetcher.fetchAllMessages();
return createPushRequest(smsList); return createPushRequest(smsList);
} }
public PostMethod createPushRequest(JSONArray smsList) throws OCSyncException { public PostMethod createPushRequest(final JSONArray smsList) throws OCSyncException {
JSONObject obj = createPushJSONObject(smsList); final JSONObject obj = createPushJSONObject(smsList);
if (obj == null) { if (obj == null) {
return null; return null;
} }
StringRequestEntity ent = createJSONRequestEntity(obj); final StringRequestEntity ent = createJSONRequestEntity(obj);
if (ent == null) { if (ent == null) {
return null; return null;
} }
PostMethod post = new PostMethod(_ocClient.getBaseUri() + OC_PUSH_ROUTE); final PostMethod post = new PostMethod(_ocClient.getBaseUri() + OC_PUSH_ROUTE);
post.addRequestHeader("OCS-APIREQUEST", "true"); post.addRequestHeader("OCS-APIREQUEST", "true");
post.setRequestEntity(ent); post.setRequestEntity(ent);
return post; return post;
} }
private JSONObject createPushJSONObject(JSONArray smsList) throws OCSyncException { private JSONObject createPushJSONObject(final JSONArray smsList) throws OCSyncException {
if (smsList == null) { if (smsList == null) {
Log.e(TAG,"NULL SMS List"); Log.e(TAG,"NULL SMS List");
throw new OCSyncException(R.string.err_sync_create_json_null_smslist, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_create_json_null_smslist, OCSyncErrorType.IO);
} }
JSONObject reqJSON = new JSONObject(); final JSONObject reqJSON = new JSONObject();
try { try {
reqJSON.put("smsDatas", smsList); reqJSON.put("smsDatas", smsList);
reqJSON.put("smsCount", smsList == null ? 0 : smsList.length()); reqJSON.put("smsCount", smsList == null ? 0 : smsList.length());
} catch (JSONException e) { } catch (final JSONException e) {
Log.e(TAG,"JSON Exception when creating JSON request object"); Log.e(TAG,"JSON Exception when creating JSON request object");
throw new OCSyncException(R.string.err_sync_create_json_put_smslist, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_create_json_put_smslist, OCSyncErrorType.PARSE);
} }
@ -246,7 +265,7 @@ public class OCSMSOwnCloudClient {
return reqJSON; return reqJSON;
} }
private StringRequestEntity createJSONRequestEntity(JSONObject obj) throws OCSyncException { private StringRequestEntity createJSONRequestEntity(final JSONObject obj) throws OCSyncException {
StringRequestEntity requestEntity; StringRequestEntity requestEntity;
try { try {
requestEntity = new StringRequestEntity( requestEntity = new StringRequestEntity(
@ -254,7 +273,7 @@ public class OCSMSOwnCloudClient {
"application/json", "application/json",
"UTF-8"); "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
Log.e(TAG,"Unsupported encoding when generating request"); Log.e(TAG,"Unsupported encoding when generating request");
throw new OCSyncException(R.string.err_sync_create_json_request_encoding, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_create_json_request_encoding, OCSyncErrorType.PARSE);
} }
@ -262,18 +281,18 @@ public class OCSMSOwnCloudClient {
return requestEntity; return requestEntity;
} }
private JSONObject doHttpRequest(HttpMethod req) throws OCSyncException { private JSONObject doHttpRequest(final HttpMethod req) throws OCSyncException {
return doHttpRequest(req, false); return doHttpRequest(req, false);
} }
// skipError permit to skip invalid JSON datas // skipError permit to skip invalid JSON datas
private JSONObject doHttpRequest(HttpMethod req, Boolean skipError) throws OCSyncException { private JSONObject doHttpRequest(final HttpMethod req, final Boolean skipError) throws OCSyncException {
JSONObject respJSON = null; JSONObject respJSON = null;
int status = 0; int status = 0;
// We try maximumHttpReqTries because sometimes network is slow or unstable // We try maximumHttpReqTries because sometimes network is slow or unstable
int tryNb = 0; int tryNb = 0;
ConnectivityMonitor cMon = new ConnectivityMonitor(_context); final ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
while (tryNb < maximumHttpReqTries) { while (tryNb < maximumHttpReqTries) {
tryNb++; tryNb++;
@ -293,7 +312,7 @@ public class OCSMSOwnCloudClient {
// Force loop exit // Force loop exit
tryNb = maximumHttpReqTries; tryNb = maximumHttpReqTries;
} catch (ConnectException e) { } catch (final ConnectException e) {
Log.e(TAG, "Unable to perform a connection to ownCloud instance", e); Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);
// If it's the last try // If it's the last try
@ -301,7 +320,7 @@ public class OCSMSOwnCloudClient {
req.releaseConnection(); req.releaseConnection();
throw new OCSyncException(R.string.err_sync_http_request_connect, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_http_request_connect, OCSyncErrorType.IO);
} }
} catch (HttpException e) { } catch (final HttpException e) {
Log.e(TAG, "Unable to perform a connection to ownCloud instance", e); Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);
// If it's the last try // If it's the last try
@ -309,7 +328,7 @@ public class OCSMSOwnCloudClient {
req.releaseConnection(); req.releaseConnection();
throw new OCSyncException(R.string.err_sync_http_request_httpexception, OCSyncErrorType.IO); throw new OCSyncException(R.string.err_sync_http_request_httpexception, OCSyncErrorType.IO);
} }
} catch (IOException e) { } catch (final IOException e) {
Log.e(TAG, "Unable to perform a connection to ownCloud instance", e); Log.e(TAG, "Unable to perform a connection to ownCloud instance", e);
// If it's the last try // If it's the last try
@ -324,7 +343,7 @@ public class OCSMSOwnCloudClient {
String response = null; String response = null;
try { try {
response = req.getResponseBodyAsString(); response = req.getResponseBodyAsString();
} catch (IOException e) { } catch (final IOException e) {
Log.e(TAG, "Unable to parse server response", e); Log.e(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);
} }
@ -333,7 +352,7 @@ public class OCSMSOwnCloudClient {
// Parse the response // Parse the response
try { try {
respJSON = new JSONObject(response); respJSON = new JSONObject(response);
} catch (JSONException e) { } catch (final JSONException e) {
if (skipError == false) { if (skipError == false) {
Log.e(TAG, "Unable to parse server response", e); Log.e(TAG, "Unable to parse server response", e);
throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
@ -349,7 +368,7 @@ public class OCSMSOwnCloudClient {
String response = null; String response = null;
try { try {
response = req.getResponseBodyAsString(); response = req.getResponseBodyAsString();
} catch (IOException e) { } catch (final IOException e) {
Log.e(TAG, "Unable to parse server response", e); Log.e(TAG, "Unable to parse server response", e);
throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.PARSE); throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.PARSE);
} }
@ -369,8 +388,8 @@ public class OCSMSOwnCloudClient {
private static int maximumHttpReqTries = 3; private static int maximumHttpReqTries = 3;
private OwnCloudClient _ocClient; private final OwnCloudClient _ocClient;
private Context _context; private final Context _context;
private Integer _serverAPIVersion; private Integer _serverAPIVersion;
@ -379,6 +398,7 @@ public class OCSMSOwnCloudClient {
private static String OC_GET_ALL_SMS_IDS_WITH_STATUS = "/index.php/apps/ocsms/get/smsidstate?format=json"; private static String OC_GET_ALL_SMS_IDS_WITH_STATUS = "/index.php/apps/ocsms/get/smsidstate?format=json";
private static String OC_GET_LAST_MSG_TIMESTAMP = "/index.php/apps/ocsms/get/lastmsgtime?format=json"; private static String OC_GET_LAST_MSG_TIMESTAMP = "/index.php/apps/ocsms/get/lastmsgtime?format=json";
private static String OC_PUSH_ROUTE = "/index.php/apps/ocsms/push?format=json"; private static String OC_PUSH_ROUTE = "/index.php/apps/ocsms/push?format=json";
private static String OC_GET_PHONELIST = "/index.php/apps/ocsms/get/phones/numberlist?format=json";
private static final String TAG = OCSMSOwnCloudClient.class.getSimpleName(); private static final String TAG = OCSMSOwnCloudClient.class.getSimpleName();