@@ -45,10 +45,8 @@ class ScreenshotDetectionFeedbackTrigger extends ContentObserver {
45
45
private static final boolean SHOULD_CHECK_IF_PENDING = Build .VERSION .SDK_INT >= 29 ;
46
46
private static final String [] PROJECTION =
47
47
SHOULD_CHECK_IF_PENDING
48
- ? new String [] {MediaStore .Images .Media .DATA }
49
- : new String [] {
50
- MediaStore .Images .Media .DATA , android .provider .MediaStore .MediaColumns .IS_PENDING
51
- };
48
+ ? new String [] {MediaStore .Images .Media .DATA , MediaStore .MediaColumns .IS_PENDING }
49
+ : new String [] {MediaStore .Images .Media .DATA };
52
50
private final Set <Uri > seenImages = new HashSet <>();
53
51
54
52
private final AppCompatActivity activity ;
@@ -58,6 +56,8 @@ class ScreenshotDetectionFeedbackTrigger extends ContentObserver {
58
56
59
57
private Uri currentUri ;
60
58
59
+ private boolean hasRequestedPermission = false ;
60
+
61
61
/**
62
62
* Creates a FeedbackTriggers instance for an activity.
63
63
*
@@ -102,13 +102,25 @@ void unRegisterScreenshotObserver() {
102
102
@ Override
103
103
public void onChange (boolean selfChange , Uri uri ) {
104
104
if (!uri .toString ().matches (String .format ("%s/[0-9]+" , Media .EXTERNAL_CONTENT_URI ))
105
- || ! seenImages .add (uri )) {
105
+ || seenImages .contains (uri )) {
106
106
return ;
107
107
}
108
108
109
109
if (ContextCompat .checkSelfPermission (activity , PERMISSION_TO_REQUEST ) == PERMISSION_GRANTED ) {
110
110
maybeStartFeedbackForScreenshot (uri );
111
- } else if (activity .shouldShowRequestPermissionRationale (PERMISSION_TO_REQUEST )) {
111
+ } else if (hasRequestedPermission ) {
112
+ Log .i (
113
+ TAG ,
114
+ "We've already request permission. Not requesting again for the life of the activity." );
115
+ } else {
116
+ // Set an in memory flag so we don't ask them again right away
117
+ hasRequestedPermission = true ;
118
+ requestReadPermission (uri );
119
+ }
120
+ }
121
+
122
+ private void requestReadPermission (Uri uri ) {
123
+ if (activity .shouldShowRequestPermissionRationale (PERMISSION_TO_REQUEST )) {
112
124
Log .i (TAG , "Showing customer rationale for requesting permission." );
113
125
new AlertDialog .Builder (activity )
114
126
.setMessage (
@@ -122,7 +134,7 @@ public void onChange(boolean selfChange, Uri uri) {
122
134
})
123
135
.show ();
124
136
} else {
125
- Log .i (TAG , "Does not have permission. Launching request ." );
137
+ Log .i (TAG , "Launching request for permission without rationale ." );
126
138
currentUri = uri ;
127
139
requestPermissionLauncher .launch (PERMISSION_TO_REQUEST );
128
140
}
@@ -133,8 +145,13 @@ private void maybeStartFeedbackForScreenshot(Uri uri) {
133
145
try {
134
146
cursor = activity .getContentResolver ().query (uri , PROJECTION , null , null , null );
135
147
if (cursor != null && cursor .moveToFirst ()) {
136
- // TODO: check if it's pending
137
- // (http://google3/lens/screenshots/demo/java/com/google/android/lensonscreenshots/ScreenshotDetector.java?l=184)
148
+ if (SHOULD_CHECK_IF_PENDING
149
+ && cursor .getInt (cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .IS_PENDING ))
150
+ == 1 ) {
151
+ Log .i (TAG , "Ignoring pending image: " + uri );
152
+ return ;
153
+ }
154
+ seenImages .add (uri );
138
155
String path = cursor .getString (cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA ));
139
156
Log .i (TAG , "Path: " + path );
140
157
if (path .toLowerCase ().contains ("screenshot" )) {
0 commit comments