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