AdvertisingId.java 源代码
package com.unity3d.services.core.device;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
import com.unity3d.services.core.log.DeviceLog;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@TargetApi(9)
public class AdvertisingId {
private static final String ADVERTISING_ID_SERVICE_NAME = "com.google.android.gms.ads.identifier.internal.IAdvertisingIdService";
private static AdvertisingId instance;
private String advertisingIdentifier = null;
private boolean limitedAdvertisingTracking = false;
public interface GoogleAdvertisingInfo extends IInterface {
public static abstract class GoogleAdvertisingInfoBinder extends Binder implements GoogleAdvertisingInfo {
public static class GoogleAdvertisingInfoImplementation implements GoogleAdvertisingInfo {
private final IBinder _binder;
GoogleAdvertisingInfoImplementation(IBinder iBinder) {
this._binder = iBinder;
}
@Override
public IBinder asBinder() {
return this._binder;
}
@Override
public boolean getEnabled(boolean z6) throws RemoteException {
int i6;
Parcel obtain = Parcel.obtain();
Parcel obtain2 = Parcel.obtain();
try {
obtain.writeInterfaceToken(AdvertisingId.ADVERTISING_ID_SERVICE_NAME);
boolean z7 = true;
if (z6) {
i6 = 1;
} else {
i6 = 0;
}
obtain.writeInt(i6);
this._binder.transact(2, obtain, obtain2, 0);
obtain2.readException();
if (obtain2.readInt() == 0) {
z7 = false;
}
return z7;
} finally {
obtain2.recycle();
obtain.recycle();
}
}
@Override
public String getId() throws RemoteException {
Parcel obtain = Parcel.obtain();
Parcel obtain2 = Parcel.obtain();
try {
obtain.writeInterfaceToken(AdvertisingId.ADVERTISING_ID_SERVICE_NAME);
this._binder.transact(1, obtain, obtain2, 0);
obtain2.readException();
return obtain2.readString();
} finally {
obtain2.recycle();
obtain.recycle();
}
}
}
public static GoogleAdvertisingInfo create(IBinder iBinder) {
if (iBinder == null) {
return null;
}
IInterface queryLocalInterface = iBinder.queryLocalInterface(AdvertisingId.ADVERTISING_ID_SERVICE_NAME);
if (queryLocalInterface != null && (queryLocalInterface instanceof GoogleAdvertisingInfo)) {
return (GoogleAdvertisingInfo) queryLocalInterface;
}
return new GoogleAdvertisingInfoImplementation(iBinder);
}
@Override
public boolean onTransact(int i6, Parcel parcel, Parcel parcel2, int i7) throws RemoteException {
boolean z6;
if (i6 != 1) {
if (i6 != 2) {
return super.onTransact(i6, parcel, parcel2, i7);
}
parcel.enforceInterface(AdvertisingId.ADVERTISING_ID_SERVICE_NAME);
if (parcel.readInt() != 0) {
z6 = true;
} else {
z6 = false;
}
boolean enabled = getEnabled(z6);
parcel2.writeNoException();
parcel2.writeInt(enabled ? 1 : 0);
return true;
}
parcel.enforceInterface(AdvertisingId.ADVERTISING_ID_SERVICE_NAME);
String id = getId();
parcel2.writeNoException();
parcel2.writeString(id);
return true;
}
}
boolean getEnabled(boolean z6) throws RemoteException;
String getId() throws RemoteException;
}
public class GoogleAdvertisingServiceConnection implements ServiceConnection {
private final BlockingQueue<IBinder> _binderQueue;
boolean _consumed;
private GoogleAdvertisingServiceConnection() {
this._consumed = false;
this._binderQueue = new LinkedBlockingQueue();
}
public IBinder getBinder() throws InterruptedException {
if (!this._consumed) {
this._consumed = true;
return this._binderQueue.take();
}
throw new IllegalStateException();
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
try {
this._binderQueue.put(iBinder);
} catch (InterruptedException unused) {
DeviceLog.debug("Couldn't put service to binder que");
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
private void fetchAdvertisingId(Context context) {
boolean z6;
GoogleAdvertisingServiceConnection googleAdvertisingServiceConnection = new GoogleAdvertisingServiceConnection();
Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
intent.setPackage("com.google.android.gms");
try {
z6 = context.bindService(intent, googleAdvertisingServiceConnection, 1);
} catch (Exception e6) {
DeviceLog.exception("Couldn't bind to identifier service intent", e6);
z6 = false;
}
try {
if (z6) {
try {
GoogleAdvertisingInfo create = GoogleAdvertisingInfo.GoogleAdvertisingInfoBinder.create(googleAdvertisingServiceConnection.getBinder());
this.advertisingIdentifier = create.getId();
this.limitedAdvertisingTracking = create.getEnabled(true);
} catch (Exception e7) {
DeviceLog.exception("Couldn't get advertising info", e7);
if (!z6) {
return;
}
}
}
if (!z6) {
return;
}
context.unbindService(googleAdvertisingServiceConnection);
} catch (Throwable th) {
if (z6) {
context.unbindService(googleAdvertisingServiceConnection);
}
throw th;
}
}
public static String getAdvertisingTrackingId() {
return getInstance().advertisingIdentifier;
}
private static AdvertisingId getInstance() {
if (instance == null) {
instance = new AdvertisingId();
}
return instance;
}
public static boolean getLimitedAdTracking() {
return getInstance().limitedAdvertisingTracking;
}
public static void init(Context context) {
getInstance().fetchAdvertisingId(context);
}
}