BleAsyncTask.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. public class BleAsyncTask extends AsyncTask<Void, Void, Boolean> {
  19. private static AlertDialog dialog = null;
  20. private static HandlerThread handlerThread = null;
  21. private static boolean mStop = false;
  22. private static BleAsyncTask mTask;
  23. private static Runnable mTimeOutTask;
  24. private BleScanWork mBleWork = null;
  25. private BLEUtility.BleWorkCallback mCallBack = null;
  26. private List<ScannedDevice> mDevicelist = null;
  27. private Boolean mFind = false;
  28. private long mTimeOut = -1;
  29. private long mTimeOutScan = -1;
  30. private Activity m_Activity;
  31. public static void setStop(boolean z) {
  32. EPLog.d("mStop " + z);
  33. mStop = z;
  34. if (z) {
  35. EPLog.d("★ stopScan");
  36. BleScanWork.getInstace().stopScan();
  37. if (mTimeOutTask != null) {
  38. mTimeOutTask = null;
  39. BleAsyncTask bleAsyncTask = mTask;
  40. if (bleAsyncTask != null) {
  41. bleAsyncTask.cancel(true);
  42. }
  43. }
  44. }
  45. }
  46. public BleAsyncTask(Activity activity) {
  47. mTimeOutTask = new Runnable() {
  48. public void run() {
  49. try {
  50. BleAsyncTask.this.get(BleAsyncTask.mTimeOut, TimeUnit.MILLISECONDS);
  51. } catch (InterruptedException unused) {
  52. EPLog.e("InterruptedException");
  53. } catch (ExecutionException unused2) {
  54. EPLog.e("ExecutionException");
  55. } catch (TimeoutException unused3) {
  56. EPLog.i("Running task has timed out!!");
  57. if (BleAsyncTask.mBleWork != null) {
  58. BleAsyncTask.mBleWork.stopScan();
  59. }
  60. BleAsyncTask.mTask.cancel(true);
  61. } catch (CancellationException unused4) {
  62. EPLog.e("CancellationException");
  63. }
  64. }
  65. };
  66. m_Activity = activity;
  67. mTimeOut = (long) (BleWork.getScanAdvertisingPacketTimeout() + 2000);
  68. mTimeOutScan = -1;
  69. }
  70. public BleAsyncTask(Activity activity, BLEUtility.BleWorkCallback bleWorkCallback, int i) {
  71. mTimeOutTask = new Runnable() {
  72. public void run() {
  73. try {
  74. BleAsyncTask.this.get(BleAsyncTask.mTimeOut, TimeUnit.MILLISECONDS);
  75. } catch (InterruptedException unused) {
  76. EPLog.e("InterruptedException");
  77. } catch (ExecutionException unused2) {
  78. EPLog.e("ExecutionException");
  79. } catch (TimeoutException unused3) {
  80. EPLog.i("Running task has timed out!!");
  81. if (BleAsyncTask.mBleWork != null) {
  82. BleAsyncTask.mBleWork.stopScan();
  83. }
  84. BleAsyncTask.mTask.cancel(true);
  85. } catch (CancellationException unused4) {
  86. EPLog.e("CancellationException");
  87. }
  88. }
  89. };
  90. m_Activity = activity;
  91. mTimeOut = (long) (i + 2000);
  92. mTimeOutScan = (long) i;
  93. mCallBack = bleWorkCallback;
  94. }
  95. protected void onPreExecute() {
  96. super.onPreExecute();
  97. mBleWork = BleScanWork.getInstace();
  98. }
  99. /* access modifiers changed from: protected */
  100. /* JADX WARNING: Removed duplicated region for block: B:14:0x0072 */
  101. /* JADX WARNING: Removed duplicated region for block: B:26:0x00ac A[EDGE_INSN: B:26:0x00ac->B:24:0x00ac ?: BREAK , SYNTHETIC] */
  102. /* Code decompiled incorrectly, please refer to instructions dump. */
  103. public java.lang.Boolean doInBackground(java.lang.Void... r5) {
  104. /*
  105. r4 = this;
  106. java.lang.StringBuilder r5 = new java.lang.StringBuilder
  107. r5.<init>()
  108. java.lang.String r0 = "mStop "
  109. r5.append(r0)
  110. boolean r0 = mStop
  111. r5.append(r0)
  112. java.lang.String r5 = r5.toString()
  113. com.epson.mobilephone.common.EPLog.i(r5)
  114. boolean r5 = mStop
  115. if (r5 == 0) goto L_0x001d
  116. java.lang.Boolean r5 = r4.mFind
  117. return r5
  118. L_0x001d:
  119. java.lang.Thread r5 = java.lang.Thread.currentThread()
  120. java.lang.String r0 = "BleAsyncTask"
  121. r5.setName(r0)
  122. com.epson.mobilephone.common.EPLog.m68d()
  123. java.lang.Thread r5 = java.lang.Thread.currentThread()
  124. java.lang.String r5 = r5.getName()
  125. com.epson.mobilephone.common.EPLog.i(r5)
  126. mTask = r4
  127. long r0 = r4.mTimeOut
  128. r2 = 0
  129. int r5 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
  130. if (r5 <= 0) goto L_0x0048
  131. java.lang.Thread r5 = new java.lang.Thread
  132. java.lang.Runnable r0 = mTimeOutTask
  133. r5.<init>(r0)
  134. r5.start()
  135. L_0x0048:
  136. com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
  137. android.app.Activity r0 = r4.m_Activity
  138. boolean r5 = r5.init_scan(r0)
  139. if (r5 != 0) goto L_0x005a
  140. java.lang.String r5 = "BLE is not available !!"
  141. com.epson.mobilephone.common.EPLog.m82w(r5)
  142. java.lang.Boolean r5 = r4.mFind
  143. return r5
  144. L_0x005a:
  145. java.lang.String r5 = "BLE is available!!"
  146. com.epson.mobilephone.common.EPLog.i(r5)
  147. com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
  148. com.epson.mobilephone.common.ble.BleAsyncTask$2 r0 = new com.epson.mobilephone.common.ble.BleAsyncTask$2
  149. r0.<init>()
  150. com.epson.mobilephone.common.ble.BleAsyncTask$3 r1 = new com.epson.mobilephone.common.ble.BleAsyncTask$3
  151. r1.<init>()
  152. r5.search(r0, r1)
  153. L_0x006e:
  154. boolean r5 = mStop
  155. if (r5 != 0) goto L_0x00ac
  156. java.lang.StringBuilder r5 = new java.lang.StringBuilder
  157. r5.<init>()
  158. java.lang.String r0 = "生きてるんです. mStop = "
  159. r5.append(r0)
  160. boolean r0 = mStop
  161. r5.append(r0)
  162. java.lang.String r5 = r5.toString()
  163. com.epson.mobilephone.common.EPLog.d(r5)
  164. r0 = 2000(0x7d0, double:9.88E-321)
  165. java.lang.Thread.sleep(r0) // Catch:{ InterruptedException -> 0x008e }
  166. goto L_0x0093
  167. L_0x008e:
  168. java.lang.String r5 = "InterruptedException"
  169. com.epson.mobilephone.common.EPLog.e((java.lang.String) r5)
  170. L_0x0093:
  171. boolean r5 = r4.isCancelled()
  172. if (r5 != 0) goto L_0x009d
  173. boolean r5 = mStop
  174. if (r5 == 0) goto L_0x006e
  175. L_0x009d:
  176. java.lang.String r5 = "死にます"
  177. com.epson.mobilephone.common.EPLog.d(r5)
  178. java.lang.String r5 = "★ stopScan"
  179. com.epson.mobilephone.common.EPLog.d(r5)
  180. com.epson.mobilephone.common.ble.BleScanWork r5 = r4.mBleWork
  181. r5.stopScan()
  182. L_0x00ac:
  183. java.lang.StringBuilder r5 = new java.lang.StringBuilder
  184. r5.<init>()
  185. java.lang.String r0 = "見つかった? "
  186. r5.append(r0)
  187. java.lang.Boolean r0 = r4.mFind
  188. r5.append(r0)
  189. java.lang.String r5 = r5.toString()
  190. com.epson.mobilephone.common.EPLog.d(r5)
  191. java.lang.Boolean r5 = r4.mFind
  192. return r5
  193. */
  194. throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.BleAsyncTask.doInBackground(java.lang.Void[]):java.lang.Boolean");
  195. }
  196. protected void onPostExecute(Boolean bool) {
  197. super.onPostExecute(bool);
  198. EPLog.d("" + bool);
  199. }
  200. private void showAdvanceToSettingDialog() {
  201. if (mCallBack != null) {
  202. mDevicelist = mBleWork.getScannedDeviceList();
  203. ArrayList arrayList = new ArrayList();
  204. for (ScannedDevice next : mDevicelist) {
  205. arrayList.add(new ScannedDevice(next.getDevice(), next.getRssi(), next.getDisplayName(), next.getHardwareType()));
  206. }
  207. EPLog.d(" ★ mDevicelist " + arrayList.size());
  208. mCallBack.call(arrayList);
  209. return;
  210. }
  211. 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() {
  212. public void onClick(DialogInterface dialogInterface, int i) {
  213. EPLog.d("★ stopScan");
  214. BleAsyncTask.mBleWork.stopScan();
  215. BleAsyncTask bleAsyncTask = BleAsyncTask.this;
  216. List unused = bleAsyncTask.mDevicelist = bleAsyncTask.mBleWork.getScannedDeviceList();
  217. ArrayList arrayList = new ArrayList();
  218. for (ScannedDevice scannedDevice : BleAsyncTask.mDevicelist) {
  219. arrayList.add(new ScannedDevice(scannedDevice.getDevice(), scannedDevice.getRssi(), scannedDevice.getDisplayName(), scannedDevice.getHardwareType()));
  220. }
  221. Intent intent = new Intent(BleAsyncTask.m_Activity.getApplicationContext(), BleWorkActivity.class);
  222. intent.putParcelableArrayListExtra(ScannedDevice.TYPE, arrayList);
  223. BleAsyncTask.m_Activity.startActivity(intent);
  224. }
  225. }).setNegativeButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_not_now_button), new DialogInterface.OnClickListener() {
  226. public void onClick(DialogInterface dialogInterface, int i) {
  227. EPLog.d("★ stopScan");
  228. BleAsyncTask.mBleWork.stopScan();
  229. BleAsyncTask.setStop(true);
  230. }
  231. }).create();
  232. dialog.show();
  233. }
  234. protected void onCancelled(Boolean bool) {
  235. super.onCancelled(bool);
  236. EPLog.d("死にました。 " + bool);
  237. }
  238. protected void onCancelled() {
  239. super.onCancelled();
  240. }
  241. public BleAsyncTask setTimeOut(long j) {
  242. mTimeOut = j;
  243. return this;
  244. }
  245. public static void blePrinterCheck(Context context) {
  246. setStop(false);
  247. EPLog.i();
  248. AlertDialog alertDialog = dialog;
  249. if (alertDialog == null || !alertDialog.isShowing()) {
  250. final Activity activity = (Activity) context;
  251. if (BLEUtility.isThreadAlive("bleCheck")) {
  252. EPLog.d("isAlive bleCheck");
  253. } else if (handlerThread != null) {
  254. EPLog.e("handlerThread != null ");
  255. } else {
  256. EPLog.i("★     handlerThread start ");
  257. handlerThread = new HandlerThread("bleCheck");
  258. handlerThread.start();
  259. new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
  260. public void run() {
  261. EPLog.i(" 0 handlerThread run !!!!");
  262. BleAsyncTask.handlerThread.quitSafely();
  263. new BleAsyncTask(activity).execute(new Void[0]);
  264. }
  265. }, 500);
  266. }
  267. }
  268. }
  269. public static void blePrinterCheck(Context context, final BLEUtility.BleWorkCallback bleWorkCallback) {
  270. setStop(false);
  271. EPLog.i();
  272. final Activity activity = (Activity) context;
  273. if (BLEUtility.isThreadAlive("bleCheck")) {
  274. EPLog.d("   ‼ isAlive bleCheck");
  275. return;
  276. }
  277. EPLog.i("handlerThread");
  278. handlerThread = new HandlerThread("bleCheck");
  279. handlerThread.start();
  280. new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
  281. public void run() {
  282. EPLog.i(" handlerThread run !!!!");
  283. BleAsyncTask.handlerThread.quitSafely();
  284. new BleAsyncTask(activity, bleWorkCallback, BleWork.getScanAdvertisingPacketTimeout()).execute(new Void[0]);
  285. }
  286. }, 1000);
  287. }
  288. public static void blePrinterCheckStop() {
  289. EPLog.i();
  290. if (handlerThread != null) {
  291. setStop(true);
  292. handlerThread.quitSafely();
  293. handlerThread = null;
  294. AlertDialog alertDialog = dialog;
  295. if (alertDialog != null && alertDialog.isShowing()) {
  296. dialog.dismiss();
  297. }
  298. Thread thread = BLEUtility.getThread("bleCheck");
  299. if (thread != null && thread.isAlive()) {
  300. EPLog.i("interrupt");
  301. thread.interrupt();
  302. try {
  303. EPLog.i("join");
  304. thread.join(50);
  305. } catch (InterruptedException e) {
  306. EPLog.e(e.getMessage());
  307. }
  308. }
  309. } else {
  310. EPLog.i("handlerThread NULL return !!");
  311. }
  312. }
  313. }