package com.epson.mobilephone.common.ble; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.AsyncTask; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.support.media.ExifInterface; import com.epson.mobilephone.common.EpLog; import com.epson.mobilephone.common.ble.BleWorkActivity; import com.epson.mobilephone.common.maintain2.MaintainPrinter2; import com.epson.mobilephone.common.wifidirect.WiFiDirectManager; import epson.common.ExternalFileUtils; import epson.common.Utils; import epson.print.CommonDefine; import epson.print.MyPrinter; import epson.print.service.EpsonService; import epson.print.service.IEpsonService; import epson.print.service.IEpsonServiceCallback; import epson.scan.lib.EscanLibException; import epson.scan.lib.ScanSettingHelper; import epson.scan.lib.escanLib; import java.io.IOException; public class ProbePrinter { static final int PROBE_TIMEOUT_SEC = 180; private static final Object lock = new Object(); private static escanLib mScanner = new escanLib(); private final int BIND_SERVICE = 17; private final int CANCEL_FIND_PRINTER = 2; private final int DELAY = 100; private final int SEARCH_PRINTER = 1; private final int SELECT_PRINTER = 4; private BleWorkActivity.CallbackWork completion; private Context context; private volatile boolean isPrinterFound; private IEpsonServiceCallback mCallback = new IEpsonServiceCallback.Stub() { public void onGetStatusState() throws RemoteException { EpLog.m76i(); } public void onGetInkState() throws RemoteException { EpLog.m76i(); } public void onFindPrinterResult(String str, String str2, String str3, String str4, String str5) throws RemoteException { EpLog.m77i("find :" + str + ":" + str2 + ":★" + str3 + "★:" + str4); if (str.contains("FINISH")) { EpLog.m77i(" !! FINISH !!"); if (!ProbePrinter.this.isPrinterFound) { ProbePrinter.this.unbindEpsonService(); EpLog.m77i(" !! 確認失敗 !!"); ProbePrinter.this.callbackCompletion(false); } } else if (str3.contains(ProbePrinter.macAddress)) { EpLog.m77i(" !! FIND OK !! :" + ProbePrinter.macAddress + ": ◆"); boolean unused = ProbePrinter.this.isPrinterFound = true; MyPrinter myPrinter = new MyPrinter(str, str2, str3, str4); Message obtainMessage = ProbePrinter.mHandler.obtainMessage(); obtainMessage.obj = myPrinter; obtainMessage.what = 4; ProbePrinter.mHandler.sendMessage(obtainMessage); ProbePrinter.mHandler.sendEmptyMessageDelayed(2, 100); } } public void onNotifyProgress(int i, int i2) throws RemoteException { EpLog.m76i(); } public void onNotifyContinueable(int i) throws RemoteException { EpLog.m76i(); } public void onNotifyError(int i, int i2, boolean z) throws RemoteException { EpLog.m76i(); } public void onNotifyEndJob(int i) throws RemoteException { EpLog.m76i(); } }; private ServiceConnection mEpsonConnection = new ServiceConnection() { public void onServiceConnected(ComponentName componentName, IBinder iBinder) { EpLog.m76i(); IEpsonService unused = ProbePrinter.mEpsonService = IEpsonService.Stub.asInterface(iBinder); if (ProbePrinter.mEpsonService != null) { try { ProbePrinter.mEpsonService.registerCallback(ProbePrinter.mCallback); } catch (RemoteException e) { e.printStackTrace(); } } } public void onServiceDisconnected(ComponentName componentName) { EpLog.m77i("ComponentName " + componentName); IEpsonService unused = ProbePrinter.mEpsonService = null; } }; private IEpsonService mEpsonService; private Handler mHandler = new Handler(new Handler.Callback() { public boolean handleMessage(Message message) { int i = message.what; if (i == 4) { ProbePrinter probePrinter = ProbePrinter.this; new printerProbeTask(probePrinter.context, (MyPrinter) message.obj).execute(new Void[0]); } else if (i != 17) { switch (i) { case 1: try { ProbePrinter.this.search(); break; } catch (IllegalStateException e) { EpLog.m71e(e.getMessage()); break; } case 2: ProbePrinter.this.interruptSearch(); break; default: EpLog.m77i("not handled (" + message.what + ")"); break; } } else { ProbePrinter.this.bindEpsonService(); } return false; } }); private String macAddress; private ProbePrinter() { } public static ProbePrinter create() { return new ProbePrinter(); } public ProbePrinter setContext(Context context2) { context = context2; return this; } public ProbePrinter setMacAddress(String str) { EpLog.m69d("★" + str + "★ "); macAddress = str; return this; } public ProbePrinter setBleCallback(BleWorkActivity.CallbackWork callbackWork) { completion = callbackWork; return this; } public void search() throws IllegalStateException { String str; if (mEpsonService == null || completion == null || (str = macAddress) == null || str.length() != 12 || completion == null) { EpLog.m71e("Required field is not set."); throw new IllegalStateException("Required field is not set."); } Context context2 = context; context2.bindService(new Intent(context2, EpsonService.class), mEpsonConnection, 1); try { synchronized (lock) { isPrinterFound = false; } EpLog.m69d("mEpsonService.searchPrinters"); mEpsonService.setTimeOut(180); mEpsonService.searchPrinters((String) null, (String) null, 1); } catch (RemoteException e) { e.printStackTrace(); } } public void interruptSearch() { EpLog.m68d(); mHandler.removeMessages(1); if (mEpsonService != null) { try { EpLog.m77i("cancelSearchPrinter"); mEpsonService.cancelSearchPrinter(); } catch (RemoteException e) { e.printStackTrace(); } } unbindEpsonService(); } public void bindEpsonService() { if (mEpsonService == null) { Context context2 = context; context2.bindService(new Intent(context2, EpsonService.class), mEpsonConnection, 1); } if (mEpsonService == null) { EpLog.m71e("mEpsonService NULL"); mHandler.sendEmptyMessageDelayed(17, 1000); return; } EpLog.m77i("bindEpsonService OK !!"); } private void unbindEpsonService() { EpLog.m76i(); if (mEpsonService != null) { if (!this.isPrinterFound) { mHandler.sendEmptyMessageDelayed(2, 200); EpLog.m77i("1"); } try { EpLog.m77i(ExifInterface.GPS_MEASUREMENT_2D); EpLog.m77i("unregisterCallback"); mEpsonService.unregisterCallback(mCallback); context.unbindService(mEpsonConnection); mEpsonService = null; } catch (RemoteException unused) { EpLog.m71e("unregisterCallback mEpsonService"); } EpLog.m77i(ExifInterface.GPS_MEASUREMENT_3D); } } private void callbackCompletion(boolean z) { BleWorkActivity.CallbackWork callbackWork = completion; if (callbackWork != null) { callbackWork.call(Boolean.valueOf(z)); completion = null; } } private void selectPrinter(MyPrinter myPrinter) { myPrinter.setCurPrinter(this.context.getApplicationContext()); Utils.savePref(this.context.getApplicationContext(), "PrintSetting", CommonDefine.RE_SEARCH, true); WiFiDirectManager.resetConnectInfo(this.context.getApplicationContext(), WiFiDirectManager.DEVICE_TYPE_PRINTER); } private class printerProbeTask extends AsyncTask { private Context mContext; private MyPrinter mMyPrinter; private MaintainPrinter2 mPrinter = MaintainPrinter2.getInstance(); public printerProbeTask(Context context, MyPrinter myPrinter) { mContext = context; mMyPrinter = myPrinter; } protected void onPreExecute() { super.onPreExecute(); } protected Integer doInBackground(Void... voidArr) { int doProbePrinter = mPrinter.doProbePrinter(60, mMyPrinter.getPrinterId(), mMyPrinter.getIp(), 3); if (doProbePrinter != 0) { return Integer.valueOf(doProbePrinter); } mPrinter.setMSearchPos(0); int doSetPrinter = mPrinter.doSetPrinter(); if (doSetPrinter != 0) { return Integer.valueOf(doSetPrinter); } mMyPrinter.setLang(mPrinter.doGetLang()); ExternalFileUtils instance = ExternalFileUtils.getInstance(mContext); instance.createTempFolder(instance.getSupportedMediaDir()); int epsWrapperGetSupportedMedia = mPrinter.getMEscpLib().epsWrapperGetSupportedMedia(ExternalFileUtils.getInstance(mContext).getSupportedMediaDir()); if (epsWrapperGetSupportedMedia != 0) { return Integer.valueOf(epsWrapperGetSupportedMedia); } try { Utils.copyFile(instance.getSupportedMedia(), instance.getSavedSupportedMedia()); EpLog.m77i("Success epsWrapperGetSupportedMedia"); } catch (IOException e) { e.printStackTrace(); } try { Utils.copyFile(instance.getAreaInfo(), instance.getSavedAreaInfo()); EpLog.m77i("Success copy AreaInfo"); } catch (IOException e2) { e2.printStackTrace(); } return Integer.valueOf(epsWrapperGetSupportedMedia); } protected void onPostExecute(Integer num) { super.onPostExecute(num); if (num.intValue() == 0) { ProbePrinter.this.selectPrinter(mMyPrinter); ProbePrinter.this.callbackCompletion(true); return; } ProbePrinter.this.callbackCompletion(false); } } private class scanProbeTask extends AsyncTask { private MyPrinter mPrinter; scanProbeTask(MyPrinter myPrinter) { mPrinter = myPrinter; } protected Context doInBackground(Context... contextArr) { boolean z; Context applicationContext = contextArr[0].getApplicationContext(); EpLog.m77i("スキャナ検索処理"); int escanWrapperInitDriver = ProbePrinter.mScanner.escanWrapperInitDriver(applicationContext); if (escanWrapperInitDriver == -1050) { z = true; } else if (escanWrapperInitDriver != 0) { EpLog.m77i("Scan 機能不明"); return contextArr[0]; } else { z = false; } try { ScanSettingHelper.recodeScannerInfo(ProbePrinter.mScanner, applicationContext, mPrinter); EpLog.m77i("Scan 機能あり"); if (!z) { ProbePrinter.mScanner.escanWrapperReleaseDriver(); } EpLog.m77i("YYY"); return contextArr[0]; } catch (EscanLibException | IOException unused) { EpLog.m77i("Scan 機能なし"); Context context = contextArr[0]; if (!z) { ProbePrinter.mScanner.escanWrapperReleaseDriver(); } return context; } catch (Throwable th) { if (!z) { ProbePrinter.mScanner.escanWrapperReleaseDriver(); } throw th; } } protected void onPostExecute(Context context) { super.onPostExecute(context); EpLog.m77i("スキャナ検索onPostExecute"); ProbePrinter.this.callbackCompletion(true); } } }