Created
November 5, 2016 20:25
-
-
Save gotev/c3ff516f6c0472054f6b7f870fc48d2d to your computer and use it in GitHub Desktop.
Android Screenshot Detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.gotev.gapp.utils; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.os.Environment; | |
import android.os.FileObserver; | |
import android.os.Handler; | |
import android.util.Log; | |
import java.io.File; | |
/** | |
* Detects when user performs a screenshot. | |
* Permissions needed in Manifest: | |
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
* <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
* | |
* For Android 6.0+ also the following runtime permissions are needed to be able to | |
* observe for screenshots: | |
* Manifest.permission.READ_EXTERNAL_STORAGE | |
* Manifest.permission.WRITE_EXTERNAL_STORAGE | |
* | |
* Usage: | |
ScreenshotDetector screenshotDetector = | |
new ScreenshotDetector(context, new ScreenshotDetector.OnScreenshotTakenListener() { | |
@Override | |
public void onScreenshotTaken(Uri uri) { | |
} | |
@Override | |
public void onScreenshotDeleted(Uri uri) { | |
} | |
}); | |
screenshotDetector.start(); | |
* | |
* Remember to invoke: | |
* screenshotDetector.stop(); | |
* | |
* in the activity's onDestroy Method. | |
* | |
* @author Alex Gotev ([email protected]) | |
*/ | |
public class ScreenshotDetector extends FileObserver { | |
private static final String PATH = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots/"; | |
public interface OnScreenshotTakenListener { | |
void onScreenshotTaken(Uri uri); | |
void onScreenshotDeleted(Uri uri); | |
} | |
private boolean deleteScreenshot = false; | |
private OnScreenshotTakenListener mListener; | |
private String mLastTakenPath; | |
private Handler mainThreadHandler; | |
public ScreenshotDetector(Context context, OnScreenshotTakenListener listener) { | |
super(PATH, FileObserver.CLOSE_WRITE); | |
mainThreadHandler = new Handler(context.getMainLooper()); | |
mListener = listener; | |
} | |
@Override | |
public void onEvent(int event, String path) { | |
Log.d(getClass().getSimpleName(), "Event:" + event + "\t" + path); | |
if (path == null || event != FileObserver.CLOSE_WRITE) | |
Log.d(getClass().getSimpleName(), "Not important"); | |
else if (mLastTakenPath != null && path.equalsIgnoreCase(mLastTakenPath)) | |
Log.d(getClass().getSimpleName(), "This event has been observed before."); | |
else { | |
mLastTakenPath = path; | |
File file = new File(PATH + path); | |
if (deleteScreenshot) { | |
if (file.exists()) | |
file.delete(); | |
if (mListener != null) | |
mainThreadHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mListener.onScreenshotDeleted(Uri.fromFile(file)); | |
} | |
}); | |
} else { | |
if (mListener != null) | |
mainThreadHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mListener.onScreenshotTaken(Uri.fromFile(file)); | |
} | |
}); | |
} | |
} | |
} | |
public void start() { | |
super.startWatching(); | |
} | |
public void stop() { | |
super.stopWatching(); | |
} | |
public void deleteScreenshot(boolean deleteScreenshot) { | |
this.deleteScreenshot = deleteScreenshot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@gotev Could you add a license statement here?