From 89774e2d2473aadf78cfc86f2a4800d6fb7b99ef Mon Sep 17 00:00:00 2001 From: Andrew Barushev Date: Thu, 23 Dec 2021 16:42:58 +0300 Subject: [PATCH 1/2] F! added macAddress as parameter in AppMetr.setup --- .../java/com/appmetr/android/AppMetr.java | 8 ++-- .../com/appmetr/android/UploadJobService.java | 9 +++-- .../com/appmetr/android/UploadService.java | 8 ++-- .../internal/AppMetrTrackingManager.java | 6 ++- .../android/internal/RequestParameters.java | 37 +++++-------------- .../android/internal/UploadCacheTask.java | 4 +- .../appmetr/android/demo/DemoActivity.java | 2 +- .../android/dummy/utils/AppMetrDirtyHack.java | 2 +- 8 files changed, 32 insertions(+), 44 deletions(-) diff --git a/appmetr/src/main/java/com/appmetr/android/AppMetr.java b/appmetr/src/main/java/com/appmetr/android/AppMetr.java index be14d71..4a893f1 100644 --- a/appmetr/src/main/java/com/appmetr/android/AppMetr.java +++ b/appmetr/src/main/java/com/appmetr/android/AppMetr.java @@ -56,11 +56,11 @@ protected AppMetr(Context context) { super(context); } - public static void setup(String token, Activity activity) throws DataFormatException, SecurityException { + public static void setup(String token, String macAddress, Activity activity) throws DataFormatException, SecurityException { if (BuildConfig.DEBUG) { Log.d(TAG, "#setup from activity"); } - setup(token, activity.getApplicationContext()); + setup(token, macAddress, activity.getApplicationContext()); trackLaunchIntent(activity); } @@ -72,7 +72,7 @@ public static void setup(String token, Activity activity) throws DataFormatExcep * @param context The application context to initialize AppMetr library * @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(String token, String macAddress, Context context) throws DataFormatException, SecurityException { if (BuildConfig.DEBUG) { Log.d(TAG, "#setup"); } @@ -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."); } diff --git a/appmetr/src/main/java/com/appmetr/android/UploadJobService.java b/appmetr/src/main/java/com/appmetr/android/UploadJobService.java index 123701e..7b97302 100644 --- a/appmetr/src/main/java/com/appmetr/android/UploadJobService.java +++ b/appmetr/src/main/java/com/appmetr/android/UploadJobService.java @@ -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() { @Override protected Boolean doInBackground(String... tokens) { - return uploadImpl(tokens[0]); + return uploadImpl(tokens[0], tokens[1]); } @Override @@ -41,7 +42,7 @@ protected void onPostExecute(Boolean success) { uploadTask = null; } }; - uploadTask.execute(token); + uploadTask.execute(token, macAddress); return true; } } @@ -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 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; diff --git a/appmetr/src/main/java/com/appmetr/android/UploadService.java b/appmetr/src/main/java/com/appmetr/android/UploadService.java index 4ac695d..54da16f 100644 --- a/appmetr/src/main/java/com/appmetr/android/UploadService.java +++ b/appmetr/src/main/java/com/appmetr/android/UploadService.java @@ -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"); @@ -31,11 +32,12 @@ 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); } }); } @@ -43,10 +45,10 @@ public void run() { } } - private void uploadImpl(String token) { + private void uploadImpl(String token, String macAddress) { LibraryPreferences preferences = new LibraryPreferences(getBaseContext()); ArrayList fileList = preferences.getFileList(); - UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token); + UploadCacheTask uploadCacheTask = new UploadCacheTask(new ContextProxy(getBaseContext()), token, macAddress); uploadCacheTask.upload(fileList); } diff --git a/appmetr/src/main/java/com/appmetr/android/internal/AppMetrTrackingManager.java b/appmetr/src/main/java/com/appmetr/android/internal/AppMetrTrackingManager.java index a84c9fe..e5b652c 100644 --- a/appmetr/src/main/java/com/appmetr/android/internal/AppMetrTrackingManager.java +++ b/appmetr/src/main/java/com/appmetr/android/internal/AppMetrTrackingManager.java @@ -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) { @@ -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) { @@ -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) @@ -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); } } diff --git a/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java b/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java index c95842e..84a2fca 100644 --- a/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java +++ b/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java @@ -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; @@ -49,9 +47,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, String macAddress) { this.token = token; - macAddress = getMacAddress(context); + this.macAddress = macAddress; deviceId = getDeviceID(context); buildSerial = getBuildSerial(); androidId = getAndroidID(context); @@ -68,6 +66,10 @@ public String getToken() { return token; } + public String getMacAddress() { + return macAddress; + } + public String getUID() { return userId; } @@ -76,7 +78,9 @@ public String getDeviceKey(Context context) { List nameValuePairs = new ArrayList(); nameValuePairs.add(new HttpNameValuePair("token", getToken().toLowerCase(Locale.US))); nameValuePairs.add(new HttpNameValuePair("mobDeviceType", getDeviceType())); - nameValuePairs.add(new HttpNameValuePair("mobMac", getHash(getMacAddress(context)))); + if (macAddress != null && !macAddress.isEmpty()) { + nameValuePairs.add(new HttpNameValuePair("mobMac", macAddress)); + } nameValuePairs.add(new HttpNameValuePair("mobTmDevId", getHash(getDeviceID(context)))); nameValuePairs.add(new HttpNameValuePair("mobAndroidID", getHash(getAndroidID(context)))); nameValuePairs.add(new HttpNameValuePair("mobGoogleAid", getHash(googleAid))); @@ -124,7 +128,7 @@ protected List getForMethod(Context context, String method) { } } - if (macAddress != null) { + if (macAddress != null && !macAddress.isEmpty()) { ret.add(new HttpNameValuePair("mobMac", macAddress)); } @@ -143,27 +147,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; diff --git a/appmetr/src/main/java/com/appmetr/android/internal/UploadCacheTask.java b/appmetr/src/main/java/com/appmetr/android/internal/UploadCacheTask.java index 1ba79b3..be1e8df 100644 --- a/appmetr/src/main/java/com/appmetr/android/internal/UploadCacheTask.java +++ b/appmetr/src/main/java/com/appmetr/android/internal/UploadCacheTask.java @@ -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) { diff --git a/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java b/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java index 51f2be0..913cddc 100644 --- a/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java +++ b/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java @@ -79,7 +79,7 @@ public void onCreate(Bundle savedInstanceState) { } private void initializeAppMetr() throws DataFormatException { - AppMetr.setup(token, this); + AppMetr.setup(token, null, this); } public void logMessage(String message) { diff --git a/dummy/src/androidTest/java/com/appmetr/android/dummy/utils/AppMetrDirtyHack.java b/dummy/src/androidTest/java/com/appmetr/android/dummy/utils/AppMetrDirtyHack.java index c0d0216..3ceaba0 100644 --- a/dummy/src/androidTest/java/com/appmetr/android/dummy/utils/AppMetrDirtyHack.java +++ b/dummy/src/androidTest/java/com/appmetr/android/dummy/utils/AppMetrDirtyHack.java @@ -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() { From b7a6d8a0eb915f51b0119ae732796b3e6fc7765f Mon Sep 17 00:00:00 2001 From: Andrew Barushev Date: Tue, 28 Dec 2021 10:23:04 +0300 Subject: [PATCH 2/2] R! by PR comments --- appmetr/src/main/java/com/appmetr/android/AppMetr.java | 8 ++++---- .../com/appmetr/android/internal/RequestParameters.java | 9 +++++---- .../main/java/com/appmetr/android/demo/DemoActivity.java | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/appmetr/src/main/java/com/appmetr/android/AppMetr.java b/appmetr/src/main/java/com/appmetr/android/AppMetr.java index 4a893f1..8053696 100644 --- a/appmetr/src/main/java/com/appmetr/android/AppMetr.java +++ b/appmetr/src/main/java/com/appmetr/android/AppMetr.java @@ -56,11 +56,11 @@ protected AppMetr(Context context) { super(context); } - public static void setup(String token, String macAddress, 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, macAddress, activity.getApplicationContext()); + setup(activity.getApplicationContext(), token, macAddress); trackLaunchIntent(activity); } @@ -68,11 +68,11 @@ public static void setup(String token, String macAddress, Activity activity) thr * 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, String macAddress, 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"); } diff --git a/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java b/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java index 84a2fca..5fc093b 100644 --- a/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java +++ b/appmetr/src/main/java/com/appmetr/android/internal/RequestParameters.java @@ -14,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; @@ -47,7 +48,7 @@ public class RequestParameters { /** * Retrieve request parameters from context */ - public RequestParameters(@NonNull final Context context, @NonNull String token, String macAddress) { + public RequestParameters(@NonNull final Context context, @NonNull String token, @Nullable String macAddress) { this.token = token; this.macAddress = macAddress; deviceId = getDeviceID(context); @@ -78,8 +79,8 @@ public String getDeviceKey(Context context) { List nameValuePairs = new ArrayList(); nameValuePairs.add(new HttpNameValuePair("token", getToken().toLowerCase(Locale.US))); nameValuePairs.add(new HttpNameValuePair("mobDeviceType", getDeviceType())); - if (macAddress != null && !macAddress.isEmpty()) { - nameValuePairs.add(new HttpNameValuePair("mobMac", macAddress)); + 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)))); @@ -128,7 +129,7 @@ protected List getForMethod(Context context, String method) { } } - if (macAddress != null && !macAddress.isEmpty()) { + if (TextUtils.isEmpty(macAddress)) { ret.add(new HttpNameValuePair("mobMac", macAddress)); } diff --git a/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java b/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java index 913cddc..9685d57 100644 --- a/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java +++ b/demo/src/main/java/com/appmetr/android/demo/DemoActivity.java @@ -79,7 +79,7 @@ public void onCreate(Bundle savedInstanceState) { } private void initializeAppMetr() throws DataFormatException { - AppMetr.setup(token, null, this); + AppMetr.setup(this, token, null); } public void logMessage(String message) {