mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-20 06:16:24 +00:00
Code cleanup & perf improvements by using members instead of object return
This commit is contained in:
parent
114cc84ce6
commit
42e12a2081
@ -51,8 +51,13 @@ import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
|
|||||||
public class MainActivity extends AppCompatActivity
|
public class MainActivity extends AppCompatActivity
|
||||||
implements NavigationView.OnNavigationItemSelectedListener{
|
implements NavigationView.OnNavigationItemSelectedListener{
|
||||||
|
|
||||||
|
private static ConnectivityMonitor mConnectivityMonitor = null;
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
if (mConnectivityMonitor == null) {
|
||||||
|
mConnectivityMonitor = new ConnectivityMonitor(getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||||
@ -106,9 +111,8 @@ public class MainActivity extends AppCompatActivity
|
|||||||
|
|
||||||
private boolean syncAllMessages () {
|
private boolean syncAllMessages () {
|
||||||
Context ctx = getApplicationContext();
|
Context ctx = getApplicationContext();
|
||||||
ConnectivityMonitor cMon = new ConnectivityMonitor(ctx);
|
|
||||||
|
|
||||||
if (cMon.isValid()) {
|
if (mConnectivityMonitor.isValid()) {
|
||||||
// Now fetch messages since last stored date
|
// Now fetch messages since last stored date
|
||||||
JSONArray smsList = new JSONArray();
|
JSONArray smsList = new JSONArray();
|
||||||
new SmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);
|
new SmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);
|
||||||
|
@ -20,65 +20,66 @@ import android.content.Context;
|
|||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
|
|
||||||
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
|
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
|
||||||
|
|
||||||
public class ConnectivityMonitor {
|
public class ConnectivityMonitor {
|
||||||
public ConnectivityMonitor(Context context) {
|
public ConnectivityMonitor(Context context) {
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid connection = WiFi or Mobile data
|
// Valid connection = WiFi or Mobile data
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
if (_cMgr == null) {
|
if (_cMgr == null) {
|
||||||
_cMgr = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
_cMgr = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
android.net.NetworkInfo niWiFi = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
android.net.NetworkInfo niWiFi = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||||
android.net.NetworkInfo niMobile = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
android.net.NetworkInfo niMobile = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||||
|
|
||||||
if (niWiFi.isAvailable() || niMobile.isAvailable()) {
|
if (niWiFi.isAvailable() || niMobile.isAvailable()) {
|
||||||
// Load the connectivity manager to determine on which network we are connected
|
// Load the connectivity manager to determine on which network we are connected
|
||||||
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
|
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
|
||||||
if (netInfo == null) {
|
if (netInfo == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
|
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
|
||||||
|
|
||||||
// Check
|
// Check
|
||||||
switch (netInfo.getType()) {
|
switch (netInfo.getType()) {
|
||||||
case ConnectivityManager.TYPE_WIFI:
|
case ConnectivityManager.TYPE_WIFI:
|
||||||
return prefs.syncInWifi();
|
return prefs.syncInWifi();
|
||||||
case ConnectivityManager.TYPE_MOBILE:
|
case ConnectivityManager.TYPE_MOBILE:
|
||||||
switch (netInfo.getSubtype()) {
|
switch (netInfo.getSubtype()) {
|
||||||
case TelephonyManager.NETWORK_TYPE_EDGE:
|
case TelephonyManager.NETWORK_TYPE_EDGE:
|
||||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||||
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
||||||
case TelephonyManager.NETWORK_TYPE_IDEN:
|
case TelephonyManager.NETWORK_TYPE_IDEN:
|
||||||
return prefs.syncIn2G();
|
return prefs.syncIn2G();
|
||||||
case TelephonyManager.NETWORK_TYPE_GPRS:
|
case TelephonyManager.NETWORK_TYPE_GPRS:
|
||||||
return prefs.syncInGPRS();
|
return prefs.syncInGPRS();
|
||||||
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
||||||
case TelephonyManager.NETWORK_TYPE_HSPA:
|
case TelephonyManager.NETWORK_TYPE_HSPA:
|
||||||
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
||||||
case TelephonyManager.NETWORK_TYPE_UMTS:
|
case TelephonyManager.NETWORK_TYPE_UMTS:
|
||||||
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
||||||
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
||||||
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
||||||
return prefs.syncIn3G();
|
return prefs.syncIn3G();
|
||||||
case TelephonyManager.NETWORK_TYPE_LTE:
|
case TelephonyManager.NETWORK_TYPE_LTE:
|
||||||
return prefs.syncIn4G();
|
return prefs.syncIn4G();
|
||||||
default:
|
default:
|
||||||
return prefs.syncInOtherModes();
|
return prefs.syncInOtherModes();
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return prefs.syncInOtherModes();
|
return prefs.syncInOtherModes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnectivityManager _cMgr;
|
private ConnectivityManager _cMgr;
|
||||||
private final Context _context;
|
private final Context _context;
|
||||||
}
|
}
|
||||||
|
@ -59,18 +59,20 @@ public class OCSMSOwnCloudClient {
|
|||||||
);
|
);
|
||||||
|
|
||||||
_serverAPIVersion = 1;
|
_serverAPIVersion = 1;
|
||||||
|
|
||||||
|
_connectivityMonitor = new ConnectivityMonitor(_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getServerAPIVersion() throws OCSyncException {
|
public Integer getServerAPIVersion() throws OCSyncException {
|
||||||
GetMethod get = createGetVersionRequest();
|
GetMethod get = createGetVersionRequest();
|
||||||
JSONObject obj = doHttpRequest(get, true);
|
doHttpRequest(get, true);
|
||||||
if (obj == null) {
|
if (_jsonQueryBuffer == null) {
|
||||||
// Return default version
|
// Return default version
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_serverAPIVersion = obj.getInt("version");
|
_serverAPIVersion = _jsonQueryBuffer.getInt("version");
|
||||||
}
|
}
|
||||||
catch (JSONException e) {
|
catch (JSONException e) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "No version received from server, assuming version 1", e);
|
Log.e(OCSMSOwnCloudClient.TAG, "No version received from server, assuming version 1", e);
|
||||||
@ -82,13 +84,13 @@ public class OCSMSOwnCloudClient {
|
|||||||
|
|
||||||
public JSONArray getServerPhoneNumbers() throws OCSyncException {
|
public JSONArray getServerPhoneNumbers() throws OCSyncException {
|
||||||
GetMethod get = createGetPhoneListRequest();
|
GetMethod get = createGetPhoneListRequest();
|
||||||
JSONObject obj = doHttpRequest(get, true);
|
doHttpRequest(get, true);
|
||||||
if (obj == null) {
|
if (_jsonQueryBuffer == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return obj.getJSONArray("phoneList");
|
return _jsonQueryBuffer.getJSONArray("phoneList");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "No phonelist received from server, empty it", e);
|
Log.e(OCSMSOwnCloudClient.TAG, "No phonelist received from server, empty it", e);
|
||||||
return null;
|
return null;
|
||||||
@ -111,18 +113,18 @@ public class OCSMSOwnCloudClient {
|
|||||||
|
|
||||||
if (smsList == null) {
|
if (smsList == null) {
|
||||||
GetMethod get = createGetSmsIdListRequest();
|
GetMethod get = createGetSmsIdListRequest();
|
||||||
JSONObject smsGetObj = doHttpRequest(get);
|
doHttpRequest(get);
|
||||||
if (smsGetObj == null) {
|
if (_jsonQueryBuffer == null) {
|
||||||
return;
|
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 {
|
||||||
smsBoxes = smsGetObj.getJSONObject("smslist");
|
smsBoxes = _jsonQueryBuffer.getJSONObject("smslist");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
try {
|
try {
|
||||||
smsGetObj.getJSONArray("smslist");
|
_jsonQueryBuffer.getJSONArray("smslist");
|
||||||
} catch (JSONException e2) {
|
} catch (JSONException e2) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server (doPushRequest, get SMS list)", e);
|
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);
|
throw new OCSyncException(R.string.err_sync_get_smslist, OCSyncErrorType.PARSE);
|
||||||
@ -170,8 +172,8 @@ public class OCSMSOwnCloudClient {
|
|||||||
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);
|
doHttpRequest(post);
|
||||||
if (obj == null) {
|
if (_jsonQueryBuffer == null) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG,"Request failed. It doesn't return a valid JSON Object");
|
Log.e(OCSMSOwnCloudClient.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);
|
||||||
}
|
}
|
||||||
@ -179,8 +181,8 @@ public class OCSMSOwnCloudClient {
|
|||||||
Boolean pushStatus;
|
Boolean pushStatus;
|
||||||
String pushMessage;
|
String pushMessage;
|
||||||
try {
|
try {
|
||||||
pushStatus = obj.getBoolean("status");
|
pushStatus = _jsonQueryBuffer.getBoolean("status");
|
||||||
pushMessage = obj.getString("msg");
|
pushMessage = _jsonQueryBuffer.getString("msg");
|
||||||
}
|
}
|
||||||
catch (JSONException e) {
|
catch (JSONException e) {
|
||||||
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server", e);
|
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server", e);
|
||||||
@ -272,23 +274,22 @@ public class OCSMSOwnCloudClient {
|
|||||||
return requestEntity;
|
return requestEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject doHttpRequest(HttpMethod req) throws OCSyncException {
|
private void doHttpRequest(HttpMethod req) throws OCSyncException {
|
||||||
return doHttpRequest(req, false);
|
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 void doHttpRequest(HttpMethod req, Boolean skipError) throws OCSyncException {
|
||||||
JSONObject respJSON;
|
// Reinit the queryBuffer
|
||||||
|
_jsonQueryBuffer = 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);
|
|
||||||
|
|
||||||
while (tryNb < OCSMSOwnCloudClient.maximumHttpReqTries) {
|
while (tryNb < OCSMSOwnCloudClient.maximumHttpReqTries) {
|
||||||
tryNb++;
|
tryNb++;
|
||||||
|
|
||||||
if (!cMon.isValid()) {
|
if (!_connectivityMonitor.isValid()) {
|
||||||
if (tryNb == OCSMSOwnCloudClient.maximumHttpReqTries) {
|
if (tryNb == OCSMSOwnCloudClient.maximumHttpReqTries) {
|
||||||
req.releaseConnection();
|
req.releaseConnection();
|
||||||
throw new OCSyncException(R.string.err_sync_no_connection_available, OCSyncErrorType.IO);
|
throw new OCSyncException(R.string.err_sync_no_connection_available, OCSyncErrorType.IO);
|
||||||
@ -342,7 +343,7 @@ public class OCSMSOwnCloudClient {
|
|||||||
|
|
||||||
// Parse the response
|
// Parse the response
|
||||||
try {
|
try {
|
||||||
respJSON = new JSONObject(response);
|
_jsonQueryBuffer = new JSONObject(response);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (!skipError) {
|
if (!skipError) {
|
||||||
if (response.contains("ownCloud") && response.contains("DOCTYPE")) {
|
if (response.contains("ownCloud") && response.contains("DOCTYPE")) {
|
||||||
@ -355,7 +356,7 @@ public class OCSMSOwnCloudClient {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (status == HttpStatus.SC_FORBIDDEN) {
|
} else if (status == HttpStatus.SC_FORBIDDEN) {
|
||||||
@ -379,15 +380,16 @@ 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);
|
||||||
}
|
}
|
||||||
return respJSON;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int maximumHttpReqTries = 3;
|
private static final int maximumHttpReqTries = 3;
|
||||||
|
|
||||||
private final OwnCloudClient _ocClient;
|
private final OwnCloudClient _ocClient;
|
||||||
private final Context _context;
|
private final Context _context;
|
||||||
|
private final ConnectivityMonitor _connectivityMonitor;
|
||||||
|
|
||||||
private Integer _serverAPIVersion;
|
private Integer _serverAPIVersion;
|
||||||
|
private JSONObject _jsonQueryBuffer;
|
||||||
|
|
||||||
private static final String OC_GET_VERSION = "/index.php/apps/ocsms/get/apiversion?format=json";
|
private static final String OC_GET_VERSION = "/index.php/apps/ocsms/get/apiversion?format=json";
|
||||||
private static final String OC_GET_ALL_SMS_IDS = "/index.php/apps/ocsms/get/smsidlist?format=json";
|
private static final String OC_GET_ALL_SMS_IDS = "/index.php/apps/ocsms/get/smsidlist?format=json";
|
||||||
|
@ -35,11 +35,7 @@ import fr.unix_experience.owncloud_sms.enums.MailboxID;
|
|||||||
|
|
||||||
public class SmsObserver extends ContentObserver implements ASyncSMSSync {
|
public class SmsObserver extends ContentObserver implements ASyncSMSSync {
|
||||||
|
|
||||||
public SmsObserver(Handler handler) {
|
public SmsObserver(Handler handler, Context ct) {
|
||||||
super(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SmsObserver(Handler handler, Context ct) {
|
|
||||||
super(handler);
|
super(handler);
|
||||||
_context = ct;
|
_context = ct;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user