InkRplnInfoClient.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package epson.print.inkrpln;
  2. import android.net.Uri;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10. public class InkRplnInfoClient {
  11. private static final String INFO_PATH = "sncheck";
  12. private static final String KEY_APPEND_SERIAL = "appendSerial";
  13. private static final String KEY_BUTTON_TYPE = "showButton";
  14. private static final String KEY_LANDING_URL = "urlLandingPage";
  15. private static final String KEY_SHOW_INVITATION = "showInvitation";
  16. private static final String VALUE_BUTTON_TYPE_BUY_INK_AND_READY_INK = "BuyInk_ReadyInk";
  17. private static final String VALUE_BUTTON_TYPE_READY_INK_ONLY = "ReadyInk";
  18. @Nullable
  19. public InkRsInfo getInkDsInfo(@NonNull String str, @NonNull String str2) throws IOException {
  20. return convertString(SimpleHttpClient.getString(getInkReplenishInfoUrl(str2, str)));
  21. }
  22. @VisibleForTesting
  23. @NonNull
  24. static URL getInkReplenishInfoUrl(String str, String str2) throws MalformedURLException {
  25. Uri.Builder builder = new Uri.Builder();
  26. builder.encodedPath(str);
  27. builder.appendPath(INFO_PATH);
  28. builder.appendPath(InkReplenishSystem.encodePrinterSerial(str2));
  29. return new URL(builder.build().toString());
  30. }
  31. @Nullable
  32. public static InkRsInfo convertString(@Nullable String str) {
  33. if (str == null) {
  34. return null;
  35. }
  36. InkRsInfo inkRsInfo = new InkRsInfo();
  37. try {
  38. JSONObject jSONObject = new JSONObject(str);
  39. if (jSONObject.has(KEY_LANDING_URL)) {
  40. inkRsInfo.landingUrl = jSONObject.getString(KEY_LANDING_URL);
  41. }
  42. if (jSONObject.has(KEY_BUTTON_TYPE)) {
  43. inkRsInfo.buttonType = 0;
  44. String string = jSONObject.getString(KEY_BUTTON_TYPE);
  45. if (VALUE_BUTTON_TYPE_READY_INK_ONLY.equals(string)) {
  46. inkRsInfo.buttonType = 1;
  47. } else if (VALUE_BUTTON_TYPE_BUY_INK_AND_READY_INK.equals(string)) {
  48. inkRsInfo.buttonType = 2;
  49. }
  50. }
  51. if (jSONObject.has(KEY_SHOW_INVITATION)) {
  52. inkRsInfo.showInvitation = jSONObject.getBoolean(KEY_SHOW_INVITATION);
  53. }
  54. if (jSONObject.has(KEY_APPEND_SERIAL)) {
  55. inkRsInfo.appendSerial = jSONObject.getBoolean(KEY_APPEND_SERIAL);
  56. }
  57. return inkRsInfo;
  58. } catch (NullPointerException | JSONException unused) {
  59. return null;
  60. }
  61. }
  62. public static class InkRsInfo {
  63. public boolean appendSerial;
  64. public int buttonType = 0;
  65. public String landingUrl = null;
  66. public boolean showInvitation = false;
  67. public void copy(InkRsInfo inkRsInfo) {
  68. if (inkRsInfo != null) {
  69. this.landingUrl = inkRsInfo.landingUrl;
  70. this.buttonType = inkRsInfo.buttonType;
  71. this.showInvitation = inkRsInfo.showInvitation;
  72. this.appendSerial = inkRsInfo.appendSerial;
  73. }
  74. }
  75. public boolean needToDisplayInvitationDialog() {
  76. return this.showInvitation && this.landingUrl != null;
  77. }
  78. public int getButtonType() {
  79. return this.buttonType;
  80. }
  81. }
  82. }