PrinterFinder.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package epson.print.screen;
  2. import android.app.Dialog;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.net.Uri;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.IBinder;
  13. import android.os.Message;
  14. import android.os.RemoteException;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.AdapterView;
  18. import android.widget.Button;
  19. import android.widget.ListView;
  20. import android.widget.ProgressBar;
  21. import android.widget.TextView;
  22. import com.epson.iprint.prtlogger.Analytics;
  23. import com.epson.mobilephone.common.escpr.MediaInfo;
  24. import com.epson.mobilephone.common.wifidirect.escprLib;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.Locale;
  28. import epson.common.Constants;
  29. import epson.common.IPAddressUtils;
  30. import epson.common.Utils;
  31. import epson.common.httpclient.IAHttpClient;
  32. import epson.print.ActivityIACommon;
  33. import epson.print.CommonDefine;
  34. import epson.print.MyPrinter;
  35. import epson.print.R;
  36. import epson.print.Util.EPLog;
  37. import epson.print.service.EpsonService;
  38. import epson.print.service.IEpsonService;
  39. import epson.print.service.IEpsonServiceCallback;
  40. import epson.print.widgets.AbstractListBuilder;
  41. import epson.print.widgets.CustomTitleAlertDialogBuilder;
  42. import epson.print.widgets.PrinterInfoBuilder;
  43. public class PrinterFinder extends ActivityIACommon {
  44. private static final int DELAY = 100;
  45. private static final int DIALOG_INFORM = 0;
  46. private static boolean foundPrinter = false;
  47. private static final Object mLock = new Object();
  48. private final int CANCEL_FIND_PRINTER = 2;
  49. private final int DISPLAY_FIND_RESULT = 6;
  50. private final int INFORM_DIALOG = 5;
  51. private final String PRINTER_COMMON_DEVICENAME = "common_devicename";
  52. private final String PRINTER_ID = "id";
  53. private final String PRINTER_INDEX = "index";
  54. private final String PRINTER_IP = escprLib.PRINTER_IP;
  55. private final String PRINTER_NAME = "name";
  56. private final int SEARCH_PRINTER = 1;
  57. private final int SELECT_PRINTER = 4;
  58. private final int UPDATE_PRINTER = 3;
  59. int curError;
  60. private boolean isDialogOpen = false;
  61. private boolean isFinishSearchPrinter = false;
  62. private Boolean isFocused = true;
  63. AbstractListBuilder mBuilder;
  64. private IEpsonServiceCallback mCallback = new IEpsonServiceCallback.Stub() {
  65. public void onGetInkState() throws RemoteException {
  66. }
  67. public void onGetStatusState() throws RemoteException {
  68. }
  69. public void onNotifyContinueable(int i) throws RemoteException {
  70. }
  71. public void onNotifyEndJob(int i) throws RemoteException {
  72. }
  73. public void onNotifyProgress(int i, int i2) throws RemoteException {
  74. }
  75. public void onFindPrinterResult(String str, String str2, String str3, String str4, String str5) throws RemoteException {
  76. Message obtain = Message.obtain();
  77. obtain.what = 3;
  78. Bundle bundle = new Bundle();
  79. bundle.putString("name", str);
  80. bundle.putString(escprLib.PRINTER_IP, str2);
  81. bundle.putString("id", str3);
  82. bundle.putString("common_devicename", str5);
  83. obtain.setData(bundle);
  84. PrinterFinder.this.mHandler.sendMessage(obtain);
  85. }
  86. public void onNotifyError(int i, int i2, boolean z) throws RemoteException {
  87. synchronized (PrinterFinder.mLock) {
  88. boolean unused = PrinterFinder.foundPrinter = Utils.getPrefBoolean(PrinterFinder.this.getApplicationContext(), "PrintSetting", CommonDefine.RE_SEARCH);
  89. if ((i2 == -1300 || i2 == -1351) && !PrinterFinder.foundPrinter) {
  90. i2 = -1100;
  91. } else if ((i2 == -1300 || i2 == -1351 || i2 == -1100) && PrinterFinder.foundPrinter) {
  92. i2 = -11001;
  93. }
  94. }
  95. PrinterFinder printerFinder = PrinterFinder.this;
  96. printerFinder.curError = i2;
  97. printerFinder.mHandler.sendEmptyMessage(5);
  98. }
  99. };
  100. Context mContext;
  101. private ServiceConnection mEpsonConnection = new ServiceConnection() {
  102. public void onServiceDisconnected(ComponentName componentName) {
  103. try {
  104. PrinterFinder.this.mEpsonService.unregisterCallback(PrinterFinder.this.mCallback);
  105. } catch (RemoteException e) {
  106. e.printStackTrace();
  107. }
  108. IEpsonService unused = PrinterFinder.this.mEpsonService = null;
  109. }
  110. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  111. IEpsonService unused = PrinterFinder.this.mEpsonService = IEpsonService.Stub.asInterface(iBinder);
  112. if (PrinterFinder.this.mEpsonService != null) {
  113. try {
  114. PrinterFinder.this.mEpsonService.registerCallback(PrinterFinder.this.mCallback);
  115. } catch (RemoteException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120. };
  121. private IEpsonService mEpsonService = null;
  122. Handler mHandler = new Handler(new Handler.Callback() {
  123. public boolean handleMessage(Message message) {
  124. switch (message.what) {
  125. case 1:
  126. if (PrinterFinder.this.mEpsonService == null) {
  127. PrinterFinder.this.mHandler.sendEmptyMessageDelayed(1, 100);
  128. break;
  129. } else {
  130. try {
  131. boolean unused = PrinterFinder.this.isFinishSearchPrinter = false;
  132. PrinterFinder.this.searchButtonSetEnabled(false);
  133. PrinterFinder.this.mProgressBar.setVisibility(0);
  134. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(PrinterFinder.this.getString(R.string.searching_text));
  135. PrinterFinder.this.mEpsonService.searchPrinters((String) null, (String) null, 1);
  136. break;
  137. } catch (RemoteException e) {
  138. e.printStackTrace();
  139. break;
  140. }
  141. }
  142. case 2:
  143. if (PrinterFinder.this.mEpsonService != null) {
  144. try {
  145. PrinterFinder.this.mEpsonService.cancelSearchPrinter();
  146. break;
  147. } catch (RemoteException e2) {
  148. e2.printStackTrace();
  149. break;
  150. }
  151. }
  152. break;
  153. case 3:
  154. final Bundle data = message.getData();
  155. String string = data.getString("name");
  156. if (string == null || string.contentEquals("FINISH")) {
  157. if (!PrinterFinder.this.mIsClickSelect) {
  158. if (PrinterFinder.this.mBuilder.getData().size() <= 0) {
  159. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(PrinterFinder.this.getString(R.string.EPS_PRNERR_COMM_TITLE1));
  160. } else if (Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPAN.getLanguage()) || Locale.getDefault().getLanguage().equalsIgnoreCase(Locale.JAPANESE.getLanguage())) {
  161. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(PrinterFinder.this.getString(R.string.numberOfPrintersAvailable));
  162. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(PrinterFinder.this.getString(R.string.numberOfPrintersAvailable));
  163. } else {
  164. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(String.format(PrinterFinder.this.getString(R.string.numberOfPrintersAvailable), new Object[]{Integer.valueOf(PrinterFinder.this.mBuilder.getData().size())}));
  165. }
  166. PrinterFinder.this.mBuilder.getAdapter().notifyDataSetChanged();
  167. PrinterFinder.this.mProgressBar.setVisibility(8);
  168. PrinterFinder.this.searchButtonSetEnabled(true);
  169. boolean unused2 = PrinterFinder.this.isFinishSearchPrinter = true;
  170. break;
  171. }
  172. } else {
  173. synchronized (PrinterFinder.mLock) {
  174. if (!PrinterFinder.this.printerList.contains(data.getString("id"))) {
  175. new AsyncTask<Void, Void, MyPrinter[]>() {
  176. protected MyPrinter[] doInBackground(Void... voidArr) {
  177. IAHttpClient.HttpResponse httpResponse;
  178. String buildURL = IPAddressUtils.buildURL(Analytics.EXTENSION_STRING_WEB, data.getString(escprLib.PRINTER_IP), "/PRESENTATION/EPSONCONNECT");
  179. IAHttpClient.HttpGet httpGet = new IAHttpClient.HttpGet(buildURL);
  180. IAHttpClient iAHttpClient = new IAHttpClient();
  181. iAHttpClient.setFollowRedirects(false);
  182. try {
  183. httpResponse = iAHttpClient.execute(httpGet);
  184. } catch (IOException unused) {
  185. EPLog.d("httpResponse", "IOException");
  186. httpResponse = null;
  187. }
  188. if (httpResponse != null) {
  189. int responseCode = httpResponse.getResponseCode();
  190. EPLog.d("Response", "res : " + responseCode);
  191. if (200 == responseCode || (300 <= responseCode && responseCode < 400)) {
  192. MyPrinter myPrinter = new MyPrinter(data.getString("name"), data.getString(escprLib.PRINTER_IP), data.getString("id"), (String) null);
  193. myPrinter.setCommonDeviceName(data.getString("common_devicename"));
  194. return new MyPrinter[]{myPrinter};
  195. } else if (404 == responseCode) {
  196. EPLog.d("Response", "NotFound:" + buildURL);
  197. } else {
  198. EPLog.d("Response", "Error:" + buildURL);
  199. }
  200. }
  201. return null;
  202. }
  203. protected void onPostExecute(MyPrinter[] myPrinterArr) {
  204. super.onPostExecute(myPrinterArr);
  205. if (myPrinterArr != null) {
  206. ((PrinterInfoBuilder) PrinterFinder.this.mBuilder).addPrinter(myPrinterArr[0]);
  207. PrinterFinder.this.printerList.add(data.getString("id"));
  208. }
  209. }
  210. }.execute(new Void[0]);
  211. boolean unused3 = PrinterFinder.foundPrinter = true;
  212. break;
  213. } else {
  214. break;
  215. }
  216. }
  217. }
  218. break;
  219. case 4:
  220. if (PrinterFinder.this.mEpsonService != null) {
  221. try {
  222. PrinterFinder.this.mEpsonService.setCurPrinter(message.getData().getInt("index"));
  223. } catch (RemoteException e3) {
  224. e3.printStackTrace();
  225. }
  226. }
  227. Intent intent = new Intent();
  228. Bundle bundle = new Bundle();
  229. bundle.putString(Constants.PRINTER_NAME, message.getData().getString("name"));
  230. bundle.putString(Constants.PRINTER_IP, message.getData().getString(escprLib.PRINTER_IP));
  231. bundle.putString(Constants.PRINTER_ID, message.getData().getString("id"));
  232. intent.putExtras(bundle);
  233. PrinterFinder.this.setResult(-1, intent);
  234. PrinterFinder.this.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(IPAddressUtils.buildURL(Analytics.EXTENSION_STRING_WEB, message.getData().getString(escprLib.PRINTER_IP), "/PRESENTATION/EPSONCONNECT"))));
  235. PrinterFinder.this.finish();
  236. break;
  237. case 5:
  238. if (!PrinterFinder.this.isDialogOpen) {
  239. boolean unused4 = PrinterFinder.this.isDialogOpen = true;
  240. EPLog.i("mHandler", "curError = " + PrinterFinder.this.curError);
  241. PrinterFinder.this.showDialog(0);
  242. break;
  243. }
  244. break;
  245. }
  246. return false;
  247. }
  248. });
  249. boolean mIsClickSelect = false;
  250. ViewGroup mLayout;
  251. ProgressBar mProgressBar;
  252. Button mSearchButton;
  253. private ArrayList<String> printerList = new ArrayList<>();
  254. protected void onCreate(Bundle bundle) {
  255. super.onCreate(bundle);
  256. if (mEpsonService == null) {
  257. bindService(new Intent(this, EpsonService.class), mEpsonConnection, 1);
  258. }
  259. printerList.clear();
  260. mLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.search_printer_layout, (ViewGroup) null);
  261. buildElements();
  262. mIsClickSelect = false;
  263. setContentView((View) mLayout);
  264. mContext = this;
  265. setActionBar((int) R.string.remoteprinter_get_emailadress, true);
  266. }
  267. private void buildElements() {
  268. mLayout.findViewById(R.id.rl_localremoteselect).setVisibility(8);
  269. mSearchButton = (Button) mLayout.findViewById(R.id.function_button);
  270. mProgressBar = (ProgressBar) mLayout.findViewById(R.id.progress_bar);
  271. searchButtonSetEnabled(false);
  272. mProgressBar.setVisibility(0);
  273. mLayout.findViewById(R.id.ble_wifi_setting).setVisibility(8);
  274. mLayout.findViewById(R.id.not_found_printer_epsonsn).setVisibility(8);
  275. mBuilder = new PrinterInfoBuilder(getBaseContext(), mLayout, 0);
  276. mBuilder.setResource(getIntent().getExtras().getString(Constants.PRINTER_NAME));
  277. mBuilder.build();
  278. mBuilder.refresh();
  279. ((ListView) mLayout.findViewById(16908298)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
  280. public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
  281. if (PrinterFinder.this.isFocused.booleanValue()) {
  282. Boolean unused = PrinterFinder.this.isFocused = false;
  283. PrinterFinder printerFinder = PrinterFinder.this;
  284. printerFinder.mIsClickSelect = true;
  285. Message obtainMessage = printerFinder.mHandler.obtainMessage();
  286. obtainMessage.what = 4;
  287. Bundle bundle = new Bundle();
  288. bundle.putInt("index", i);
  289. bundle.putString("name", ((MyPrinter) PrinterFinder.this.mBuilder.getData().elementAt(i)).getName());
  290. bundle.putString("id", ((MyPrinter) PrinterFinder.this.mBuilder.getData().elementAt(i)).getPrinterId());
  291. bundle.putString(escprLib.PRINTER_IP, ((MyPrinter) PrinterFinder.this.mBuilder.getData().elementAt(i)).getIp());
  292. obtainMessage.setData(bundle);
  293. PrinterFinder.this.mHandler.sendMessageDelayed(obtainMessage, 100);
  294. }
  295. }
  296. });
  297. mSearchButton.setOnClickListener(new View.OnClickListener() {
  298. public void onClick(View view) {
  299. synchronized (PrinterFinder.mLock) {
  300. PrinterFinder.this.printerList.clear();
  301. PrinterFinder.this.mBuilder.refresh();
  302. }
  303. boolean unused = PrinterFinder.this.isFinishSearchPrinter = false;
  304. PrinterFinder.this.searchButtonSetEnabled(false);
  305. PrinterFinder.this.mProgressBar.setVisibility(0);
  306. ((TextView) PrinterFinder.this.mLayout.findViewById(R.id.empty)).setText(PrinterFinder.this.getString(R.string.searching_text));
  307. PrinterFinder.this.mHandler.sendEmptyMessageDelayed(1, 100);
  308. }
  309. });
  310. }
  311. protected void onDestroy() {
  312. super.onDestroy();
  313. if (mEpsonService != null) {
  314. if (!isFinishSearchPrinter) {
  315. mHandler.sendEmptyMessageDelayed(2, 200);
  316. }
  317. try {
  318. mEpsonService.unregisterCallback(mCallback);
  319. unbindService(mEpsonConnection);
  320. } catch (RemoteException e) {
  321. e.printStackTrace();
  322. }
  323. }
  324. mBuilder.destructor();
  325. }
  326. public void onBackPressed() {
  327. super.onBackPressed();
  328. try {
  329. mEpsonService.cancelSearchPrinter();
  330. } catch (RemoteException e) {
  331. e.printStackTrace();
  332. }
  333. }
  334. protected void onResume() {
  335. super.onResume();
  336. EPLog.e("Epson", "PrinterFinder.java call onResume()");
  337. mHandler.sendEmptyMessageDelayed(1, 100);
  338. EPLog.d("PrinterFinder", "Send CHECK_PRINTER Message.");
  339. }
  340. protected void onPause() {
  341. EPLog.d("SearchPrinterScr", "onPause");
  342. super.onPause();
  343. }
  344. protected Dialog onCreateDialog(int i) {
  345. if (i != 0) {
  346. return null;
  347. }
  348. EPLog.i("DIALOG_INFORM", "curError = " + curError);
  349. int i2 = curError;
  350. if (!(i2 == -1100 && i2 == -11001)) {
  351. if (Utils.getPrefBoolean(getApplicationContext(), "PrintSetting", CommonDefine.RE_SEARCH)) {
  352. curError = -11001;
  353. } else {
  354. curError = -1100;
  355. }
  356. }
  357. Integer[] stringId = MediaInfo.ErrorTable.getStringId(curError);
  358. if (stringId == null) {
  359. stringId = new Integer[]{Integer.valueOf(R.string.NOT_IMPLEMENT), Integer.valueOf(R.string.NOT_IMPLEMENT_TITLE)};
  360. }
  361. String string = getString(stringId[0].intValue());
  362. String ssid = Utils.getSSID(this);
  363. if (Utils.isConnectedWifi(this) && curError == -11001 && ssid != null) {
  364. string = String.format(getString(R.string.EPS_ERR_PRINTER_NOT_FOUND_RESEARCH), new Object[]{ssid});
  365. }
  366. return new CustomTitleAlertDialogBuilder(this).setCancelable(false).setTitle(getString(stringId[1].intValue())).setMessage(string).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  367. public void onClick(DialogInterface dialogInterface, int i) {
  368. PrinterFinder.this.removeDialog(0);
  369. boolean unused = PrinterFinder.this.isDialogOpen = false;
  370. }
  371. }).create();
  372. }
  373. public void searchButtonSetEnabled(boolean z) {
  374. mSearchButton.setVisibility(0);
  375. if (z) {
  376. mSearchButton.setEnabled(true);
  377. } else {
  378. mSearchButton.setEnabled(false);
  379. }
  380. }
  381. }