001package com.intentsoftware.addapptr.consent;
002
003import android.app.Activity;
004import android.app.Application;
005import android.util.Log;
006
007import com.intentsoftware.addapptr.AdNetwork;
008import com.intentsoftware.addapptr.CMPImplementation;
009import com.intentsoftware.addapptr.ManagedConsent;
010import com.intentsoftware.addapptr.NonIABConsent;
011import com.intentsoftware.addapptr.ad.networkhelpers.OguryHelper;
012import com.intentsoftware.addapptr.module.Logger;
013import com.ogury.cm.OguryChoiceManager;
014import com.ogury.cm.OguryConsentListener;
015import com.ogury.core.OguryError;
016
017public final class CMPOgury extends CMPImplementation {
018
019    private CMPDelegate delegate;
020    private boolean asked;
021    private OguryConsentListener oguryListener;
022    private String initializationError;
023
024    /**
025     * Constructor.
026     *
027     * @param application the Application context.
028     * @param assetKey    the Asset key of your application.
029     */
030    public CMPOgury(Application application, String assetKey) {
031        if (checkRequiredClasses("com.ogury.sdk.Ogury", "com.ogury.cm.OguryChoiceManager", "com.ogury.cm.OguryConsentListener", "com.ogury.core.OguryError")) {
032            try {
033                OguryHelper.initOgurySDK(application, assetKey);
034                oguryListener = new OguryConsentListener() {
035                    @Override
036                    public void onComplete(OguryChoiceManager.Answer answer) {
037                        if (delegate != null) {
038                            ManagedConsent.ManagedConsentState state;
039                            switch (answer) {
040                                case FULL_APPROVAL:
041                                    state = ManagedConsent.ManagedConsentState.OBTAINED;
042                                    break;
043                                case PARTIAL_APPROVAL:
044                                    state = ManagedConsent.ManagedConsentState.CUSTOM;
045                                    break;
046                                case REFUSAL:
047                                    state = ManagedConsent.ManagedConsentState.WITHHELD;
048                                    break;
049                                default:
050                                    state = ManagedConsent.ManagedConsentState.UNKNOWN;
051                            }
052
053                            delegate.onConsentUpdated(state);
054                        }
055                    }
056
057                    @Override
058                    public void onError(OguryError oguryError) {
059                        if (delegate != null) {
060                            delegate.onCMPFailedToShow(oguryError.getMessage());
061                        }
062                    }
063                };
064                onSuccessfulInitialization();
065            } catch (Exception e) {
066                if (delegate != null) {
067                    delegate.onCMPFailedToLoad(e.getMessage());
068                } else {
069                    initializationError = e.getMessage();
070                }
071
072                if (Logger.isLoggable(Log.ERROR)) {
073                    Logger.e(this, e.getMessage());
074                }
075            }
076        }
077    }
078
079    @Override
080    protected void showIfNeeded(Activity activity) {
081        OguryChoiceManager.ask(activity, oguryListener);
082        asked = true;
083    }
084
085    @Override
086    protected void editConsent(Activity activity) {
087        OguryChoiceManager.edit(activity, oguryListener);
088    }
089
090    @Override
091    protected void setDelegate(CMPDelegate delegate) {
092        this.delegate = delegate;
093        if (initializationError != null) { //handle init failing before delegate is set
094            delegate.onCMPFailedToLoad(initializationError);
095            initializationError = null;
096        }
097    }
098
099    @Override
100    protected NonIABConsent getConsentForNetwork(AdNetwork network) {
101        if (!asked) {
102            return NonIABConsent.WITHHELD; //handle checks before CMP is asked for consent status
103        }
104
105        switch (network) {
106            case APPLOVIN:
107                return getCosentForVendorId(5);
108            case FACEBOOK:
109                return getCosentForVendorId(9);
110            case ADMOB:
111            case ADX:
112            case DFP:
113                return getCosentForVendorId(3);
114            case INMOBI:
115                return getCosentForVendorId(296);
116            case MOPUB:
117                return getCosentForVendorId(12);
118            case UNITYADS:
119                return getCosentForVendorId(16);
120            case PUBNATIVE:
121                return getCosentForVendorId(139);
122            default:
123                // TODO: no mappings for Huawei and Yandex
124                if (Logger.isLoggable(Log.WARN)) {
125                    Logger.w(this, "No mapping for network " + network + " available, treating consent as withheld");
126                }
127                return NonIABConsent.WITHHELD;
128        }
129    }
130
131    private NonIABConsent getCosentForVendorId(int vendorId) {
132        return OguryChoiceManager.TcfV2.isAccepted(vendorId) ? NonIABConsent.OBTAINED : NonIABConsent.WITHHELD;
133    }
134
135    @Override
136    public void reload(Activity activity) {
137    }
138}