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

F! added macAddress as parameter in AppMetr.setup <PLAT-1811> #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions appmetr/src/main/java/com/appmetr/android/AppMetr.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ protected AppMetr(Context context) {
super(context);
}

public static void setup(String token, Activity activity) throws DataFormatException, SecurityException {
public static void setup(Activity activity, String token, String macAddress) throws DataFormatException, SecurityException {
if (BuildConfig.DEBUG) {
Log.d(TAG, "#setup from activity");
}
setup(token, activity.getApplicationContext());
setup(activity.getApplicationContext(), token, macAddress);
trackLaunchIntent(activity);
}

/**
* Static method for additional setup libraries data. Must be valid
* parameter token.
*
* @param token parameter which is needed for data upload.
* @param context The application context to initialize AppMetr library
* @param token parameter which is needed for data upload.
* @throws DataFormatException - library throws exception if token is not valid
*/
public static void setup(String token, Context context) throws DataFormatException, SecurityException {
public static void setup(Context context, String token, String macAddress) throws DataFormatException, SecurityException {
if (BuildConfig.DEBUG) {
Log.d(TAG, "#setup");
}
Expand All @@ -82,7 +82,7 @@ public static void setup(String token, Context context) throws DataFormatExcepti

if (msInstance == null) {
msInstance = new AppMetr(context);
msInstance.initialize(token);
msInstance.initialize(token, macAddress);
} else if (BuildConfig.DEBUG) {
Log.d(TAG, "setup failed. Library already initialized.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public final class UploadJobService extends JobService {
public boolean onStartJob(final JobParameters params) {
if (params.getExtras().containsKey(UploadService.EXTRA_PARAMS_TOKEN)) {
String token = params.getExtras().getString(UploadService.EXTRA_PARAMS_TOKEN);
String macAddress = params.getExtras().getString(UploadService.EXTRA_PARAMS_MACADDRESS);
if (!TextUtils.isEmpty(token)) {
uploadTask = new AsyncTask<String, Void, Boolean>() {

@Override
protected Boolean doInBackground(String... tokens) {
return uploadImpl(tokens[0]);
return uploadImpl(tokens[0], tokens[1]);
}

@Override
Expand All @@ -41,7 +42,7 @@ protected void onPostExecute(Boolean success) {
uploadTask = null;
}
};
uploadTask.execute(token);
uploadTask.execute(token, macAddress);
return true;
}
}
Expand All @@ -57,10 +58,10 @@ public boolean onStopJob(JobParameters params) {
return false;
}

private boolean uploadImpl(String token) {
private boolean uploadImpl(String token, String macAddress) {
LibraryPreferences preferences = new LibraryPreferences(getBaseContext());
ArrayList<String> fileList = preferences.getFileList();
UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token);
UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token, macAddress);
uploadCacheTask.upload(fileList);
// only if network error, we retry later
return uploadCacheTask.getStatus() != UploadCacheTask.UploadStatus.NetworkError;
Expand Down
8 changes: 5 additions & 3 deletions appmetr/src/main/java/com/appmetr/android/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class UploadService extends IntentService {
public static final String ACTION_APPMETR_UPLOAD = "com.appmetr.actions.ACTION_UPLOAD";
public static final String EXTRA_PARAMS_TOKEN = "appmetrToken";
public static final String EXTRA_PARAMS_MACADDRESS = "appmetrMacAddress";

public UploadService() {
super("AppmetrUploadService");
Expand All @@ -31,22 +32,23 @@ protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null) {
if (ACTION_APPMETR_UPLOAD.equals(intent.getAction())) {
final String token = intent.getStringExtra(EXTRA_PARAMS_TOKEN);
final String macAddress = intent.getStringExtra(EXTRA_PARAMS_MACADDRESS);
if (!TextUtils.isEmpty(token)) {
executeWithWakeLock(new Runnable() {
@Override
public void run() {
uploadImpl(token);
uploadImpl(token, macAddress);
}
});
}
}
}
}

private void uploadImpl(String token) {
private void uploadImpl(String token, String macAddress) {
LibraryPreferences preferences = new LibraryPreferences(getBaseContext());
ArrayList<String> fileList = preferences.getFileList();
UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token);
UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token, macAddress);
uploadCacheTask.upload(fileList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ else if(mFlushEventsOnResume)
* @param token - parameter which is needed for data upload.
* @throws DataFormatException - library throws exception if token is not valid
*/
protected void initialize(String token) throws DataFormatException, SecurityException {
protected void initialize(String token, String macAddress) throws DataFormatException, SecurityException {
String wesServiceUrl = mContextProxy.webServiceUrl;

if (BuildConfig.DEBUG) {
Expand All @@ -161,7 +161,7 @@ protected void initialize(String token) throws DataFormatException, SecurityExce
throw new DataFormatException("Not valid token!");
}

mRequestParameters = new RequestParameters(mContextProxy.getContext(), token);
mRequestParameters = new RequestParameters(mContextProxy.getContext(), token, macAddress);

mStartLock.lock();
if (!mStarted) {
Expand Down Expand Up @@ -599,6 +599,7 @@ private void uploadCacheDeferred() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
PersistableBundle extras = new PersistableBundle();
extras.putString(UploadService.EXTRA_PARAMS_TOKEN, mRequestParameters.getToken());
extras.putString(UploadService.EXTRA_PARAMS_MACADDRESS, mRequestParameters.getMacAddress());
JobInfo jobInfo = new JobInfo.Builder(UPLOAD_JOB_ID, new ComponentName(mContextProxy.getContext(), UploadJobService.class))
.setExtras(extras)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
Expand All @@ -613,6 +614,7 @@ private void uploadCacheDeferred() {
Intent intent = new Intent(mContextProxy.getContext(), UploadService.class);
intent.setAction(UploadService.ACTION_APPMETR_UPLOAD);
intent.putExtra(UploadService.EXTRA_PARAMS_TOKEN, mRequestParameters.getToken());
intent.putExtra(UploadService.EXTRA_PARAMS_MACADDRESS, mRequestParameters.getMacAddress());
mContextProxy.getContext().startService(intent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Build;
import android.provider.Settings;
Expand All @@ -16,6 +14,7 @@
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.appmetr.android.BuildConfig;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
Expand Down Expand Up @@ -49,9 +48,9 @@ public class RequestParameters {
/**
* Retrieve request parameters from context
*/
public RequestParameters(@NonNull final Context context, @NonNull String token) {
public RequestParameters(@NonNull final Context context, @NonNull String token, @Nullable String macAddress) {
this.token = token;
macAddress = getMacAddress(context);
this.macAddress = macAddress;
deviceId = getDeviceID(context);
buildSerial = getBuildSerial();
androidId = getAndroidID(context);
Expand All @@ -68,6 +67,10 @@ public String getToken() {
return token;
}

public String getMacAddress() {
return macAddress;
}

public String getUID() {
return userId;
}
Expand All @@ -76,7 +79,9 @@ public String getDeviceKey(Context context) {
List<HttpNameValuePair> nameValuePairs = new ArrayList<HttpNameValuePair>();
nameValuePairs.add(new HttpNameValuePair("token", getToken().toLowerCase(Locale.US)));
nameValuePairs.add(new HttpNameValuePair("mobDeviceType", getDeviceType()));
nameValuePairs.add(new HttpNameValuePair("mobMac", getHash(getMacAddress(context))));
if (!TextUtils.isEmpty(macAddress)) {
nameValuePairs.add(new HttpNameValuePair("mobMac", getHash(macAddress)));
}
nameValuePairs.add(new HttpNameValuePair("mobTmDevId", getHash(getDeviceID(context))));
nameValuePairs.add(new HttpNameValuePair("mobAndroidID", getHash(getAndroidID(context))));
nameValuePairs.add(new HttpNameValuePair("mobGoogleAid", getHash(googleAid)));
Expand Down Expand Up @@ -124,7 +129,7 @@ protected List<HttpNameValuePair> getForMethod(Context context, String method) {
}
}

if (macAddress != null) {
if (TextUtils.isEmpty(macAddress)) {
ret.add(new HttpNameValuePair("mobMac", macAddress));
}

Expand All @@ -143,27 +148,6 @@ private static String getDeviceType() {
return Build.MANUFACTURER + "," + Build.MODEL;
}

private static String getMacAddress(Context context) {
String ret = null;
try {
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr != null) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo != null) {
ret = wifiInfo.getMacAddress();
if (ret != null) {
ret = ret.replaceAll("\\W", "").toUpperCase(Locale.US);
}
}
}
} catch (final Throwable t) {
if (BuildConfig.DEBUG) {
Log.w(TAG, "Failed to retrieve wifi MAC address: " + t.getMessage());
}
}
return ret;
}

@SuppressLint("MissingPermission")
private static String getDeviceID(Context context) {
String ret = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public enum UploadStatus {
private RequestParameters mRequestParameters;
private UploadStatus mStatus = UploadStatus.None;

public UploadCacheTask(ContextProxy contextProxy, String token) {
this(contextProxy, new WebServiceRequest(contextProxy.webServiceUrl), new RequestParameters(contextProxy.getContext(), token));
public UploadCacheTask(ContextProxy contextProxy, String token, String macAddress) {
this(contextProxy, new WebServiceRequest(contextProxy.webServiceUrl), new RequestParameters(contextProxy.getContext(), token, macAddress));
}

public UploadCacheTask(ContextProxy contextProxy, WebServiceRequest webServiceRequest, RequestParameters requestParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onCreate(Bundle savedInstanceState) {
}

private void initializeAppMetr() throws DataFormatException {
AppMetr.setup(token, this);
AppMetr.setup(this, token, null);
}

public void logMessage(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AppMetrDirtyHack(String tocken, Activity activity) throws DataFormatExcep
}

public void initialize(String token) throws DataFormatException {
super.initialize(token);
super.initialize(token, null);
}

public static void dirtyDestroySingletonInstance() {
Expand Down