Skip to content

Commit

Permalink
Migrate to PIano Consents library
Browse files Browse the repository at this point in the history
  • Loading branch information
DeKaN committed Apr 16, 2024
1 parent 7fd192f commit 91d1fe5
Show file tree
Hide file tree
Showing 19 changed files with 840 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
run: chmod +x gradlew
- name: Check code style
run: ./gradlew ktlintCheck --continue
- name: Check API binary compatibility
run: ./gradlew apiCheck
- name: Run tests
run: ./gradlew testDebug
- name: Build project
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Piano Analytics SDK for Android

## v3.3.6-SNAPSHOT
## v3.4.0-SNAPSHOT
* Updated to Kotlin 1.9
* Added support for `PianoConsents`
* Added `PrivacyMode.CUSTOM`, that will be used if set `ConsentMode.CUSTOM`
* Deprecated `PrivacyModesStorage`, use `PianoConsents` instead
* Removed requirement for `READ_PHONE_STATE` permission for Android 6-9
* Updated dependencies:
- Kotlin [1.8.22 -> 1.9.23]
- com.squareup.moshi:moshi [1.15.0 -> 1.15.1]
https://github.com/square/moshi/
- androidx.lifecycle:lifecycle-process [2.6.2 -> 2.7.0]
https://developer.android.com/jetpack/androidx/releases/lifecycle#2.7.0

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Pianoanlayticsandroid.NoActionBar">
android:theme="@style/Theme.Pianoanlayticsandroid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/example/pianoanalytics/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.app.Application
import io.piano.android.analytics.Configuration
import io.piano.android.analytics.PianoAnalytics
import io.piano.android.analytics.model.VisitorIDType
import io.piano.android.consents.PianoConsents
import io.piano.android.consents.models.ConsentConfiguration
import timber.log.Timber

class App: Application() {
Expand All @@ -15,7 +17,11 @@ class App: Application() {
site = 552987,
visitorIDType = VisitorIDType.ADVERTISING_ID
).ignoreLimitedAdTracking(true).build()
PianoAnalytics.init(applicationContext, configuration).apply {
val pianoConsents = PianoConsents.init(
applicationContext,
ConsentConfiguration(requireConsent = true)
)
PianoAnalytics.init(applicationContext, configuration, pianoConsents).apply {
// just an example of callback
eventProcessorCallback = PianoAnalytics.EventProcessorCallback { events ->
events.forEach { event ->
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/example/pianoanalytics/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.example.pianoanalytics

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
Expand All @@ -10,9 +12,13 @@ import io.piano.android.analytics.PianoAnalytics
import io.piano.android.analytics.model.Event
import io.piano.android.analytics.model.Property
import io.piano.android.analytics.model.PropertyName
import io.piano.android.consents.PianoConsents
import io.piano.android.consents.models.ConsentMode
import io.piano.android.consents.models.Purpose

class MainActivity : AppCompatActivity(R.layout.activity_main) {
private val binding: ActivityMainBinding by viewBinding(R.id.recyclerview)
private val pianoConsents by lazy { PianoConsents.getInstance() }

private val animals = listOf(
"___media___",
Expand Down Expand Up @@ -43,6 +49,40 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
return true
}

override fun onPrepareOptionsMenu(menu: Menu): Boolean {
val currentPaConsent = requireNotNull(pianoConsents.consents[Purpose.AUDIENCE_MEASUREMENT]).mode
val chosenItem = when (currentPaConsent) {
ConsentMode.OPT_IN -> R.id.consent_opt_in
ConsentMode.ESSENTIAL -> R.id.consent_essential
ConsentMode.OPT_OUT -> R.id.consent_opt_out
ConsentMode.CUSTOM -> R.id.consent_custom
ConsentMode.NOT_ACQUIRED -> null
}
if (chosenItem != null) {
menu.findItem(chosenItem).isChecked = true
}
return super.onPrepareOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val newMode = when (item.itemId) {
R.id.consent_opt_in -> ConsentMode.OPT_IN
R.id.consent_essential -> ConsentMode.ESSENTIAL
R.id.consent_opt_out -> ConsentMode.OPT_OUT
R.id.consent_custom -> ConsentMode.CUSTOM
else -> null
}
return if (newMode != null) {
pianoConsents.set(Purpose.AUDIENCE_MEASUREMENT, newMode)
true
} else super.onOptionsItemSelected(item)
}

private fun onItemClick(item: String) {
PianoAnalytics.getInstance().sendEvents(
Event.Builder(Event.CLICK_NAVIGATION)
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single" >
<item
android:id="@+id/consent_opt_in"
android:title="opt in" />
<item
android:id="@+id/consent_essential"
android:title="essential" />
<item
android:id="@+id/consent_opt_out"
android:title="opt out" />
<item
android:id="@+id/consent_custom"
android:title="custom" />
</group>
</menu>
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.ktlint) apply false
alias(libs.plugins.binaryCompatibility) apply false
alias(libs.plugins.mavenRelease) apply false
alias(libs.plugins.moshiIR) apply false
alias(libs.plugins.versionUpdater)
Expand Down
8 changes: 6 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Plugins
kotlin = "1.9.23"
android = "8.3.2"
binaryCompatibility = "0.14.0"
versionUpdater = "0.51.0"
ktlint = "12.1.0"
mavenRelease = "0.28.0"
Expand All @@ -16,8 +17,9 @@ materialLibrary = "1.11.0"
# Third party Libraries
googleAdsId = "18.0.1"
huaweiAdsId = "3.4.26.303"
okhttp = "4.12.0"
moshi = "1.15.1"
okhttp = "4.12.0"
pianoConsents = "1.0.0"
timber = "5.0.1"
viewBindingProperty = "1.5.9"

Expand All @@ -30,6 +32,7 @@ mockitoCore = "5.11.0"
[plugins]
android-library = { id = "com.android.library", version.ref = "android" }
android-app = { id = "com.android.application", version.ref = "android" }
binaryCompatibility = {id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibility" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
Expand All @@ -43,10 +46,11 @@ lifecycleProcess = { module = "androidx.lifecycle:lifecycle-process", version.re
material = { module = "com.google.android.material:material", version.ref = "materialLibrary" }
googleAdsId = { module = "com.google.android.gms:play-services-ads-identifier", version.ref = "googleAdsId" }
huaweiAdsId = { module = "com.huawei.hms:hms-ads-identifier", version.ref = "huaweiAdsId" }
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttpLogging = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
pianoConsents = { module = "io.piano.android:consents", version.ref = "pianoConsents" }
timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
viewBindingProperty = { module = "com.github.kirich1409:viewbindingpropertydelegate", version.ref = "viewBindingProperty" }

kotlinJunit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
Expand Down
Loading

0 comments on commit 91d1fe5

Please sign in to comment.