Skip to content

Instantly share code, notes, and snippets.

@vaelen
Created July 8, 2014 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaelen/2e953fb782d04f6612bf to your computer and use it in GitHub Desktop.
Save vaelen/2e953fb782d04f6612bf to your computer and use it in GitHub Desktop.
This class determines whether or not the current Android device has support for Google Play's advertising identifier. If so, it returns the advertising identifier. If not, it returns the Android device identifier.
package com.kabam.sdk;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import android.app.Activity;
import android.provider.Settings.Secure;
import android.util.Log;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
public class AdvertisingIdentifier {
public static final String TAG = AdvertisingIdentifier.class.getSimpleName();
public enum Type {
ANDROID_ID,
GOOGLEPLAY_ADVERTISING_ID
}
private static AdvertisingIdentifier adId;
private static final Lock lock = new ReentrantLock(true);
private final Type type;
private final String id;
private AdvertisingIdentifier(final Type type, final String id) {
this.type = type;
this.id = id;
}
/**
* Returns either the Android Device ID or the Google Play Advertising ID
* AdvertisingIdClient.getAdvertisingIdInfo() will throw IllegalStateException
* if this method is called on the main thread.
*/
public static AdvertisingIdentifier getInstance(final Activity activity) {
if (adId == null || adId.type == Type.ANDROID_ID) {
// We don't have an advertising id, so try to get one.
lock.lock();
try {
if (adId == null) {
// Default to Android ID
String androidId = Secure.getString(activity.getContentResolver(), Secure.ANDROID_ID);
adId = new AdvertisingIdentifier(Type.ANDROID_ID, androidId);
}
// This check is necessary to prevent the work from being done twice if the lock above blocked.
if (adId.getType() == Type.ANDROID_ID) {
// Try to get an Advertising ID
try {
Info advertisingInfo = AdvertisingIdClient.getAdvertisingIdInfo(activity);
String advertisingId = advertisingInfo.getId();
adId = new AdvertisingIdentifier(Type.GOOGLEPLAY_ADVERTISING_ID, advertisingId);
} catch (GooglePlayServicesNotAvailableException e) {
// This is a device which doesn't support Google Play, for example a Kindle
Log.i(TAG, "Not a Google Play device, using Android ID");
} catch (GooglePlayServicesRepairableException e) {
// This device supports Google Play, but it is using an older version (2.x or 3.x)
Log.i(TAG, "Google Play version does not support Advertising Id, using Android ID.");
} catch (IllegalStateException ex) {
Log.e(TAG, "AdvertisingIdentifier.getInstance() can not be called from the main thread.");
throw ex;
} catch (Exception e) {
// Something unexpected happened.
Log.e(TAG, "Couldn't Get Advertising ID, defaulting to Android ID. Error : " + e);
}
}
} finally {
lock.unlock();
}
}
return adId;
}
public Type getType() {
return type;
}
public String getId() {
return id;
}
@Override
public String toString() {
if (id == null) {
return "";
} else {
return id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment