Bug 1229274 - Investigate tracking when a screenshot is taken in Firefox r=liuche f=nalexander

This commit is contained in:
Mark Finkle 2015-12-17 13:04:47 -05:00
parent 518f0b588b
commit b2f4665d6e
3 changed files with 147 additions and 0 deletions

View File

@ -75,6 +75,7 @@ import org.mozilla.gecko.util.MenuUtils;
import org.mozilla.gecko.util.NativeEventListener;
import org.mozilla.gecko.util.NativeJSObject;
import org.mozilla.gecko.util.PrefUtils;
import org.mozilla.gecko.util.ScreenshotObserver;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UIAsyncTask;
@ -88,6 +89,7 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -107,6 +109,7 @@ import android.nfc.NfcEvent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
@ -268,6 +271,7 @@ public class BrowserApp extends GeckoApp
private boolean mHideWebContentOnAnimationEnd;
private final DynamicToolbar mDynamicToolbar = new DynamicToolbar();
private final ScreenshotObserver mScreenshotObserver = new ScreenshotObserver();
@Override
public View onCreateView(final String name, final Context context, final AttributeSet attrs) {
@ -726,6 +730,15 @@ public class BrowserApp extends GeckoApp
}
});
// Watch for screenshots while browser is in foreground.
mScreenshotObserver.setListener(getContext(), new ScreenshotObserver.OnScreenshotListener() {
@Override
public void onScreenshotTaken(String data, String title) {
// Treat screenshots as a sharing method.
Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.BUTTON, "screenshot");
}
});
// Set the maximum bits-per-pixel the favicon system cares about.
IconDirectoryEntry.setMaxBPP(GeckoAppShell.getScreenDepth());
}
@ -905,6 +918,8 @@ public class BrowserApp extends GeckoApp
"Prompt:ShowTop");
processTabQueue();
mScreenshotObserver.start();
}
@Override
@ -917,6 +932,8 @@ public class BrowserApp extends GeckoApp
// Register for Prompt:ShowTop so we can foreground this activity even if it's hidden.
EventDispatcher.getInstance().registerGeckoThreadListener((GeckoEventListener) this,
"Prompt:ShowTop");
mScreenshotObserver.stop();
}
@Override

View File

@ -0,0 +1,129 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.util;
import org.mozilla.gecko.AppConstants.Versions;
import org.mozilla.gecko.util.ThreadUtils;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
public class ScreenshotObserver {
private static final String LOGTAG = "GeckoScreenshotObserver";
public Context context;
/**
* Listener for screenshot changes.
*/
public interface OnScreenshotListener {
/**
* This callback is executed on the UI thread.
*/
public void onScreenshotTaken(String data, String title);
}
private OnScreenshotListener listener;
public ScreenshotObserver() {
}
public void setListener(Context context, OnScreenshotListener listener) {
this.context = context;
this.listener = listener;
}
private MediaObserver mediaObserver;
private String[] mediaProjections = new String[] {
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.TITLE
};
public void start() {
if (!Versions.feature14Plus) {
return;
}
try {
if (mediaObserver == null) {
mediaObserver = new MediaObserver();
context.getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, mediaObserver);
}
} catch (Exception e) {
Log.e(LOGTAG, "Failure to start watching media: ", e);
}
}
public void stop() {
if (!Versions.feature14Plus) {
return;
}
try {
context.getContentResolver().unregisterContentObserver(mediaObserver);
mediaObserver = null;
} catch (Exception e) {
Log.e(LOGTAG, "Failure to stop watching media: ", e);
}
}
public void onMediaChange(final Uri uri) {
// Make sure we are on not on the main thread.
final ContentResolver cr = context.getContentResolver();
ThreadUtils.postToBackgroundThread(new Runnable() {
@Override
public void run() {
// Find the most recent image added to the MediaStore and see if it's a screenshot.
try {
Cursor cursor = cr.query(uri, mediaProjections, null, null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC LIMIT 1");
if (cursor == null) {
return;
}
while (cursor.moveToNext()) {
String data = cursor.getString(0);
Log.i(LOGTAG, "data: " + data);
String display = cursor.getString(1);
Log.i(LOGTAG, "display: " + display);
String album = cursor.getString(2);
Log.i(LOGTAG, "album: " + album);
long date = cursor.getLong(3);
String title = cursor.getString(4);
Log.i(LOGTAG, "title: " + title);
if (album != null && album.toLowerCase().contains("screenshot")) {
if (listener != null) {
listener.onScreenshotTaken(data, title);
break;
}
}
}
cursor.close();
} catch (Exception e) {
Log.e(LOGTAG, "Failure to process media change: ", e);
}
}
});
}
private class MediaObserver extends ContentObserver {
public MediaObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
onMediaChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
}
}

View File

@ -118,6 +118,7 @@ gujar.sources += ['java/org/mozilla/gecko/' + x for x in [
'util/PrefUtils.java',
'util/ProxySelector.java',
'util/RawResource.java',
'util/ScreenshotObserver.java',
'util/StringUtils.java',
'util/ThreadUtils.java',
'util/UIAsyncTask.java',