Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Java Plugin for Android Advertising Id. Unity 2020

Discussion in 'Scripting' started by SamTyurenkov, Jan 20, 2021.

  1. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    Hi, fellow coders.

    Would you please help me understand what am I missing. For the first time Im trying to use Java.

    First I found Unity Ads package on github, because it has the class for the very same purpose:
    (the only thing I changed is package. Is that correct?)

    Code (CSharp):
    1.  
    2. package com.my.trackingid;
    3.  
    4. import android.content.ComponentName;
    5. import android.content.Context;
    6. import android.content.Intent;
    7. import android.content.ServiceConnection;
    8. import android.os.Binder;
    9. import android.os.Build;
    10. import android.os.IBinder;
    11. import android.os.IInterface;
    12. import android.os.Parcel;
    13. import android.os.RemoteException;
    14.  
    15. import com.unity3d.services.core.log.DeviceLog;
    16.  
    17. import java.util.concurrent.BlockingQueue;
    18. import java.util.concurrent.LinkedBlockingQueue;
    19.  
    20. public class AdvertisingId {
    21.  
    22.     private static final String ADVERTISING_ID_SERVICE_NAME = "com.google.android.gms.ads.identifier.internal.IAdvertisingIdService";
    23.  
    24.     private static AdvertisingId instance = null;
    25.     private String advertisingIdentifier = null;
    26.     private boolean limitedAdvertisingTracking = false;
    27.  
    28.     private static AdvertisingId getInstance() {
    29.         if (instance == null) {
    30.             instance = new AdvertisingId();
    31.         }
    32.         return instance;
    33.     }
    34.  
    35.     public static void init(final Context context) {
    36.         getInstance().fetchAdvertisingId(context);
    37.     }
    38.  
    39.     public static String getAdvertisingTrackingId() {
    40.         return getInstance().advertisingIdentifier;
    41.     }
    42.  
    43.     public static boolean getLimitedAdTracking() {
    44.         return getInstance().limitedAdvertisingTracking;
    45.     }
    46.  
    47.  
    48.     private void fetchAdvertisingId(Context context) {
    49.         GoogleAdvertisingServiceConnection connection = new GoogleAdvertisingServiceConnection();
    50.         Intent localIntent = new Intent("com.google.android.gms.ads.identifier.service.START");
    51.         localIntent.setPackage("com.google.android.gms");
    52.         boolean didBind = false;
    53.         try {
    54.             didBind = context.bindService(localIntent, connection, Context.BIND_AUTO_CREATE);
    55.         } catch (Exception e) {
    56.             DeviceLog.exception("Couldn't bind to identifier service intent", e);
    57.         }
    58.         try {
    59.             if (didBind) {
    60.                 GoogleAdvertisingInfo advertisingInfo = GoogleAdvertisingInfo.GoogleAdvertisingInfoBinder.create(connection.getBinder());
    61.                 advertisingIdentifier = advertisingInfo.getId();
    62.                 limitedAdvertisingTracking = advertisingInfo.getEnabled(true);
    63.             }
    64.         } catch (Exception e) {
    65.             DeviceLog.exception("Couldn't get advertising info", e);
    66.         } finally {
    67.             if (didBind) {
    68.                 context.unbindService(connection);
    69.             }
    70.         }
    71.     }
    72.  
    73.     private interface GoogleAdvertisingInfo extends IInterface {
    74.  
    75.         String getId() throws RemoteException;
    76.  
    77.         boolean getEnabled(boolean paramBoolean) throws RemoteException;
    78.  
    79.         abstract class GoogleAdvertisingInfoBinder extends Binder implements GoogleAdvertisingInfo {
    80.  
    81.             public static GoogleAdvertisingInfo create(IBinder binder) {
    82.                 if (binder == null) {
    83.                     return null;
    84.                 }
    85.                 IInterface localIInterface = binder.queryLocalInterface(ADVERTISING_ID_SERVICE_NAME);
    86.                 if ((localIInterface != null) && ((localIInterface instanceof GoogleAdvertisingInfo))) {
    87.                     return (GoogleAdvertisingInfo) localIInterface;
    88.                 }
    89.                 return new GoogleAdvertisingInfoImplementation(binder);
    90.             }
    91.  
    92.             @SuppressWarnings("NullableProblems")
    93.             public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
    94.                 switch (code) {
    95.                     case 1:
    96.                         data.enforceInterface(ADVERTISING_ID_SERVICE_NAME);
    97.                         String str1 = getId();
    98.                         reply.writeNoException();
    99.                         reply.writeString(str1);
    100.                         return true;
    101.                     case 2:
    102.                         data.enforceInterface(ADVERTISING_ID_SERVICE_NAME);
    103.                         boolean bool1 = 0 != data.readInt();
    104.                         boolean bool2 = getEnabled(bool1);
    105.                         reply.writeNoException();
    106.                         reply.writeInt(bool2 ? 1 : 0);
    107.                         return true;
    108.                 }
    109.                 return super.onTransact(code, data, reply, flags);
    110.             }
    111.  
    112.             private static class GoogleAdvertisingInfoImplementation implements GoogleAdvertisingInfo {
    113.  
    114.                 private final IBinder _binder;
    115.  
    116.                 GoogleAdvertisingInfoImplementation(IBinder binder) {
    117.                     _binder = binder;
    118.                 }
    119.  
    120.                 @Override
    121.                 public IBinder asBinder() {
    122.                     return _binder;
    123.                 }
    124.  
    125.                 @Override
    126.                 public String getId() throws RemoteException {
    127.                     Parcel localParcel1 = Parcel.obtain();
    128.                     Parcel localParcel2 = Parcel.obtain();
    129.                     String str;
    130.                     try {
    131.                         localParcel1.writeInterfaceToken(ADVERTISING_ID_SERVICE_NAME);
    132.                         _binder.transact(1, localParcel1, localParcel2, 0);
    133.                         localParcel2.readException();
    134.                         str = localParcel2.readString();
    135.                     } finally {
    136.                         localParcel2.recycle();
    137.                         localParcel1.recycle();
    138.                     }
    139.                     return str;
    140.                 }
    141.  
    142.                 @Override
    143.                 public boolean getEnabled(boolean paramBoolean) throws RemoteException {
    144.                     Parcel localParcel1 = Parcel.obtain();
    145.                     Parcel localParcel2 = Parcel.obtain();
    146.                     boolean bool;
    147.                     try {
    148.                         localParcel1.writeInterfaceToken(ADVERTISING_ID_SERVICE_NAME);
    149.                         localParcel1.writeInt(paramBoolean ? 1 : 0);
    150.                         _binder.transact(2, localParcel1, localParcel2, 0);
    151.                         localParcel2.readException();
    152.                         bool = 0 != localParcel2.readInt();
    153.                     } finally {
    154.                         localParcel2.recycle();
    155.                         localParcel1.recycle();
    156.                     }
    157.                     return bool;
    158.                 }
    159.             }
    160.         }
    161.     }
    162.  
    163.     private class GoogleAdvertisingServiceConnection implements ServiceConnection {
    164.  
    165.         boolean _consumed = false;
    166.         private final BlockingQueue<IBinder> _binderQueue = new LinkedBlockingQueue<>();
    167.  
    168.         @Override
    169.         public void onServiceConnected(ComponentName name, IBinder service) {
    170.             try {
    171.                 _binderQueue.put(service);
    172.             } catch (InterruptedException localInterruptedException) {
    173.                 DeviceLog.debug("Couldn't put service to binder que");
    174.             }
    175.         }
    176.  
    177.         @Override
    178.         public void onServiceDisconnected(ComponentName name) {
    179.  
    180.         }
    181.  
    182.         public IBinder getBinder() throws InterruptedException {
    183.             if (_consumed) {
    184.                 throw new IllegalStateException();
    185.             }
    186.             _consumed = true;
    187.             return _binderQueue.take();
    188.         }
    189.     }
    190. }
    Then, in C# im trying to access this class with that code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class AdsIds : MonoBehaviour
    6. {
    7.     public static AdsIds adId;
    8.  
    9. #if UNITY_ANDROID
    10.     AndroidJavaObject currentActivity = null;
    11.     AndroidJavaObject adInfo = null;
    12.  
    13. #endif
    14.  
    15.     private void Awake()
    16.     {
    17.         adId = this;
    18.     }
    19.     public void getID()
    20.     {
    21.         StartCoroutine(getIDcouroutine());
    22.     }
    23.  
    24.     private IEnumerator getIDcouroutine()
    25.     {
    26. #if UNITY_ANDROID
    27.  
    28.         if (currentActivity == null)
    29.         {
    30.             using (AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    31.             {
    32.                 currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
    33.                 using (AndroidJavaClass client = new AndroidJavaClass("com.my.trackingid"))
    34.                 {
    35.  
    36.                     adInfo = client.CallStatic<AndroidJavaObject>("init", currentActivity);
    37.                 }
    38.             }
    39.         }
    40.             DataBasePlayer.dbplayer.ifa = adInfo.CallStatic<string>("getAdvertisingTrackingId").ToString();
    41.  
    42. #elif UNITY_IOS
    43.             Application.RequestAdvertisingIdentifierAsync(
    44.             (string advertisingId, bool trackingEnabled, string error) =>
    45.             {
    46.             DataBasePlayer.dbplayer.ifa = advertisingId;
    47.             }
    48.             );
    49. #else
    50.             DataBasePlayer.dbplayer.ifa = "platform not supported";
    51. #endif
    52.             yield return null;
    53.         }
    54.     }
    55.  
    56.  
    Then I build, and log the result from device and there is default result - string "none".
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
  3. SamTyurenkov

    SamTyurenkov

    Joined:
    May 12, 2018
    Posts:
    83
    Actually I just broke entire project when tried to replace gradle, so cant look at the logs right now.
    Last errors were about missing dependencies, but I didn't find out how to resolve them without pain, so I tried to open the project in Android Studio and it didnt help, just made it worse.

    This method is no longer supported in Unity 2020 for Android platform.