ProbePrinter.java 13 KB

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