mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-07-03 12:46:16 +00:00
More code cleanup
This commit is contained in:
parent
e2c65cf867
commit
a5a03dbfcd
src/main/java/fr/unix_experience/owncloud_sms
@ -49,14 +49,7 @@ public class ContactListActivity extends AppCompatActivity implements ASyncConta
|
|||||||
|
|
||||||
assert getIntent().getExtras() != null;
|
assert getIntent().getExtras() != null;
|
||||||
|
|
||||||
String accountName = getIntent().getExtras().getString("account");
|
|
||||||
|
|
||||||
// accountName cannot be null, devel error
|
|
||||||
assert accountName != null;
|
|
||||||
|
|
||||||
ContactListActivity.mAccountMgr = AccountManager.get(getBaseContext());
|
ContactListActivity.mAccountMgr = AccountManager.get(getBaseContext());
|
||||||
Account[] myAccountList =
|
|
||||||
ContactListActivity.mAccountMgr.getAccountsByType(getString(R.string.account_type));
|
|
||||||
|
|
||||||
// Init view
|
// Init view
|
||||||
mObjects = new ArrayList<>();
|
mObjects = new ArrayList<>();
|
||||||
@ -66,9 +59,7 @@ public class ContactListActivity extends AppCompatActivity implements ASyncConta
|
|||||||
|
|
||||||
mAdapter = new ContactListAdapter(getBaseContext(), mObjects);
|
mAdapter = new ContactListAdapter(getBaseContext(), mObjects);
|
||||||
|
|
||||||
final Spinner sp = (Spinner) findViewById(R.id.contact_spinner);
|
|
||||||
mContactInfos = (LinearLayout) findViewById(R.id.contactinfos_layout);
|
mContactInfos = (LinearLayout) findViewById(R.id.contactinfos_layout);
|
||||||
final ProgressBar contactProgressBar = (ProgressBar) findViewById(R.id.contactlist_pgbar);
|
|
||||||
ListView contactPhoneListView = (ListView) findViewById(R.id.contact_phonelistView);
|
ListView contactPhoneListView = (ListView) findViewById(R.id.contact_phonelistView);
|
||||||
mContactPhoneListAdapter = new RecoveryPhoneNumberListViewAdapter(getBaseContext());
|
mContactPhoneListAdapter = new RecoveryPhoneNumberListViewAdapter(getBaseContext());
|
||||||
assert contactPhoneListView != null;
|
assert contactPhoneListView != null;
|
||||||
@ -76,14 +67,60 @@ public class ContactListActivity extends AppCompatActivity implements ASyncConta
|
|||||||
|
|
||||||
mContactInfos.setVisibility(View.INVISIBLE);
|
mContactInfos.setVisibility(View.INVISIBLE);
|
||||||
|
|
||||||
assert sp != null;
|
initSpinner();
|
||||||
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
createAccountList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createAccountList() {
|
||||||
|
final ProgressBar contactProgressBar = (ProgressBar) findViewById(R.id.contactlist_pgbar);
|
||||||
|
assert contactProgressBar != null;
|
||||||
|
|
||||||
|
String accountName = getIntent().getExtras().getString("account");
|
||||||
|
assert accountName != null;
|
||||||
|
|
||||||
|
Account[] myAccountList =
|
||||||
|
ContactListActivity.mAccountMgr.getAccountsByType(getString(R.string.account_type));
|
||||||
|
|
||||||
|
for (final Account element : myAccountList) {
|
||||||
|
if (element.name.equals(accountName)) {
|
||||||
|
// Load "contacts"
|
||||||
|
contactProgressBar.setVisibility(View.VISIBLE);
|
||||||
|
new ContactLoadTask(element, getBaseContext(), mAdapter, mObjects, mLayout,
|
||||||
|
contactProgressBar, mContactInfos).execute();
|
||||||
|
|
||||||
|
// Add refresh handler
|
||||||
|
mLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
||||||
|
@Override
|
||||||
|
public void onRefresh() {
|
||||||
|
mLayout.setRefreshing(true);
|
||||||
|
mContactInfos.setVisibility(View.INVISIBLE);
|
||||||
|
contactProgressBar.setVisibility(View.VISIBLE);
|
||||||
|
(new Handler()).post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mObjects.clear();
|
||||||
|
mAdapter.notifyDataSetChanged();
|
||||||
|
new ContactLoadTask(element, getBaseContext(), mAdapter, mObjects,
|
||||||
|
mLayout, contactProgressBar, mContactInfos).execute();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initSpinner() {
|
||||||
|
final Spinner sp = (Spinner) findViewById(R.id.contact_spinner);
|
||||||
|
assert sp != null;
|
||||||
|
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
mContactInfos.setVisibility(View.INVISIBLE);
|
mContactInfos.setVisibility(View.INVISIBLE);
|
||||||
mContactPhoneListAdapter.clear();
|
mContactPhoneListAdapter.clear();
|
||||||
|
|
||||||
mFetchedContact = sp.getSelectedItem().toString();
|
mFetchedContact = sp.getSelectedItem().toString();
|
||||||
fetchContact(mFetchedContact);
|
fetchContact(mFetchedContact);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,37 +132,9 @@ public class ContactListActivity extends AppCompatActivity implements ASyncConta
|
|||||||
|
|
||||||
});
|
});
|
||||||
sp.setAdapter(mAdapter);
|
sp.setAdapter(mAdapter);
|
||||||
|
|
||||||
for (final Account element : myAccountList) {
|
|
||||||
if (element.name.equals(accountName)) {
|
|
||||||
// Load "contacts"
|
|
||||||
assert contactProgressBar != null;
|
|
||||||
contactProgressBar.setVisibility(View.VISIBLE);
|
|
||||||
new ContactLoadTask(element, getBaseContext(), mAdapter, mObjects, mLayout, contactProgressBar, mContactInfos).execute();
|
|
||||||
|
|
||||||
// Add refresh handler
|
|
||||||
mLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
||||||
@Override
|
|
||||||
public void onRefresh() {
|
|
||||||
mLayout.setRefreshing(true);
|
|
||||||
mContactInfos.setVisibility(View.INVISIBLE);
|
|
||||||
contactProgressBar.setVisibility(View.VISIBLE);
|
|
||||||
(new Handler()).post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mObjects.clear();
|
|
||||||
mAdapter.notifyDataSetChanged();
|
|
||||||
new ContactLoadTask(element, getBaseContext(), mAdapter, mObjects, mLayout, contactProgressBar, mContactInfos).execute();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fetchContact(String name) {
|
private void fetchContact(String name) {
|
||||||
if (!PermissionChecker.checkPermission(this, Manifest.permission.READ_CONTACTS,
|
if (!PermissionChecker.checkPermission(this, Manifest.permission.READ_CONTACTS,
|
||||||
REQUEST_CONTACTS)) {
|
REQUEST_CONTACTS)) {
|
||||||
return;
|
return;
|
||||||
|
@ -104,6 +104,48 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private AndroidSmsFetcher collectMessages(JSONArray smsList) throws OCSyncException {
|
||||||
|
JSONObject smsBoxes = new JSONObject();
|
||||||
|
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
|
||||||
|
try {
|
||||||
|
smsBoxes = _jsonQueryBuffer.getJSONObject("smslist");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
try {
|
||||||
|
_jsonQueryBuffer.getJSONArray("smslist");
|
||||||
|
} catch (JSONException e2) {
|
||||||
|
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server (doPushRequest, get SMS list)", e);
|
||||||
|
throw new OCSyncException(R.string.err_sync_get_smslist, OCSyncErrorType.PARSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
inboxSmsList = smsBoxes.getJSONArray("inbox");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.i(OCSMSOwnCloudClient.TAG, "No inbox Sms received from server (doPushRequest, get SMS list)");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
sentSmsList = smsBoxes.getJSONArray("sent");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.i(OCSMSOwnCloudClient.TAG, "No sent Sms received from server (doPushRequest, get SMS list)");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
draftsSmsList = smsBoxes.getJSONArray("drafts");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.i(OCSMSOwnCloudClient.TAG, "No drafts Sms received from server (doPushRequest, get SMS list)");
|
||||||
|
}
|
||||||
|
|
||||||
|
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
|
||||||
|
fetcher.setExistingInboxMessages(inboxSmsList);
|
||||||
|
fetcher.setExistingSentMessages(sentSmsList);
|
||||||
|
fetcher.setExistingDraftsMessages(draftsSmsList);
|
||||||
|
|
||||||
|
fetcher.fetchAllMessages(smsList);
|
||||||
|
|
||||||
|
return fetcher;
|
||||||
|
}
|
||||||
|
|
||||||
private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
|
private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
|
||||||
// We need to save this date as a step for connectivity change
|
// We need to save this date as a step for connectivity change
|
||||||
Long lastMsgDate = (long) 0;
|
Long lastMsgDate = (long) 0;
|
||||||
@ -114,47 +156,10 @@ public class OCSMSOwnCloudClient {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject smsBoxes = new JSONObject();
|
// Create new JSONArray to get results
|
||||||
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
|
smsList = new JSONArray();
|
||||||
try {
|
|
||||||
smsBoxes = _jsonQueryBuffer.getJSONObject("smslist");
|
|
||||||
} catch (JSONException e) {
|
|
||||||
try {
|
|
||||||
_jsonQueryBuffer.getJSONArray("smslist");
|
|
||||||
} catch (JSONException e2) {
|
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server (doPushRequest, get SMS list)", e);
|
|
||||||
throw new OCSyncException(R.string.err_sync_get_smslist, OCSyncErrorType.PARSE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
inboxSmsList = smsBoxes.getJSONArray("inbox");
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.i(OCSMSOwnCloudClient.TAG, "No inbox Sms received from server (doPushRequest, get SMS list)");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
sentSmsList = smsBoxes.getJSONArray("sent");
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.i(OCSMSOwnCloudClient.TAG, "No sent Sms received from server (doPushRequest, get SMS list)");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
draftsSmsList = smsBoxes.getJSONArray("drafts");
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.i(OCSMSOwnCloudClient.TAG, "No drafts Sms received from server (doPushRequest, get SMS list)");
|
|
||||||
}
|
|
||||||
|
|
||||||
AndroidSmsFetcher fetcher = new AndroidSmsFetcher(_context);
|
|
||||||
fetcher.setExistingInboxMessages(inboxSmsList);
|
|
||||||
fetcher.setExistingSentMessages(sentSmsList);
|
|
||||||
fetcher.setExistingDraftsMessages(draftsSmsList);
|
|
||||||
|
|
||||||
smsList = new JSONArray();
|
|
||||||
fetcher.fetchAllMessages(smsList);
|
|
||||||
|
|
||||||
// Get maximum message date present in smsList to keep a step when connectivity changes
|
// Get maximum message date present in smsList to keep a step when connectivity changes
|
||||||
lastMsgDate = fetcher.getLastMessageDate();
|
lastMsgDate = collectMessages(smsList).getLastMessageDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (smsList.length() == 0) {
|
if (smsList.length() == 0) {
|
||||||
@ -312,39 +317,47 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status == 200) {
|
handleHTTPResponse(req, status, skipError);
|
||||||
String response = getResponseBody(req);
|
}
|
||||||
|
|
||||||
// Parse the response
|
private void handleHTTPResponse(HttpMethod req, int status, Boolean skipError) throws OCSyncException {
|
||||||
try {
|
switch (status) {
|
||||||
_jsonQueryBuffer = new JSONObject(response);
|
case 200: {
|
||||||
} catch (JSONException e) {
|
String response = getResponseBody(req);
|
||||||
if (!skipError) {
|
|
||||||
if (response.contains("ownCloud") && response.contains("DOCTYPE")) {
|
// Parse the response
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "OcSMS app not enabled or ownCloud upgrade is required");
|
try {
|
||||||
throw new OCSyncException(R.string.err_sync_ocsms_not_installed_or_oc_upgrade_required,
|
_jsonQueryBuffer = new JSONObject(response);
|
||||||
OCSyncErrorType.SERVER_ERROR);
|
} catch (JSONException e) {
|
||||||
}
|
if (!skipError) {
|
||||||
else {
|
if (response.contains("ownCloud") && response.contains("DOCTYPE")) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e);
|
Log.e(OCSMSOwnCloudClient.TAG, "OcSMS app not enabled or ownCloud upgrade is required");
|
||||||
throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
|
throw new OCSyncException(R.string.err_sync_ocsms_not_installed_or_oc_upgrade_required,
|
||||||
|
OCSyncErrorType.SERVER_ERROR);
|
||||||
|
} else {
|
||||||
|
Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e);
|
||||||
|
throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if (status == 403) {
|
case 403: {
|
||||||
// Authentication failed
|
// Authentication failed
|
||||||
throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH);
|
throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH);
|
||||||
} else {
|
}
|
||||||
// Unk error
|
default: {
|
||||||
String response = getResponseBody(req);
|
// Unk error
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Server set unhandled HTTP return code " + status);
|
String response = getResponseBody(req);
|
||||||
|
Log.e(OCSMSOwnCloudClient.TAG, "Server set unhandled HTTP return code " + status);
|
||||||
|
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Status code: " + status + ". Response message: " + response);
|
Log.e(OCSMSOwnCloudClient.TAG, "Status code: " + status + ". Response message: " + response);
|
||||||
} else {
|
} else {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Status code: " + status);
|
Log.e(OCSMSOwnCloudClient.TAG, "Status code: " + status);
|
||||||
|
}
|
||||||
|
throw new OCSyncException(R.string.err_sync_http_request_returncode_unhandled, OCSyncErrorType.SERVER_ERROR);
|
||||||
}
|
}
|
||||||
throw new OCSyncException(R.string.err_sync_http_request_returncode_unhandled, OCSyncErrorType.SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user