ActivityIpPrinterSetting.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package epson.print.screen;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. import com.epson.mobilephone.common.escpr.MediaInfo;
  16. import com.epson.mobilephone.common.maintain2.MaintainPrinter2;
  17. import epson.common.Constants;
  18. import epson.common.Utils;
  19. import epson.print.ActivityIACommon;
  20. import epson.print.EPPrinterInfo;
  21. import epson.print.EPPrinterManager;
  22. import epson.print.R;
  23. import epson.print.Util.EPLog;
  24. import epson.scan.lib.escanLib;
  25. public class ActivityIpPrinterSetting extends ActivityIACommon {
  26. private static final String IPADDRESS_EXP = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
  27. public static final int PRINTER = 0;
  28. private static final String PRINTER_ID = "id";
  29. private static final String PRINTER_NAME = "name";
  30. private static final String PRINTER_SERIAL_NO = "serial_no";
  31. public static final int SCANNER = 1;
  32. private static final String TAG = "ActivityIpPrinterSetting";
  33. static MaintainPrinter2 mPrinter = MaintainPrinter2.getInstance();
  34. static escanLib mScanner = new escanLib();
  35. private final int ADD_PRINTER = 0;
  36. private final int ADD_SCANNER = 8;
  37. Button clearPrinterIpAdressButton;
  38. Button clearPrinterNameButton;
  39. Handler mHandler = new Handler(new Handler.Callback() {
  40. public boolean handleMessage(Message message) {
  41. Bundle data = message.getData();
  42. int i = message.what;
  43. if (i == 0) {
  44. EPLog.w(ActivityIpPrinterSetting.TAG, "Recieve ADD_PRINTER");
  45. printerInfo.printerName = data.getString("name");
  46. printerInfo.printerID = data.getString("id");
  47. printerInfo.printerSerialNo = data.getString("serial_no");
  48. return false;
  49. } else if (i != 8) {
  50. return false;
  51. } else {
  52. EPLog.w(ActivityIpPrinterSetting.TAG, "Recieve ADD_SCANNER");
  53. printerInfo.scannerID = data.getString("id");
  54. return false;
  55. }
  56. }
  57. });
  58. EPPrinterManager manager;
  59. String oldItemKey = null;
  60. EPPrinterInfo printerInfo;
  61. TextView printerIpAdress;
  62. TextView printerName;
  63. ViewGroup printerNameArea;
  64. protected void onCreate(Bundle bundle) {
  65. Bundle extras;
  66. super.onCreate(bundle);
  67. setContentView((int) R.layout.ipprinter_setting_layout);
  68. setActionBar((int) R.string.str_ipprinter_add, true);
  69. printerNameArea = (ViewGroup) findViewById(R.id.printerNameArea);
  70. printerName = (TextView) findViewById(R.id.printer_name_edit);
  71. printerIpAdress = (TextView) findViewById(R.id.printer_ip_address_edit);
  72. clearPrinterNameButton = (Button) findViewById(R.id.clear_printer_name_btn);
  73. clearPrinterIpAdressButton = (Button) findViewById(R.id.clear_printer_ip_address_btn);
  74. manager = new EPPrinterManager(this);
  75. Intent intent = getIntent();
  76. if (!(intent == null || (extras = intent.getExtras()) == null)) {
  77. oldItemKey = extras.getString(Constants.PRINTER_KEY);
  78. String str = oldItemKey;
  79. if (str != null && str.length() > 0) {
  80. printerInfo = manager.loadIpPrinterInfo(oldItemKey);
  81. }
  82. }
  83. EPPrinterInfo ePPrinterInfo = printerInfo;
  84. if (ePPrinterInfo == null) {
  85. printerNameArea.setVisibility(8);
  86. } else {
  87. printerName.setText(ePPrinterInfo.userDefName);
  88. printerIpAdress.setText(printerInfo.printerIP);
  89. setTitle(R.string.str_ipprinter_edit);
  90. }
  91. clearPrinterNameButton.setOnClickListener(new View.OnClickListener() {
  92. public void onClick(View view) {
  93. printerName.setText("");
  94. }
  95. });
  96. clearPrinterIpAdressButton.setOnClickListener(new View.OnClickListener() {
  97. public void onClick(View view) {
  98. printerIpAdress.setText("");
  99. }
  100. });
  101. }
  102. public void onSaveButton() {
  103. String charSequence = printerIpAdress.getText().toString();
  104. if (printerNameArea.getVisibility() != 8 && printerName.length() <= 0) {
  105. new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.epsonconnect_str_printer_name_error_empty)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  106. public void onClick(DialogInterface dialogInterface, int i) {
  107. dialogInterface.dismiss();
  108. }
  109. }).show();
  110. } else if (charSequence.length() == 0) {
  111. new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.str_ipprinter_ip_null)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  112. public void onClick(DialogInterface dialogInterface, int i) {
  113. dialogInterface.dismiss();
  114. }
  115. }).show();
  116. } else if (!charSequence.matches(IPADDRESS_EXP)) {
  117. new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.str_ipprinter_ip_fomaterror)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  118. public void onClick(DialogInterface dialogInterface, int i) {
  119. dialogInterface.dismiss();
  120. }
  121. }).show();
  122. } else {
  123. new AsyncTask<String, Void, Integer>() {
  124. boolean bAddNewPrinter = false;
  125. public String mNewIpAddress;
  126. WorkingDialog progress;
  127. protected void onPreExecute() {
  128. progress = new WorkingDialog(ActivityIpPrinterSetting.this);
  129. progress.show();
  130. ActivityIpPrinterSetting.mPrinter.getMEscpLib().setHanlder(mHandler);
  131. ActivityIpPrinterSetting.mScanner.setScanHandler(mHandler);
  132. }
  133. protected Integer doInBackground(String... strArr) {
  134. boolean z;
  135. String str = strArr[0];
  136. if (printerNameArea.getVisibility() == 8) {
  137. printerInfo = new EPPrinterInfo();
  138. printerInfo.printerLocation = 3;
  139. printerInfo.printerID = null;
  140. printerInfo.scannerID = null;
  141. bAddNewPrinter = true;
  142. }
  143. mNewIpAddress = str;
  144. int doInitDriver = ActivityIpPrinterSetting.mPrinter.doInitDriver(ActivityIpPrinterSetting.this, 192);
  145. if (doInitDriver != -1050 && doInitDriver != 0) {
  146. return Integer.valueOf(doInitDriver);
  147. }
  148. ActivityIpPrinterSetting.mPrinter.getMEscpLib().setSearchStt(true);
  149. int doProbePrinter = ActivityIpPrinterSetting.mPrinter.doProbePrinter(60, printerInfo.printerID, str, 3);
  150. ActivityIpPrinterSetting.mPrinter.getMEscpLib().setSearchStt(false);
  151. if (doProbePrinter != 0) {
  152. return Integer.valueOf(doProbePrinter);
  153. }
  154. int escanWrapperInitDriver = ActivityIpPrinterSetting.mScanner.escanWrapperInitDriver(ActivityIpPrinterSetting.this);
  155. if (escanWrapperInitDriver == -1050) {
  156. z = true;
  157. } else if (escanWrapperInitDriver != 0) {
  158. return Integer.valueOf(escanWrapperInitDriver);
  159. } else {
  160. z = false;
  161. }
  162. ActivityIpPrinterSetting.mScanner.setSearchStt(true);
  163. int probeScannerByIp = ActivityIpPrinterSetting.mScanner.probeScannerByIp(printerInfo.scannerID, str);
  164. ActivityIpPrinterSetting.mScanner.setSearchStt(false);
  165. if (!z) {
  166. ActivityIpPrinterSetting.mScanner.escanWrapperReleaseDriver();
  167. }
  168. if (probeScannerByIp == -1306) {
  169. EPLog.w(ActivityIpPrinterSetting.TAG, "eScanLib return ESCAN_ERR_SCANNER_NOT_USEFUL");
  170. probeScannerByIp = 0;
  171. }
  172. return Integer.valueOf(probeScannerByIp);
  173. }
  174. protected void onPostExecute(Integer num) {
  175. Integer[] numArr;
  176. if (progress.isShowing()) {
  177. progress.dismiss();
  178. }
  179. if (num.intValue() != 0) {
  180. if (-500001 == num.intValue()) {
  181. numArr = new Integer[]{Integer.valueOf(R.string.str_ipprinter_comerror_edit), Integer.valueOf(R.string.EPS_PRNERR_COMM_TITLE2), 0};
  182. } else {
  183. numArr = MediaInfo.ErrorTable.getStringId(num.intValue());
  184. if (numArr == null) {
  185. numArr = new Integer[]{Integer.valueOf(R.string.NOT_IMPLEMENT), Integer.valueOf(R.string.NOT_IMPLEMENT_TITLE), 1};
  186. }
  187. }
  188. ActivityIpPrinterSetting activityIpPrinterSetting = ActivityIpPrinterSetting.this;
  189. Utils.makeMessageBox(activityIpPrinterSetting, activityIpPrinterSetting.getString(numArr[1].intValue()), getString(numArr[0].intValue()), getString(R.string.str_ok)).show();
  190. } else if (!bAddNewPrinter || manager.loadIpPrinterInfo(printerInfo.printerID) == null) {
  191. if (oldItemKey != null && oldItemKey.length() > 0) {
  192. manager.deleteIpPrinterInfo(oldItemKey);
  193. }
  194. if (printerName.getText().toString().length() > 0) {
  195. printerInfo.userDefName = printerName.getText().toString();
  196. } else {
  197. printerInfo.userDefName = printerInfo.printerName;
  198. }
  199. manager.saveIPPrinterInfo(printerInfo.printerID, printerInfo);
  200. printerInfo.printerIP = mNewIpAddress;
  201. Intent intent = new Intent();
  202. intent.putExtra(Constants.PRINTER_NAME, printerInfo.printerName);
  203. intent.putExtra(Constants.PRINTER_ID, printerInfo.printerID);
  204. intent.putExtra(Constants.SCAN_REFS_SCANNER_ID, printerInfo.scannerID);
  205. intent.putExtra(Constants.PRINTER_IP, printerInfo.printerIP);
  206. intent.putExtra(Constants.PRINTER_SERIAL_NO, printerInfo.printerSerialNo);
  207. setResult(-1, intent);
  208. finish();
  209. } else {
  210. new AlertDialog.Builder(ActivityIpPrinterSetting.this).setCancelable(false).setMessage(getString(R.string.str_ipprinter_registed_already)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
  211. public void onClick(DialogInterface dialogInterface, int i) {
  212. dialogInterface.dismiss();
  213. }
  214. }).show();
  215. }
  216. }
  217. }.execute(new String[]{charSequence});
  218. }
  219. }
  220. public boolean onCreateOptionsMenu(Menu menu) {
  221. getMenuInflater().inflate(R.C2138menu.menu_done, menu);
  222. return super.onCreateOptionsMenu(menu);
  223. }
  224. public boolean onOptionsItemSelected(MenuItem menuItem) {
  225. if (menuItem.getItemId() != R.id.menuSettingsDone) {
  226. return super.onOptionsItemSelected(menuItem);
  227. }
  228. onSaveButton();
  229. return true;
  230. }
  231. }