Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance handling for network error type. #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.javiersantos.appupdater;

import android.util.Log;
import android.util.Pair;

import com.github.javiersantos.appupdater.objects.Update;

Expand Down Expand Up @@ -34,7 +35,7 @@ public ParserJSON(String url) {

}

public Update parse(){
public Pair<Update,Exception> parse(){

try {
JSONObject json = readJsonFromUrl();
Expand All @@ -53,14 +54,14 @@ public Update parse(){
}
URL url = new URL(json.getString(KEY_URL).trim());
update.setUrlToDownload(url);
return update;
return new Pair(update,null);
} catch (IOException e) {
Log.e("AppUpdater", "The server is down or there isn't an active Internet connection.", e);
return new Pair(null,e);
} catch (JSONException e) {
Log.e("AppUpdater", "The JSON updater file is mal-formatted. AppUpdate can't check for updates.");
return new Pair(null,e);
}

return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.Nullable;
import android.util.Log;
import android.util.Pair;

import com.github.javiersantos.appupdater.objects.Update;

Expand Down Expand Up @@ -32,7 +33,7 @@ public ParserXML(String url) {
}

@Nullable
public Update parse() {
public Pair<Update,Exception> parse() {
SAXParserFactory factory = SAXParserFactory.newInstance();

InputStream inputStream = null;
Expand All @@ -43,19 +44,19 @@ public Update parse() {
SAXParser parser = factory.newSAXParser();
HandlerXML handler = new HandlerXML();
parser.parse(inputStream, handler);
return handler.getUpdate();
return new Pair(handler.getUpdate(),null);
} catch (ParserConfigurationException | SAXException e) {
Log.e("AppUpdater", "The XML updater file is mal-formatted. AppUpdate can't check for updates.", e);
return null;
return new Pair(null,e);
} catch (FileNotFoundException | UnknownHostException | ConnectException e) {
Log.e("AppUpdater", "The XML updater file is invalid or is down. AppUpdate can't check for updates.");
return null;
return new Pair(null,e);
} catch (IOException e) {
Log.e("AppUpdater", "I/O error. AppUpdate can't check for updates.", e);
return null;
return new Pair(null,e);
} catch (Exception e) {
Log.e("AppUpdater", "The server is down or there isn't an active Internet connection.", e);
return null;
return new Pair(null,e);
} finally {
if (inputStream != null) {
try {
Expand Down
185 changes: 100 additions & 85 deletions library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,124 @@

import android.content.Context;
import android.os.AsyncTask;
import android.util.Pair;

import com.github.javiersantos.appupdater.enums.AppUpdaterError;
import com.github.javiersantos.appupdater.enums.UpdateFrom;
import com.github.javiersantos.appupdater.objects.GitHub;
import com.github.javiersantos.appupdater.objects.Update;

import java.lang.ref.WeakReference;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

class UtilsAsync {

static class LatestAppVersion extends AsyncTask<Void, Void, Update> {
private WeakReference<Context> contextRef;
private LibraryPreferences libraryPreferences;
private Boolean fromUtils;
private UpdateFrom updateFrom;
private GitHub gitHub;
private String xmlOrJsonUrl;
private AppUpdater.LibraryListener listener;
static class LatestAppVersion extends AsyncTask<Void, Void, Update> {
private WeakReference<Context> contextRef;
private LibraryPreferences libraryPreferences;
private Boolean fromUtils;
private UpdateFrom updateFrom;
private GitHub gitHub;
private String xmlOrJsonUrl;
private AppUpdater.LibraryListener listener;

public LatestAppVersion(Context context, Boolean fromUtils, UpdateFrom updateFrom, GitHub gitHub, String xmlOrJsonUrl, AppUpdater.LibraryListener listener) {
this.contextRef = new WeakReference<>(context);
this.libraryPreferences = new LibraryPreferences(context);
this.fromUtils = fromUtils;
this.updateFrom = updateFrom;
this.gitHub = gitHub;
this.xmlOrJsonUrl = xmlOrJsonUrl;
this.listener = listener;
}
public LatestAppVersion(Context context, Boolean fromUtils, UpdateFrom updateFrom, GitHub gitHub, String xmlOrJsonUrl, AppUpdater.LibraryListener listener) {
this.contextRef = new WeakReference<>(context);
this.libraryPreferences = new LibraryPreferences(context);
this.fromUtils = fromUtils;
this.updateFrom = updateFrom;
this.gitHub = gitHub;
this.xmlOrJsonUrl = xmlOrJsonUrl;
this.listener = listener;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
@Override
protected void onPreExecute() {
super.onPreExecute();

Context context = contextRef.get();
if (context == null || listener == null) {
cancel(true);
} else if (UtilsLibrary.isNetworkAvailable(context)) {
if (!fromUtils && !libraryPreferences.getAppUpdaterShow()) {
cancel(true);
} else {
if (updateFrom == UpdateFrom.GITHUB && !GitHub.isGitHubValid(gitHub)) {
listener.onFailed(AppUpdaterError.GITHUB_USER_REPO_INVALID);
cancel(true);
} else if (updateFrom == UpdateFrom.XML && (xmlOrJsonUrl == null || !UtilsLibrary.isStringAnUrl(xmlOrJsonUrl))) {
listener.onFailed(AppUpdaterError.XML_URL_MALFORMED);
Context context = contextRef.get();
if (context == null || listener == null) {
cancel(true);
} else if (UtilsLibrary.isNetworkAvailable(context)) {
if (!fromUtils && !libraryPreferences.getAppUpdaterShow()) {
cancel(true);
} else {
if (updateFrom == UpdateFrom.GITHUB && !GitHub.isGitHubValid(gitHub)) {
listener.onFailed(AppUpdaterError.GITHUB_USER_REPO_INVALID);
cancel(true);
} else if (updateFrom == UpdateFrom.XML && (xmlOrJsonUrl == null || !UtilsLibrary.isStringAnUrl(xmlOrJsonUrl))) {
listener.onFailed(AppUpdaterError.XML_URL_MALFORMED);

cancel(true);
} else if (updateFrom == UpdateFrom.JSON && (xmlOrJsonUrl == null || !UtilsLibrary.isStringAnUrl(xmlOrJsonUrl))) {
listener.onFailed(AppUpdaterError.JSON_URL_MALFORMED);
cancel(true);
} else if (updateFrom == UpdateFrom.JSON && (xmlOrJsonUrl == null || !UtilsLibrary.isStringAnUrl(xmlOrJsonUrl))) {
listener.onFailed(AppUpdaterError.JSON_URL_MALFORMED);

cancel(true);
}
}
} else {
listener.onFailed(AppUpdaterError.NETWORK_NOT_AVAILABLE);
cancel(true);
}
}
cancel(true);
}
}
} else {
listener.onFailed(AppUpdaterError.NETWORK_NOT_AVAILABLE);
cancel(true);
}
}

@Override
protected Update doInBackground(Void... voids) {
try {
if (updateFrom == UpdateFrom.XML || updateFrom == UpdateFrom.JSON) {
Update update = UtilsLibrary.getLatestAppVersion(updateFrom, xmlOrJsonUrl);
if (update != null) {
return update;
} else {
AppUpdaterError error = updateFrom == UpdateFrom.XML ? AppUpdaterError.XML_ERROR
: AppUpdaterError.JSON_ERROR;
@Override
protected Update doInBackground(Void... voids) {
try {
if (updateFrom == UpdateFrom.XML || updateFrom == UpdateFrom.JSON) {
Pair<Update, Exception> result = UtilsLibrary.getLatestAppVersion(updateFrom, xmlOrJsonUrl);
Update update = result.first;
Exception ex = result.second;
if (update != null) {
return update;
} else if (ex instanceof ConnectException
|| ex instanceof SocketTimeoutException
|| ex instanceof UnknownHostException
) {
if (listener != null) {
listener.onFailed(AppUpdaterError.NETWORK_NOT_AVAILABLE);
}
cancel(true);
return null;
} else {
AppUpdaterError error = updateFrom == UpdateFrom.XML ? AppUpdaterError.XML_ERROR
: AppUpdaterError.JSON_ERROR;

if (listener != null) {
listener.onFailed(error);
}
cancel(true);
return null;
}
} else {
Context context = contextRef.get();
if (context != null) {
return UtilsLibrary.getLatestAppVersionStore(context, updateFrom, gitHub);
} else {
cancel(true);
return null;
}
}
} catch (Exception ex) {
cancel(true);
return null;
}
}
if (listener != null) {
listener.onFailed(error);
}
cancel(true);
return null;
}
} else {
Context context = contextRef.get();
if (context != null) {
return UtilsLibrary.getLatestAppVersionStore(context, updateFrom, gitHub);
} else {
cancel(true);
return null;
}
}
} catch (Exception ex) {
cancel(true);
return null;
}
}

@Override
protected void onPostExecute(Update update) {
super.onPostExecute(update);
@Override
protected void onPostExecute(Update update) {
super.onPostExecute(update);

if (listener != null) {
if (UtilsLibrary.isStringAVersion(update.getLatestVersion())) {
listener.onSuccess(update);
} else {
listener.onFailed(AppUpdaterError.UPDATE_VARIES_BY_DEVICE);
}
}
}
}
if (listener != null) {
if (UtilsLibrary.isStringAVersion(update.getLatestVersion())) {
listener.onSuccess(update);
} else {
listener.onFailed(AppUpdaterError.UPDATE_VARIES_BY_DEVICE);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.github.javiersantos.appupdater.enums.Duration;
import com.github.javiersantos.appupdater.enums.UpdateFrom;
Expand Down Expand Up @@ -274,7 +275,7 @@ private static String getVersion(UpdateFrom updateFrom, Boolean isAvailable, Str
return version;
}

static Update getLatestAppVersion(UpdateFrom updateFrom, String url) {
static Pair<Update,Exception> getLatestAppVersion(UpdateFrom updateFrom, String url) {
if (updateFrom == UpdateFrom.XML){
ParserXML parser = new ParserXML(url);
return parser.parse();
Expand Down