diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 07d20c655..86fd9315f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -79,12 +79,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.ManageFiltersAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveInstanceAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveMetaDataAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteDataAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Filters; import fr.gouv.etalab.mastodon.client.Entities.Notification; import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.client.Entities.Status; @@ -101,6 +103,7 @@ import fr.gouv.etalab.mastodon.fragments.DisplaySearchFragment; import fr.gouv.etalab.mastodon.fragments.WhoToFollowFragment; import fr.gouv.etalab.mastodon.helper.CrossActions; import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnFilterActionInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiAccountInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveInstanceInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface; @@ -115,6 +118,8 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment; import fr.gouv.etalab.mastodon.fragments.TabLayoutSettingsFragment; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; + +import static fr.gouv.etalab.mastodon.asynctasks.ManageFiltersAsyncTask.action.GET_ALL_FILTER; import static fr.gouv.etalab.mastodon.helper.Helper.ADD_USER_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.BACKUP_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT; @@ -137,7 +142,7 @@ import android.support.v4.app.FragmentStatePagerAdapter; public abstract class BaseMainActivity extends BaseActivity - implements NavigationView.OnNavigationItemSelectedListener, OnUpdateAccountInfoInterface, OnRetrieveMetaDataInterface, OnRetrieveInstanceInterface, OnRetrieveRemoteAccountInterface, OnRetrieveEmojiAccountInterface { + implements NavigationView.OnNavigationItemSelectedListener, OnUpdateAccountInfoInterface, OnRetrieveMetaDataInterface, OnRetrieveInstanceInterface, OnRetrieveRemoteAccountInterface, OnRetrieveEmojiAccountInterface, OnFilterActionInterface { private FloatingActionButton toot, delete_all, add_new; private HashMap tagTile = new HashMap<>(); @@ -150,7 +155,7 @@ public abstract class BaseMainActivity extends BaseActivity private ViewPager viewPager; private RelativeLayout main_app_container; private Stack stackBack = new Stack<>(); - + public static List filters = new ArrayList<>(); private DisplayStatusFragment homeFragment, federatedFragment, localFragment; private DisplayNotificationsFragment notificationsFragment; private static final int ERROR_DIALOG_REQUEST_CODE = 97; @@ -1336,8 +1341,10 @@ public abstract class BaseMainActivity extends BaseActivity updateNotifCounter(); updateHomeCounter(); //Proceeds to update of the authenticated account - if(Helper.isLoggedIn(getApplicationContext())) + if(Helper.isLoggedIn(getApplicationContext())) { new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + new ManageFiltersAsyncTask(getApplicationContext(), GET_ALL_FILTER, null,BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } if( lastHomeId != null && homeFragment != null){ homeFragment.retrieveMissingToots(lastHomeId); } @@ -1647,6 +1654,13 @@ public abstract class BaseMainActivity extends BaseActivity displayedName.setText(account.getdisplayNameSpan(), TextView.BufferType.SPANNABLE); } + @Override + public void onActionDone(ManageFiltersAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { + if( apiResponse != null && apiResponse.getFilters() != null && apiResponse.getFilters().size() > 0){ + filters = apiResponse.getFilters(); + } + } + /** * Page Adapter for settings diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 1cd284115..2ed5cfd6b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -82,6 +82,7 @@ import java.util.regex.Pattern; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.activities.BaseMainActivity; +import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.activities.MediaActivity; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; @@ -96,6 +97,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Attachment; import fr.gouv.etalab.mastodon.client.Entities.Card; import fr.gouv.etalab.mastodon.client.Entities.Emojis; import fr.gouv.etalab.mastodon.client.Entities.Error; +import fr.gouv.etalab.mastodon.client.Entities.Filters; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment; import fr.gouv.etalab.mastodon.helper.CrossActions; @@ -112,7 +114,10 @@ import fr.gouv.etalab.mastodon.sqlite.Sqlite; import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO; import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO; import fr.gouv.etalab.mastodon.sqlite.TempMuteDAO; + +import static fr.gouv.etalab.mastodon.activities.BaseMainActivity.filters; import static fr.gouv.etalab.mastodon.activities.MainActivity.currentLocale; +import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_DARK; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; @@ -390,16 +395,39 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_LOCAL, null); else filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_PUBLIC, null); + String content; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + content = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString(); + else + //noinspection deprecation + content = Html.fromHtml(status.getContent()).toString(); + if(MainActivity.filters != null){ + for(Filters mfilter: filters){ + ArrayList filterContext = mfilter.getContext(); + if( + (type == RetrieveFeedsAsyncTask.Type.HOME && filterContext.contains("home")) || + (type == RetrieveFeedsAsyncTask.Type.LOCAL && filterContext.contains("local")) || + (type == RetrieveFeedsAsyncTask.Type.PUBLIC && filterContext.contains("public")) + ) { + if (mfilter.isWhole_word() && content.contains(mfilter.getPhrase())) { + return HIDDEN_STATUS; + } else { + try { + Pattern filterPattern = Pattern.compile("(" + mfilter.getPhrase() + ")", Pattern.CASE_INSENSITIVE); + Matcher matcher = filterPattern.matcher(content); + if (matcher.find()) + return HIDDEN_STATUS; + } catch (Exception e) { + return DISPLAYED_STATUS; + } + } + } + } + } if( filter != null && filter.length() > 0){ try { Pattern filterPattern = Pattern.compile("(" + filter + ")", Pattern.CASE_INSENSITIVE); - String content; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) - content = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString(); - else - //noinspection deprecation - content = Html.fromHtml(status.getContent()).toString(); Matcher matcher = filterPattern.matcher(content); if (matcher.find()) return HIDDEN_STATUS; @@ -499,17 +527,6 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct boolean isCompactMode = sharedpreferences.getBoolean(Helper.SET_COMPACT_MODE, true); - - /* int status_account_profile = holder.status_account_profile.getWidth(); - int status_account_profile_boost = holder.status_account_profile_boost.getWidth(); - int status_account_profile_boost_by = holder.status_account_profile_boost_by.getWidth(); - RelativeLayout.LayoutParams lp_status_account_profile = new RelativeLayout.LayoutParams(status_account_profile*textSizePercent/110, status_account_profile*textSizePercent/110); - holder.status_account_profile.setLayoutParams(lp_status_account_profile); - RelativeLayout.LayoutParams lp_status_account_profile_boost = new RelativeLayout.LayoutParams(status_account_profile_boost*textSizePercent/110, status_account_profile*textSizePercent/110); - holder.status_account_profile_boost.setLayoutParams(lp_status_account_profile_boost); - RelativeLayout.LayoutParams lp_status_account_profile_boost_by = new RelativeLayout.LayoutParams(status_account_profile_boost_by*textSizePercent/110, status_account_profile*textSizePercent/110); - holder.status_account_profile_boost_by.setLayoutParams(lp_status_account_profile_boost_by); -*/ if( getItemViewType(position) == FOCUSED_STATUS ) { holder.status_content.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16*textSizePercent/100); holder.status_account_displayname.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16 * textSizePercent / 100);