Skip to content

Commit

Permalink
Add Firebase Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Oct 1, 2017
1 parent d3f97cf commit 29c0d86
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/
.idea_modules/

/app/google-services.json
9 changes: 6 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
applicationId 'com.stevenschoen.putionew'
minSdkVersion 19
targetSdkVersion 26
versionCode 124
versionName '4.2 beta 5'
versionCode 125
versionName '4.2'
multiDexEnabled true
}
buildTypes {
Expand Down Expand Up @@ -77,6 +77,7 @@ dependencies {
compile "com.android.support:cardview-v7:$supportLibVersion"
compile "com.android.support:mediarouter-v7:$supportLibVersion"
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.google.android.gms:play-services-cast-framework:11.0.4'
compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
transitive = true;
Expand Down Expand Up @@ -111,4 +112,6 @@ dependencies {
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-beta1'
kapt "android.arch.persistence.room:compiler:1.0.0-beta1"
compile 'android.arch.persistence.room:rxjava2:1.0.0-beta1'
}
}

apply plugin: 'com.google.gms.google-services'
100 changes: 100 additions & 0 deletions app/src/main/java/com/stevenschoen/putionew/Analytics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.stevenschoen.putionew

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import com.google.firebase.analytics.FirebaseAnalytics
import com.stevenschoen.putionew.model.files.PutioFile

class Analytics(context: Context) {

object Event {
const val VIEW_FOLDER = "view_folder"
const val DOWNLOAD_ITEM = "download_item"
const val FINISH_DOWNLOAD = "finish_download"
const val OPEN_DOWNLOADED_FILE = "open_downloaded_file"
const val OPEN_DOWNLOADED_VIDEO = "open_downloaded_video"
const val STREAM_VIDEO = "stream_video"
}

object Param {
const val CONTENT_SIZE = "content_size"
const val IS_VIDEO = "is_video"
const val MP4_SELECTED = "mp4_selected"
}

private val putioApp = putioApp(context)

fun logViewedFile(file: PutioFile) {
makeFileBundle(file)
.log(FirebaseAnalytics.Event.VIEW_ITEM)
}

fun logBrowsedToFolder(folder: PutioFile) {
makeFolderBundle(folder)
.log(Event.VIEW_FOLDER)
}

fun logSearched(query: String) {
Bundle().putString(FirebaseAnalytics.Param.SEARCH_TERM, query)
.log(FirebaseAnalytics.Event.SEARCH)
}

fun logStartedFileDownload(file: PutioFile) {
makeFileBundle(file)
.log(Event.DOWNLOAD_ITEM)
}

fun logStartedVideoDownload(video: PutioFile, mp4: Boolean) {
makeFileBundle(video)
.putBoolean(Param.MP4_SELECTED, mp4)
.log(Event.DOWNLOAD_ITEM)
}

fun logDownloadFinished(file: PutioFile) {
makeFileBundle(file)
.log(Event.FINISH_DOWNLOAD)
}

fun logOpenedDownloadedFile(file: PutioFile) {
makeFileBundle(file)
.log(Event.OPEN_DOWNLOADED_FILE)
}

fun logOpenedDownloadedVideo(video: PutioFile, mp4: Boolean) {
makeFileBundle(video)
.putBoolean(Param.MP4_SELECTED, mp4)
.log(Event.OPEN_DOWNLOADED_VIDEO)
}

fun logStreamedVideo(video: PutioFile, mp4: Boolean) {
makeFileBundle(video)
.putBoolean(Param.MP4_SELECTED, mp4)
.log(Event.STREAM_VIDEO)
}

private fun makeFileBundle(file: PutioFile) = Bundle().apply { addBasicFileInfo(file) }
private fun makeFolderBundle(folder: PutioFile) = Bundle().apply { addIdAndName(folder) }

private fun Bundle.addBasicFileInfo(file: PutioFile) {
addIdAndName(file)
putString(FirebaseAnalytics.Param.CONTENT_TYPE, file.contentType)
file.size?.let { putLong(Param.CONTENT_SIZE, it) }
putBoolean(Param.IS_VIDEO, file.isVideo)
}

private fun Bundle.addIdAndName(file: PutioFile) {
putLong(FirebaseAnalytics.Param.ITEM_ID, file.id)
putString(FirebaseAnalytics.Param.ITEM_NAME, file.name)
}

private fun Bundle.log(event: String) = putioApp.firebaseAnalytics.logEvent(event, this)
}

val Context.analytics
get() = Analytics(this)
val Fragment.analytics
get() = Analytics(context)

private val Context.firebaseAnalytics
get() = FirebaseAnalytics.getInstance(this)!!
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.support.v4.app.JobIntentService
import com.stevenschoen.putionew.analytics
import com.stevenschoen.putionew.putioApp

class DownloadFinishedService : JobIntentService() {
Expand Down Expand Up @@ -36,6 +37,13 @@ class DownloadFinishedService : JobIntentService() {
})
}
}

