|
| 1 | +package ly.count.android.demo; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.widget.Toast; |
| 5 | + |
| 6 | +import androidx.appcompat.app.AppCompatActivity; |
| 7 | + |
| 8 | +import com.google.android.material.switchmaterial.SwitchMaterial; |
| 9 | + |
| 10 | +import ly.count.android.sdk.Countly; |
| 11 | + |
| 12 | +public class ActivityExampleConsent extends AppCompatActivity { |
| 13 | + |
| 14 | + private static final String[][] FEATURES = { |
| 15 | + {"sessions", "switchSessions"}, |
| 16 | + {"events", "switchEvents"}, |
| 17 | + {"views", "switchViews"}, |
| 18 | + {"crashes", "switchCrashes"}, |
| 19 | + {"attribution", "switchAttribution"}, |
| 20 | + {"users", "switchUsers"}, |
| 21 | + {"push", "switchPush"}, |
| 22 | + {"starRating", "switchStarRating"}, |
| 23 | + {"remoteConfig", "switchRemoteConfig"}, |
| 24 | + {"location", "switchLocation"}, |
| 25 | + {"feedback", "switchFeedback"}, |
| 26 | + {"apm", "switchApm"}, |
| 27 | + {"content", "switchContent"}, |
| 28 | + }; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setContentView(R.layout.activity_example_consent); |
| 34 | + |
| 35 | + // Set initial switch states and listeners |
| 36 | + for (String[] feature : FEATURES) { |
| 37 | + String featureName = feature[0]; |
| 38 | + int resId = getResources().getIdentifier(feature[1], "id", getPackageName()); |
| 39 | + SwitchMaterial sw = findViewById(resId); |
| 40 | + if (sw != null) { |
| 41 | + sw.setChecked(Countly.sharedInstance().consent().getConsent(featureName)); |
| 42 | + sw.setOnCheckedChangeListener((buttonView, isChecked) -> { |
| 43 | + if (isChecked) { |
| 44 | + Countly.sharedInstance().consent().giveConsent(new String[]{featureName}); |
| 45 | + } else { |
| 46 | + Countly.sharedInstance().consent().removeConsent(new String[]{featureName}); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + findViewById(R.id.btnGiveAllConsent).setOnClickListener(v -> { |
| 53 | + Countly.sharedInstance().consent().giveConsentAll(); |
| 54 | + refreshSwitches(); |
| 55 | + Toast.makeText(this, "All consent given", Toast.LENGTH_SHORT).show(); |
| 56 | + }); |
| 57 | + |
| 58 | + findViewById(R.id.btnRemoveAllConsent).setOnClickListener(v -> { |
| 59 | + Countly.sharedInstance().consent().removeConsentAll(); |
| 60 | + refreshSwitches(); |
| 61 | + Toast.makeText(this, "All consent removed", Toast.LENGTH_SHORT).show(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private void refreshSwitches() { |
| 66 | + for (String[] feature : FEATURES) { |
| 67 | + int resId = getResources().getIdentifier(feature[1], "id", getPackageName()); |
| 68 | + SwitchMaterial sw = findViewById(resId); |
| 69 | + if (sw != null) { |
| 70 | + sw.setChecked(Countly.sharedInstance().consent().getConsent(feature[0])); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments