From 181bd3dfa4fe564c0e149dc792de2b0436e2b461 Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Wed, 2 Jul 2025 16:10:42 -0500 Subject: [PATCH] Moved screen capture into its own util class --- .../nrw/commons/profile/ProfileActivity.kt | 4 +-- .../nrw/commons/utils/ScreenCaptureUtils.java | 25 +++++++++++++++++++ .../java/fr/free/nrw/commons/utils/Utils.java | 20 --------------- 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/ScreenCaptureUtils.java diff --git a/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.kt b/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.kt index 9e5e99851..c9503e892 100644 --- a/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.kt +++ b/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.kt @@ -11,7 +11,6 @@ import android.widget.TextView import androidx.core.content.FileProvider import androidx.fragment.app.Fragment import fr.free.nrw.commons.R -import fr.free.nrw.commons.utils.Utils import fr.free.nrw.commons.ViewPagerAdapter import fr.free.nrw.commons.auth.SessionManager import fr.free.nrw.commons.contributions.ContributionsFragment @@ -20,6 +19,7 @@ import fr.free.nrw.commons.profile.achievements.AchievementsFragment import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment import fr.free.nrw.commons.theme.BaseActivity import fr.free.nrw.commons.utils.DialogUtil +import fr.free.nrw.commons.utils.ScreenCaptureUtils import java.io.File import java.io.FileOutputStream import java.util.* @@ -132,7 +132,7 @@ class ProfileActivity : BaseActivity() { return when (item.itemId) { R.id.share_app_icon -> { val rootView = window.decorView.findViewById(android.R.id.content) - val screenShot = Utils.getScreenShot(rootView) + val screenShot = ScreenCaptureUtils.getScreenShot(rootView) if (screenShot == null) { Log.e("ERROR", "ScreenShot is null") return false diff --git a/app/src/main/java/fr/free/nrw/commons/utils/ScreenCaptureUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/ScreenCaptureUtils.java new file mode 100644 index 000000000..7f6aa8104 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/ScreenCaptureUtils.java @@ -0,0 +1,25 @@ +package fr.free.nrw.commons.utils; + +import android.graphics.Bitmap; +import android.view.View; + +public final class ScreenCaptureUtils { + + /** + * To take screenshot of the screen and return it in Bitmap format + * + * @param view + * @return + */ + public static Bitmap getScreenShot(View view) { + View screenView = view.getRootView(); + screenView.setDrawingCacheEnabled(true); + Bitmap drawingCache = screenView.getDrawingCache(); + if (drawingCache != null) { + Bitmap bitmap = Bitmap.createBitmap(drawingCache); + screenView.setDrawingCacheEnabled(false); + return bitmap; + } + return null; + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/utils/Utils.java b/app/src/main/java/fr/free/nrw/commons/utils/Utils.java index 4b65d71a3..a13281c56 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/Utils.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/Utils.java @@ -4,11 +4,9 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; -import android.graphics.Bitmap; import android.net.Uri; import android.text.SpannableString; import android.text.style.UnderlineSpan; -import android.view.View; import android.widget.TextView; import androidx.browser.customtabs.CustomTabColorSchemeParams; @@ -76,24 +74,6 @@ public class Utils { customTabsIntent.launchUrl(context, url); } - /** - * To take screenshot of the screen and return it in Bitmap format - * - * @param view - * @return - */ - public static Bitmap getScreenShot(View view) { - View screenView = view.getRootView(); - screenView.setDrawingCacheEnabled(true); - Bitmap drawingCache = screenView.getDrawingCache(); - if (drawingCache != null) { - Bitmap bitmap = Bitmap.createBitmap(drawingCache); - screenView.setDrawingCacheEnabled(false); - return bitmap; - } - return null; - } - /* *Copies the content to the clipboard *