InkRplnInfoClient.java 3.3 KB

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