forked from ltrudu/DeviceIdentifiersWrapper
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GetBtMacAddress method to retrieve the Bluetooth Mac Adrress of the device
- Loading branch information
Showing
4 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
DeviceIdentifiersWrapper/src/main/java/com/zebra/deviceidentifierswrapper/ExecutorTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters