From fee593d059d3c2ca4d908fe1a777a5f5c20dd44f Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 8 Nov 2020 16:45:38 +0100 Subject: [PATCH] Fix issue #44 - Sync NSFW + add them in settings to apply them. --- app/build.gradle | 1 + app/src/acad/res/values/strings.xml | 2 +- app/src/full/res/values/strings.xml | 2 +- .../app/fedilab/fedilabtube/MainActivity.java | 1 + .../fedilabtube/client/PeertubeService.java | 3 +- .../client/RetrofitPeertubeAPI.java | 3 +- .../client/entities/UserSettings.java | 9 ++ .../fedilabtube/drawer/PeertubeAdapter.java | 5 +- .../fragment/SettingsFragment.java | 96 ++++++++++++++----- .../fedilab/fedilabtube/helper/Helper.java | 29 ++++-- .../res/drawable/ic_baseline_blur_on_24.xml | 10 ++ app/src/main/res/xml/main_preferences.xml | 4 + 12 files changed, 127 insertions(+), 38 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_blur_on_24.xml diff --git a/app/build.gradle b/app/build.gradle index 5d4b5a8..0d3bcd7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -135,5 +135,6 @@ dependencies { implementation "androidx.work:work-runtime:2.4.0" implementation "androidx.work:work-runtime-ktx:2.4.0" + implementation 'jp.wasabeef:glide-transformations:4.0.0' } \ No newline at end of file diff --git a/app/src/acad/res/values/strings.xml b/app/src/acad/res/values/strings.xml index 873d985..63e9350 100644 --- a/app/src/acad/res/values/strings.xml +++ b/app/src/acad/res/values/strings.xml @@ -14,7 +14,7 @@ set_store_in_history set_play_screen_lock_choice set_video_in_list_choice - + set_video_sensitive_choice Vidéos dans une liste Change la mise en page pour afficher les vidéos dans une liste diff --git a/app/src/full/res/values/strings.xml b/app/src/full/res/values/strings.xml index 5fd6a48..0672486 100644 --- a/app/src/full/res/values/strings.xml +++ b/app/src/full/res/values/strings.xml @@ -13,7 +13,7 @@ set_fullscreen_choice set_play_screen_lock_choice set_video_in_list_choice - + set_video_sensitive_choice Videos in list Change the layout for displaying videos in a list diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index b6a28dc..993eb56 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -243,6 +243,7 @@ public class MainActivity extends AppCompatActivity { editor.putBoolean(getString(R.string.set_autoplay_choice), userMe.isAutoPlayVideo()); editor.putBoolean(getString(R.string.set_store_in_history), userMe.isVideosHistoryEnabled()); editor.putBoolean(getString(R.string.set_autoplay_next_video_choice), userMe.isAutoPlayNextVideo()); + editor.putString(getString(R.string.set_video_sensitive_choice), userMe.getNsfwPolicy()); //Sync languages from server List videoLanguageServer = userMe.getVideoLanguages(); if (videoLanguageServer != null) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java index 670d9bb..0a89718 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java @@ -134,7 +134,8 @@ public interface PeertubeService { @Field("webTorrentEnabled") Boolean webTorrentEnabled, @Field("videoLanguages") List videoLanguages, @Field("description") String description, - @Field("displayName") String displayName + @Field("displayName") String displayName, + @Field("nsfwPolicy") String nsfwPolicy ); @Multipart diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index 3e4c998..588e06e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -495,7 +495,8 @@ public class RetrofitPeertubeAPI { userSettings.isWebTorrentEnabled(), userSettings.getVideoLanguages(), userSettings.getDescription(), - userSettings.getDisplayName() + userSettings.getDisplayName(), + userSettings.getNsfwPolicy() ); Response response = updateUser.execute(); if (response.isSuccessful()) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/UserSettings.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/UserSettings.java index 1c78d87..a6f8272 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/UserSettings.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/UserSettings.java @@ -31,6 +31,7 @@ public class UserSettings { private Uri avatarfile; private String fileName; private NotificationSettings notificationSettings; + private String nsfwPolicy; public Boolean isVideosHistoryEnabled() { return videosHistoryEnabled; @@ -131,5 +132,13 @@ public class UserSettings { public void setNotificationSettings(NotificationSettings notificationSettings) { this.notificationSettings = notificationSettings; } + + public String getNsfwPolicy() { + return nsfwPolicy; + } + + public void setNsfwPolicy(String nsfwPolicy) { + this.nsfwPolicy = nsfwPolicy; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/PeertubeAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/PeertubeAdapter.java index a27f188..d2c72a8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/PeertubeAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/PeertubeAdapter.java @@ -128,12 +128,13 @@ public class PeertubeAdapter extends RecyclerView.Adapter. */ +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import app.fedilab.fedilabtube.MainActivity; +import app.fedilab.fedilabtube.R; +import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; +import app.fedilab.fedilabtube.client.entities.Error; +import app.fedilab.fedilabtube.client.entities.UserSettings; +import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.helper.ThemeHelper; +import es.dmoral.toasty.Toasty; + +import static app.fedilab.fedilabtube.MainActivity.peertubeInformation; +import static app.fedilab.fedilabtube.MainActivity.userMe; + public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { @Override @@ -131,6 +130,26 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared ThemeHelper.switchTo(choice); } } + if (key.compareTo(getString(R.string.set_video_sensitive_choice)) == 0) { + ListPreference set_video_sensitive_choice = findPreference(getString(R.string.set_video_sensitive_choice)); + if (set_video_sensitive_choice != null) { + editor.putString(getString(R.string.set_video_sensitive_choice), set_video_sensitive_choice.getValue()); + editor.apply(); + if(Helper.isLoggedIn(getActivity())) { + new Thread(() -> { + UserSettings userSettings = new UserSettings(); + userSettings.setNsfwPolicy(set_video_sensitive_choice.getValue()); + try { + RetrofitPeertubeAPI api = new RetrofitPeertubeAPI(getActivity()); + api.updateUser(userSettings); + userMe.setNsfwPolicy(set_video_sensitive_choice.getValue()); + } catch (Exception | Error e) { + e.printStackTrace(); + } + }).start(); + } + } + } if (key.compareTo(getString(R.string.set_video_quality_choice)) == 0) { ListPreference set_video_quality_choice = findPreference(getString(R.string.set_video_quality_choice)); if (set_video_quality_choice != null) { @@ -287,6 +306,8 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared set_theme_choice.setValueIndex(currentTheme); } + + //****** Video mode ******* ListPreference set_video_mode_choice = findPreference(getString(R.string.set_video_mode_choice)); List array = Arrays.asList(getResources().getStringArray(R.array.settings_video_mode)); @@ -387,6 +408,31 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared if (selection != null) { set_video_language_choice.setValues(selection); } + + //****** Display sensitive content ******* + ListPreference set_video_sensitive_choice = findPreference(getString(R.string.set_video_sensitive_choice)); + List arraySensitive = new ArrayList<>(); + arraySensitive.add(getString(R.string.do_not_list)); + arraySensitive.add(getString(R.string.blur)); + arraySensitive.add(getString(R.string.display)); + CharSequence[] entriesSensitive = arraySensitive.toArray(new CharSequence[0]); + CharSequence[] entryValuesSensitive = new CharSequence[3]; + String currentSensitive = sharedpref.getString(getString(R.string.set_video_sensitive_choice), Helper.BLUR); + entryValuesSensitive[0] = Helper.DO_NOT_LIST.toLowerCase(); + entryValuesSensitive[1] = Helper.BLUR.toLowerCase(); + entryValuesSensitive[2] = Helper.DISPLAY.toLowerCase(); + int currentSensitivePosition = 0; + for(CharSequence val : entryValuesSensitive) { + if(val.equals(currentSensitive)) { + break; + } + currentSensitivePosition++; + } + if (set_video_sensitive_choice != null) { + set_video_sensitive_choice.setEntries(entriesSensitive); + set_video_sensitive_choice.setEntryValues(entryValuesSensitive); + set_video_sensitive_choice.setValueIndex(currentSensitivePosition); + } } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 5fefc08..6b60653 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -26,6 +26,7 @@ import android.content.res.Resources; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -45,6 +46,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; @@ -71,6 +73,7 @@ import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.webview.CustomWebview; import app.fedilab.fedilabtube.webview.ProxyHelper; import es.dmoral.toasty.Toasty; +import jp.wasabeef.glide.transformations.BlurTransformation; import static android.content.Context.DOWNLOAD_SERVICE; @@ -83,6 +86,9 @@ public class Helper { public static final int LIGHT_MODE = 0; public static final int DARK_MODE = 1; public static final int DEFAULT_MODE = 2; + public static final String DO_NOT_LIST = "do_not_list"; + public static final String BLUR = "blur"; + public static final String DISPLAY = "display"; public static final String TIMELINE_TYPE = "timeline_type"; public static final int VIDEO_MODE_NORMAL = 0; public static final int VIDEO_MODE_MAGNET = 2; @@ -439,16 +445,21 @@ public class Helper { loadGiF(context, url, imageView, 10); } + public static void loadGiF(final Context context, String instance, String url, final ImageView imageView, boolean blur) { + loadGif(context, instance, url, imageView, 10, blur); + } + public static void loadGiF(final Context context, String instance, String url, final ImageView imageView) { - loadGif(context, instance, url, imageView, 10); + loadGif(context, instance, url, imageView, 10, false); } public static void loadGiF(final Context context, String url, final ImageView imageView, int round) { - loadGif(context, null, url, imageView, round); + loadGif(context, null, url, imageView, round, false); } + @SuppressLint("CheckResult") @SuppressWarnings("SameParameterValue") - private static void loadGif(final Context context, String instance, String url, final ImageView imageView, int round) { + private static void loadGif(final Context context, String instance, String url, final ImageView imageView, int round, boolean blur) { if (url == null || url.trim().toLowerCase().compareTo("null") == 0 || url.endsWith("null")) { Glide.with(imageView.getContext()) .asDrawable() @@ -464,11 +475,15 @@ public class Helper { url = "https://" + url; } try { - Glide.with(imageView.getContext()) + RequestBuilder requestBuilder = Glide.with(imageView.getContext()) .load(url) - .thumbnail(0.1f) - .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(round))) - .into(imageView); + .thumbnail(0.1f); + if( blur ) { + requestBuilder.apply(new RequestOptions().transform(new BlurTransformation(50, 3), new CenterCrop(), new RoundedCorners(10))); + }else { + requestBuilder.apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(round))); + } + requestBuilder.into(imageView); } catch (Exception e) { try { Glide.with(imageView.getContext()) diff --git a/app/src/main/res/drawable/ic_baseline_blur_on_24.xml b/app/src/main/res/drawable/ic_baseline_blur_on_24.xml new file mode 100644 index 0000000..121ad7d --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_blur_on_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 5095e15..f96ac1d 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -68,6 +68,10 @@ android:key="@string/set_theme_choice" android:summary="@string/set_theme_description" android:title="@string/set_theme" /> +