ProbePrinter.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package com.epson.mobilephone.common.ble;
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.media.ExifInterface;
  7. import android.os.AsyncTask;
  8. import android.os.Handler;
  9. import android.os.IBinder;
  10. import android.os.Message;
  11. import android.os.RemoteException;
  12. import com.epson.mobilephone.common.maintain2.MaintainPrinter2;
  13. import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
  14. import epson.common.ExternalFileUtils;
  15. import epson.common.Utils;
  16. import epson.print.CommonDefine;
  17. import epson.print.MyPrinter;
  18. import epson.print.Util.EPLog;
  19. import epson.print.service.EpsonService;
  20. import epson.print.service.IEpsonService;
  21. import epson.print.service.IEpsonServiceCallback;
  22. import epson.scan.lib.EscanLibException;
  23. import epson.scan.lib.ScanSettingHelper;
  24. import epson.scan.lib.escanLib;
  25. import java.io.IOException;
  26. public class ProbePrinter {
  27. static final int PROBE_TIMEOUT_SEC = 180;
  28. private static final Object lock = new Object();
  29. private static escanLib mScanner = new escanLib();
  30. private final int BIND_SERVICE = 17;
  31. private final int CANCEL_FIND_PRINTER = 2;
  32. private final int DELAY = 100;
  33. private final int SEARCH_PRINTER = 1;
  34. private final int SELECT_PRINTER = 4;
  35. private BleWorkActivity.CallbackWork completion;
  36. private Context context;
  37. private volatile boolean isPrinterFound;
  38. private IEpsonServiceCallback mCallback = new IEpsonServiceCallback.Stub() {
  39. public void onGetStatusState() throws RemoteException {
  40. EPLog.i();
  41. }
  42. public void onGetInkState() throws RemoteException {
  43. EPLog.i();
  44. }
  45. public void onFindPrinterResult(String str, String str2, String str3, String str4, String str5) throws RemoteException {
  46. EPLog.i("find :" + str + ":" + str2 + ":★" + str3 + "★:" + str4);
  47. if (str.contains("FINISH")) {
  48. EPLog.i(" !! FINISH !!");
  49. if (!isPrinterFound) {
  50. unbindEpsonService();
  51. EPLog.i(" !! 確認失敗 !!");
  52. callbackCompletion(false);
  53. }
  54. } else if (str3.contains(macAddress)) {
  55. EPLog.i(" !! FIND OK !! :" + macAddress + ": ◆");
  56. boolean unused = isPrinterFound = true;
  57. MyPrinter myPrinter = new MyPrinter(str, str2, str3, str4);
  58. Message obtainMessage = mHandler.obtainMessage();
  59. obtainMessage.obj = myPrinter;
  60. obtainMessage.what = 4;
  61. mHandler.sendMessage(obtainMessage);
  62. mHandler.sendEmptyMessageDelayed(2, 100);
  63. }
  64. }
  65. public void onNotifyProgress(int i, int i2) throws RemoteException {
  66. EPLog.i();
  67. }
  68. public void onNotifyContinueable(int i) throws RemoteException {
  69. EPLog.i();
  70. }
  71. public void onNotifyError(int i, int i2, boolean z) throws RemoteException {
  72. EPLog.i();
  73. }
  74. public void onNotifyEndJob(int i) throws RemoteException {
  75. EPLog.i();
  76. }
  77. };
  78. private ServiceConnection mEpsonConnection = new ServiceConnection() {
  79. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  80. EPLog.i();
  81. IEpsonService unused = mEpsonService = IEpsonService.Stub.asInterface(iBinder);
  82. if (mEpsonService != null) {
  83. try {
  84. mEpsonService.registerCallback(mCallback);
  85. } catch (RemoteException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90. public void onServiceDisconnected(ComponentName componentName) {
  91. EPLog.i("ComponentName " + componentName);
  92. IEpsonService unused = mEpsonService = null;
  93. }
  94. };
  95. private IEpsonService mEpsonService;
  96. private Handler mHandler = new Handler(new Handler.Callback() {
  97. public boolean handleMessage(Message message) {
  98. int i = message.what;
  99. if (i == 4) {
  100. ProbePrinter probePrinter = this;
  101. new printerProbeTask(context, (MyPrinter) message.obj).execute(new Void[0]);
  102. } else if (i != 17) {
  103. switch (i) {
  104. case 1:
  105. try {
  106. search();
  107. break;
  108. } catch (IllegalStateException e) {
  109. EPLog.e(e.getMessage());
  110. break;
  111. }
  112. case 2:
  113. interruptSearch();
  114. break;
  115. default:
  116. EPLog.i("not handled (" + message.what + ")");
  117. break;
  118. }
  119. } else {
  120. bindEpsonService();
  121. }
  122. return false;
  123. }
  124. });
  125. private String macAddress;
  126. private ProbePrinter() {
  127. }
  128. public static ProbePrinter create() {
  129. return new ProbePrinter();
  130. }
  131. public ProbePrinter setContext(Context context2) {
  132. context = context2;
  133. return this;
  134. }
  135. public ProbePrinter setMacAddress(String str) {
  136. EPLog.d("★" + str + "★ ");
  137. macAddress = str;
  138. return this;
  139. }
  140. public ProbePrinter setBleCallback(BleWorkActivity.CallbackWork callbackWork) {
  141. completion = callbackWork;
  142. return this;
  143. }
  144. public void search() throws IllegalStateException {
  145. String str;
  146. if (mEpsonService == null || completion == null || (str = macAddress) == null || str.length() != 12 || completion == null) {
  147. EPLog.e("Required field is not set.");
  148. throw new IllegalStateException("Required field is not set.");
  149. }
  150. Context context2 = context;
  151. context2.bindService(new Intent(context2, EpsonService.class), mEpsonConnection, 1);
  152. try {
  153. synchronized (lock) {
  154. isPrinterFound = false;
  155. }
  156. EPLog.d("mEpsonService.searchPrinters");
  157. mEpsonService.setTimeOut(180);
  158. mEpsonService.searchPrinters((String) null, (String) null, 1);
  159. } catch (RemoteException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. public void interruptSearch() {
  164. EPLog.m68d();
  165. mHandler.removeMessages(1);
  166. if (mEpsonService != null) {
  167. try {
  168. EPLog.i("cancelSearchPrinter");
  169. mEpsonService.cancelSearchPrinter();
  170. } catch (RemoteException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. unbindEpsonService();
  175. }
  176. public void bindEpsonService() {
  177. if (mEpsonService == null) {
  178. Context context2 = context;
  179. context2.bindService(new Intent(context2, EpsonService.class), mEpsonConnection, 1);
  180. }
  181. if (mEpsonService == null) {
  182. EPLog.e("mEpsonService NULL");
  183. mHandler.sendEmptyMessageDelayed(17, 1000);
  184. return;
  185. }
  186. EPLog.i("bindEpsonService OK !!");
  187. }
  188. private void unbindEpsonService() {
  189. EPLog.i();
  190. if (mEpsonService != null) {
  191. if (!isPrinterFound) {
  192. mHandler.sendEmptyMessageDelayed(2, 200);
  193. EPLog.i("1");
  194. }
  195. try {
  196. EPLog.i(ExifInterface.GPS_MEASUREMENT_2D);
  197. EPLog.i("unregisterCallback");
  198. mEpsonService.unregisterCallback(mCallback);
  199. context.unbindService(mEpsonConnection);
  200. mEpsonService = null;
  201. } catch (RemoteException unused) {
  202. EPLog.e("unregisterCallback mEpsonService");
  203. }
  204. EPLog.i(ExifInterface.GPS_MEASUREMENT_3D);
  205. }
  206. }
  207. private void callbackCompletion(boolean z) {
  208. BleWorkActivity.CallbackWork callbackWork = completion;
  209. if (callbackWork != null) {
  210. callbackWork.call(Boolean.valueOf(z));
  211. completion = null;
  212. }
  213. }
  214. private void selectPrinter(MyPrinter myPrinter) {
  215. myPrinter.setCurPrinter(context.getApplicationContext());
  216. Utils.savePref(context.getApplicationContext(), "PrintSetting", CommonDefine.RE_SEARCH, true);
  217. WiFiDirectManager.resetConnectInfo(context.getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_PRINTER);
  218. }
  219. private class printerProbeTask extends AsyncTask<Void, Void, Integer> {
  220. private Context mContext;
  221. private MyPrinter mMyPrinter;
  222. private MaintainPrinter2 mPrinter = MaintainPrinter2.getInstance();
  223. public printerProbeTask(Context context, MyPrinter myPrinter) {
  224. mContext = context;
  225. mMyPrinter = myPrinter;
  226. }
  227. protected void onPreExecute() {
  228. super.onPreExecute();
  229. }
  230. protected Integer doInBackground(Void... voidArr) {
  231. int doProbePrinter = mPrinter.doProbePrinter(60, mMyPrinter.getPrinterId(), mMyPrinter.getIp(), 3);
  232. if (doProbePrinter != 0) {
  233. return Integer.valueOf(doProbePrinter);
  234. }
  235. mPrinter.setMSearchPos(0);
  236. int doSetPrinter = mPrinter.doSetPrinter();
  237. if (doSetPrinter != 0) {
  238. return Integer.valueOf(doSetPrinter);
  239. }
  240. mMyPrinter.setLang(mPrinter.doGetLang());
  241. ExternalFileUtils instance = ExternalFileUtils.getInstance(mContext);
  242. instance.createTempFolder(instance.getSupportedMediaDir());
  243. int epsWrapperGetSupportedMedia = mPrinter.getMEscpLib().epsWrapperGetSupportedMedia(ExternalFileUtils.getInstance(mContext).getSupportedMediaDir());
  244. if (epsWrapperGetSupportedMedia != 0) {
  245. return Integer.valueOf(epsWrapperGetSupportedMedia);
  246. }
  247. try {
  248. Utils.copyFile(instance.getSupportedMedia(), instance.getSavedSupportedMedia());
  249. EPLog.i("Success epsWrapperGetSupportedMedia");
  250. } catch (IOException e) {
  251. e.printStackTrace();
  252. }
  253. try {
  254. Utils.copyFile(instance.getAreaInfo(), instance.getSavedAreaInfo());
  255. EPLog.i("Success copy AreaInfo");
  256. } catch (IOException e2) {
  257. e2.printStackTrace();
  258. }
  259. return Integer.valueOf(epsWrapperGetSupportedMedia);
  260. }
  261. protected void onPostExecute(Integer num) {
  262. super.onPostExecute(num);
  263. if (num.intValue() == 0) {
  264. selectPrinter(mMyPrinter);
  265. callbackCompletion(true);
  266. return;
  267. }
  268. callbackCompletion(false);
  269. }
  270. }
  271. private class scanProbeTask extends AsyncTask<Context, Void, Context> {
  272. private MyPrinter mPrinter;
  273. scanProbeTask(MyPrinter myPrinter) {
  274. mPrinter = myPrinter;
  275. }
  276. protected Context doInBackground(Context... contextArr) {
  277. boolean z;
  278. Context applicationContext = contextArr[0].getApplicationContext();
  279. EPLog.i("スキャナ検索処理");
  280. int escanWrapperInitDriver = mScanner.escanWrapperInitDriver(applicationContext);
  281. if (escanWrapperInitDriver == -1050) {
  282. z = true;
  283. } else if (escanWrapperInitDriver != 0) {
  284. EPLog.i("Scan 機能不明");
  285. return contextArr[0];
  286. } else {
  287. z = false;
  288. }
  289. try {
  290. ScanSettingHelper.recodeScannerInfo(mScanner, applicationContext, mPrinter);
  291. EPLog.i("Scan 機能あり");
  292. if (!z) {
  293. mScanner.escanWrapperReleaseDriver();
  294. }
  295. EPLog.i("YYY");
  296. return contextArr[0];
  297. } catch (EscanLibException | IOException unused) {
  298. EPLog.i("Scan 機能なし");
  299. Context context = contextArr[0];
  300. if (!z) {
  301. mScanner.escanWrapperReleaseDriver();
  302. }
  303. return context;
  304. } catch (Throwable th) {
  305. if (!z) {
  306. mScanner.escanWrapperReleaseDriver();
  307. }
  308. throw th;
  309. }
  310. }
  311. protected void onPostExecute(Context context) {
  312. super.onPostExecute(context);
  313. EPLog.i("スキャナ検索onPostExecute");
  314. callbackCompletion(true);
  315. }
  316. }
  317. }