1
0
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:
Loic Blot 2016-03-18 11:13:35 +01:00
parent 114cc84ce6
commit 42e12a2081
4 changed files with 85 additions and 82 deletions

View File

@ -51,8 +51,13 @@ import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private static ConnectivityMonitor mConnectivityMonitor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (mConnectivityMonitor == null) {
mConnectivityMonitor = new ConnectivityMonitor(getApplicationContext());
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
@ -106,9 +111,8 @@ public class MainActivity extends AppCompatActivity
private boolean syncAllMessages () {
Context ctx = getApplicationContext();
ConnectivityMonitor cMon = new ConnectivityMonitor(ctx);
if (cMon.isValid()) {
if (mConnectivityMonitor.isValid()) {
// Now fetch messages since last stored date
JSONArray smsList = new JSONArray();
new SmsFetcher(ctx).bufferMessagesSinceDate(smsList, (long) 0);

View File

@ -20,65 +20,66 @@ import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class ConnectivityMonitor {
public ConnectivityMonitor(Context context) {
_context = context;
}
public ConnectivityMonitor(Context context) {
_context = context;
}
// Valid connection = WiFi or Mobile data
public boolean isValid() {
if (_cMgr == null) {
_cMgr = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
// Valid connection = WiFi or Mobile data
public boolean isValid() {
if (_cMgr == null) {
_cMgr = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
android.net.NetworkInfo niWiFi = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo niMobile = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
android.net.NetworkInfo niWiFi = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo niMobile = _cMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (niWiFi.isAvailable() || niMobile.isAvailable()) {
// Load the connectivity manager to determine on which network we are connected
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
if (netInfo == null) {
return false;
}
if (niWiFi.isAvailable() || niMobile.isAvailable()) {
// Load the connectivity manager to determine on which network we are connected
NetworkInfo netInfo = _cMgr.getActiveNetworkInfo();
if (netInfo == null) {
return false;
}
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
// Check
switch (netInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return prefs.syncInWifi();
case ConnectivityManager.TYPE_MOBILE:
switch (netInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return prefs.syncIn2G();
case TelephonyManager.NETWORK_TYPE_GPRS:
return prefs.syncInGPRS();
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return prefs.syncIn3G();
case TelephonyManager.NETWORK_TYPE_LTE:
return prefs.syncIn4G();
default:
return prefs.syncInOtherModes();
}
default:
return prefs.syncInOtherModes();
}
}
// Check
switch (netInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
return prefs.syncInWifi();
case ConnectivityManager.TYPE_MOBILE:
switch (netInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return prefs.syncIn2G();
case TelephonyManager.NETWORK_TYPE_GPRS:
return prefs.syncInGPRS();
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return prefs.syncIn3G();
case TelephonyManager.NETWORK_TYPE_LTE:
return prefs.syncIn4G();
default:
return prefs.syncInOtherModes();
}
default:
return prefs.syncInOtherModes();
}
}
return false;
}
return false;
}
private ConnectivityManager _cMgr;
private final Context _context;
private ConnectivityManager _cMgr;
private final Context _context;
}

View File

@ -59,18 +59,20 @@ public class OCSMSOwnCloudClient {
);
_serverAPIVersion = 1;
_connectivityMonitor = new ConnectivityMonitor(_context);
}
public Integer getServerAPIVersion() throws OCSyncException {
GetMethod get = createGetVersionRequest();
JSONObject obj = doHttpRequest(get, true);
if (obj == null) {
doHttpRequest(get, true);
if (_jsonQueryBuffer == null) {
// Return default version
return 1;
}
try {
_serverAPIVersion = obj.getInt("version");
_serverAPIVersion = _jsonQueryBuffer.getInt("version");
}
catch (JSONException 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 {
GetMethod get = createGetPhoneListRequest();
JSONObject obj = doHttpRequest(get, true);
if (obj == null) {
doHttpRequest(get, true);
if (_jsonQueryBuffer == null) {
return null;
}
try {
return obj.getJSONArray("phoneList");
return _jsonQueryBuffer.getJSONArray("phoneList");
} catch (JSONException e) {
Log.e(OCSMSOwnCloudClient.TAG, "No phonelist received from server, empty it", e);
return null;
@ -111,18 +113,18 @@ public class OCSMSOwnCloudClient {
if (smsList == null) {
GetMethod get = createGetSmsIdListRequest();
JSONObject smsGetObj = doHttpRequest(get);
if (smsGetObj == null) {
doHttpRequest(get);
if (_jsonQueryBuffer == null) {
return;
}
JSONObject smsBoxes = new JSONObject();
JSONArray inboxSmsList = null, sentSmsList = null, draftsSmsList = null;
try {
smsBoxes = smsGetObj.getJSONObject("smslist");
smsBoxes = _jsonQueryBuffer.getJSONObject("smslist");
} catch (JSONException e) {
try {
smsGetObj.getJSONArray("smslist");
_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);
@ -170,8 +172,8 @@ public class OCSMSOwnCloudClient {
throw new OCSyncException(R.string.err_sync_craft_http_request, OCSyncErrorType.IO);
}
JSONObject obj = doHttpRequest(post);
if (obj == null) {
doHttpRequest(post);
if (_jsonQueryBuffer == null) {
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);
}
@ -179,8 +181,8 @@ public class OCSMSOwnCloudClient {
Boolean pushStatus;
String pushMessage;
try {
pushStatus = obj.getBoolean("status");
pushMessage = obj.getString("msg");
pushStatus = _jsonQueryBuffer.getBoolean("status");
pushMessage = _jsonQueryBuffer.getString("msg");
}
catch (JSONException e) {
Log.e(OCSMSOwnCloudClient.TAG, "Invalid datas received from server", e);
@ -272,23 +274,22 @@ public class OCSMSOwnCloudClient {
return requestEntity;
}
private JSONObject doHttpRequest(HttpMethod req) throws OCSyncException {
return doHttpRequest(req, false);
private void doHttpRequest(HttpMethod req) throws OCSyncException {
doHttpRequest(req, false);
}
// skipError permit to skip invalid JSON datas
private JSONObject doHttpRequest(HttpMethod req, Boolean skipError) throws OCSyncException {
JSONObject respJSON;
private void doHttpRequest(HttpMethod req, Boolean skipError) throws OCSyncException {
// Reinit the queryBuffer
_jsonQueryBuffer = null;
int status = 0;
// We try maximumHttpReqTries because sometimes network is slow or unstable
int tryNb = 0;
ConnectivityMonitor cMon = new ConnectivityMonitor(_context);
while (tryNb < OCSMSOwnCloudClient.maximumHttpReqTries) {
tryNb++;
if (!cMon.isValid()) {
if (!_connectivityMonitor.isValid()) {
if (tryNb == OCSMSOwnCloudClient.maximumHttpReqTries) {
req.releaseConnection();
throw new OCSyncException(R.string.err_sync_no_connection_available, OCSyncErrorType.IO);
@ -342,7 +343,7 @@ public class OCSMSOwnCloudClient {
// Parse the response
try {
respJSON = new JSONObject(response);
_jsonQueryBuffer = new JSONObject(response);
} catch (JSONException e) {
if (!skipError) {
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);
}
}
return null;
return;
}
} 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);
}
return respJSON;
}
private static final int maximumHttpReqTries = 3;
private final OwnCloudClient _ocClient;
private final Context _context;
private final ConnectivityMonitor _connectivityMonitor;
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_ALL_SMS_IDS = "/index.php/apps/ocsms/get/smsidlist?format=json";

View File

@ -35,11 +35,7 @@ import fr.unix_experience.owncloud_sms.enums.MailboxID;
public class SmsObserver extends ContentObserver implements ASyncSMSSync {
public SmsObserver(Handler handler) {
super(handler);
}
public SmsObserver(Handler handler, Context ct) {
public SmsObserver(Handler handler, Context ct) {
super(handler);
_context = ct;
}