Skip to content

Some fixes to the test app's screenshot detection trigger #4145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private Bitmap readThumbnail() {
thumbnail =
ImageUtils.readScaledImage(
getContentResolver(), screenshotUri, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
} catch (IOException e) {
} catch (IOException | SecurityException e) {
LogWrapper.getInstance()
.e(TAG, "Could not read screenshot image from URI: " + screenshotUri, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static ImageSize read(InputStream inputStream) {
*
* @return the image, or null if it could not be decoded
* @throws IllegalArgumentException if target height or width are less than or equal to zero
* @throws SecurityException if the app does not have access to the image
* @throws IOException if the image can't be read
*/
@Nullable
public static Bitmap readScaledImage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ class ScreenshotDetectionFeedbackTrigger extends ContentObserver {
private static final boolean SHOULD_CHECK_IF_PENDING = Build.VERSION.SDK_INT >= 29;
private static final String[] PROJECTION =
SHOULD_CHECK_IF_PENDING
? new String[] {MediaStore.Images.Media.DATA}
: new String[] {
MediaStore.Images.Media.DATA, android.provider.MediaStore.MediaColumns.IS_PENDING
};
? new String[] {MediaStore.Images.Media.DATA, MediaStore.MediaColumns.IS_PENDING}
: new String[] {MediaStore.Images.Media.DATA};
private final Set<Uri> seenImages = new HashSet<>();

private final AppCompatActivity activity;
Expand All @@ -58,6 +56,8 @@ class ScreenshotDetectionFeedbackTrigger extends ContentObserver {

private Uri currentUri;

private boolean hasRequestedPermission = false;

/**
* Creates a FeedbackTriggers instance for an activity.
*
Expand Down Expand Up @@ -102,13 +102,25 @@ void unRegisterScreenshotObserver() {
@Override
public void onChange(boolean selfChange, Uri uri) {
if (!uri.toString().matches(String.format("%s/[0-9]+", Media.EXTERNAL_CONTENT_URI))
|| !seenImages.add(uri)) {
|| seenImages.contains(uri)) {
return;
}

if (ContextCompat.checkSelfPermission(activity, PERMISSION_TO_REQUEST) == PERMISSION_GRANTED) {
maybeStartFeedbackForScreenshot(uri);
} else if (activity.shouldShowRequestPermissionRationale(PERMISSION_TO_REQUEST)) {
} else if (hasRequestedPermission) {
Log.i(
TAG,
"We've already request permission. Not requesting again for the life of the activity.");
} else {
// Set an in memory flag so we don't ask them again right away
hasRequestedPermission = true;
requestReadPermission(uri);
}
}

private void requestReadPermission(Uri uri) {
if (activity.shouldShowRequestPermissionRationale(PERMISSION_TO_REQUEST)) {
Log.i(TAG, "Showing customer rationale for requesting permission.");
new AlertDialog.Builder(activity)
.setMessage(
Expand All @@ -122,7 +134,7 @@ public void onChange(boolean selfChange, Uri uri) {
})
.show();
} else {
Log.i(TAG, "Does not have permission. Launching request.");
Log.i(TAG, "Launching request for permission without rationale.");
currentUri = uri;
requestPermissionLauncher.launch(PERMISSION_TO_REQUEST);
}
Expand All @@ -133,8 +145,13 @@ private void maybeStartFeedbackForScreenshot(Uri uri) {
try {
cursor = activity.getContentResolver().query(uri, PROJECTION, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
// TODO: check if it's pending
// (http://google3/lens/screenshots/demo/java/com/google/android/lensonscreenshots/ScreenshotDetector.java?l=184)
if (SHOULD_CHECK_IF_PENDING
&& cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.IS_PENDING))
== 1) {
Log.i(TAG, "Ignoring pending image: " + uri);
return;
}
seenImages.add(uri);
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
Log.i(TAG, "Path: " + path);
if (path.toLowerCase().contains("screenshot")) {
Expand Down