From c9f88caa647c116327815d8b0234eba2abf9e650 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 31 Jan 2021 21:58:01 +0800 Subject: Update accesstoken in current account SharedPreferences after refreshing it. --- .../infinityforreddit/AccessTokenAuthenticator.java | 10 +++++++++- .../java/ml/docilealligator/infinityforreddit/AppModule.java | 5 +++-- .../infinityforreddit/PullNotificationWorker.java | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 8221b612..1ac3b368 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit; +import android.content.SharedPreferences; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -13,6 +15,7 @@ import java.util.Map; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.Authenticator; import okhttp3.Headers; import okhttp3.Request; @@ -24,10 +27,12 @@ import retrofit2.Retrofit; class AccessTokenAuthenticator implements Authenticator { private Retrofit mRetrofit; private RedditDataRoomDatabase mRedditDataRoomDatabase; + private SharedPreferences mCurrentAccountSharedPreferences; - AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) { + AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, SharedPreferences currentAccountSharedPreferences) { mRetrofit = retrofit; mRedditDataRoomDatabase = accountRoomDatabase; + mCurrentAccountSharedPreferences = currentAccountSharedPreferences; } @Nullable @@ -77,6 +82,9 @@ class AccessTokenAuthenticator implements Authenticator { } else { mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken); } + if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "").equals(account.getAccountName())) { + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); + } return newAccessToken; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 83cc3515..82796cec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -144,9 +144,10 @@ class AppModule { @Provides @Singleton - OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) { + OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, + @Named("current_account") SharedPreferences currentAccountSharedPreferences) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); - okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase)) + okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences)) .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index a0017f1c..44668b13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -56,6 +56,9 @@ public class PullNotificationWorker extends Worker { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; private Context context; @@ -255,6 +258,9 @@ public class PullNotificationWorker extends Worker { } else { mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken); } + if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "").equals(account.getAccountName())) { + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); + } return newAccessToken; } return ""; -- cgit v1.2.3 From e9b937eb9f11f4f7fc40c0c7737d82d00f391749 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 1 Feb 2021 15:00:14 +0800 Subject: Use a dirty method to fix user info and subscribe chip not shown in ViewUserDetailActivity. The problem is caused by CommentsListingFragment. So weird. --- .../infinityforreddit/fragments/CommentsListingFragment.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index 9183fc1a..3675a8ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -9,6 +9,7 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.View; @@ -247,7 +248,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); - bindView(resources); + new Handler().postDelayed(() -> bindView(resources), 0); return rootView; } -- cgit v1.2.3 From 6ffe6a6432dcfe5a94b8b897e9dd6fc751a9a3dc Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 1 Feb 2021 16:13:23 +0800 Subject: Version 4.2.3. Remove logs. --- .../infinityforreddit/adapters/PostRecyclerViewAdapter.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index cce3db99..e50aaf1b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -10,7 +10,6 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -1068,7 +1067,6 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 10_000_000) { - Log.i("afasfasdf", "ssss " + preview.getPreviewUrl()); int divisor = 2; do { preview.setPreviewWidth(preview.getPreviewWidth() / divisor); @@ -1076,8 +1074,6 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 10_000_000); } - - Log.i("afasfasdf", "s " + preview.getPreviewWidth() * preview.getPreviewHeight() + " " + preview.getPreviewHeight() + " " + preview.getPreviewWidth() + " " + preview.getPreviewUrl()); return preview; } -- cgit v1.2.3 From b7b9a7f3bd0c7c7d5dd3ed86db7c0929defb1ba6 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 2 Feb 2021 22:47:18 +0800 Subject: Fix downloading videos without sound failed. --- .../services/DownloadRedditVideoService.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index 73839464..f1b9633e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -25,6 +25,7 @@ import android.os.Looper; import android.os.Message; import android.os.Process; import android.provider.MediaStore; +import android.util.Log; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -207,17 +208,18 @@ public class DownloadRedditVideoService extends Service { updateNotification(R.string.downloading_reddit_video_audio_track, 0, randomNotificationIdOffset, null); + String videoFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp4"; + String savedVideoFilePath = writeResponseBodyToDisk(videoResponse.body(), videoFilePath); + if (savedVideoFilePath == null) { + downloadFinished(null, ERROR_VIDEO_FILE_CANNOT_SAVE, randomNotificationIdOffset); + return; + } + Response audioResponse = downloadFile.downloadFile(audioUrl).execute(); if (audioResponse.isSuccessful() && audioResponse.body() != null) { - String videoFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp4"; String audioFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp3"; String outputFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + ".mp4"; - String savedVideoFilePath = writeResponseBodyToDisk(videoResponse.body(), videoFilePath); - if (savedVideoFilePath == null) { - downloadFinished(null, ERROR_VIDEO_FILE_CANNOT_SAVE, randomNotificationIdOffset); - return; - } String savedAudioFilePath = writeResponseBodyToDisk(audioResponse.body(), audioFilePath); if (savedAudioFilePath == null) { downloadFinished(null, ERROR_AUDIO_FILE_CANNOT_SAVE, randomNotificationIdOffset); @@ -246,8 +248,6 @@ public class DownloadRedditVideoService extends Service { downloadFinished(null, ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE, randomNotificationIdOffset); } } else { - String videoFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp4"; - updateNotification(R.string.downloading_reddit_video_save_file_to_public_dir, -1, randomNotificationIdOffset, null); try { -- cgit v1.2.3 From 36c7cfcc6bd9b3be1eb51303d2c3448e85401eac Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 2 Feb 2021 23:33:14 +0800 Subject: Prepare to add quarantined subreddit support. --- .../infinityforreddit/activities/ViewPostDetailActivity.java | 2 +- .../infinityforreddit/activities/ViewSubredditDetailActivity.java | 2 +- .../java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java | 3 +++ .../infinityforreddit/asynctasks/LoadSubredditIconAsyncTask.java | 2 +- .../infinityforreddit/fragments/SidebarFragment.java | 2 +- .../infinityforreddit/subreddit/FetchSubredditData.java | 8 ++++---- .../infinityforreddit/subreddit/SubredditSubscription.java | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 30100d86..145fdadc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -935,7 +935,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } @Override - public void onFetchSubredditDataFail() { + public void onFetchSubredditDataFail(boolean isQuarantined) { mRespectSubredditRecommendedSortType = false; ViewPostDetailActivity.this.sortType = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT, SortType.Type.BEST.value.toUpperCase()); if (ViewPostDetailActivity.this.sortType != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 3bf7fdf8..cd514c0f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -519,7 +519,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp } @Override - public void onFetchSubredditDataFail() { + public void onFetchSubredditDataFail(boolean isQuarantined) { makeSnackbar(R.string.cannot_fetch_subreddit_info, true); mFetchSubredditInfoSuccess = false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 1799ff57..67da922d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -346,4 +346,7 @@ public interface RedditAPI { Call addSubredditToMultiReddit(@HeaderMap Map headers, @FieldMap Map params, @Path(value = "multipath", encoded = true) String multipath, @Path("subredditName") String subredditName); + @FormUrlEncoded + @POST("/api/quarantine_optin?raw_json=1") + Call optInQuarantinedSubreddit(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIconAsyncTask.java index 7092bae7..695e4e46 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIconAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIconAsyncTask.java @@ -57,7 +57,7 @@ public class LoadSubredditIconAsyncTask extends AsyncTask { } @Override - public void onFetchSubredditDataFail() { + public void onFetchSubredditDataFail(boolean isQuarantined) { loadSubredditIconAsyncTaskListener.loadIconSuccess(null); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java index 571350ce..b6fb5795 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -193,7 +193,7 @@ public class SidebarFragment extends Fragment { } @Override - public void onFetchSubredditDataFail() { + public void onFetchSubredditDataFail(boolean isQuarantined) { swipeRefreshLayout.setRefreshing(false); Toast.makeText(activity, R.string.cannot_fetch_sidebar, Toast.LENGTH_SHORT).show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java index 37587ede..b5e9b553 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java @@ -31,17 +31,17 @@ public class FetchSubredditData { @Override public void onParseSubredditDataFail() { - fetchSubredditDataListener.onFetchSubredditDataFail(); + fetchSubredditDataListener.onFetchSubredditDataFail(false); } }); } else { - fetchSubredditDataListener.onFetchSubredditDataFail(); + fetchSubredditDataListener.onFetchSubredditDataFail(response.code() == 403); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchSubredditDataListener.onFetchSubredditDataFail(); + fetchSubredditDataListener.onFetchSubredditDataFail(false); } }); } @@ -84,7 +84,7 @@ public class FetchSubredditData { public interface FetchSubredditDataListener { void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers); - void onFetchSubredditDataFail(); + void onFetchSubredditDataFail(boolean isQuarantined); } interface FetchSubredditListingDataListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java index 52664234..31615903 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java @@ -56,7 +56,7 @@ public class SubredditSubscription { } @Override - public void onFetchSubredditDataFail() { + public void onFetchSubredditDataFail(boolean isQuarantined) { } }); -- cgit v1.2.3 From 6f64303df8c776cf187d3e8820b67c1901804815 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 3 Feb 2021 09:58:46 +0800 Subject: Make all gifcay id all lower case. --- .../docilealligator/infinityforreddit/post/ParsePost.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 809f0806..2ba685eb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -403,7 +403,7 @@ public class ParsePost { if (gfycatId.contains("-")) { gfycatId = gfycatId.substring(0, gfycatId.indexOf('-')); } - post.setGfycatId(gfycatId); + post.setGfycatId(gfycatId.toLowerCase()); } else if (authority != null && authority.contains("redgifs.com")) { String gfycatId = url.substring(url.lastIndexOf("/") + 1); if (gfycatId.contains("-")) { @@ -411,7 +411,7 @@ public class ParsePost { } post.setIsRedgifs(true); post.setVideoUrl(url); - post.setGfycatId(gfycatId); + post.setGfycatId(gfycatId.toLowerCase()); } } catch (IllegalArgumentException ignore) { } } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { @@ -449,16 +449,21 @@ public class ParsePost { } else if (post.getPostType() == Post.LINK_TYPE) { Uri uri = Uri.parse(url); String authority = uri.getAuthority(); + + // Gyfcat ids must be lowercase to resolve to a video through the api, we are not + // guaranteed to get an id that is all lowercase. + String gfycatId = url.substring(url.lastIndexOf("/") + 1).toLowerCase(); + if (authority != null && (authority.contains("gfycat.com"))) { post.setPostType(Post.VIDEO_TYPE); post.setIsGfycat(true); post.setVideoUrl(url); - post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + post.setGfycatId(gfycatId); } else if (authority != null && authority.contains("redgifs.com")) { post.setPostType(Post.VIDEO_TYPE); post.setIsRedgifs(true); post.setVideoUrl(url); - post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + post.setGfycatId(gfycatId); } } } -- cgit v1.2.3 From 4652b70b376d2a8589bed77ee0f2089eb36b233d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 3 Feb 2021 11:14:25 +0800 Subject: Add a FAB in LoginActivity to setDomStorageEnabled. --- .../activities/LoginActivity.java | 43 ++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java index 210a81c7..49455183 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java @@ -10,6 +10,7 @@ import android.os.Bundle; import android.util.Log; import android.view.InflateException; import android.view.MenuItem; +import android.view.View; import android.webkit.CookieManager; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -19,8 +20,11 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.app.ActivityCompat; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.r0adkll.slidr.Slidr; import org.json.JSONException; @@ -34,13 +38,13 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.asynctasks.ParseAndInsertNewAccountAsyncTask; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.FetchMyInfo; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.asynctasks.ParseAndInsertNewAccountAsyncTask; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -51,6 +55,8 @@ import retrofit2.Retrofit; public class LoginActivity extends BaseActivity { + private static final String ENABLE_DOM_STATE = "EDS"; + @BindView(R.id.coordinator_layout_login_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_login_activity) @@ -61,6 +67,8 @@ public class LoginActivity extends BaseActivity { TextView twoFAInfoTextView; @BindView(R.id.webview_login_activity) WebView webView; + @BindView(R.id.fab_login_activity) + FloatingActionButton fab; @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -78,6 +86,7 @@ public class LoginActivity extends BaseActivity { @Inject CustomThemeWrapper mCustomThemeWrapper; private String authCode; + private boolean enableDom = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -108,7 +117,28 @@ public class LoginActivity extends BaseActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); + if (savedInstanceState != null) { + enableDom = savedInstanceState.getBoolean(ENABLE_DOM_STATE); + } + + fab.setOnClickListener(view -> { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.have_trouble_login_title) + .setMessage(R.string.have_trouble_login_message) + .setPositiveButton(R.string.yes, (dialogInterface, i) -> { + enableDom = !enableDom; + ActivityCompat.recreate(this); + }) + .setNegativeButton(R.string.no, null) + .show(); + }); + + if (enableDom) { + twoFAInfoTextView.setVisibility(View.GONE); + } + webView.getSettings().setJavaScriptEnabled(true); + webView.getSettings().setDomStorageEnabled(enableDom); Uri baseUri = Uri.parse(APIUtils.OAUTH_URL); Uri.Builder uriBuilder = baseUri.buildUpon(); @@ -227,6 +257,12 @@ public class LoginActivity extends BaseActivity { }); } + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(ENABLE_DOM_STATE, enableDom); + } + @Override public SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; @@ -244,6 +280,7 @@ public class LoginActivity extends BaseActivity { twoFAInfoTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); Drawable infoDrawable = Utils.getTintedDrawable(this, R.drawable.ic_info_preference_24dp, mCustomThemeWrapper.getPrimaryIconColor()); twoFAInfoTextView.setCompoundDrawablesWithIntrinsicBounds(infoDrawable, null, null, null); + applyFABTheme(fab); } @Override -- cgit v1.2.3 From 370e02c311c2c3f12bb7ad3e461321c825653732 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 4 Feb 2021 13:00:09 +0800 Subject: Trim keywords in PostFilter.isPostAllowed. --- .../infinityforreddit/postfilter/PostFilter.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 9c8dbcc8..30125ba8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -170,7 +170,7 @@ public class PostFilter implements Parcelable { if (postFilter.postTitleExcludesStrings != null && !postFilter.postTitleExcludesStrings.equals("")) { String[] titles = postFilter.postTitleExcludesStrings.split(",", 0); for (String t : titles) { - if (!t.equals("") && post.getTitle().toLowerCase().contains(t.toLowerCase())) { + if (!t.trim().equals("") && post.getTitle().toLowerCase().contains(t.toLowerCase().trim())) { return false; } } @@ -178,7 +178,7 @@ public class PostFilter implements Parcelable { if (postFilter.excludeSubreddits != null && !postFilter.excludeSubreddits.equals("")) { String[] subreddits = postFilter.excludeSubreddits.split(",", 0); for (String s : subreddits) { - if (!s.equals("") && post.getSubredditName().equalsIgnoreCase(s)) { + if (!s.trim().equals("") && post.getSubredditName().equalsIgnoreCase(s.trim())) { return false; } } @@ -186,7 +186,7 @@ public class PostFilter implements Parcelable { if (postFilter.excludeUsers != null && !postFilter.excludeUsers.equals("")) { String[] users = postFilter.excludeUsers.split(",", 0); for (String u : users) { - if (!u.equals("") && post.getAuthor().equalsIgnoreCase(u)) { + if (!u.trim().equals("") && post.getAuthor().equalsIgnoreCase(u.trim())) { return false; } } @@ -194,7 +194,7 @@ public class PostFilter implements Parcelable { if (postFilter.excludeFlairs != null && !postFilter.excludeFlairs.equals("")) { String[] flairs = postFilter.excludeFlairs.split(",", 0); for (String f : flairs) { - if (!f.equals("") && post.getFlair().equalsIgnoreCase(f)) { + if (!f.trim().equals("") && post.getFlair().equalsIgnoreCase(f.trim())) { return false; } } @@ -202,7 +202,7 @@ public class PostFilter implements Parcelable { if (postFilter.containFlairs != null && !postFilter.containFlairs.equals("")) { String[] flairs = postFilter.containFlairs.split(",", 0); for (String f : flairs) { - if (!f.equals("") && post.getFlair().equalsIgnoreCase(f)) { + if (!f.trim().equals("") && post.getFlair().equalsIgnoreCase(f.trim())) { return false; } } -- cgit v1.2.3 From efd8d1c080d1f69775dc8952fb521b9bd466d774 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 5 Feb 2021 12:27:58 +0800 Subject: Fix NullPointerException in PostFilter.mergePostFilter. Minor bugs fixed in CommentsListingFragment. --- .../fragments/CommentsListingFragment.java | 128 +++++++++++---------- .../infinityforreddit/postfilter/PostFilter.java | 51 ++++---- 2 files changed, 95 insertions(+), 84 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index 3675a8ea..c730b4ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -254,80 +254,82 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } private void bindView(Resources resources) { - mLinearLayoutManager = new LinearLayoutManager(mActivity); - mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); - - mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, - getResources().getConfiguration().locale, mSharedPreferences, - getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), - () -> mCommentViewModel.retryLoadingMore()); - - String username = getArguments().getString(EXTRA_USERNAME); - String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.name()); - if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { - String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, SortType.Time.ALL.name()); - sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()), SortType.Time.valueOf(sortTime.toUpperCase())); - } else { - sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); - } + if (mActivity != null && !mActivity.isFinishing() && !mActivity.isDestroyed()) { + mLinearLayoutManager = new LinearLayoutManager(mActivity); + mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); + + mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, + getResources().getConfiguration().locale, mSharedPreferences, + getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), + () -> mCommentViewModel.retryLoadingMore()); + + String username = getArguments().getString(EXTRA_USERNAME); + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.name()); + if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { + String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, SortType.Time.ALL.name()); + sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()), SortType.Time.valueOf(sortTime.toUpperCase())); + } else { + sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); + } - mCommentRecyclerView.setAdapter(mAdapter); + mCommentRecyclerView.setAdapter(mAdapter); - if (mActivity instanceof RecyclerViewContentScrollingInterface) { - mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (dy > 0) { - ((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown(); - } else if (dy < 0) { - ((RecyclerViewContentScrollingInterface) mActivity).contentScrollUp(); + if (mActivity instanceof RecyclerViewContentScrollingInterface) { + mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + if (dy > 0) { + ((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown(); + } else if (dy < 0) { + ((RecyclerViewContentScrollingInterface) mActivity).contentScrollUp(); + } } - } - }); - } - - CommentViewModel.Factory factory; - - if (mAccessToken == null) { - factory = new CommentViewModel.Factory(mRetrofit, - resources.getConfiguration().locale, null, username, sortType, - getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); - } else { - factory = new CommentViewModel.Factory(mOauthRetrofit, - resources.getConfiguration().locale, mAccessToken, username, sortType, - getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); - } + }); + } - mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); - mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); + CommentViewModel.Factory factory; - mCommentViewModel.hasComment().observe(getViewLifecycleOwner(), hasComment -> { - mSwipeRefreshLayout.setRefreshing(false); - if (hasComment) { - mFetchCommentInfoLinearLayout.setVisibility(View.GONE); + if (mAccessToken == null) { + factory = new CommentViewModel.Factory(mRetrofit, + resources.getConfiguration().locale, null, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); } else { - mFetchCommentInfoLinearLayout.setOnClickListener(view -> { - //Do nothing - }); - showErrorView(R.string.no_comments); + factory = new CommentViewModel.Factory(mOauthRetrofit, + resources.getConfiguration().locale, mAccessToken, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); } - }); - mCommentViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { - if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { - mSwipeRefreshLayout.setRefreshing(false); - } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { + mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); + mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); + + mCommentViewModel.hasComment().observe(getViewLifecycleOwner(), hasComment -> { mSwipeRefreshLayout.setRefreshing(false); - mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh()); - showErrorView(R.string.load_comments_failed); - } else { - mSwipeRefreshLayout.setRefreshing(true); - } - }); + if (hasComment) { + mFetchCommentInfoLinearLayout.setVisibility(View.GONE); + } else { + mFetchCommentInfoLinearLayout.setOnClickListener(view -> { + //Do nothing + }); + showErrorView(R.string.no_comments); + } + }); - mCommentViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); + mCommentViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { + if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { + mSwipeRefreshLayout.setRefreshing(false); + } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { + mSwipeRefreshLayout.setRefreshing(false); + mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh()); + showErrorView(R.string.load_comments_failed); + } else { + mSwipeRefreshLayout.setRefreshing(true); + } + }); + + mCommentViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); - mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); + mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); + } } public void changeSortType(SortType sortType) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 30125ba8..99d40536 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -212,10 +212,10 @@ public class PostFilter implements Parcelable { } public static PostFilter mergePostFilter(List postFilterList) { - PostFilter postFilter = new PostFilter(); if (postFilterList.size() == 1) { return postFilterList.get(0); } + PostFilter postFilter = new PostFilter(); StringBuilder stringBuilder; postFilter.name = "Merged"; for (PostFilter p : postFilterList) { @@ -229,30 +229,39 @@ public class PostFilter implements Parcelable { postFilter.onlyNSFW = p.onlyNSFW ? p.onlyNSFW : postFilter.onlyNSFW; postFilter.onlySpoiler = p.onlySpoiler ? p.onlySpoiler : postFilter.onlySpoiler; - postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex.equals("") ? postFilter.postTitleExcludesRegex : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings); - stringBuilder.append(",").append(p.postTitleExcludesStrings); - postFilter.postTitleExcludesStrings = stringBuilder.toString(); + if (p.postTitleExcludesRegex != null && !p.postTitleExcludesRegex.equals("")) { + postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex; + } - postFilter.excludeSubreddits = p.excludeSubreddits.equals("") ? postFilter.excludeSubreddits : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeSubreddits); - stringBuilder.append(",").append(p.excludeSubreddits); - postFilter.excludeSubreddits = stringBuilder.toString(); + if (p.postTitleExcludesStrings != null && !p.postTitleExcludesStrings.equals("")) { + stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings == null ? "" : postFilter.postTitleExcludesStrings); + stringBuilder.append(",").append(p.postTitleExcludesStrings); + postFilter.postTitleExcludesStrings = stringBuilder.toString(); + } - postFilter.excludeUsers = p.excludeUsers.equals("") ? postFilter.excludeUsers : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeUsers); - stringBuilder.append(",").append(p.excludeUsers); - postFilter.excludeUsers = stringBuilder.toString(); + if (p.excludeSubreddits != null && !p.excludeSubreddits.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeSubreddits == null ? "" : postFilter.excludeSubreddits); + stringBuilder.append(",").append(p.excludeSubreddits); + postFilter.excludeSubreddits = stringBuilder.toString(); + } - postFilter.containFlairs = p.containFlairs.equals("") ? postFilter.containFlairs : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.containFlairs); - stringBuilder.append(",").append(p.containFlairs); - postFilter.containFlairs = stringBuilder.toString(); + if (p.excludeUsers != null && !p.excludeUsers.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeUsers == null ? "" : postFilter.excludeUsers); + stringBuilder.append(",").append(p.excludeUsers); + postFilter.excludeUsers = stringBuilder.toString(); + } + + if (p.containFlairs != null && !p.containFlairs.equals("")) { + stringBuilder = new StringBuilder(postFilter.containFlairs == null ? "" : postFilter.containFlairs); + stringBuilder.append(",").append(p.containFlairs); + postFilter.containFlairs = stringBuilder.toString(); + } - postFilter.excludeFlairs = p.excludeFlairs.equals("") ? postFilter.excludeFlairs : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeFlairs); - stringBuilder.append(",").append(p.excludeFlairs); - postFilter.excludeFlairs = stringBuilder.toString(); + if (p.excludeFlairs != null && !p.excludeFlairs.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeFlairs == null ? "" : postFilter.excludeFlairs); + stringBuilder.append(",").append(p.excludeFlairs); + postFilter.excludeFlairs = stringBuilder.toString(); + } postFilter.containTextType = p.containTextType || postFilter.containTextType; postFilter.containLinkType = p.containLinkType || postFilter.containLinkType; -- cgit v1.2.3