Browse Source

Improved Report system, minor fixed in SessionManager, tiny tweaks

pull/24/head
Ezerous 8 years ago
parent
commit
9dc2064ac1
  1. 30
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  2. 5
      app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java
  3. 22
      app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java
  4. 47
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  5. 13
      app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java
  6. 14
      app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/UnknownException.java
  7. 4
      app/src/release/java/mthmmy.utils/Report.java

30
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");
}
}

5
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()) {

22
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);
}

47
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
@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-----------------------------------------

13
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);
}
}

14
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);
}
}

4
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)

Loading…
Cancel
Save