ScreenshotDetection

Screenshot Detection Library

License

License

GroupId

GroupId

com.akexorcist
ArtifactId

ArtifactId

screenshotdetection
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

ScreenshotDetection
Screenshot Detection Library
Project URL

Project URL

https://github.com/akexorcist/Android-ScreenshotDetection
Source Code Management

Source Code Management

https://github.com/akexorcist/Android-ScreenshotDetection

Download screenshotdetection

How to add to project

<!-- https://jarcasting.com/artifacts/com.akexorcist/screenshotdetection/ -->
<dependency>
    <groupId>com.akexorcist</groupId>
    <artifactId>screenshotdetection</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.akexorcist/screenshotdetection/
implementation 'com.akexorcist:screenshotdetection:1.0.0'
// https://jarcasting.com/artifacts/com.akexorcist/screenshotdetection/
implementation ("com.akexorcist:screenshotdetection:1.0.0")
'com.akexorcist:screenshotdetection:aar:1.0.0'
<dependency org="com.akexorcist" name="screenshotdetection" rev="1.0.0">
  <artifact name="screenshotdetection" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.akexorcist', module='screenshotdetection', version='1.0.0')
)
libraryDependencies += "com.akexorcist" % "screenshotdetection" % "1.0.0"
[com.akexorcist/screenshotdetection "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.android.support » support-v4 jar 26.1.0

Project Modules

There are no modules declared in this project.

Android Arsenal Maven Central Minimum SDK Version Workflow Status

Android-ScreenshotDetection

Screenshot Detection Library

Download

Since version 1.0.1 will move from JCenter to MavenCentral

// build.gradle (project)
allprojects {
    repositories {
        mavenCentral()
        /* ... */
    }
}

Gradle

implementation 'com.akexorcist:screenshot-detection:1.0.1'

Permission in this library

This library has declared the permission to read the external storage. So you need to handle the runtime permission by yourself. If not, app will not crash and screenshot detection still work but no file path.

Usage

Implement the library to your activity or your base activity.

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

import com.akexorcist.screenshotdetection.ScreenshotDetectionDelegate

open class ScreenshotDetectionActivity : AppCompatActivity(), ScreenshotDetectionDelegate.ScreenshotDetectionListener {
    private val screenshotDetectionDelegate = ScreenshotDetectionDelegate(this, this)

    override fun onStart() {
        super.onStart()
        screenshotDetectionDelegate.startScreenshotDetection()
    }

    override fun onStop() {
        super.onStop()
        screenshotDetectionDelegate.stopScreenshotDetection()
    }

    override fun onScreenCaptured(path: String) {
        // Do something when screen was captured
    }

    override fun onScreenCapturedWithDeniedPermission() {
        // Do something when screen was captured but read external storage permission has denied
    }
}

But above example will not work because read the external storage permission has denied. To fix this, you need to add the code for runtime permission request.

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

open class ScreenshotDetectionActivity : AppCompatActivity(), ScreenshotDetectionDelegate.ScreenshotDetectionListener {
    /* ... */

    companion object {
        private const val REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION = 3009
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkReadExternalStoragePermission()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION -> {
                if (grantResults.getOrNull(0) == PackageManager.PERMISSION_DENIED) {
                    showReadExternalStoragePermissionDeniedMessage()
                }
            }
            else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    private fun checkReadExternalStoragePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestReadExternalStoragePermission()
        }
    }

    private fun requestReadExternalStoragePermission() {
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSION)
    }

    private fun showReadExternalStoragePermissionDeniedMessage() {
        Toast.makeText(this, "Read external storage permission has denied", Toast.LENGTH_SHORT).show()
    }
}

Then extends your target activity with that base activity class and declare onScreenCaptured(path: String) and onScreenCapturedWithDeniedPermission() when you want to detect the screenshot.

import android.os.Bundle
import android.widget.Toast

class MainActivity : ScreenshotDetectionActivity() {
	/* ... */

    override fun onScreenCaptured(path: String) {
        Toast.make(this, path, Toast.LENGTH_SHORT).show();
        // Do something when screen was captured
    }

    override fun onScreenCapturedWithDeniedPermission() {
        Toast.make(this, "Please grant read external storage permission for screenshot detection", Toast.LENGTH_SHORT).show()
        // Do something when screen was captured but read external storage permission has denied
    }
}

Demo

Demo

Licence

Copyright 2021 Akexorcist

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
1.0.0