putioApp.fileDownloadDatabase.fileDownloadsDao()
.getByDownloadId(downloadId)
.map { it.fileId }
.flatMapSingle(putioApp.putioUtils!!.restInterface::file)
.map { it.file }
.subscribe(analytics::logDownloadFinished)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,24 @@ class FileDetailsFragment : RxFragment() {
private fun play(mp4: Boolean, downloaded: Boolean) {
if (downloaded && castCallbacks?.isCasting() != true) {
open()
analytics.logOpenedDownloadedVideo(file, mp4)
} else {
val url = file.getStreamUrl(putioApp.putioUtils!!, mp4)
castCallbacks?.load(file, url, putioApp.putioUtils!!)
analytics.logStreamedVideo(file, mp4)
}
}

private fun download(mp4: Boolean = false) {
if (fileDownloadHelper.hasPermission()) {
if (file.isVideo) {
fileDownloadHelper.downloadVideo(file, mp4 && !file.isMp4)
fileDownloadHelper.downloadVideo(file, mp4 && !file.isMp4).also {
analytics.logStartedVideoDownload(file, mp4)
}
} else {
fileDownloadHelper.downloadFile(file)
fileDownloadHelper.downloadFile(file).also {
analytics.logStartedFileDownload(file)
}
}.subscribe()
} else {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
Expand All @@ -458,6 +464,7 @@ class FileDetailsFragment : RxFragment() {
Toast.makeText(context, "Error opening", Toast.LENGTH_SHORT).show()
PutioUtils.getRxJavaThrowable(error).printStackTrace()
})
if (!file.isVideo) analytics.logOpenedDownloadedFile(file)
}

private fun deleteDownload() {
Expand Down
40 changes: 19 additions & 21 deletions app/src/main/java/com/stevenschoen/putionew/files/FilesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.stevenschoen.putionew.PutioApplication
import com.stevenschoen.putionew.R
import com.stevenschoen.putionew.UIUtils
import com.stevenschoen.putionew.*
import com.stevenschoen.putionew.model.files.PutioFile
import com.stevenschoen.putionew.putioApp
import com.trello.rxlifecycle2.components.support.RxFragment
import com.trello.rxlifecycle2.kotlin.bindToLifecycle
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -160,6 +157,12 @@ open class FilesFragment : RxFragment() {

fileListFragmentsAdapter.notifyDataSetChanged()
pagerView!!.setCurrentItem(pages.lastIndex, true)

if (file.isFolder) {
analytics.logBrowsedToFolder(file)
} else {
analytics.logViewedFile(file)
}
}

fun addSearch(query: String) {
Expand All @@ -168,6 +171,8 @@ open class FilesFragment : RxFragment() {

fileListFragmentsAdapter.notifyDataSetChanged()
pagerView!!.setCurrentItem(pages.lastIndex, true)

analytics.logSearched(query)
}

fun goToFile(parentId: Long, id: Long) {
Expand Down Expand Up @@ -301,16 +306,16 @@ open class FilesFragment : RxFragment() {

override fun getItem(position: Int): Fragment {
val page = pages[position]
when (page.type) {
return when (page.type) {
Page.Type.File -> {
val file = page.file!!
return when {
when {
file.isFolder -> FolderFragment.newInstance(context, file, padForFab, canSelect, showSearch, showCreateFolder)
else -> FileDetailsFragment.newInstance(context, file)
}
}
Page.Type.Search -> {
return SearchFragment.newInstance(context, page.searchQuery!!, page.parentFolder!!, canSelect)
SearchFragment.newInstance(context, page.searchQuery!!, page.parentFolder!!, canSelect)
}
}
}
Expand Down Expand Up @@ -405,11 +410,9 @@ open class FilesFragment : RxFragment() {
this.parentFolder = parentFolder
}

fun getUniqueId(): Long {
when (type) {
Type.File -> return makeUniqueId(file!!)
Type.Search -> return makeUniqueId(searchQuery!!, parentFolder!!)
}
fun getUniqueId() = when (type) {
Type.File -> makeUniqueId(file!!)
Type.Search -> makeUniqueId(searchQuery!!, parentFolder!!)
}

constructor(source: Parcel) {
Expand All @@ -434,21 +437,16 @@ open class FilesFragment : RxFragment() {
}
}

override fun describeContents(): Int {
return 0
}
override fun describeContents() = 0

enum class Type {
File, Search
}

companion object {
fun makeUniqueId(file: PutioFile): Long {
return file.id
}
fun makeUniqueId(searchQuery: String, parentFolder: PutioFile): Long {
return searchQuery.hashCode() + parentFolder.id
}
fun makeUniqueId(file: PutioFile) = file.id
fun makeUniqueId(searchQuery: String, parentFolder: PutioFile) =
searchQuery.hashCode() + parentFolder.id

@JvmField val CREATOR: Parcelable.Creator<Page> = object : Parcelable.Creator<Page> {
override fun createFromParcel(source: Parcel): Page {
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.15.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:3.1.0'
}
}
allprojects {
Expand Down

0 comments on commit 29c0d86

Please sign in to comment.