All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.treasuredata.android.GetAdvertisingIdAsyncTask Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.treasuredata.android;

import android.os.AsyncTask;
import android.content.Context;

import org.komamitsu.android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class GetAdvertisingIdAsyncTask extends AsyncTask {
    private static final String TAG = GetAdvertisingIdAsyncTask.class.getSimpleName();
    private static Object advertisingInfo;
    private static Method isLimitAdTrackingEnabledMethod;
    private static Method getIdMethod;
    private final GetAdvertisingIdAsyncTaskCallback callback;

    GetAdvertisingIdAsyncTask(GetAdvertisingIdAsyncTaskCallback callback) {
        this.callback = callback;
    }

    synchronized private static void cacheAdvertisingInfoClass(Context context) throws Exception {
        try {
            if (advertisingInfo == null) {
                advertisingInfo = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient")
                        .getMethod("getAdvertisingIdInfo", Context.class)
                        .invoke(null, context);
                isLimitAdTrackingEnabledMethod = advertisingInfo.getClass()
                        .getMethod("isLimitAdTrackingEnabled");
                getIdMethod = advertisingInfo.getClass().getMethod("getId");
            }
        } catch (ClassNotFoundException e) {
            // Customer does not include google services ad library, indicate not wanting to track Advertising Id
            Log.w(TAG, "Exception getting advertising id: " + e.getMessage(), e);
            Log.w(TAG, "You are attempting to enable auto append Advertising Identifier but AdvertisingIdClient class is not detected. To use this feature, you must use Google Mobile Ads library");
        }  catch (Exception e) {
            throw e;
        }
    }

    @Override
    protected String doInBackground(Context... params) {
        Context context = params[0];
        try {
            cacheAdvertisingInfoClass(context);
            if (advertisingInfo != null && !(Boolean) isLimitAdTrackingEnabledMethod.invoke(advertisingInfo)) {
                return (String) getIdMethod.invoke(advertisingInfo);
            } else {
                return null;
            }
        } catch (Exception e) {
            Log.w(TAG, "Exception getting advertising id: " + e.getMessage(), e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(String advertisingId) {
        callback.onGetAdvertisingIdAsyncTaskCompleted(advertisingId);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy