Android Open Source - countly-sdk-android Advertising Id Adapter






From Project

Back to project page countly-sdk-android.

License

The source code is released under:

Copyright (c) 2012, 2013 Countly Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project countly-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ly.count.android.api;
//  w w  w. ja  v  a  2  s.  c  om
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

import java.lang.reflect.Method;

public class AdvertisingIdAdapter {
    private static final String TAG = "AdvertisingIdAdapter";
    private final static String ADVERTISING_ID_CLIENT_CLASS_NAME = "com.google.android.gms.ads.identifier.AdvertisingIdClient";
    private static Handler handler;

    public static boolean isAdvertisingIdAvailable() {
        boolean advertisingIdAvailable = false;
        try {
            Class.forName(ADVERTISING_ID_CLIENT_CLASS_NAME);
            advertisingIdAvailable = true;
        }
        catch (ClassNotFoundException ignored) {}
        return advertisingIdAvailable;
    }

    public static void setAdvertisingId(final Context context, final CountlyStore store, final DeviceId deviceId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    deviceId.setId(DeviceId.Type.ADVERTISING_ID, getAdvertisingId(context));
                } catch (Throwable t) {
                    if (t.getCause() != null && t.getCause().getClass().toString().contains("GooglePlayServicesAvailabilityException")) {
                        // recoverable, let device ID be null, which will result in storing all requests to Countly server
                        // and rerunning them whenever Advertising ID becomes available
                        if (Countly.sharedInstance().isLoggingEnabled()) {
                            Log.i(TAG, "Advertising ID cannot be determined yet");
                        }
                    } else if (t.getCause() != null && t.getCause().getClass().toString().contains("GooglePlayServicesNotAvailableException")) {
                        // non-recoverable, fallback to OpenUDID
                        if (Countly.sharedInstance().isLoggingEnabled()) {
                            Log.w(TAG, "Advertising ID cannot be determined because Play Services are not available");
                        }
                        deviceId.switchToIdType(DeviceId.Type.OPEN_UDID, context, store);
                    } else {
                        // unexpected
                        Log.e(TAG, "Couldn't get advertising ID", t);
                    }
                }
            }
        }).start();
    }

    private static String getAdvertisingId(Context context) throws Throwable{
        final Class<?> cls = Class.forName(ADVERTISING_ID_CLIENT_CLASS_NAME);
        final Method getAdvertisingIdInfo = cls.getMethod("getAdvertisingIdInfo", Context.class);
        Object info = getAdvertisingIdInfo.invoke(null, context);
        if (info != null) {
            final Method getId = info.getClass().getMethod("getId");
            Object id = getId.invoke(info);
            return (String)id;
        }
        return null;
    }
}




Java Source Code List

ly.count.android.api.AdvertisingIdAdapter.java
ly.count.android.api.ConnectionProcessor.java
ly.count.android.api.ConnectionQueue.java
ly.count.android.api.CountlyStore.java
ly.count.android.api.Countly.java
ly.count.android.api.DeviceId.java
ly.count.android.api.DeviceInfo.java
ly.count.android.api.EventQueue.java
ly.count.android.api.Event.java
ly.count.android.api.OpenUDIDAdapter.java
ly.count.android.api.UserData.java
ly.count.android.example.CountlyActivity.java
org.OpenUDID.OpenUDID_manager.java
org.OpenUDID.OpenUDID_service.java