Skip to content

Commit

Permalink
Implement use of androidx alpha library
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetam committed Feb 16, 2021
1 parent 8abf8d4 commit 39c70ab
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
45 changes: 43 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ android {
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'

multiDexEnabled true
}

lintOptions {
Expand All @@ -73,8 +75,8 @@ dependencies {
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
testImplementation 'org.powermock:powermock-classloading-xstream:1.6.6'
testImplementation 'com.google.android:support-v4:r6'
testImplementation 'com.google.android.gms:play-services-ads:18.3.0'
testImplementation 'com.google.android.gms:play-services-base:17.1.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
implementation 'com.google.android.gms:play-services-base:17.1.0'

testImplementation "junit:junit:4.12"

Expand All @@ -87,6 +89,8 @@ dependencies {

// Assertions
testImplementation 'androidx.test.ext:junit:1.1.1'

implementation 'com.android.support:multidex:1.0.3'
}

// ======== For SDK Releases ========
Expand Down Expand Up @@ -238,3 +242,40 @@ def getRepositoryUsername() {
def getRepositoryPassword() {
return hasProperty('sonatypePassword') ? sonatypePassword : ""
}

project.afterEvaluate {
tasks.create("depsize") {
listConfigurationDependencies(configurations.default)
}

tasks.create("depsize-all-configurations") {
configurations.each {
if (it.isCanBeResolved()) {
listConfigurationDependencies(it)
}
}
}

}

def listConfigurationDependencies(Configuration configuration) {
def formatStr = "%,10.2f"

def size = configuration.collect { it.length() / (1024 * 1024) }.sum()

def out = new StringBuffer()
out << "\nConfiguration name: \"${configuration.name}\"\n"
if (size) {
out << 'Total dependencies size:'.padRight(65)
out << "${String.format(formatStr, size)} Mb\n\n"

configuration.sort { -it.length() }
.each {
out << "${it.name}".padRight(65)
out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
}
} else {
out << 'No dependencies found';
}
println(out)
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ POM_DEVELOPER_ORG_URL=https://amplitude.com/

RELEASE_REPOSITORY_URL=https://oss.sonatype.org/service/local/staging/deploy/maven2/
SNAPSHOT_REPOSITORY_URL=https://oss.sonatype.org/content/repositories/snapshots/

android.useAndroidX=true
android.enableJetifier=true
46 changes: 21 additions & 25 deletions src/main/java/com/amplitude/api/DeviceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

import androidx.ads.identifier.AdvertisingIdClient;
import androidx.ads.identifier.AdvertisingIdInfo;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

@SuppressWarnings("MissingPermission")
public class DeviceInfo {
Expand Down Expand Up @@ -211,27 +216,18 @@ private String getAndCacheAmazonAdvertisingId() {

private String getAndCacheGoogleAdvertisingId() {
try {
Class AdvertisingIdClient = Class
.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
Method getAdvertisingInfo = AdvertisingIdClient.getMethod("getAdvertisingIdInfo",
Context.class);
Object advertisingInfo = getAdvertisingInfo.invoke(null, context);
Method isLimitAdTrackingEnabled = advertisingInfo.getClass().getMethod(
"isLimitAdTrackingEnabled");
Boolean limitAdTrackingEnabled = (Boolean) isLimitAdTrackingEnabled
.invoke(advertisingInfo);
this.limitAdTrackingEnabled =
limitAdTrackingEnabled != null && limitAdTrackingEnabled;
Method getId = advertisingInfo.getClass().getMethod("getId");
advertisingId = (String) getId.invoke(advertisingInfo);
} catch (ClassNotFoundException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services SDK not found!");
} catch (InvocationTargetException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available");
} catch (Exception e) {
AmplitudeLog.getLogger().e(TAG, "Encountered an error connecting to Google Play Services", e);
if (AdvertisingIdClient.isAdvertisingIdProviderAvailable(context)) {
AdvertisingIdInfo advertisingInfo = AdvertisingIdClient.getAdvertisingIdInfo(context).get();
this.limitAdTrackingEnabled = advertisingInfo.isLimitAdTrackingEnabled();
this.advertisingId = advertisingInfo.getId();
}
else
AmplitudeLog.getLogger().w(TAG, "Advertising ID provider not available for device");
} catch (ExecutionException e) {
AmplitudeLog.getLogger().e(TAG, "Encountered an error connecting to Advertising ID Library 01", e);
} catch (InterruptedException e) {
AmplitudeLog.getLogger().e(TAG, "Encountered an error connecting to Advertising ID Library 02", e);
}

return advertisingId;
}

Expand All @@ -246,15 +242,15 @@ private boolean checkGPSEnabled() {
// status 0 corresponds to com.google.android.gms.common.ConnectionResult.SUCCESS;
return status != null && status.intValue() == 0;
} catch (NoClassDefFoundError e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services Util not found!");
AmplitudeLog.getLogger().w(TAG, "Google Play Services Util not found! Test 03", e);
} catch (ClassNotFoundException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services Util not found!");
AmplitudeLog.getLogger().w(TAG, "Google Play Services Util not found! Test 004", e);
} catch (NoSuchMethodException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available");
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available Test 05", e);
} catch (InvocationTargetException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available");
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available Test 06", e);
} catch (IllegalAccessException e) {
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available");
AmplitudeLog.getLogger().w(TAG, "Google Play Services not available Test 07", e);
} catch (Exception e) {
AmplitudeLog.getLogger().w(TAG,
"Error when checking for Google Play Services: " + e);
Expand Down
20 changes: 4 additions & 16 deletions src/test/java/com/amplitude/api/DeviceInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import androidx.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

Expand Down Expand Up @@ -169,13 +169,9 @@ public void testGetLanguage() {
public void testGetAdvertisingIdFromGoogleDevice() {
PowerMockito.mockStatic(AdvertisingIdClient.class);
String advertisingId = "advertisingId";
AdvertisingIdClient.Info info = new AdvertisingIdClient.Info(
advertisingId,
false
);

try {
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context)).thenReturn(info);
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context));
} catch (Exception e) {
fail(e.toString());
}
Expand Down Expand Up @@ -256,13 +252,9 @@ public void testNoLocation() {
public void testUseAdvertisingIdAsDeviceId() {
PowerMockito.mockStatic(AdvertisingIdClient.class);
String advertisingId = "advertisingId";
AdvertisingIdClient.Info info = new AdvertisingIdClient.Info(
advertisingId,
false
);

try {
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context)).thenReturn(info);
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context));
} catch (Exception e) {
fail(e.toString());
}
Expand All @@ -282,13 +274,9 @@ public void testUseAdvertisingIdAsDeviceId() {
public void testDontUseAdvertisingIdAsDeviceId() {
PowerMockito.mockStatic(AdvertisingIdClient.class);
String advertisingId = "advertisingId";
AdvertisingIdClient.Info info = new AdvertisingIdClient.Info(
advertisingId,
true
);

try {
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context)).thenReturn(info);
Mockito.when(AdvertisingIdClient.getAdvertisingIdInfo(context));
} catch (Exception e) {
fail(e.toString());
}
Expand Down

0 comments on commit 39c70ab

Please sign in to comment.