Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from appmetr/feature/PLAT-1865-appset-id
Browse files Browse the repository at this point in the history
Feature/plat 1865 appset
  • Loading branch information
grooze authored Mar 17, 2022
2 parents 8b12b40 + 6b16367 commit 3c04abc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
5 changes: 3 additions & 2 deletions appmetr/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 30
versionCode 11805
versionName "1.18.5"
versionCode 11806
versionName "1.18.6"
}
buildTypes {
release {
Expand All @@ -26,6 +26,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
implementation 'com.android.installreferrer:installreferrer:1.1'
implementation 'com.google.android.gms:play-services-appset:16.0.2'
}

task clearLibraryJar(type: Delete) {
Expand Down
1 change: 1 addition & 0 deletions appmetr/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>

<application>
<service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

import com.appmetr.android.BuildConfig;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.appset.AppSet;
import com.google.android.gms.appset.AppSetIdClient;
import com.google.android.gms.appset.AppSetIdInfo;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand All @@ -36,6 +42,7 @@ public class RequestParameters {
private static final String MAGIC_ANDROID_ID = "9774d56d682e549c";
private static String googleAid = null;
private static String fireOsId = null;
private static String appSetId;
private final String deviceId;
private final String buildSerial;
private final String androidId;
Expand All @@ -51,6 +58,7 @@ public RequestParameters(@NonNull final Context context, @NonNull String token)
buildSerial = getBuildSerial();
androidId = getAndroidID(context);
userId = getUserID();
getAppSetId(context);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Expand All @@ -75,6 +83,7 @@ public String getDeviceKey(Context context) {
nameValuePairs.add(new HttpNameValuePair("mobAndroidID", getHash(getAndroidID(context))));
nameValuePairs.add(new HttpNameValuePair("mobGoogleAid", getHash(googleAid)));
// use googleAid only if we already have it
nameValuePairs.add(new HttpNameValuePair("mobAppSetId", getHash(appSetId)));
nameValuePairs.add(new HttpNameValuePair("mobFireOsAid", getHash(getFireOsId(context))));
StringBuilder res = new StringBuilder();
for (HttpNameValuePair pair : nameValuePairs) {
Expand Down Expand Up @@ -107,6 +116,7 @@ protected List<HttpNameValuePair> getForMethod(Context context, String method) {
ret.add(new HttpNameValuePair("mobLibVer", LibraryPreferences.VERSION_STRING));
ret.add(new HttpNameValuePair("mobAndroidID", androidId));


String aid = getGoogleId(context);
if (!TextUtils.isEmpty(aid)) {
ret.add(new HttpNameValuePair("mobGoogleAid", aid));
Expand All @@ -118,6 +128,10 @@ protected List<HttpNameValuePair> getForMethod(Context context, String method) {
}
}

if (!TextUtils.isEmpty(appSetId)) {
ret.add(new HttpNameValuePair("mobAppSetId", appSetId));
}

if (buildSerial != null) {
ret.add(new HttpNameValuePair("mobBuildSerial", buildSerial));
}
Expand Down Expand Up @@ -249,6 +263,41 @@ private static String getFireOsId(Context context) {
return fireOsId;
}

/**
* Set Google App set ID if it preset
* If operation failed, returns empty string,
* doesn't return null in any situation.
* Call it only in background thread!
*
* @param context Current context
*/
private static void getAppSetId(Context context) {
if (appSetId == null) {
try {
AppSetIdClient client = AppSet.getClient(context);
Task<AppSetIdInfo> task = client.getAppSetIdInfo();

task.addOnSuccessListener(new OnSuccessListener<AppSetIdInfo>() {
@Override
public void onSuccess(AppSetIdInfo appSetIdInfo) {
appSetId = appSetIdInfo.getId();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Failed to retrieve APP_SET_ID", e);
appSetId = "";
}
});
} catch (Throwable t) {
if (BuildConfig.DEBUG) {
Log.e(TAG, "Failed to retrieve APP_SET_ID", t);
}
appSetId = "";
}
}
}

private static String getHash(String data) {
return TextUtils.isEmpty(data) ?
data : new Murmur3with128bit().putString(data.toLowerCase(Locale.US)).hashString();
Expand Down
4 changes: 2 additions & 2 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.appmetr.android.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 11805
versionName "1.18.5"
versionCode 11806
versionName "1.18.6"
}
buildTypes {
release {
Expand Down

0 comments on commit 3c04abc

Please sign in to comment.