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