BleAsyncTask.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.epson.mobilephone.common.ble;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.os.AsyncTask;
  8. import android.os.Handler;
  9. import android.os.HandlerThread;
  10. import com.epson.mobilephone.common.ble.util.BLEUtility;
  11. import com.epson.mobilephone.common.ble.util.ScannedDevice;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.concurrent.CancellationException;
  15. import java.util.concurrent.ExecutionException;
  16. import java.util.concurrent.TimeUnit;
  17. import java.util.concurrent.TimeoutException;
  18. import epson.print.R;
  19. import epson.print.Util.EPLog;
  20. public class BleAsyncTask extends AsyncTask<Void, Void, Boolean> {
  21. private static AlertDialog dialog = null;
  22. private static HandlerThread handlerThread = null;
  23. private static boolean mStop = false;
  24. private static BleAsyncTask mTask;
  25. private static Runnable mTimeOutTask;
  26. private BleScanWork mBleWork = null;
  27. private BLEUtility.BleWorkCallback mCallBack = null;
  28. private List<ScannedDevice> mDevicelist = null;
  29. private Boolean mFind = false;
  30. private long mTimeOut = -1;
  31. private long mTimeOutScan = -1;
  32. private Activity m_Activity;
  33. public static void setStop(boolean z) {
  34. EPLog.d("mStop " + z);
  35. mStop = z;
  36. if (z) {
  37. EPLog.d("★ stopScan");
  38. BleScanWork.getInstace().stopScan();
  39. if (mTimeOutTask != null) {
  40. mTimeOutTask = null;
  41. BleAsyncTask bleAsyncTask = mTask;
  42. if (bleAsyncTask != null) {
  43. cancel(true);
  44. }
  45. }
  46. }
  47. }
  48. public BleAsyncTask(Activity activity) {
  49. mTimeOutTask = new Runnable() {
  50. public void run() {
  51. try {
  52. get(mTimeOut, TimeUnit.MILLISECONDS);
  53. } catch (InterruptedException unused) {
  54. EPLog.e("InterruptedException");
  55. } catch (ExecutionException unused2) {
  56. EPLog.e("ExecutionException");
  57. } catch (TimeoutException unused3) {
  58. EPLog.i("Running task has timed out!!");
  59. if (mBleWork != null) {
  60. mBleWork.stopScan();
  61. }
  62. mTask.cancel(true);
  63. } catch (CancellationException unused4) {
  64. EPLog.e("CancellationException");
  65. }
  66. }
  67. };
  68. m_Activity = activity;
  69. mTimeOut = (long) (BleWork.getScanAdvertisingPacketTimeout() + 2000);
  70. mTimeOutScan = -1;
  71. }
  72. public BleAsyncTask(Activity activity, BLEUtility.BleWorkCallback bleWorkCallback, int i) {
  73. mTimeOutTask = new Runnable() {
  74. public void run() {
  75. try {
  76. get(mTimeOut, TimeUnit.MILLISECONDS);
  77. } catch (InterruptedException unused) {
  78. EPLog.e("InterruptedException");
  79. } catch (ExecutionException unused2) {
  80. EPLog.e("ExecutionException");
  81. } catch (TimeoutException unused3) {
  82. EPLog.i("Running task has timed out!!");
  83. if (mBleWork != null) {
  84. mBleWork.stopScan();
  85. }
  86. mTask.cancel(true);
  87. } catch (CancellationException unused4) {
  88. EPLog.e("CancellationException");
  89. }
  90. }
  91. };
  92. m_Activity = activity;
  93. mTimeOut = (long) (i + 2000);
  94. mTimeOutScan = (long) i;
  95. mCallBack = bleWorkCallback;
  96. }
  97. protected void onPreExecute() {
  98. super.onPreExecute();
  99. mBleWork = BleScanWork.getInstace();
  100. }
  101. public java.lang.Boolean doInBackground(java.lang.Void... r5) {
  102. throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.doInBackground(java.lang.Void[]):java.lang.Boolean");
  103. }
  104. protected void onPostExecute(Boolean bool) {
  105. super.onPostExecute(bool);
  106. EPLog.d("" + bool);
  107. }
  108. private void showAdvanceToSettingDialog() {
  109. if (mCallBack != null) {
  110. mDevicelist = mBleWork.getScannedDeviceList();
  111. ArrayList arrayList = new ArrayList();
  112. for (ScannedDevice next : mDevicelist) {
  113. arrayList.add(new ScannedDevice(next.getDevice(), next.getRssi(), next.getDisplayName(), next.getHardwareType()));
  114. }
  115. EPLog.d(" ★ mDevicelist " + arrayList.size());
  116. mCallBack.call(arrayList);
  117. return;
  118. }
  119. dialog = new AlertDialog.Builder(m_Activity).setCancelable(false).setMessage(m_Activity.getString(R.string.BLE_find_unconfigured_printer_title)).setPositiveButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_setup_button), new DialogInterface.OnClickListener() {
  120. public void onClick(DialogInterface dialogInterface, int i) {
  121. EPLog.d("★ stopScan");
  122. mBleWork.stopScan();
  123. BleAsyncTask bleAsyncTask = BleAsyncTask.this;
  124. List unused = mDevicelist = mBleWork.getScannedDeviceList();
  125. ArrayList arrayList = new ArrayList();
  126. for (ScannedDevice scannedDevice : mDevicelist) {
  127. arrayList.add(new ScannedDevice(scannedDevice.getDevice(), scannedDevice.getRssi(), scannedDevice.getDisplayName(), scannedDevice.getHardwareType()));
  128. }
  129. Intent intent = new Intent(m_Activity.getApplicationContext(), BleWorkActivity.class);
  130. intent.putParcelableArrayListExtra(ScannedDevice.TYPE, arrayList);
  131. m_Activity.startActivity(intent);
  132. }
  133. }).setNegativeButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_not_now_button), new DialogInterface.OnClickListener() {
  134. public void onClick(DialogInterface dialogInterface, int i) {
  135. EPLog.d("★ stopScan");
  136. mBleWork.stopScan();
  137. setStop(true);
  138. }
  139. }).create();
  140. dialog.show();
  141. }
  142. protected void onCancelled(Boolean bool) {
  143. super.onCancelled(bool);
  144. EPLog.d("死にました。 " + bool);
  145. }
  146. protected void onCancelled() {
  147. super.onCancelled();
  148. }
  149. public BleAsyncTask setTimeOut(long j) {
  150. mTimeOut = j;
  151. return this;
  152. }
  153. public static void blePrinterCheck(Context context) {
  154. setStop(false);
  155. EPLog.i();
  156. AlertDialog alertDialog = dialog;
  157. if (alertDialog == null || !alertDialog.isShowing()) {
  158. final Activity activity = (Activity) context;
  159. if (BLEUtility.isThreadAlive("bleCheck")) {
  160. EPLog.d("isAlive bleCheck");
  161. } else if (handlerThread != null) {
  162. EPLog.e("handlerThread != null ");
  163. } else {
  164. EPLog.i("★     handlerThread start ");
  165. handlerThread = new HandlerThread("bleCheck");
  166. handlerThread.start();
  167. new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
  168. public void run() {
  169. EPLog.i(" 0 handlerThread run !!!!");
  170. handlerThread.quitSafely();
  171. new BleAsyncTask(activity).execute(new Void[0]);
  172. }
  173. }, 500);
  174. }
  175. }
  176. }
  177. public static void blePrinterCheck(Context context, final BLEUtility.BleWorkCallback bleWorkCallback) {
  178. setStop(false);
  179. EPLog.i();
  180. final Activity activity = (Activity) context;
  181. if (BLEUtility.isThreadAlive("bleCheck")) {
  182. EPLog.d("   ‼ isAlive bleCheck");
  183. return;
  184. }
  185. EPLog.i("handlerThread");
  186. handlerThread = new HandlerThread("bleCheck");
  187. handlerThread.start();
  188. new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
  189. public void run() {
  190. EPLog.i(" handlerThread run !!!!");
  191. handlerThread.quitSafely();
  192. new BleAsyncTask(activity, bleWorkCallback, BleWork.getScanAdvertisingPacketTimeout()).execute(new Void[0]);
  193. }
  194. }, 1000);
  195. }
  196. public static void blePrinterCheckStop() {
  197. EPLog.i();
  198. if (handlerThread != null) {
  199. setStop(true);
  200. handlerThread.quitSafely();
  201. handlerThread = null;
  202. AlertDialog alertDialog = dialog;
  203. if (alertDialog != null && alertDialog.isShowing()) {
  204. dialog.dismiss();
  205. }
  206. Thread thread = BLEUtility.getThread("bleCheck");
  207. if (thread != null && thread.isAlive()) {
  208. EPLog.i("interrupt");
  209. thread.interrupt();
  210. try {
  211. EPLog.i("join");
  212. thread.join(50);
  213. } catch (InterruptedException e) {
  214. EPLog.e(e.getMessage());
  215. }
  216. }
  217. } else {
  218. EPLog.i("handlerThread NULL return !!");
  219. }
  220. }
  221. }