Last active
November 20, 2017 02:56
-
-
Save tianbin-dev/f7de98c36b6f567d8729b1194ae0edd8 to your computer and use it in GitHub Desktop.
Android 监听截屏工具类
This file contains 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
public class RxDetectScreenShot { | |
private final String EXTERNAL_CONTENT_URI_MATCHER = | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString(); | |
private final String[] PROJECTION = new String[]{ | |
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA, | |
MediaStore.Images.Media.DATE_ADDED | |
}; | |
private final String SORT_ORDER = MediaStore.Images.Media.DATE_ADDED + " DESC"; | |
private final long DEFAULT_DETECT_WINDOW_SECONDS = 10; | |
private ContentResolver mContentResolver; | |
private ContentObserver mContentObserver; | |
private WeakReference<Context> mContextWeakReference; | |
public static RxDetectScreenShot newInstance(Context context) { | |
return new RxDetectScreenShot(context); | |
} | |
private RxDetectScreenShot(Context context) { | |
mContextWeakReference = new WeakReference<>(context); | |
} | |
public Observable<String> createDetectScreenShotObservable() { | |
return Observable.unsafeCreate( | |
new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(final Subscriber<? super String> subscriber) { | |
mContentResolver = mContextWeakReference.get().getContentResolver(); | |
mContentObserver = new ContentObserver(null) { | |
@Override | |
public void onChange(boolean selfChange, Uri uri) { | |
filterUri(uri, subscriber); | |
super.onChange(selfChange, uri); | |
} | |
}; | |
mContentResolver.registerContentObserver( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, mContentObserver); | |
} | |
}) | |
.doOnUnsubscribe(new Action0() { | |
@Override | |
public void call() { | |
mContentResolver.unregisterContentObserver(mContentObserver); | |
mContextWeakReference.clear(); | |
} | |
}); | |
} | |
private void filterUri(Uri uri, Subscriber<? super String> subscriber) { | |
if (!uri.toString().startsWith(EXTERNAL_CONTENT_URI_MATCHER)) { | |
return; | |
} | |
Cursor cursor = null; | |
try { | |
cursor = mContentResolver.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; | |
if (matchPath(path) && matchTime(currentTime, dateAdded)) { | |
subscriber.onNext(path); | |
} | |
} | |
} catch (Exception e) { | |
subscriber.onError(e); | |
CrashPlatform.postCatchedException("ScreenShot detect exception --- " + e); | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
} | |
private boolean matchPath(String path) { | |
return path.toLowerCase().contains("screenshot") || path.contains("截屏") || path.contains("截图"); | |
} | |
private boolean matchTime(long currentTime, long dateAdded) { | |
return Math.abs(currentTime - dateAdded) <= DEFAULT_DETECT_WINDOW_SECONDS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment