Skip to content

Commit

Permalink
Changed AsyncTask to Executor task
Browse files Browse the repository at this point in the history
Added GetBtMacAddress method to retrieve the Bluetooth Mac Adrress of the device
  • Loading branch information
ltrudu committed Mar 8, 2024
1 parent 0d86dbc commit 67e3cf8
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DeviceIdentifiersWrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 33
versionCode 12
versionName "0.9.3"
versionCode 100
versionName "0.10.0"

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.Manifest.permission;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand All @@ -27,6 +28,8 @@ public class DIHelper {
protected static String sIMEI = null;
protected static String sSerialNumber = null;

protected static String sBtMacAddress = null;

public static final long SEC_IN_MS = 1000;
public static final long MIN_IN_MS = SEC_IN_MS * 60;
public static long MAX_EMDK_TIMEOUT_IN_MS = 10 * MIN_IN_MS; // 10 minutes
Expand Down Expand Up @@ -92,7 +95,7 @@ public void onDebugStatus(String message) {
};

new RetrieveOEMInfoTask()
.execute(context, Uri.parse("content://oem_info/oem.zebra.secure/build_serial"),
.executeAsync(context, Uri.parse("content://oem_info/oem.zebra.secure/build_serial"),
tempCallbackInterface);
}

Expand Down Expand Up @@ -159,7 +162,57 @@ public void onDebugStatus(String message) {
}
};

new RetrieveOEMInfoTask().execute(context, Uri.parse("content://oem_info/wan/imei"),
new RetrieveOEMInfoTask().executeAsync(context, Uri.parse("content://oem_info/wan/imei"),
tempCallbackInterface);
}

public static void getBtMacAddress(Context context, IDIResultCallbacks callbackInterface)
{
if(sBtMacAddress != null)
{
if(callbackInterface != null)
{
callbackInterface.onDebugStatus("BT Mac address already in cache.");
}
callbackInterface.onSuccess(sBtMacAddress);
return;
}
if (android.os.Build.VERSION.SDK_INT < 23) {
returnBtMacAddressUsingAndroidAPIs(context, callbackInterface);
} else {
returnBtMacAddressUsingZebraAPIs(context, callbackInterface);
}
}

private static void returnBtMacAddressUsingZebraAPIs(Context context, IDIResultCallbacks callbackInterface) {
IDIResultCallbacks tempCallbackInterface = new IDIResultCallbacks() {
@Override
public void onSuccess(String message) {
sBtMacAddress = message;
callbackInterface.onSuccess(message);
}

@Override
public void onError(String message) {
callbackInterface.onError(message);
}

@Override
public void onDebugStatus(String message) {
callbackInterface.onDebugStatus(message);
}
};

new RetrieveOEMInfoTask().executeAsync(context, Uri.parse("content://oem_info/oem.zebra.secure/bt_mac"),
tempCallbackInterface);
}

private static void returnBtMacAddressUsingAndroidAPIs(Context context, IDIResultCallbacks callbackInterface) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String macAddress = mBluetoothAdapter.getAddress();
if(callbackInterface != null)
{
callbackInterface.onSuccess(macAddress);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.zebra.deviceidentifierswrapper;

// Inspiration from Vitality answer : https://stackoverflow.com/a/68395429

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public abstract class ExecutorTask<Params, Progress, Result> {
public static final String TAG = "ExecutorTask";

private static final Executor THREAD_POOL_EXECUTOR =
new ThreadPoolExecutor(5, 128, 1,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

private final Handler mHandler = new Handler(Looper.getMainLooper());
private boolean mIsInterrupted = false;

protected void onPreExecute(){}
protected abstract Result doInBackground(Params... params);
protected void onPostExecute(Result result){}

protected void onProgressUpdate(Progress... values) {
}
protected void onCancelled() {}

@SafeVarargs
public final void executeAsync(Params... params) {
THREAD_POOL_EXECUTOR.execute(() -> {
try {
checkInterrupted();
mHandler.post(this::onPreExecute);

checkInterrupted();
final Result results = doInBackground(params);

checkInterrupted();
mHandler.post(new Runnable() {
@Override
public void run() {
onPostExecute(results);
}
});
} catch (InterruptedException ex) {
mHandler.post(this::onCancelled);
} catch (Exception ex) {
Log.e(TAG, "executeAsync: " + ex.getMessage() + "\n" + ex.getStackTrace());

}
});
}

private void checkInterrupted() throws InterruptedException {
if (isInterrupted()){
throw new InterruptedException();
}
}

public void cancel(boolean mayInterruptIfRunning){
setInterrupted(mayInterruptIfRunning);
}

public boolean isInterrupted() {
return mIsInterrupted;
}

public void setInterrupted(boolean interrupted) {
mIsInterrupted = interrupted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* (c) Zebra 2020
*/

class RetrieveOEMInfoTask extends AsyncTask<Object, Void, Boolean> {
class RetrieveOEMInfoTask extends ExecutorTask<Object, Void, Boolean> {

@Override
protected Boolean doInBackground(Object... objects) {
Expand Down

0 comments on commit 67e3cf8

Please sign in to comment.