Last active
April 8, 2016 15:03
-
-
Save Piasy/8cda8672231529c430dc to your computer and use it in GitHub Desktop.
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
private static final String TAG = "RxScreenshotDetector"; | |
private static final String EXTERNAL_CONTENT_URI_MATCHER = | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString(); | |
private static final String[] PROJECTION = new String[] { | |
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA, | |
MediaStore.Images.Media.DATE_ADDED | |
}; | |
private static final String SORT_ORDER = MediaStore.Images.Media.DATE_ADDED + " DESC"; | |
private static final long DEFAULT_DETECT_WINDOW_SECONDS = 10; | |
final ContentResolver contentResolver = context.getContentResolver(); | |
final ContentObserver contentObserver = new ContentObserver(null) { | |
@Override | |
public void onChange(boolean selfChange, Uri uri) { | |
Log.d(TAG, "onChange: " + selfChange + ", " + uri.toString()); | |
if (uri.toString().matches(EXTERNAL_CONTENT_URI_MATCHER)) { | |
Cursor cursor = null; | |
try { | |
cursor = contentResolver.query(uri, PROJECTION, null, null, | |
SORT_ORDER); | |
if (cursor != null && cursor.moveToFirst()) { | |
String path = cursor.getString( | |
cursor.getColumnIndex(MediaStore.Images.Media.DATA)); | |
long dateAdded = cursor.getLong(cursor.getColumnIndex( | |
MediaStore.Images.Media.DATE_ADDED)); | |
long currentTime = System.currentTimeMillis() / 1000; | |
Log.d(TAG, "path: " + path + ", dateAdded: " + dateAdded + | |
", currentTime: " + currentTime); | |
if (path.toLowerCase().contains("screenshot") && | |
Math.abs(currentTime - dateAdded) <= | |
DEFAULT_DETECT_WINDOW_SECONDS) { | |
// screenshot added! | |
} | |
} | |
} catch (Exception e) { | |
Log.d(TAG, "open cursor fail"); | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
} | |
super.onChange(selfChange, uri); | |
} | |
}; | |
contentResolver.registerContentObserver( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, contentObserver); |
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
RxScreenshotDetector.start(getApplicationContext()) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.compose(this.<String>bindUntilEvent(ActivityEvent.PAUSE)) | |
.subscribe(new Subscriber<String>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
e.printStackTrace(); | |
} | |
@Override | |
public void onNext(String path) { | |
mTextView.setText(mTextView.getText() + "\nScreenshot: " + path); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment