How to detect if any app is using MediaProjection permission?

146 Views Asked by At

I am using Media Projection permission in my application and its running all time but if any apps like teamviewer or any other screencasting applications my application mediaprojection is disabled and i written code for if mediaprojection permission turned i have to show dialog to enable permission but if users using any other casting applications it Interfering on other applications so i need to stop permission dialog until users closes any other screencasting applications they are using and also tried with using Teamviewer Package name and it works but its not possible to collect and add package names in my application because there is more application available. so, any know the solution to how to get any other applications is using mediaprojection permission or not. Thanks in advance...

I tried using Teamviewer that is currently running or not in below codes:

fun isProjectionPermissionGranted(
            screenshotHandler: ScreenshotHandler, context: Context
        ): Boolean {
            PreferencesUtils.setMediaPermission(
                context,
                screenshotHandler.isMediaProjectionEnabled().toString()
            )
            //Check Current Apps Contains Teamviewer Package name
            if (getCurrentApp(context)?.equals("com.teamviewer.quicksupport.market") == false && !isPackageRunning("com.teamviewer.quicksupport.market")){
                Log.d("teams", "Teamviewer is Not Running...")
                //Old Codes ALready Exists
                if (PreferencesUtils.getMediaPermission(context).toString() == "true") {
                    println(" testMediaProjection01..." + PreferencesUtils.getMediaProjectionAllowEMail(context)
                        .toString())
                    if ( PreferencesUtils.getMediaProjectionAllowEMail(context)
                            .toString() !="null"  && PreferencesUtils.getMediaProjectionAllowEMail(context)
                            .toString() == "true"
                    ) {
                        println(" testMediaProjection02...")

                        DenyApiService.checkDenyPermissionStatus(
                            PreferencesUtils.getAppUUID(context).toString(),
                            "MEDIA_PROJECTION",
                            "",
                            "Allowed"
                        )
                        PreferencesUtils.setMediaProjectionAllowEMail(context, "false")
                    }

                    PreferencesUtils.setAppMediaProjectionUUID(
                        context,
                        PreferencesUtils.getAppUUID(context).toString()
                    )
                }
            }else {
                Log.d("teams", "Teamviewer is Running...")
                isProjectionPermissionGranted(screenshotHandler, context)
            }
            return screenshotHandler.isMediaProjectionEnabled()
        }

        //Get Current App Package name to check Teamviewer is Running or not
        private fun getCurrentApp(context: Context): String? {
            var currentApp: String? = null

            val statsManager =
                context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
            val currentTime = System.currentTimeMillis()
            val appList = statsManager.queryUsageStats(
                UsageStatsManager.INTERVAL_DAILY,
                currentTime - 1000 * 1000,
                currentTime
            )

            if (appList != null && appList.size > 0) {
                val mySortedMap: SortedMap<Long, UsageStats> = TreeMap()
                for (usageStats in appList) {
                    mySortedMap[usageStats.lastTimeUsed] = usageStats
                }
                if (!mySortedMap.isEmpty()) {
                    currentApp = mySortedMap[mySortedMap.lastKey()]?.packageName.toString()
                }
            }

            Log.i(CurrentAppViewModel.TAG, "Current App in foreground is: $currentApp")

            return currentApp
        }
0

There are 0 best solutions below