aboutsummaryrefslogtreecommitdiffstats
path: root/src/playstore/java/de/pixart/messenger/utils/InstallReferrerUtils.java
blob: 05824b9b28e1ff6b9c845387d6ad803c14275dc1 (plain)
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
package de.pixart.messenger.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

import de.pixart.messenger.Config;
import de.pixart.messenger.ui.WelcomeActivity;

public class InstallReferrerUtils implements InstallReferrerStateListener {

    private static final String PROCESSED_INSTALL_REFERRER = "processed_install_referrer";


    private final WelcomeActivity welcomeActivity;
    private final InstallReferrerClient installReferrerClient;


    public InstallReferrerUtils(WelcomeActivity welcomeActivity) {
        this.welcomeActivity = welcomeActivity;
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(welcomeActivity);
        if (preferences.getBoolean(PROCESSED_INSTALL_REFERRER, false)) {
            Log.d(Config.LOGTAG, "install referrer already processed");
            this.installReferrerClient = null;
            return;
        }
        this.installReferrerClient = InstallReferrerClient.newBuilder(welcomeActivity).build();
        this.installReferrerClient.startConnection(this);
    }

    public static void markInstallReferrerExecuted(final Activity context) {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        preferences.edit().putBoolean(PROCESSED_INSTALL_REFERRER, true).apply();
    }

    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                try {
                    final ReferrerDetails referrerDetails = installReferrerClient.getInstallReferrer();
                    final String referrer = referrerDetails.getInstallReferrer();
                    welcomeActivity.onInstallReferrerDiscovered(referrer);
                } catch (RemoteException e) {
                    Log.d(Config.LOGTAG, "unable to get install referrer", e);
                }
                break;
            default:
                Log.d(Config.LOGTAG, "unable to setup install referrer client. code=" + responseCode);
        }
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {
    }
}