mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-28 10:16:13 +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,6 +67,52 @@ public class ContactListActivity extends AppCompatActivity implements ASyncConta
|
|||||||
|
|
||||||
mContactInfos.setVisibility(View.INVISIBLE);
|
mContactInfos.setVisibility(View.INVISIBLE);
|
||||||
|
|
||||||
|
initSpinner();
|
||||||
|
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;
|
assert sp != null;
|
||||||
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -95,34 +132,6 @@ 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) {
|
||||||
|
@ -104,16 +104,7 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
|
private AndroidSmsFetcher collectMessages(JSONArray smsList) throws OCSyncException {
|
||||||
// We need to save this date as a step for connectivity change
|
|
||||||
Long lastMsgDate = (long) 0;
|
|
||||||
|
|
||||||
if (smsList == null) {
|
|
||||||
doHttpRequest(_http.getAllSmsIds());
|
|
||||||
if (_jsonQueryBuffer == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject smsBoxes = new JSONObject();
|
JSONObject smsBoxes = new JSONObject();
|
||||||
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
|
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
|
||||||
try {
|
try {
|
||||||
@ -150,11 +141,25 @@ public class OCSMSOwnCloudClient {
|
|||||||
fetcher.setExistingSentMessages(sentSmsList);
|
fetcher.setExistingSentMessages(sentSmsList);
|
||||||
fetcher.setExistingDraftsMessages(draftsSmsList);
|
fetcher.setExistingDraftsMessages(draftsSmsList);
|
||||||
|
|
||||||
smsList = new JSONArray();
|
|
||||||
fetcher.fetchAllMessages(smsList);
|
fetcher.fetchAllMessages(smsList);
|
||||||
|
|
||||||
|
return fetcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doPushRequestV1(JSONArray smsList) throws OCSyncException {
|
||||||
|
// We need to save this date as a step for connectivity change
|
||||||
|
Long lastMsgDate = (long) 0;
|
||||||
|
|
||||||
|
if (smsList == null) {
|
||||||
|
doHttpRequest(_http.getAllSmsIds());
|
||||||
|
if (_jsonQueryBuffer == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new JSONArray to get results
|
||||||
|
smsList = new JSONArray();
|
||||||
// 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,7 +317,12 @@ public class OCSMSOwnCloudClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status == 200) {
|
handleHTTPResponse(req, status, skipError);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleHTTPResponse(HttpMethod req, int status, Boolean skipError) throws OCSyncException {
|
||||||
|
switch (status) {
|
||||||
|
case 200: {
|
||||||
String response = getResponseBody(req);
|
String response = getResponseBody(req);
|
||||||
|
|
||||||
// Parse the response
|
// Parse the response
|
||||||
@ -324,17 +334,19 @@ public class OCSMSOwnCloudClient {
|
|||||||
Log.e(OCSMSOwnCloudClient.TAG, "OcSMS app not enabled or ownCloud upgrade is required");
|
Log.e(OCSMSOwnCloudClient.TAG, "OcSMS app not enabled or ownCloud upgrade is required");
|
||||||
throw new OCSyncException(R.string.err_sync_ocsms_not_installed_or_oc_upgrade_required,
|
throw new OCSyncException(R.string.err_sync_ocsms_not_installed_or_oc_upgrade_required,
|
||||||
OCSyncErrorType.SERVER_ERROR);
|
OCSyncErrorType.SERVER_ERROR);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
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_parse_resp, OCSyncErrorType.PARSE);
|
throw new OCSyncException(R.string.err_sync_http_request_parse_resp, OCSyncErrorType.PARSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (status == 403) {
|
break;
|
||||||
|
}
|
||||||
|
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 {
|
}
|
||||||
|
default: {
|
||||||
// Unk error
|
// Unk error
|
||||||
String response = getResponseBody(req);
|
String response = getResponseBody(req);
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Server set unhandled HTTP return code " + status);
|
Log.e(OCSMSOwnCloudClient.TAG, "Server set unhandled HTTP return code " + status);
|
||||||
@ -347,6 +359,7 @@ public class OCSMSOwnCloudClient {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String getResponseBody(HttpMethod req) throws OCSyncException {
|
private String getResponseBody(HttpMethod req) throws OCSyncException {
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user