From 67e3cf8bd0402224bc11ab4b082f62f61e249578 Mon Sep 17 00:00:00 2001 From: Trudu Laurent Date: Fri, 8 Mar 2024 13:35:14 +0100 Subject: [PATCH] Changed AsyncTask to Executor task Added GetBtMacAddress method to retrieve the Bluetooth Mac Adrress of the device --- DeviceIdentifiersWrapper/build.gradle | 4 +- .../deviceidentifierswrapper/DIHelper.java | 57 +++++++++++++- .../ExecutorTask.java | 75 +++++++++++++++++++ .../RetrieveOEMInfoTask.java | 2 +- 4 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/ExecutorTask.java diff --git a/DeviceIdentifiersWrapper/build.gradle b/DeviceIdentifiersWrapper/build.gradle index 23eb99d..480cafd 100644 --- a/DeviceIdentifiersWrapper/build.gradle +++ b/DeviceIdentifiersWrapper/build.gradle @@ -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' diff --git a/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/DIHelper.java b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/DIHelper.java index fcca803..436b88c 100644 --- a/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/DIHelper.java +++ b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/DIHelper.java @@ -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; @@ -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 @@ -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); } @@ -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); + } + } } diff --git a/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/ExecutorTask.java b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/ExecutorTask.java new file mode 100644 index 0000000..f7b99aa --- /dev/null +++ b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/ExecutorTask.java @@ -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 { + public static final String TAG = "ExecutorTask"; + + private static final Executor THREAD_POOL_EXECUTOR = + new ThreadPoolExecutor(5, 128, 1, + TimeUnit.SECONDS, new LinkedBlockingQueue()); + + 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; + } +} \ No newline at end of file diff --git a/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/RetrieveOEMInfoTask.java b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/RetrieveOEMInfoTask.java index 1d885b7..671cad1 100644 --- a/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/RetrieveOEMInfoTask.java +++ b/DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/RetrieveOEMInfoTask.java @@ -25,7 +25,7 @@ * (c) Zebra 2020 */ -class RetrieveOEMInfoTask extends AsyncTask { +class RetrieveOEMInfoTask extends ExecutorTask { @Override protected Boolean doInBackground(Object... objects) {