ECConfigurationTask.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package epson.epsonconnectregistration;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.net.Uri;
  6. import android.util.Xml;
  7. import android.view.View;
  8. import epson.common.httpclient.IAHttpClient;
  9. import epson.print.R;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.IOException;
  12. import org.xmlpull.v1.XmlPullParserException;
  13. public class ECConfigurationTask extends ECBaseTask {
  14. private static final int CHECK_INTERVAL = 5000;
  15. private static final int MAX_RETRY = 6;
  16. private static final String TAG = "ECConfigurationTask";
  17. private Uri registrationUri;
  18. protected void onPreExecute() {
  19. super.onPreExecute();
  20. enableProgressText(true);
  21. }
  22. private void enableProgressText(boolean z) {
  23. View findViewById;
  24. Activity activity = (Activity) activityWeakReference.get();
  25. if (activity == null || (findViewById = activity.findViewById(R.id.status)) == null) {
  26. EPLog.m83w(TAG, "enableProgressText Failed");
  27. return;
  28. }
  29. EPLog.d(TAG, "enableProgressText called");
  30. findViewById.setVisibility(z ? 0 : 4);
  31. }
  32. protected ECStatus doInBackground(Void... voidArr) {
  33. IAHttpClient iAHttpClient = new IAHttpClient();
  34. Context checkParam = checkParam();
  35. if (checkParam == null) {
  36. return ECStatus.ERROR_UNEXPECTED;
  37. }
  38. Uri endpoint = getEndpoint();
  39. IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(endpoint.toString());
  40. SoapWEBSETUPDataInfo soapWEBSETUPDataInfo = new SoapWEBSETUPDataInfo();
  41. try {
  42. String createSetRequestWEBSETUP = SoapRequestFactory.createSetRequestWEBSETUP(checkParam, endpoint, getLang(checkParam));
  43. httpPost.setContentType("application/soap+xml");
  44. byte[] bytes = createSetRequestWEBSETUP.getBytes(Xml.Encoding.UTF_8.name());
  45. httpPost.setEntity(bytes);
  46. httpPost.setContentLength(Integer.valueOf(bytes.length));
  47. IAHttpClient.HttpResponse execute = iAHttpClient.execute(httpPost);
  48. if (execute.getResponseCode() == 200) {
  49. soapWEBSETUPDataInfo.parseXml(new ByteArrayInputStream(execute.getEntity().toByteArray()));
  50. if (!soapWEBSETUPDataInfo.getRetVal()) {
  51. EPLog.m83w(TAG, "Failed enable EC 1");
  52. return ECStatus.ERROR_CANNOT_ENABLE;
  53. }
  54. int i = 0;
  55. do {
  56. String createGetRequestWEBSETUP = SoapRequestFactory.createGetRequestWEBSETUP(checkParam, endpoint);
  57. httpPost.setContentType("application/soap+xml");
  58. byte[] bytes2 = createGetRequestWEBSETUP.getBytes(Xml.Encoding.UTF_8.name());
  59. httpPost.setEntity(bytes2);
  60. httpPost.setContentLength(Integer.valueOf(bytes2.length));
  61. IAHttpClient.HttpResponse execute2 = iAHttpClient.execute(httpPost);
  62. if (execute2.getResponseCode() == 200) {
  63. soapWEBSETUPDataInfo.parseXml(new ByteArrayInputStream(execute2.getEntity().toByteArray()));
  64. if (!soapWEBSETUPDataInfo.inProgress()) {
  65. break;
  66. }
  67. try {
  68. Thread.sleep(5000);
  69. } catch (InterruptedException e) {
  70. e.printStackTrace();
  71. }
  72. i++;
  73. } else {
  74. throw new IOException("soapRequest ResponseCode = " + execute2.getResponseCode());
  75. }
  76. } while (i < 6);
  77. if (!soapWEBSETUPDataInfo.isSucceeded()) {
  78. EPLog.m83w(TAG, "Failed enable EC 2");
  79. return ECStatus.ERROR_CANNOT_ENABLE;
  80. }
  81. registrationUri = soapWEBSETUPDataInfo.getRegistrationUri();
  82. return ECStatus.ERROR_NONE;
  83. }
  84. throw new IOException("soapRequest ResponseCode = " + execute.getResponseCode());
  85. } catch (IOException e2) {
  86. e2.printStackTrace();
  87. return ECStatus.ERROR_COMMUNICATION;
  88. } catch (XmlPullParserException e3) {
  89. e3.printStackTrace();
  90. return ECStatus.ERROR_UNEXPECTED;
  91. }
  92. }
  93. protected void onPostExecute(ECStatus eCStatus) {
  94. super.onPostExecute(eCStatus);
  95. enableProgressText(false);
  96. final Activity activity = (Activity) activityWeakReference.get();
  97. if (activity == null) {
  98. EPLog.m83w(TAG, "activity is null. Maybe activity died");
  99. } else if (ECStatus.ERROR_NONE.equals(eCStatus)) {
  100. EPLog.d(TAG, "Succeed Configuration");
  101. AlertDialog create = new AlertDialog.Builder(activity).setTitle(R.string.ec_enabled_title).setMessage(R.string.ec_enabled_message).setPositiveButton(R.string.str_next, (DialogInterface.OnClickListener) new DialogInterface.OnClickListener() {
  102. public void onClick(DialogInterface dialogInterface, int i) {
  103. ECConfigurationTask eCConfigurationTask = ECConfigurationTask.this;
  104. eCConfigurationTask.openWebPage(activity, eCConfigurationTask.registrationUri);
  105. activity.finish();
  106. }
  107. }).create();
  108. create.setCancelable(false);
  109. create.show();
  110. } else {
  111. EPLog.d(TAG, "Failed Configuration");
  112. AlertDialog create2 = new AlertDialog.Builder(activity).setTitle(R.string.ec_enabling_error_title).setMessage(R.string.ec_enabling_error_message).setPositiveButton(R.string.str_ok, (DialogInterface.OnClickListener) new DialogInterface.OnClickListener() {
  113. public void onClick(DialogInterface dialogInterface, int i) {
  114. activity.finish();
  115. }
  116. }).create();
  117. create2.setCanceledOnTouchOutside(false);
  118. create2.setOnCancelListener(new DialogInterface.OnCancelListener() {
  119. public void onCancel(DialogInterface dialogInterface) {
  120. activity.finish();
  121. }
  122. });
  123. create2.show();
  124. }
  125. }
  126. }