Untitled
user_0695378
plain_text
2 years ago
6.1 kB
10
Indexable
public class GDPRHelper { private static final String CONSENT_STATUS = "consent_status"; private static final String PREFERENCES_NAME = "ads_preferences"; private static final String TAG = "GDPRHelper"; private Activity activity; private SharedPreferences sharedPreferences; private ConsentInformation consentInformation; private ConsentForm consentForm; public GDPRHelper(Activity activity) { this.activity = activity; this.sharedPreferences = activity.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); } public void checkForConsent() { ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) .addTestDeviceHashedId(DeviceIdHasher.hashDeviceId("CC3EBF623628DAA1E3AD2124AE690FBD")) .build(); ConsentRequestParameters params = new ConsentRequestParameters.Builder() .setConsentDebugSettings(debugSettings) .setTagForUnderAgeOfConsent(false) .build(); consentInformation = UserMessagingPlatform.getConsentInformation(activity); consentInformation.requestConsentInfoUpdate( activity, params, new ConsentInformation.OnConsentInfoUpdateSuccessListener() { @Override public void onConsentInfoUpdateSuccess() { // The consent information state was updated. // You are now ready to check if a form is available. if (consentInformation.isConsentFormAvailable()) { loadForm(); } else { Log.d(TAG, "Consent form is not available."); } } }, new ConsentInformation.OnConsentInfoUpdateFailureListener() { @Override public void onConsentInfoUpdateFailure(@NonNull FormError formError) { // Handle the error. Log.e(TAG, "Consent info update failed. Error: " + formError.getMessage()); } } ); Log.d(TAG, "Checking for consent..."); } private void loadForm() { UserMessagingPlatform.loadConsentForm( activity, new UserMessagingPlatform.OnConsentFormLoadSuccessListener() { @Override public void onConsentFormLoadSuccess(@NonNull ConsentForm form) { consentForm = form; consentForm.show( activity, new ConsentForm.OnConsentFormDismissedListener() { @Override public void onConsentFormDismissed(@Nullable FormError formError) { int consentStatus = consentInformation.getConsentStatus(); if (consentStatus == ConsentInformation.ConsentStatus.OBTAINED) { Log.d(TAG, "Consent form dismissed. Consent status: OBTAINED"); sharedPreferences.edit().putString(CONSENT_STATUS, "PERSONALIZED").apply(); } else { Log.d(TAG, "Consent form dismissed. Consent status: " + consentStatus); if (consentStatus == ConsentInformation.ConsentStatus.REQUIRED) { sharedPreferences.edit().putString(CONSENT_STATUS, "NON_PERSONALIZED").apply(); } loadForm(); // Panggil ulang loadForm() tanpa argumen activity } } } ); } }, new UserMessagingPlatform.OnConsentFormLoadFailureListener() { @Override public void onConsentFormLoadFailure(@NonNull FormError formError) { // Handle the error. loadForm(); Log.e(TAG, "Consent form load failed. Error: " + formError.getMessage()); } } ); Log.d(TAG, "Loading consent form..."); } public AdRequest getAdRequest() { AdRequest.Builder builder = new AdRequest.Builder(); String consentStatus = sharedPreferences.getString(CONSENT_STATUS, null); if ("NON_PERSONALIZED".equals(consentStatus)) { Bundle extras = new Bundle(); extras.putString("npa", "1"); builder.addNetworkExtrasBundle(AdMobAdapter.class, extras); } return builder.build(); } public void presentForm() { if (consentForm != null) { consentForm.show( activity, new ConsentForm.OnConsentFormDismissedListener() { @Override public void onConsentFormDismissed(@Nullable FormError formError) { // Handle dismissal by reloading form. loadForm(); } }); } else { Log.e(TAG, "Consent form is null. Please make sure to call loadForm() first."); } } public void resetConsent() { Log.d(TAG, "Resetting consent..."); consentInformation.reset(); Log.d(TAG, "Consent reset complete. Consent status: " + consentInformation.getConsentStatus()); loadForm(); } }
Editor is loading...