Skip to content

Commit

Permalink
Fixes app crashes reported on Android Version 5 (#828)
Browse files Browse the repository at this point in the history
* added exception handling for illegalArgument in HttpAgent
added inclusion rules for serialization in proguard
added exception check in getUserInitials in utils file
added null check in setSelectedLocation

* updated unit test

* reverted http agent

* reverted http agent TEST

* Updated naming of exception variable in HTTPAgent.java
updated version name in gradle.proerties

* removed the proguard rule will be adding it in path-zier
  • Loading branch information
junaidwarsivd committed Aug 25, 2021
1 parent 41c86e4 commit cfff3ef
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 65 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=4.3.17-SNAPSHOT
VERSION_NAME=4.3.18-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void setSelectedLocation(final String locationName) {
&& views.containsKey(locationName)) {
for (String curLocation : locationNames) {
View view = views.get(curLocation);
refreshView(view, curLocation.equals(locationName));
refreshView(view, curLocation != null ? curLocation.equals(locationName) : false);
}
}
}
Expand Down
106 changes: 53 additions & 53 deletions opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private HttpURLConnection initializeHttp(String requestURLPath, boolean setOauth
}

@VisibleForTesting
protected HttpURLConnection getHttpURLConnection(String requestURLPath) throws IOException, URISyntaxException {
protected HttpURLConnection getHttpURLConnection(String requestURLPath) throws IOException, URISyntaxException{
URI inputURI = new URI(requestURLPath.replaceAll(" ", "%20"));
URL url = inputURI.normalize().toURL();
return (HttpURLConnection) url.openConnection();
Expand All @@ -170,8 +170,8 @@ public Response<String> fetch(String requestURLPath) {

return processResponse(urlConnection);

} catch (IOException | URISyntaxException ex) {
Timber.e(ex, "EXCEPTION %s", ex.toString());
} catch (IOException | URISyntaxException exception ) {
Timber.e(exception, "EXCEPTION %s", exception.toString());
return new Response<>(ResponseStatus.failure, null);
}
}
Expand Down Expand Up @@ -204,8 +204,8 @@ public Response<String> post(String postURLPath, String jsonPayload) {

return processResponse(urlConnection);

} catch (IOException | URISyntaxException ex) {
Timber.e(ex, "EXCEPTION: %s", ex.toString());
} catch (IOException | URISyntaxException exception) {
Timber.e(exception, "EXCEPTION: %s", exception.toString());
return new Response<>(ResponseStatus.failure, null);
}
}
Expand Down Expand Up @@ -285,15 +285,15 @@ public LoginResponse urlCanBeAccessWithGivenCredentials(String requestURL, Strin
Timber.e("Bad response from Dristhi. Status code: %s username: %s using %s ", statusCode, userName, url);
loginResponse = UNKNOWN_RESPONSE;
}
} catch (MalformedURLException | URISyntaxException e) {
Timber.e(e, "Failed to check credentials bad url %s", url);
} catch (MalformedURLException | URISyntaxException exception) {
Timber.e(exception, "Failed to check credentials bad url %s", url);
loginResponse = MALFORMED_URL;
} catch (SocketTimeoutException e) {
Timber.e(e, "SocketTimeoutException when authenticating %s", userName);
} catch (SocketTimeoutException exception) {
Timber.e(exception, "SocketTimeoutException when authenticating %s", userName);
loginResponse = TIMEOUT;
Timber.e(e, "Failed to check credentials of: %s using %s . Error: %s", userName, url, e.toString());
} catch (IOException e) {
Timber.e(e, "Failed to check credentials of: %s using %s . Error: %s", userName, url, e.toString());
Timber.e(exception, "Failed to check credentials of: %s using %s . Error: %s", userName, url, exception.toString());
} catch (IOException exception) {
Timber.e(exception, "Failed to check credentials of: %s using %s . Error: %s", userName, url, exception.toString());
loginResponse = NO_INTERNET_CONNECTIVITY;
} finally {
closeConnection(urlConnection);
Expand Down Expand Up @@ -329,8 +329,8 @@ public Response<String> fetchWithCredentials(String requestURL, String accessTok
}
return processResponse(urlConnection);

} catch (IOException | URISyntaxException ex) {
Timber.e(ex, "EXCEPTION %s", ex.toString());
} catch (IOException | URISyntaxException exception) {
Timber.e(exception, "EXCEPTION %s", exception.toString());
return new Response<>(ResponseStatus.failure, null);
}

Expand All @@ -356,16 +356,16 @@ private Response<String> processResponse(HttpURLConnection urlConnection) {

Timber.d("response string: %s using url %s", responseString, urlConnection.getURL());

} catch (MalformedURLException e) {
Timber.e(e, "%s %s", MALFORMED_URL, e.toString());
} catch (MalformedURLException exception) {
Timber.e(exception, "%s %s", MALFORMED_URL, exception.toString());
ResponseStatus.failure.setDisplayValue(ResponseErrorStatus.malformed_url.name());
return new Response<>(ResponseStatus.failure, null);
} catch (SocketTimeoutException e) {
Timber.e(e, "%s %s", TIMEOUT, e.toString());
} catch (SocketTimeoutException exception) {
Timber.e(exception, "%s %s", TIMEOUT, exception.toString());
ResponseStatus.failure.setDisplayValue(ResponseErrorStatus.timeout.name());
return new Response<>(ResponseStatus.failure, null);
} catch (IOException e) {
Timber.e(e, "%s %s", NO_INTERNET_CONNECTIVITY, e.toString());
} catch (IOException exception) {
Timber.e(exception, "%s %s", NO_INTERNET_CONNECTIVITY, exception.toString());
return new Response<>(ResponseStatus.failure, null);
} finally {
closeConnection(urlConnection);
Expand Down Expand Up @@ -438,14 +438,14 @@ public String httpImagePost(String urlString, ProfileImage image) {
reader.close();
}

} catch (ProtocolException e) {
Timber.e(e, "Protocol exception %s", e.toString());
} catch (SocketTimeoutException e) {
Timber.e(e, "SocketTimeout %s %s", TIMEOUT, e.toString());
} catch (MalformedURLException | URISyntaxException e) {
Timber.e(e, "MalformedUrl %s %s", MALFORMED_URL, e.toString());
} catch (IOException e) {
Timber.e(e, "IOException %s %s", NO_INTERNET_CONNECTIVITY, e.toString());
} catch (ProtocolException exception) {
Timber.e(exception, "Protocol exception %s", exception.toString());
} catch (SocketTimeoutException exception) {
Timber.e(exception, "SocketTimeout %s %s", TIMEOUT, exception.toString());
} catch (MalformedURLException | URISyntaxException exception) {
Timber.e(exception, "MalformedUrl %s %s", MALFORMED_URL, exception.toString());
} catch (IOException exception) {
Timber.e(exception, "IOException %s %s", NO_INTERNET_CONNECTIVITY, exception.toString());
} finally {

closeConnection(httpUrlConnection);
Expand Down Expand Up @@ -652,18 +652,18 @@ public AccountResponse oauth2authenticateCore(StringBuffer requestParamBuffer, S
return new AccountResponse(statusCode, accountError);

}
} catch (MalformedURLException | URISyntaxException e) {
Timber.e(e, "Failed to check credentials bad url %s", tokenEndpointURL);
} catch (MalformedURLException | URISyntaxException exception) {
Timber.e(exception, "Failed to check credentials bad url %s", tokenEndpointURL);
accountError = new AccountError(0, MALFORMED_URL.name());

} catch (SocketTimeoutException e) {
Timber.e(e, "SocketTimeoutException when authenticating");
} catch (SocketTimeoutException exception) {
Timber.e(exception, "SocketTimeoutException when authenticating");

accountError = new AccountError(0, TIMEOUT.name());

Timber.e(e, "Failed to check credentials using %s . Error: %s", tokenEndpointURL, e.toString());
} catch (IOException e) {
Timber.e(e, "Failed to check credentials using %s . Error: %s", tokenEndpointURL, e.toString());
Timber.e(exception, "Failed to check credentials using %s . Error: %s", tokenEndpointURL, exception.toString());
} catch (IOException exception) {
Timber.e(exception, "Failed to check credentials using %s . Error: %s", tokenEndpointURL, exception.toString());
accountError = new AccountError(0, NO_INTERNET_CONNECTIVITY.name());

} finally {
Expand Down Expand Up @@ -738,14 +738,14 @@ public LoginResponse fetchUserDetails(String requestURL, String oauthAccessToken
Timber.e("Bad response from Server. Status code: %s using %s ", statusCode, url);
loginResponse = UNKNOWN_RESPONSE;
}
} catch (MalformedURLException | URISyntaxException e) {
Timber.e(e, "Failed to check credentials bad url %s", url);
} catch (MalformedURLException | URISyntaxException exception) {
Timber.e(exception, "Failed to check credentials bad url %s", url);
loginResponse = MALFORMED_URL;
} catch (SocketTimeoutException e) {
Timber.e(e, "SocketTimeoutException when authenticating");
} catch (SocketTimeoutException exception) {
Timber.e(exception, "SocketTimeoutException when authenticating");
loginResponse = TIMEOUT;
} catch (IOException e) {
Timber.e(e, "Failed to connect to %s check, check internet connection. Error: %s", url, e.toString());
} catch (IOException exception) {
Timber.e(exception, "Failed to connect to %s check, check internet connection. Error: %s", url, exception.toString());
loginResponse = NO_INTERNET_CONNECTIVITY;
} finally {

Expand Down Expand Up @@ -837,8 +837,8 @@ public Response<DownloadStatus> downloadFromURL(String downloadURL_, String file
return new Response<DownloadStatus>(ResponseStatus.failure, DownloadStatus.failedDownloaded);
}

} catch (IOException | URISyntaxException e) {
Timber.d(e, "DownloadFormService");
} catch (IOException | URISyntaxException exception) {
Timber.d(exception, "DownloadFormService");
return new Response<DownloadStatus>(ResponseStatus.success, DownloadStatus.failedDownloaded);
} finally {

Expand Down Expand Up @@ -941,9 +941,9 @@ public boolean verifyAuthorization() {
return false;
}

} catch (IOException | URISyntaxException e) {
} catch (IOException | URISyntaxException exception ) {

Timber.e(e);
Timber.e(exception);

} finally {

Expand Down Expand Up @@ -996,8 +996,8 @@ public boolean verifyAuthorizationLegacy() {
Timber.i("User is Authorized");
}

} catch (IOException | URISyntaxException e) {
Timber.e(e);
} catch (IOException | URISyntaxException exception) {
Timber.e(exception);
} finally {

closeConnection(urlConnection);
Expand Down Expand Up @@ -1032,8 +1032,8 @@ public AccountConfiguration fetchOAuthConfiguration() {
return gson.fromJson(responseString, AccountConfiguration.class);
}

} catch (Exception e) {
Timber.e(e);
} catch (Exception exception) {
Timber.e(exception);
} finally {

closeConnection(urlConnection);
Expand All @@ -1047,8 +1047,8 @@ private void closeConnection(HttpURLConnection urlConnection) {
if (urlConnection != null) {
try {
urlConnection.disconnect();
} catch (Exception ex) {
Timber.e(ex, "Error closing input HttpUrlConnection");
} catch (Exception exception) {
Timber.e(exception, "Error closing input HttpUrlConnection");
}

}
Expand All @@ -1060,9 +1060,9 @@ private void closeIOStream(Closeable inputStream) {

try {
inputStream.close();
} catch (IOException ex) {
} catch (IOException exception) {

Timber.e(ex, "Error closing input stream");
Timber.e(exception, "Error closing input stream");
}

}
Expand Down
26 changes: 16 additions & 10 deletions opensrp-app/src/main/java/org/smartregister/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,18 +613,24 @@ public String getName() {
public static String getUserInitials() {
String initials = "Me";
String preferredName = getPrefferedName();

if (StringUtils.isNotBlank(preferredName)) {
preferredName = preferredName.trim();
String[] preferredNameArray = preferredName.split(" ");
initials = "";
if (preferredNameArray.length > 1) {
initials = String.valueOf(preferredNameArray[0].charAt(0)) + String.valueOf(preferredNameArray[1].charAt(0));
} else if (preferredNameArray.length == 1) {
initials = String.valueOf(preferredNameArray[0].charAt(0));
try {
if (StringUtils.isNotBlank(preferredName)) {
preferredName = preferredName.trim();
String[] preferredNameArray = preferredName.split(" ");
initials = "";
if (preferredNameArray.length > 1) {
initials = String.valueOf(preferredNameArray[0].charAt(0)) + String.valueOf(preferredNameArray[1].charAt(0));
} else if (preferredNameArray.length == 1) {
initials = String.valueOf(preferredNameArray[0].charAt(0));
}
}
return initials;
}
catch (ArrayIndexOutOfBoundsException e)
{
Timber.e("Index out of Bounds "+e.getMessage());
return "";
}
return initials;
}

public static AllSharedPreferences getAllSharedPreferences() {
Expand Down

0 comments on commit cfff3ef

Please sign in to comment.