-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathDemoActivity.java
139 lines (120 loc) · 5.27 KB
/
DemoActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.michaelflisar.gdprdialog.demo;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.michaelflisar.gdprdialog.GDPR;
import com.michaelflisar.gdprdialog.GDPRConsent;
import com.michaelflisar.gdprdialog.GDPRConsentState;
import com.michaelflisar.gdprdialog.GDPRDefinitions;
import com.michaelflisar.gdprdialog.GDPRSetup;
import com.michaelflisar.gdprdialog.demo.app.App;
import com.michaelflisar.gdprdialog.demo.gdpr.DemoGDPRActivity;
import com.michaelflisar.gdprdialog.helper.GDPRPreperationData;
public class DemoActivity extends AppCompatActivity implements View.OnClickListener, GDPR.IGDPRCallback
{
private final int DEMO_GDPR_ACTIVITY_REQUEST_CODE = 123;
// Setup
private GDPRSetup mSetup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
// get setup from intent
mSetup = getIntent().getParcelableExtra("setup");
// init state texts
GDPRConsentState consent = GDPR.getInstance().getConsentState();
if (consent != null) {
((TextView) findViewById(R.id.tvCurrentConsent)).setText(consent.logString() );
}
// show GDPR Dialog if necessary, the library takes care about if and how to show it
showGDPRIfNecessary();
}
@Override
public void onClick(View v) {
GDPR.getInstance().resetConsent();
// reshow dialog instantly
showGDPRIfNecessary();
}
private void showGDPRIfNecessary() {
GDPR.getInstance().checkIfNeedsToBeShown(this, mSetup);
}
// --------------------
// GDPR.IGDPRCallback
// --------------------
@Override
public void onConsentNeedsToBeRequested(GDPRPreperationData data) {
// eventually add the admob providers to the AdMob network
// we add it to the FIRST admob network we find...
if (data.getSubNetworks().size() > 0) {
for (int i = 0; i < mSetup.networks().length; i++) {
if (mSetup.networks()[i].getName().equals(GDPRDefinitions.ADMOB.getName())) {
mSetup.networks()[i].addSubNetworks(data.getSubNetworks());
break;
}
}
}
if (App.USE_ACTIVITY) {
DemoGDPRActivity.startActivityForResult(this, mSetup, data.getLocation(), DemoGDPRActivity.class, DEMO_GDPR_ACTIVITY_REQUEST_CODE);
} else {
// default: forward the result and show the dialog
GDPR.getInstance().showDialog(this, mSetup, data.getLocation());
}
}
@Override
public void onConsentInfoUpdate(GDPRConsentState consentState, boolean isNewState) {
if (isNewState) {
// user just selected this consent, do whatever you want...
switch (consentState.getConsent()) {
case UNKNOWN:
// never happens!
break;
case NO_CONSENT:
Toast.makeText(this, "User does NOT accept ANY ads - depending on your setup he may want to buy the app though, handle this!", Toast.LENGTH_LONG).show();
break;
case NON_PERSONAL_CONSENT_ONLY:
Toast.makeText(this, "User accepts NON PERSONAL ads", Toast.LENGTH_LONG).show();
onConsentKnown(consentState.getConsent() == GDPRConsent.PERSONAL_CONSENT);
break;
case PERSONAL_CONSENT:
case AUTOMATIC_PERSONAL_CONSENT:
Toast.makeText(this, "User accepts PERSONAL ads", Toast.LENGTH_LONG).show();
onConsentKnown(consentState.getConsent().isPersonalConsent());
break;
}
} else {
switch (consentState.getConsent()) {
case UNKNOWN:
// never happens!
break;
case NO_CONSENT:
// with the default setup, the dialog will shown in this case again anyways!
break;
case NON_PERSONAL_CONSENT_ONLY:
case PERSONAL_CONSENT:
case AUTOMATIC_PERSONAL_CONSENT:
// user restarted activity and consent was already given...
onConsentKnown(consentState.getConsent().isPersonalConsent());
break;
}
}
((TextView) findViewById(R.id.tvCurrentConsent)).setText(consentState.logString());
}
private void onConsentKnown(boolean allowsPersonalAds) {
// TODO:
// init your ads based on allowsPersonalAds
}
// --------------------
// Only necessaqry for the activity demo!!!
// --------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == DEMO_GDPR_ACTIVITY_REQUEST_CODE) {
GDPRConsentState consentState = GDPR.getInstance().getConsentState();
((TextView) findViewById(R.id.tvCurrentConsent)).setText(consentState != null ? consentState.logString() : "");
}
}
}