Browse Source

Add InvalidSessionException, refactoring

pull/70/head
Ezerous 5 years ago
parent
commit
2af55e1aca
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  3. 36
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  4. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  5. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java
  6. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java
  7. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java
  8. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  9. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java
  10. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java
  11. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java
  12. 4
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  13. 13
      app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java
  14. 50
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  15. 1
      app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java
  16. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkResultCodes.java
  17. 13
      app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkTask.java
  18. 7
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java
  19. 2
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  20. 1
      app/src/main/res/layout/fragment_unread.xml

2
app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java

@ -30,7 +30,7 @@ import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.Board;
import gr.thmmy.mthmmy.model.Category;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.views.CustomRecyclerView;

2
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -26,7 +26,7 @@ import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.views.CustomRecyclerView;

36
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

@ -29,9 +29,10 @@ import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.TopicSummary;
import gr.thmmy.mthmmy.session.InvalidSessionException;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.session.ValidateSessionTask;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.views.CustomRecyclerView;
@ -266,6 +267,8 @@ public class UnreadFragment extends BaseFragment {
hideProgressUI();
if (resultCode == NetworkResultCodes.NETWORK_ERROR)
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
else if (resultCode == SessionManager.INVALID_SESSION)
Toast.makeText(getContext(), "Session verification failed. Please try logging in again.", Toast.LENGTH_LONG).show();
else
Toast.makeText(getContext(), "Unexpected error," +
" please contact the developers with the details", Toast.LENGTH_LONG).show();
@ -278,7 +281,10 @@ public class UnreadFragment extends BaseFragment {
}
@Override
protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException {
protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException, InvalidSessionException {
if(!document.select("td:containsOwn(Only registered members are allowed to access this section.)").isEmpty())
throw new InvalidSessionException();
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
ArrayList<TopicSummary> fetchedTopicSummaries = new ArrayList<>();
if (!unread.isEmpty()) {
@ -338,42 +344,42 @@ public class UnreadFragment extends BaseFragment {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
private void onMarkReadTaskFinished(int resultCode, Boolean isSessionVerified) {
private void onMarkReadTaskFinished(int resultCode, Void isSessionVerified) {
hideProgressUI();
if (resultCode == NetworkResultCodes.SUCCESSFUL) {
if (!isSessionVerified){
Toast.makeText(getContext(), "Session verification failed", Toast.LENGTH_SHORT).show();
startValidateSessionTask();
}
else
if (resultCode == NetworkResultCodes.SUCCESSFUL)
startUnreadTask();
}
else{
hideProgressUI();
if (resultCode == NetworkResultCodes.NETWORK_ERROR)
Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show();
else if (resultCode == SessionManager.INVALID_SESSION){
Toast.makeText(getContext(), "Session verification failed. Please try logging out and back in again", Toast.LENGTH_LONG).show();
startValidateSessionTask();
}
else
Toast.makeText(getContext(), "Unexpected error," +
" please contact the developers with the details", Toast.LENGTH_LONG).show();
}
}
private class MarkReadTask extends NewParseTask<Boolean> {
MarkReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Boolean> onParseTaskFinishedListener) {
private class MarkReadTask extends NewParseTask<Void> {
MarkReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected Boolean parse(Document document, Response response) throws ParseException {
protected Void parse(Document document, Response response) throws ParseException {
Elements sessionVerificationFailed = document.select("td:containsOwn(Session " +
"verification failed. Please try logging out and back in again, and then try " +
"again.), td:containsOwn(Η επαλήθευση συνόδου απέτυχε. Παρακαλούμε κάντε " +
"αποσύνδεση, επανασύνδεση και ξαναδοκιμάστε.)");
return sessionVerificationFailed.isEmpty();
if(!sessionVerificationFailed.isEmpty())
throw new InvalidSessionException();
return null;
}
@Override
protected int getResultCode(Response response, Boolean isSessionVerified) {
protected int getResultCode(Response response, Void v) {
return NetworkResultCodes.SUCCESSFUL;
}
}

2
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -45,8 +45,8 @@ import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.PostSummary;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.Parcel;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.ui.CenterVerticalSpan;

4
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java

@ -4,8 +4,8 @@ import org.jsoup.nodes.Document;
import java.io.IOException;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

2
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java

@ -21,7 +21,7 @@ import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.Shoutbox;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel;
import gr.thmmy.mthmmy.views.CustomRecyclerView;
import gr.thmmy.mthmmy.views.editorview.EditorView;

2
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java

@ -7,7 +7,7 @@ import java.util.ArrayList;
import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.Shoutbox;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -54,7 +54,7 @@ import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.HTMLUtils;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import gr.thmmy.mthmmy.viewmodel.TopicViewModel;
import gr.thmmy.mthmmy.views.CustomLinearLayoutManager;

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java

@ -4,8 +4,8 @@ import org.jsoup.nodes.Document;
import java.io.IOException;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java

@ -2,8 +2,8 @@ package gr.thmmy.mthmmy.activities.topic.tasks;
import org.jsoup.nodes.Document;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import okhttp3.Response;
public class RemoveVoteTask extends NetworkTask<Void> {

4
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java

@ -4,8 +4,8 @@ import org.jsoup.nodes.Document;
import java.io.IOException;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

4
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -463,8 +463,7 @@ public abstract class BaseActivity extends AppCompatActivity {
private void updateDrawer() {
if (drawer != null) {
if (!sessionManager.isLoggedIn()) //When logged out or if user is guest
{
if (!sessionManager.isLoggedIn()){ //When logged out or if user is guest
drawer.removeItem(DOWNLOADS_ID);
drawer.removeItem(UPLOAD_ID);
loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login
@ -486,7 +485,6 @@ public abstract class BaseActivity extends AppCompatActivity {
}
accountHeader.updateProfile(profileDrawerItem);
drawer.updateItem(loginLogoutItem);
}
}

13
app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java

@ -0,0 +1,13 @@
package gr.thmmy.mthmmy.session;
public class InvalidSessionException extends RuntimeException {
public InvalidSessionException() {}
public InvalidSessionException(String message) {
super(message);
}
public InvalidSessionException(String message, Throwable cause) {
super(message, cause);
}
}

50
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -43,15 +43,16 @@ public class SessionManager {
private static final String baseMarkAllAsReadLink = "https://www.thmmy.gr/smf/index.php?action=markasread;sa=all;sesc=";
private static final String guestName = "Guest";
//Response Codes
public static final int SUCCESS = 0;
public static final int FAILURE = 1; //Generic Error
public static final int WRONG_USER = 2;
public static final int WRONG_PASSWORD = 3;
public static final int CANCELLED = 4;
public static final int CONNECTION_ERROR = 5;
public static final int EXCEPTION = 6;
public static final int BANNED_USER = 7;
//Response Codes - make sure they do not overlap with NetworkResultCodes, just in case
public static final int SUCCESS = 20;
public static final int FAILURE = 21; //Generic Error
public static final int WRONG_USER = 22;
public static final int WRONG_PASSWORD = 23;
public static final int CANCELLED = 24;
public static final int CONNECTION_ERROR = 25;
public static final int EXCEPTION = 26;
public static final int BANNED_USER = 27;
public static final int INVALID_SESSION = 28;
// Client & Cookies
private final OkHttpClient client;
@ -63,12 +64,12 @@ public class SessionManager {
private final SharedPreferences draftsPrefs;
private static final String USERNAME = "Username";
private static final String USER_ID = "UserID";
private static final String AVATAR_LINK = "AvatarLink";
private static final String HAS_AVATAR = "HasAvatar";
public static final String AVATAR_LINK = "AvatarLink";
public static final String HAS_AVATAR = "HasAvatar";
private static final String SESC = "Sesc";
private static final String LOGOUT_LINK = "LogoutLink";
private static final String MARK_ALL_AS_READ_LINK = "MarkAllAsReadLink";
private static final String LOGGED_IN = "LoggedIn";
public static final String LOGGED_IN = "LoggedIn";
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault";
//Constructor
@ -224,8 +225,7 @@ public class SessionManager {
Document document = Jsoup.parse(response.body().string());
Elements loginButton = document.select("[value=Login]"); //Attempt to find login button
if (!loginButton.isEmpty()) //If login button exists, logout was successful
{
if (!loginButton.isEmpty()){ //If login button exists, logout was successful
Timber.i("Logout successful!");
return SUCCESS;
} else {
@ -245,6 +245,16 @@ public class SessionManager {
}
}
public void clearSessionData() {
cookieJar.clear();
sharedPrefs.edit().clear().apply(); //Clear session data
sharedPrefs.edit().putString(USERNAME, guestName).apply();
sharedPrefs.edit().putInt(USER_ID, -1).apply();
sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out
draftsPrefs.edit().clear().apply(); //Clear saved drafts
Timber.i("Session data cleared.");
}
public void refreshSescFromUrl(String url){
String sesc = extractSescFromLink(url);
if(sesc!=null){
@ -356,16 +366,6 @@ public class SessionManager {
}
private void clearSessionData() {
cookieJar.clear();
sharedPrefs.edit().clear().apply(); //Clear session data
sharedPrefs.edit().putString(USERNAME, guestName).apply();
sharedPrefs.edit().putInt(USER_ID, -1).apply();
sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out
draftsPrefs.edit().clear().apply(); //Clear saved drafts
Timber.i("Session data cleared.");
}
private void setLoginScreenAsDefault(boolean b){
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply();
}
@ -421,7 +421,6 @@ public class SessionManager {
return -1;
}
@Nullable
private String extractAvatarLink(@NonNull Document doc) {
Elements avatar = doc.getElementsByClass("avatar");
@ -461,5 +460,4 @@ public class SessionManager {
return baseMarkAllAsReadLink + sesc;
}
//----------------------------------OTHER FUNCTIONS END-----------------------------------------
}

1
app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.utils;
public class Parcel<T> {
private int resultCode;
private T data;

2
app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java → app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkResultCodes.java

@ -1,4 +1,4 @@
package gr.thmmy.mthmmy.utils;
package gr.thmmy.mthmmy.utils.networking;
public class NetworkResultCodes {
/**

13
app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java → app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkTask.java

@ -1,4 +1,4 @@
package gr.thmmy.mthmmy.utils;
package gr.thmmy.mthmmy.utils.networking;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
@ -10,6 +10,10 @@ import java.io.IOException;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.session.InvalidSessionException;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.Parcel;
import gr.thmmy.mthmmy.utils.crashreporting.CrashReporter;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import okhttp3.OkHttpClient;
@ -63,7 +67,12 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
.getString(R.string.pref_privacy_crashlytics_enable_key), false))
CrashReporter.reportForumInfo(Jsoup.parse(responseBodyString));
return new Parcel<>(NetworkResultCodes.PARSE_ERROR, null);
} catch (Exception e) {
} catch (InvalidSessionException ise) {
//TODO: Uncomment the lines below when UI is ready to auto-adjust to changes in session data
// BaseApplication.getInstance().getSessionManager().clearSessionData();
// BaseApplication.getInstance().getSessionManager().guestLogin();
return new Parcel<>(SessionManager.INVALID_SESSION, null);
}catch (Exception e) {
Timber.e(e);
return new Parcel<>(NetworkResultCodes.PERFORM_TASK_ERROR, null);
}

7
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java

@ -2,7 +2,8 @@ package gr.thmmy.mthmmy.utils.parsing;
import org.jsoup.nodes.Document;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.session.InvalidSessionException;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import okhttp3.Response;
public abstract class NewParseTask<T> extends NetworkTask<T> {
@ -22,8 +23,10 @@ public abstract class NewParseTask<T> extends NetworkTask<T> {
protected final T performTask(Document document, Response response) {
try {
return parse(document, response);
} catch (InvalidSessionException ise) {
throw ise;
} catch (Exception e) {
throw new ParseException("Parse failed.", e);
throw new ParseException("Parsing failed", e);
}
}

2
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -30,7 +30,7 @@ import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.networking.NetworkTask;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber;

1
app/src/main/res/layout/fragment_unread.xml

@ -61,6 +61,7 @@
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margins"
android:layout_marginBottom="@dimen/fab_margins"
android:visibility="gone"
app:layout_behavior="gr.thmmy.mthmmy.utils.ui.ScrollAwareFABBehavior"
app:srcCompat="@drawable/ic_mark_as_read" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading…
Cancel
Save