Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
Add Google Analytics.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Nov 1, 2014
1 parent 6c1fe5a commit 1b06512
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change Log
==========

0.3.1 *(2014-10-31)*
--------------------

Internal:
* Add Google Analytics events.


0.3.0 *(2014-10-31)*
--------------------

Expand Down
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Manifest version information!
def versionMajor = 0
def versionMinor = 4
def versionPatch = 0
def versionMinor = 3
def versionPatch = 1
def versionBuild = 0 // bump for dogfood builds, public betas, etc.

buildscript {
Expand Down Expand Up @@ -60,6 +60,7 @@ android {

dependencies {
compile 'com.android.support:support-annotations:21.0.0'
compile 'com.google.android.gms:play-services:6.1.11'
compile 'com.jakewharton:butterknife:6.0.0'
compile 'com.jakewharton.timber:timber:2.4.1'
compile 'com.bugsnag:bugsnag-android:2.2.2'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/jakewharton/telecine/Analytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jakewharton.telecine;

interface Analytics {
String CATEGORY_SETTINGS = "Settings";
String CATEGORY_RECORDING = "Recording";

String ACTION_CAPTURE_INTENT_LAUNCH = "Launch Overlay Launch";
String ACTION_CAPTURE_INTENT_RESULT = "Launch Overlay Result";
String ACTION_CHANGE_VIDEO_SIZE = "Change Video Size";
String ACTION_CHANGE_SHOW_COUNTDOWN = "Show Countdown";
String ACTION_CHANGE_HIDE_RECENTS = "Hide In Recents";
String ACTION_OVERLAY_SHOW = "Overlay Show";
String ACTION_OVERLAY_HIDE = "Overlay Hide";
String ACTION_OVERLAY_CANCEL = "Overlay Cancel";
String ACTION_RECORDING_START = "Recording Start";
String ACTION_RECORDING_STOP = "Recording Stop";

String VARIABLE_RECORDING_LENGTH = "Recording Length";
}
14 changes: 12 additions & 2 deletions app/src/main/java/com/jakewharton/telecine/BugsnagTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
import timber.log.Timber;

/**
* A logging implementation which buffers the last 100 messages and notifies on error exceptions.
* A logging implementation which buffers the last 200 messages and notifies on error exceptions.
*/
final class BugsnagTree extends Timber.HollowTree {
private static final int BUFFER_SIZE = 100;
private static final int BUFFER_SIZE = 200;

// Adding one to the initial size accounts for the add before remove.
private final Deque<String> buffer = new ArrayDeque<>(BUFFER_SIZE + 1);

@Override public void d(String message, Object... args) {
logMessage(Log.DEBUG, message, args);
}

@Override public void d(Throwable t, String message, Object... args) {
logMessage(Log.DEBUG, message, args);
}

@Override public void i(String message, Object... args) {
logMessage(Log.INFO, message, args);
}
Expand Down Expand Up @@ -71,6 +79,8 @@ private static String priorityToString(int priority) {
return "W";
case Log.INFO:
return "I";
case Log.DEBUG:
return "D";
default:
return String.valueOf(priority);
}
Expand Down
45 changes: 41 additions & 4 deletions app/src/main/java/com/jakewharton/telecine/RecordingSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.WindowManager;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.inject.Provider;
import timber.log.Timber;

Expand Down Expand Up @@ -57,6 +60,7 @@ interface Listener {
private final int resultCode;
private final Intent data;

private final Tracker tracker;
private final Provider<Boolean> showCountDown;
private final Provider<Integer> videoSizePercentage;

Expand All @@ -74,13 +78,15 @@ interface Listener {
private VirtualDisplay display;
private String outputFile;
private boolean running;
private long recordingStartNanos;

RecordingSession(Context context, Listener listener, int resultCode, Intent data,
RecordingSession(Context context, Listener listener, int resultCode, Intent data, Tracker tracker,
Provider<Boolean> showCountDown, Provider<Integer> videoSizePercentage) {
this.context = context;
this.listener = listener;
this.resultCode = resultCode;
this.data = data;
this.tracker = tracker;

this.showCountDown = showCountDown;
this.videoSizePercentage = videoSizePercentage;
Expand All @@ -98,7 +104,7 @@ public void showOverlay() {

OverlayView.Listener overlayListener = new OverlayView.Listener() {
@Override public void onCancel() {
cancelRecording();
cancelOverlay();
}

@Override public void onStart() {
Expand All @@ -111,25 +117,38 @@ public void showOverlay() {
};
overlayView = OverlayView.create(context, overlayListener, showCountDown.get());
windowManager.addView(overlayView, OverlayView.createLayoutParams(context));

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING).setAction(Analytics.ACTION_OVERLAY_SHOW).build());
}

private void hideOverlay() {
if (overlayView != null) {
Timber.d("Removing overlay view from window.");
windowManager.removeView(overlayView);
overlayView = null;

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING)
.setAction(Analytics.ACTION_OVERLAY_HIDE)
.build());
}
}

private void cancelRecording() {
private void cancelOverlay() {
hideOverlay();
listener.onEnd();

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING)
.setAction(Analytics.ACTION_OVERLAY_CANCEL)
.build());
}

private void startRecording() {
Timber.d("Starting screen recording...");

if (!outputRoot.mkdirs()) {
if (outputRoot.mkdirs()) {
Timber.e("Unable to create output directory '%s'.", outputRoot.getAbsolutePath());
// We're probably about to crash, but at least the log will indicate as to why.
}
Expand Down Expand Up @@ -168,8 +187,14 @@ private void startRecording() {

recorder.start();
running = true;
recordingStartNanos = System.nanoTime();

Timber.d("Screen recording started.");

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING)
.setAction(Analytics.ACTION_RECORDING_START)
.build());
}

private void stopRecording() {
Expand All @@ -188,9 +213,21 @@ private void stopRecording() {
// Stop the recorder which writes the contents to the file.
recorder.stop();

long recordingStopNanos = System.nanoTime();

recorder.release();
display.release();

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING)
.setAction(Analytics.ACTION_RECORDING_STOP)
.build());
tracker.send(new HitBuilders.TimingBuilder() //
.setCategory(Analytics.CATEGORY_RECORDING)
.setValue(TimeUnit.NANOSECONDS.toMillis(recordingStopNanos - recordingStartNanos))
.setVariable(Analytics.VARIABLE_RECORDING_LENGTH)
.build());

Timber.d("Screen recording stopped. Notifying media scanner of new video.");

MediaScannerConnection.scanFile(context, new String[] { outputFile }, null,
Expand Down
55 changes: 49 additions & 6 deletions app/src/main/java/com/jakewharton/telecine/TelecineActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnItemSelected;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import javax.inject.Inject;
import timber.log.Timber;

Expand All @@ -25,6 +27,8 @@ public final class TelecineActivity extends Activity {
@Inject @ShowCountdown BooleanPreference showCountdownPreference;
@Inject @HideFromRecents BooleanPreference hideFromRecentsPreference;

@Inject Tracker tracker;

private VideoSizePercentageAdapter videoSizePercentageAdapter;

@Override protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -52,25 +56,57 @@ public final class TelecineActivity extends Activity {
(MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
Intent intent = manager.createScreenCaptureIntent();
startActivityForResult(intent, CREATE_SCREEN_CAPTURE);

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SETTINGS)
.setAction(Analytics.ACTION_CAPTURE_INTENT_LAUNCH)
.build());
}

@OnItemSelected(R.id.spinner_video_size_percentage) void onVideoSizePercentageSelected(
int position) {
int newValue = videoSizePercentageAdapter.getItem(position);
Timber.d("Video size percentage changing to %s%%", newValue);
videoSizePreference.set(newValue);
int oldValue = videoSizePreference.get();
if (newValue != oldValue) {
Timber.d("Video size percentage changing to %s%%", newValue);
videoSizePreference.set(newValue);

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SETTINGS)
.setAction(Analytics.ACTION_CHANGE_VIDEO_SIZE)
.setValue(newValue)
.build());
}
}

@OnCheckedChanged(R.id.switch_show_countdown) void onShowCountdownChanged() {
boolean newValue = showCountdownView.isChecked();
Timber.d("Hide show countdown changing to %s", newValue);
showCountdownPreference.set(newValue);
boolean oldValue = showCountdownPreference.get();
if (newValue != oldValue) {
Timber.d("Hide show countdown changing to %s", newValue);
showCountdownPreference.set(newValue);

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SETTINGS)
.setAction(Analytics.ACTION_CHANGE_SHOW_COUNTDOWN)
.setValue(newValue ? 1 : 0)
.build());
}
}

@OnCheckedChanged(R.id.switch_hide_from_recents) void onHideFromRecentsChanged() {
boolean newValue = hideFromRecentsView.isChecked();
Timber.d("Hide from recents preference changing to %s", newValue);
hideFromRecentsPreference.set(newValue);
boolean oldValue = hideFromRecentsPreference.get();
if (newValue != oldValue) {
Timber.d("Hide from recents preference changing to %s", newValue);
hideFromRecentsPreference.set(newValue);

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SETTINGS)
.setAction(Analytics.ACTION_CHANGE_HIDE_RECENTS)
.setValue(newValue ? 1 : 0)
.build());
}
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand All @@ -82,6 +118,13 @@ public final class TelecineActivity extends Activity {
Timber.d("Acquired permission to screen capture. Starting service.");
startService(TelecineService.newIntent(this, resultCode, data));
}

tracker.send(new HitBuilders.EventBuilder() //
.setCategory(Analytics.CATEGORY_SETTINGS)
.setAction(Analytics.ACTION_CAPTURE_INTENT_RESULT)
.setValue(resultCode)
.build());

break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jakewharton.telecine;

import android.content.SharedPreferences;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import dagger.Module;
import dagger.Provides;
import javax.inject.Singleton;
Expand All @@ -22,6 +24,11 @@ final class TelecineModule {
this.app = app;
}

@Provides @Singleton Tracker provideAnalyticsTracker() {
GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(app);
return googleAnalytics.newTracker(R.xml.analytics_tracker);
}

@Provides @Singleton SharedPreferences provideSharedPreferences() {
return app.getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.NonNull;
import com.google.android.gms.analytics.Tracker;
import javax.inject.Inject;
import javax.inject.Provider;
import timber.log.Timber;
Expand All @@ -23,6 +24,8 @@ public static Intent newIntent(Context context, int resultCode, Intent data) {
@Inject @ShowCountdown Provider<Boolean> showCountdownProvider;
@Inject @VideoSizePercentage Provider<Integer> videoSizePercentageProvider;

@Inject Tracker tracker;

private boolean running;
private RecordingSession recordingSession;

Expand All @@ -49,8 +52,9 @@ public static Intent newIntent(Context context, int resultCode, Intent data) {

((TelecineApplication) getApplication()).inject(this);

recordingSession = new RecordingSession(this, listener, resultCode, data, showCountdownProvider,
videoSizePercentageProvider);
recordingSession =
new RecordingSession(this, listener, resultCode, data, tracker, showCountdownProvider,
videoSizePercentageProvider);
recordingSession.showOverlay();

return START_NOT_STICKY;
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/analytics_tracker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<string name="ga_trackingId">UA-3637749-20</string>
</resources>

0 comments on commit 1b06512

Please sign in to comment.