From 94d5ccd4e982b12306e11752caab114bb284b6da Mon Sep 17 00:00:00 2001 From: pkumar Date: Thu, 30 Jul 2015 15:20:12 -0700 Subject: [PATCH] Added comments & code cleanup. --- src/android/AdvertisingIdClient.java | 46 +++++++++++++--------------- src/android/AndroidIDFA.java | 28 +++++++++++++++++ src/android/CoreException.java | 4 +++ www/androidIDFA.js | 23 ++++++++++++++ 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/android/AdvertisingIdClient.java b/src/android/AdvertisingIdClient.java index d404d4f..a0b52e3 100644 --- a/src/android/AdvertisingIdClient.java +++ b/src/android/AdvertisingIdClient.java @@ -1,9 +1,13 @@ package com.praves.cordova.android.idfa; /** - * Created by pkumar on 7/28/15. + * Class AdvertisingIdClient + * Description: Determines advertiser id info using Google Play API. If google play is not installed + * on device then it handle it gracefully by unbinding service connection for context. + * Author: praves */ + import java.io.IOException; import java.util.concurrent.LinkedBlockingQueue; import android.content.ComponentName; @@ -37,29 +41,12 @@ public boolean isLimitAdTrackingEnabled() { } } -// public static AdInfo getAdvertisingIdInfo(Context context) throws Exception { -// if(Looper.myLooper() == Looper.getMainLooper()) throw new IllegalStateException("Cannot be called from the main thread"); -// -// try { PackageManager pm = context.getPackageManager(); pm.getPackageInfo("com.android.vending", 0); } -// catch (Exception e) { throw e; } -// -// AdvertisingConnection connection = new AdvertisingConnection(); -// Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START"); -// intent.setPackage("com.google.android.gms"); -// if(context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) { -// try { -// AdvertisingInterface adInterface = new AdvertisingInterface(connection.getBinder()); -// AdInfo adInfo = new AdInfo(adInterface.getId(), adInterface.isLimitAdTrackingEnabled(true)); -// return adInfo; -// } catch (Exception exception) { -// throw exception; -// } finally { -// context.unbindService(connection); -// } -// } -// throw new IOException("Google Play connection failed"); -// } - + /** + * Returns advertiser id info. + * @param context + * @return + * @throws Exception + */ public static AdInfo getAdvertisingIdInfo(Context context) throws Exception { if(Looper.myLooper() == Looper.getMainLooper()) throw new IllegalStateException("Cannot be called from the main thread"); @@ -117,6 +104,11 @@ public IBinder asBinder() { return binder; } + /** + * Returns advertiser id. + * @return + * @throws RemoteException + */ public String getId() throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); @@ -133,6 +125,12 @@ public String getId() throws RemoteException { return id; } + /** + * Function to get limit ad tracking flag. + * @param paramBoolean + * @return + * @throws RemoteException + */ public boolean isLimitAdTrackingEnabled(boolean paramBoolean) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); diff --git a/src/android/AndroidIDFA.java b/src/android/AndroidIDFA.java index ef7c813..cbeda2e 100644 --- a/src/android/AndroidIDFA.java +++ b/src/android/AndroidIDFA.java @@ -1,5 +1,11 @@ package com.praves.cordova.android.idfa; +/** + * Class AndroidIDFA + * Description: Extends cordova plugin to get advertiser id (IDFA) for Android using Google Play API + * Author: praves + */ + //Cordova imports import org.apache.cordova.CordovaInterface; import org.apache.cordova.CallbackContext; @@ -34,6 +40,7 @@ //pgcore import import com.praves.cordova.android.idfa.CoreException; + public class AndroidIDFA extends CordovaPlugin { Context context; @@ -91,6 +98,15 @@ public void onResume(boolean multitasking) { super.onResume(multitasking); } + /** + * Driver function to get advertiser info. Returns advertiser id info (defaults), or advertiser id + * or limit ad flag based on 'flag' parameter's values (0, 1, 2). + * @param callbackContext + * @param flag + * 0: adInfo is returned (defaults). + * 1: ad id is returned + * 2: limit ad flag is returned + */ private void _getAdInfo(final CallbackContext callbackContext, final int flag) { new Thread(new Runnable() { public void run() { @@ -127,16 +143,28 @@ public void run() { }).start(); } + /** + * Function to get advertiser id info. + * @param callbackContext + */ public void getAdInfo(final CallbackContext callbackContext) { final int flag = 0; _getAdInfo(callbackContext, flag); } + /** + * Function to get advertiser id. + * @param callbackContext + */ public void getAdId(final CallbackContext callbackContext) { final int flag = 1; _getAdInfo(callbackContext, flag); } + /** + * Function to get limit advertisement flag. + * @param callbackContext + */ public void getLimitAd(final CallbackContext callbackContext) { final int flag = 2; _getAdInfo(callbackContext, flag); diff --git a/src/android/CoreException.java b/src/android/CoreException.java index 635aeab..925a9b3 100644 --- a/src/android/CoreException.java +++ b/src/android/CoreException.java @@ -1,5 +1,9 @@ package com.praves.cordova.android.idfa; +/** + * Package level global exception handler. + * Author: praves + */ class CoreException extends Exception { public CoreException(String message){ diff --git a/www/androidIDFA.js b/www/androidIDFA.js index 6af09e0..0795e7e 100644 --- a/www/androidIDFA.js +++ b/www/androidIDFA.js @@ -3,18 +3,41 @@ pluginVersion : "1.0.1", //TODO Need to read from plugin.xml file. this value should be in sync plugin.xml value + /** + * Function to get version of the plugin. + * @param successCallback + * @param errorCallback + */ getPluginVersion : function(successCallback, errorCallback){ successCallback(this.pluginVersion); }, + /** + * Function to get advertiser id info (id and isLimit advertising flag) on success. On error, it returns error object/string. + * successCallback function returns advertiser info as object {adId: , limitAd: } + * @param successCallback + * @param errorCallback + */ getAdInfo : function(successCallback, errorCallback){ cordova.exec(successCallback, errorCallback, 'AndroidIDFA', 'getAdInfo', []); }, + /** + * Function to get advertiser id on success. On error, it returns error object/string. + * successCallback function returns advertiser info as object {adId: } + * @param successCallback + * @param errorCallback + */ getAdId : function(successCallback, errorCallback){ cordova.exec(successCallback, errorCallback, 'AndroidIDFA', 'getAdId', []); }, + /** + * Function to get isLimit advertising flag on success. On error, it returns error object/string. + * successCallback function returns isLimit ad flag info as object { limitAd: } + * @param successCallback + * @param errorCallback + */ getLimitAd : function(successCallback, errorCallback){ cordova.exec(successCallback, errorCallback, 'AndroidIDFA', 'getLimitAd', []); }