Skip to content

Commit

Permalink
Added comments & code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkumar committed Jul 30, 2015
1 parent f01b316 commit 94d5ccd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/android/AdvertisingIdClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions src/android/AndroidIDFA.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +40,7 @@
//pgcore import
import com.praves.cordova.android.idfa.CoreException;


public class AndroidIDFA extends CordovaPlugin {

Context context;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/android/CoreException.java
Original file line number Diff line number Diff line change
@@ -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){
Expand Down
23 changes: 23 additions & 0 deletions www/androidIDFA.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: <adervtiser-id>, limitAd: <boolean> }
* @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: <adervtiser-id> }
* @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: <boolean> }
* @param successCallback
* @param errorCallback
*/
getLimitAd : function(successCallback, errorCallback){
cordova.exec(successCallback, errorCallback, 'AndroidIDFA', 'getLimitAd', []);
}
Expand Down

0 comments on commit 94d5ccd

Please sign in to comment.