From d74331b05dc94c9c259290815efc30a4247bd2f4 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Wed, 22 Feb 2017 19:40:46 +0200 Subject: [PATCH] Fix for embedded videos and reversed order in user stats. --- .../activities/board/BoardActivity.java | 2 +- .../downloads/DownloadsActivity.java | 2 +- .../profile/stats/StatsFragment.java | 3 +++ .../gr/thmmy/mthmmy/base/BaseActivity.java | 1 + .../gr/thmmy/mthmmy/base/BaseApplication.java | 25 ++++++++++++++++++- .../thmmy/mthmmy/session/SessionManager.java | 2 +- .../gr/thmmy/mthmmy/utils/ParseHelpers.java | 7 ++++-- 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index c3587b1b..77d2165d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -201,7 +201,7 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo .url(boardUrl[0]) .build(); try { - Response response = BaseActivity.getClient().newCall(request).execute(); + Response response = client.newCall(request).execute(); parseBoard(Jsoup.parse(response.body().string())); } catch (SSLHandshakeException e) { Report.w(TAG, "Certificate problem (please switch to unsafe connection)."); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java index 2343f78d..ea02b7b8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java @@ -192,7 +192,7 @@ public class DownloadsActivity extends BaseActivity implements DownloadsAdapter. .url(downloadsUrl[0]) .build(); try { - Response response = BaseActivity.getClient().newCall(request).execute(); + Response response = client.newCall(request).execute(); parseDownloads(Jsoup.parse(response.body().string())); } catch (SSLHandshakeException e) { Report.w(TAG, "Certificate problem (please switch to unsafe connection)."); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java index d99f7d65..2e102c0e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/stats/StatsFragment.java @@ -33,6 +33,7 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import javax.net.ssl.SSLHandshakeException; @@ -206,6 +207,7 @@ public class StatsFragment extends Fragment { Integer.parseInt(dataCols.last().text()))); mostPopularBoardsByPostsLabels.add(dataCols.first().text()); } + Collections.reverse(mostPopularBoardsByPostsLabels); } { Elements mostPopularBoardsByActivityRows = statsRows.last().select(">td").last() @@ -218,6 +220,7 @@ public class StatsFragment extends Fragment { Float.parseFloat(tmp.substring(0, tmp.indexOf("%"))))); mostPopularBoardsByActivityLabels.add(dataCols.first().text()); } + Collections.reverse(mostPopularBoardsByActivityLabels); } } return true; diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 501e2452..2147a1d3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -79,6 +79,7 @@ public abstract class BaseActivity extends AppCompatActivity { super.onCreate(savedInstanceState); if (client == null) client = BaseApplication.getInstance().getClient(); //must check every time - e.g. + // they become null when app restarts after crash if (sessionManager == null) sessionManager = BaseApplication.getInstance().getSessionManager(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index 1e5ffdd6..b119dabe 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java @@ -19,11 +19,17 @@ import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader; import com.mikepenz.materialdrawer.util.DrawerImageLoader; import com.squareup.picasso.Picasso; +import java.io.IOException; +import java.util.Objects; import java.util.concurrent.TimeUnit; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.session.SessionManager; +import okhttp3.HttpUrl; +import okhttp3.Interceptor; import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; public class BaseApplication extends Application { @@ -40,7 +46,7 @@ public class BaseApplication extends Application { //Display Metrics private static float dpHeight, dpWidth; - public static BaseApplication getInstance(){ + public static BaseApplication getInstance() { return baseApplication; } @@ -54,6 +60,22 @@ public class BaseApplication extends Application { PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); client = new OkHttpClient.Builder() .cookieJar(cookieJar) + .addInterceptor(new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + HttpUrl oldUrl = chain.request().url(); + if (Objects.equals(chain.request().url().host(), "www.thmmy.gr")) { + if (!oldUrl.toString().contains("theme=4")) { + //Probably works but needs more testing: + HttpUrl newUrl = oldUrl.newBuilder().addQueryParameter("theme", "4").build(); + request = request.newBuilder().url(newUrl).build(); + } + } + return chain.proceed(request); + + } + }) .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) @@ -71,6 +93,7 @@ public class BaseApplication extends Application { public void set(ImageView imageView, Uri uri, Drawable placeholder) { Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView); } + @Override public void cancel(ImageView imageView) { Picasso.with(imageView.getContext()).cancelRequest(imageView); 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 2321b88a..c6bbd713 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -332,7 +332,7 @@ public class SessionManager { if (!avatar.isEmpty()) return avatar.first().attr("src"); - Report.e(TAG, "Extracting avatar's link failed!"); + Report.i(TAG, "Extracting avatar's link failed!"); return null; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java index 2520cc89..2e8867f5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java @@ -1,5 +1,7 @@ package gr.thmmy.mthmmy.utils; +import android.util.Log; + import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; @@ -153,15 +155,16 @@ public class ParseHelpers { fixed = fixed.replace( fixed.substring(fixed.indexOf("") + 9) , "
" - + "" + "" + "" + "\"\"" + "
"); + ++tmp_counter; } return fixed; }