diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 546b2db3..7cd9ef63 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -27,6 +27,7 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.exceptions.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import mthmmy.utils.Report; import okhttp3.HttpUrl; @@ -159,12 +160,15 @@ public class RecentFragment extends BaseFragment { document = Jsoup.parse(response.body().string()); parse(document); return 0; - } catch (IOException e) { - Report.d(TAG, "Network Error", e); + } catch (ParseException e) { + Report.e(TAG, "ParseException", e); return 1; - } catch (Exception e) { - Report.d(TAG, "Exception", e); + } catch (IOException e) { + Report.i(TAG, "Network Error", e); return 2; + } catch (Exception e) { + Report.e(TAG, "Exception", e); + return 3; } } @@ -174,14 +178,14 @@ public class RecentFragment extends BaseFragment { if (result == 0) recentAdapter.notifyDataSetChanged(); - else if (result == 1) + else if (result == 2) Toast.makeText(getActivity(), "Network error", Toast.LENGTH_SHORT).show(); progressBar.setVisibility(ProgressBar.INVISIBLE); swipeRefreshLayout.setRefreshing(false); } - private void parse(Document document) { + private void parse(Document document) throws ParseException { Elements recent = document.select("#block8 :first-child div"); if (!recent.isEmpty()) { topicSummaries.clear(); @@ -195,27 +199,23 @@ public class RecentFragment extends BaseFragment { Matcher matcher = pattern.matcher(lastUser); if (matcher.find()) lastUser = matcher.group(1); - else { - Report.e(TAG, "Parsing failed (lastUser)!"); - return; - } + else + throw new ParseException("Parsing failed (lastUser)"); String dateTime = recent.get(i + 2).text(); pattern = Pattern.compile("\\[(.*)\\]"); matcher = pattern.matcher(dateTime); if (matcher.find()) dateTime = matcher.group(1); - else { - Report.e(TAG, "Parsing failed (dateTime)!"); - return; - } + else + throw new ParseException("Parsing failed (dateTime)"); topicSummaries.add(new TopicSummary(link, title, lastUser, dateTime)); } return; } - Report.e(TAG, "Parsing failed!"); + throw new ParseException("Parsing failed"); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java b/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java index 573ab018..3e811134 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java +++ b/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java @@ -16,12 +16,10 @@ import java.io.File; import gr.thmmy.mthmmy.R; import mthmmy.utils.Report; -import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD; import static gr.thmmy.mthmmy.services.DownloadService.COMPLETED; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_ID; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_STATE; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_EXTENSION; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_NAME; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TEXT; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TICKER; @@ -51,14 +49,13 @@ public class Receiver extends BroadcastReceiver { builder.setContentTitle(title) .setContentText(text) .setTicker(ticker) - .setAutoCancel(true) //??? + .setAutoCancel(true) .setSmallIcon(R.mipmap.ic_launcher); if (state.equals(STARTED)) builder.setOngoing(true); else if (state.equals(COMPLETED)) { String fileName = extras.getString(EXTRA_FILE_NAME, "NONE"); - String extension = extras.getString(EXTRA_FILE_EXTENSION, "extension"); File file = new File(SAVE_DIR, fileName); if (file.exists()) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java b/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java index 5eca8376..7339fa73 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java @@ -38,7 +38,6 @@ public class DownloadService extends IntentService { public static final String EXTRA_DOWNLOAD_ID = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_ID"; public static final String EXTRA_DOWNLOAD_STATE = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_STATE"; public static final String EXTRA_FILE_NAME = "gr.thmmy.mthmmy.services.extra.FILE_NAME"; - public static final String EXTRA_FILE_EXTENSION = "gr.thmmy.mthmmy.services.extra.FILE_EXTENSION"; public static final String EXTRA_NOTIFICATION_TITLE = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TITLE"; public static final String EXTRA_NOTIFICATION_TEXT = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TEXT"; public static final String EXTRA_NOTIFICATION_TICKER = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TICKER"; @@ -100,7 +99,7 @@ public class DownloadService extends IntentService { OkHttpClient client = BaseApplication.getInstance().getClient(); BufferedSink sink = null; String fileName = "file"; - String extension = "extension"; + int downloadId = sDownloadId; sDownloadId++; @@ -132,10 +131,8 @@ public class DownloadService extends IntentService { nameFormat = fileName + "(%d)"; } else - { nameFormat = tokens[0] + "(%d)." + tokens[1]; - extension = tokens[1]; - } + @@ -152,26 +149,26 @@ public class DownloadService extends IntentService { fileName = file.getName(); Report.v(TAG, "Started saving file " + fileName); - sendNotification(downloadId, STARTED, fileName, extension); + sendNotification(downloadId, STARTED, fileName); sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.flush(); Report.i(TAG, "Download OK!"); - sendNotification(downloadId, COMPLETED, fileName, extension); + sendNotification(downloadId, COMPLETED, fileName); } else Report.e(TAG, "Response not a binary file!"); } catch (FileNotFoundException e){ - Report.e(TAG, "FileNotFound", e); Report.i(TAG, "Download failed..."); - sendNotification(downloadId, FAILED, fileName, extension); + Report.e(TAG, "FileNotFound", e); + sendNotification(downloadId, FAILED, fileName); } catch (IOException e){ - Report.e(TAG, "IOException", e); Report.i(TAG, "Download failed..."); - sendNotification(downloadId, FAILED, fileName, extension); + Report.e(TAG, "IOException", e); + sendNotification(downloadId, FAILED, fileName); } finally { if (sink!= null) { try { @@ -183,7 +180,7 @@ public class DownloadService extends IntentService { } } - private void sendNotification(int downloadId, String type, @NonNull String fileName, String fileExtension) + private void sendNotification(int downloadId, String type, @NonNull String fileName) { Intent intent = new Intent(ACTION_DOWNLOAD); switch (type) { @@ -213,7 +210,6 @@ public class DownloadService extends IntentService { intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId); intent.putExtra(EXTRA_DOWNLOAD_STATE, type); intent.putExtra(EXTRA_FILE_NAME, fileName); - intent.putExtra(EXTRA_FILE_EXTENSION, fileExtension); sendBroadcast(intent); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index d47e4969..2321b88a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import gr.thmmy.mthmmy.utils.exceptions.ParseException; import mthmmy.utils.Report; import okhttp3.Cookie; import okhttp3.FormBody; @@ -56,12 +57,12 @@ public class SessionManager { //Shared Preferences & its keys private SharedPreferences sharedPrefs; - public static final String USERNAME = "Username"; - public static final String AVATAR_LINK = "AvatarLink"; - public static final String HAS_AVATAR = "HasAvatar"; - public static final String LOGOUT_LINK = "LogoutLink"; - public static final String LOGGED_IN = "LoggedIn"; - public static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; + private static final String USERNAME = "Username"; + private static final String AVATAR_LINK = "AvatarLink"; + private static final String HAS_AVATAR = "HasAvatar"; + private static final String LOGOUT_LINK = "LogoutLink"; + private static final String LOGGED_IN = "LoggedIn"; + private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; //Constructor public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, @@ -290,10 +291,11 @@ public class SessionManager { Report.i(TAG, "Session data cleared."); } - @Nullable - private String extractUserName(@NonNull Document doc) { + @NonNull + private String extractUserName(@NonNull Document doc){ //Scribbles2 Theme Elements user = doc.select("div[id=myuser] > h3"); + String userName = null; if (user.size() == 1) { String txt = user.first().ownText(); @@ -301,26 +303,26 @@ public class SessionManager { Pattern pattern = Pattern.compile(", (.*?),"); Matcher matcher = pattern.matcher(txt); if (matcher.find()) - return matcher.group(1); + userName = matcher.group(1); } - else - { + else { //Helios_Multi and SMF_oneBlue user = doc.select("td.smalltext[width=100%] b"); if (user.size() == 1) - return user.first().ownText(); - else - { + userName = user.first().ownText(); + else { //SMF Default Theme user = doc.select("td.titlebg2[height=32] b"); if (user.size() == 1) - return user.first().ownText(); + userName = user.first().ownText(); } } + + if(userName != null && !userName.isEmpty()) + return userName; - - Report.e(TAG, "Extracting username failed!"); - return null; + Report.e(TAG, "ParseException", new ParseException("Parsing failed(username extraction)")); + return "User"; //return a default username } @@ -334,15 +336,18 @@ public class SessionManager { return null; } - @Nullable + @NonNull private String extractLogoutLink(@NonNull Document doc) { Elements logoutLink = doc.select("a[href^=https://www.thmmy.gr/smf/index.php?action=logout;sesc=]"); if (!logoutLink.isEmpty()) - return logoutLink.first().attr("href"); - - Report.e(TAG, "Extracting logout link failed!"); - return null; + { + String link = logoutLink.first().attr("href"); + if(link != null && !link.isEmpty()) + return link; + } + Report.e(TAG, "ParseException", new ParseException("Parsing failed(logoutLink extraction)")); + return "https://www.thmmy.gr/smf/index.php?action=logout"; //return a default link } //----------------------------------OTHER FUNCTIONS END----------------------------------------- diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java b/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java new file mode 100644 index 00000000..b0947f04 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java @@ -0,0 +1,13 @@ +package gr.thmmy.mthmmy.utils.exceptions; + +/** + * ParseException is to be used for errors while parsing. + */ +public class ParseException extends Exception { + public ParseException() {} + + public ParseException(String message) + { + super(message); + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/UnknownException.java b/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/UnknownException.java new file mode 100644 index 00000000..fc0e7f65 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/UnknownException.java @@ -0,0 +1,14 @@ +package gr.thmmy.mthmmy.utils.exceptions; + +/** + * UnknownException is thrown upon an error (see Report.java in release), when no other specific + * exception is set, to report to FireBase. + */ +public class UnknownException extends Exception { + public UnknownException() {} + + public UnknownException(String message) + { + super(message); + } +} diff --git a/app/src/release/java/mthmmy.utils/Report.java b/app/src/release/java/mthmmy.utils/Report.java index ce2ea89d..138afd92 100644 --- a/app/src/release/java/mthmmy.utils/Report.java +++ b/app/src/release/java/mthmmy.utils/Report.java @@ -2,6 +2,8 @@ package mthmmy.utils; import com.google.firebase.crash.FirebaseCrash; +import gr.thmmy.mthmmy.utils.exceptions.UnknownException; + public class Report { @@ -68,6 +70,8 @@ public class Report private static void log(String level, String TAG, String message) { FirebaseCrash.log(level + "/" + TAG + ": " + message); + if(level.equals("E")||level.equals("WTF")) //report only serious exceptions + FirebaseCrash.report(new UnknownException("UnknownException")); } private static void exception(String level, String TAG, String message, Throwable tr)