From 4ad6f7a14fead92b607430df1ada9e7f9348cc1e Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 15 Dec 2018 18:27:54 +0100 Subject: [PATCH] Fix NSFW for art --- .../mastodon/activities/BaseMainActivity.java | 88 ++++++++++++--- .../mastodon/drawers/StatusListAdapter.java | 2 +- .../fragments/DisplayStatusFragment.java | 3 + .../gouv/etalab/mastodon/helper/Helper.java | 106 +++++++++--------- 4 files changed, 131 insertions(+), 68 deletions(-) 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 6e5f9e7ba..1ff2dd7f2 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 @@ -183,7 +183,7 @@ public abstract class BaseMainActivity extends BaseActivity private RelativeLayout main_app_container; private Stack stackBack = new Stack<>(); public static List filters = new ArrayList<>(); - private DisplayStatusFragment homeFragment, federatedFragment, localFragment; + private DisplayStatusFragment homeFragment, federatedFragment, localFragment, artFragment; private DisplayNotificationsFragment notificationsFragment; private static final int ERROR_DIALOG_REQUEST_CODE = 97; private static BroadcastReceiver receive_data, receive_federated_data, receive_local_data; @@ -192,7 +192,7 @@ public abstract class BaseMainActivity extends BaseActivity public static int countNewNotifications = 0; private String userIdService; public static String lastHomeId = null, lastNotificationId = null; - boolean notif_follow, notif_add, notif_mention, notif_share, show_boosts, show_replies; + boolean notif_follow, notif_add, notif_mention, notif_share, show_boosts, show_replies , show_nsfw; String show_filtered; private AppBarLayout appBar; private String bookmark; @@ -853,6 +853,13 @@ public abstract class BaseMainActivity extends BaseActivity return manageFilters(tabStrip, sharedpreferences); } }); + if( tabPosition.containsKey("art")) + tabStrip.getChildAt(tabPosition.get("art")).setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + return manageFilters(tabStrip, sharedpreferences); + } + }); viewPager.setOffscreenPageLimit(countPage); main_app_container = findViewById(R.id.main_app_container); @@ -1566,9 +1573,10 @@ public abstract class BaseMainActivity extends BaseActivity //Only shown if the tab has focus if( (homeFragment != null && homeFragment.getUserVisibleHint()) || - (federatedFragment != null && federatedFragment.getUserVisibleHint()) || - (localFragment != null && localFragment.getUserVisibleHint()) - ){ + (federatedFragment != null && federatedFragment.getUserVisibleHint()) || + (localFragment != null && localFragment.getUserVisibleHint()) || + (artFragment != null && artFragment.getUserVisibleHint()) + ){ PopupMenu popup = null; if(homeFragment != null && homeFragment.getUserVisibleHint()) popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(0)); @@ -1576,6 +1584,55 @@ public abstract class BaseMainActivity extends BaseActivity popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(tabPosition.get("local"))); else if(federatedFragment != null && federatedFragment.getUserVisibleHint()){ popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(tabPosition.get("global"))); + }else if(artFragment != null && artFragment.getUserVisibleHint()){ + popup = new PopupMenu(BaseMainActivity.this, tabStrip.getChildAt(tabPosition.get("art"))); + popup.getMenuInflater() + .inflate(R.menu.option_tag_timeline, popup.getMenu()); + Menu menu = popup.getMenu(); + + show_nsfw = sharedpreferences.getBoolean(Helper.SET_ART_WITH_NSFW, false); + final MenuItem itemShowNSFW = menu.findItem(R.id.action_show_nsfw); + final MenuItem itemMedia = menu.findItem(R.id.action_show_media_only); + final MenuItem itemDelete = menu.findItem(R.id.action_delete); + itemMedia.setVisible(false); + itemDelete.setVisible(false); + itemShowNSFW.setChecked(show_nsfw); + popup.setOnDismissListener(new PopupMenu.OnDismissListener() { + @Override + public void onDismiss(PopupMenu menu) { + refreshFilters(); + } + }); + popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + public boolean onMenuItemClick(MenuItem item) { + item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); + item.setActionView(new View(getApplicationContext())); + item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { + @Override + public boolean onMenuItemActionExpand(MenuItem item) { + return false; + } + + @Override + public boolean onMenuItemActionCollapse(MenuItem item) { + return false; + } + }); + switch (item.getItemId()) { + + case R.id.action_show_nsfw: + show_nsfw = !show_nsfw; + itemShowNSFW.setChecked(show_nsfw); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_ART_WITH_NSFW, show_nsfw); + editor.apply(); + break; + } + return false; + } + }); + popup.show(); + return false; } if( popup == null) return true; @@ -1709,6 +1766,8 @@ public abstract class BaseMainActivity extends BaseActivity localFragment.refreshFilter(); if(federatedFragment != null) federatedFragment.refreshFilter(); + if(artFragment != null) + artFragment.refreshFilter(); } @Override @@ -2394,19 +2453,16 @@ public abstract class BaseMainActivity extends BaseActivity homeFragment = (DisplayStatusFragment) createdFragment; }else if( position == 1){ notificationsFragment = (DisplayNotificationsFragment) createdFragment; - }else if( position ==2 && countPage > 2){ - if( !display_direct && display_local) + }else{ + if( display_local && position == tabPosition.get("local")) localFragment = (DisplayStatusFragment) createdFragment; - else if (!display_local) - federatedFragment = (DisplayStatusFragment) createdFragment; - }else if (position == 3 && countPage > 3){ - if( display_local) - localFragment = (DisplayStatusFragment) createdFragment; - else if (display_global) - federatedFragment = (DisplayStatusFragment) createdFragment; - }else if( position == 4 && countPage > 4) - if( display_global) + else if( display_global && position == tabPosition.get("global")) federatedFragment = (DisplayStatusFragment) createdFragment; + else if( display_art && position == tabPosition.get("art")) + artFragment = (DisplayStatusFragment) createdFragment; + + } + return createdFragment; } 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 02077c162..1074c0ad1 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 @@ -462,7 +462,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) { final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - if( type == RetrieveFeedsAsyncTask.Type.ART || (tagTimeline != null && tagTimeline.isART())) { + if( (type == RetrieveFeedsAsyncTask.Type.ART || (tagTimeline != null && tagTimeline.isART())) && viewHolder.getItemViewType() != HIDDEN_STATUS ) { final ViewHolderArt holder = (ViewHolderArt) viewHolder; final Status status = statuses.get(viewHolder.getAdapterPosition()); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 863b58997..52e3d94eb 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -491,6 +491,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size()); }else { //If it's an Art timeline not allowing NSFW ArrayList safeStatuses = new ArrayList<>(); + for(Status status: statuses){ if( !status.isSensitive()) safeStatuses.add(status); @@ -498,6 +499,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn this.statuses.addAll(safeStatuses); statusListAdapter.notifyItemRangeInserted(previousPosition, safeStatuses.size()); } + + } if( type == RetrieveFeedsAsyncTask.Type.HOME ) { //Update the id of the last toot retrieved diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 2f312cdfa..7e3d45cf3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -340,6 +340,7 @@ public class Helper { public static final String SET_AUTOMATICALLY_SPLIT_TOOTS = "set_automatically_split_toots"; public static final String SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE = "set_automatically_split_toots_size"; public static final String SET_TRUNCATE_TOOTS_SIZE = "set_truncate_toots_size"; + public static final String SET_ART_WITH_NSFW = "set_art_with_nsfw"; //End points public static final String EP_AUTHORIZE = "/oauth/authorize"; @@ -2707,65 +2708,68 @@ public class Helper { else filter = sharedpreferences.getString(Helper.SET_FILTER_REGEX_PUBLIC, null); - String content = status.getContent(); - if( status.getSpoiler_text() != null) - content += " "+ status.getSpoiler_text(); - boolean addToot = true; //Flag to tell if the current toot will be added. - if( status.getAccount() == null) - addToot = false; - if(addToot && 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("public")) || - (type == RetrieveFeedsAsyncTask.Type.PUBLIC && filterContext.contains("public")) + String content = status.getContent(); + if( status.getSpoiler_text() != null) + content += " "+ status.getSpoiler_text(); + boolean addToot = true; //Flag to tell if the current toot will be added. + if( status.getAccount() == null) + addToot = false; + boolean show_nsfw = sharedpreferences.getBoolean(Helper.SET_ART_WITH_NSFW, false); + if( type == RetrieveFeedsAsyncTask.Type.ART && !show_nsfw && status.isSensitive()) { + addToot = false; + } + if(addToot && 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("public")) || + (type == RetrieveFeedsAsyncTask.Type.PUBLIC && filterContext.contains("public")) - ) { - if (mfilter.isWhole_word() && content.contains(mfilter.getPhrase())) { - addToot = false; - } else { - try { - Pattern filterPattern = Pattern.compile("(" + mfilter.getPhrase() + ")", Pattern.CASE_INSENSITIVE); - Matcher matcher = filterPattern.matcher(content); - if (matcher.find()) - addToot = false; - } catch (Exception ignored) { } - } + ) { + if (mfilter.isWhole_word() && content.contains(mfilter.getPhrase())) { + addToot = false; + } else { + try { + Pattern filterPattern = Pattern.compile("(" + mfilter.getPhrase() + ")", Pattern.CASE_INSENSITIVE); + Matcher matcher = filterPattern.matcher(content); + if (matcher.find()) + addToot = false; + } catch (Exception ignored) { } } } } - if( addToot && filter != null && filter.length() > 0){ - try { - Pattern filterPattern = Pattern.compile("(" + filter + ")", Pattern.CASE_INSENSITIVE); - Matcher matcher = filterPattern.matcher(content); - if (matcher.find()) - addToot = false; - }catch (Exception ignored){ } - } - if(addToot) { - if (type == RetrieveFeedsAsyncTask.Type.HOME) { - if (status.getReblog() != null && !sharedpreferences.getBoolean(Helper.SET_SHOW_BOOSTS, true)) - addToot = false; - else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null") && !sharedpreferences.getBoolean(Helper.SET_SHOW_REPLIES, true)) { - addToot = false; - } - } else { - if (context instanceof ShowAccountActivity) { - if (status.getReblog() != null && !((ShowAccountActivity) context).showBoosts()) - addToot = false; - else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null") && !((ShowAccountActivity) context).showReplies()) - addToot = false; - } + } + if( addToot && filter != null && filter.length() > 0){ + try { + Pattern filterPattern = Pattern.compile("(" + filter + ")", Pattern.CASE_INSENSITIVE); + Matcher matcher = filterPattern.matcher(content); + if (matcher.find()) + addToot = false; + }catch (Exception ignored){ } + } + if(addToot) { + if (type == RetrieveFeedsAsyncTask.Type.HOME) { + if (status.getReblog() != null && !sharedpreferences.getBoolean(Helper.SET_SHOW_BOOSTS, true)) + addToot = false; + else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null") && !sharedpreferences.getBoolean(Helper.SET_SHOW_REPLIES, true)) { + addToot = false; } - } - if( addToot){ - if (timedMute != null && timedMute.size() > 0) { - if (timedMute.contains(status.getAccount().getId())) + } else { + if (context instanceof ShowAccountActivity) { + if (status.getReblog() != null && !((ShowAccountActivity) context).showBoosts()) + addToot = false; + else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null") && !((ShowAccountActivity) context).showReplies()) addToot = false; } } - + } + if( addToot){ + if (timedMute != null && timedMute.size() > 0) { + if (timedMute.contains(status.getAccount().getId())) + addToot = false; + } + } return addToot; }