You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandExecActivity.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.rthoni.camotion.ui;
  2. import android.app.Activity;
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AlertDialog;
  6. import android.widget.Toast;
  7. import com.luticate.auth.dbo.LuFullLoginDbo;
  8. import com.luticate.auth.dbo.LuLoginDbo;
  9. import com.luticate.utils.business.LuPromise;
  10. import com.rthoni.camotion.R;
  11. import com.rthoni.camotion.business.CamotionBusiness;
  12. import com.rthoni.camotion.dbo.CommandDbo;
  13. import com.rthoni.camotion.dbo.LocationDbo;
  14. import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
  15. import com.rthoni.camotion.ui.dialogs.LoginDialog;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18. import java.util.List;
  19. public class CommandExecActivity extends Activity {
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_command_exec);
  24. CommandDbo command = new CommandDbo();
  25. try {
  26. JSONObject commandJson = new JSONObject(getIntent().getStringExtra("COMMAND"));
  27. command.fromJson(commandJson);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. setResult(RESULT_CANCELED);
  31. finish();
  32. return;
  33. }
  34. LocationDbo location = getLocation();
  35. if (location == null) {
  36. setResult(RESULT_CANCELED);
  37. finish();
  38. return;
  39. }
  40. askConfirmation(command, location);
  41. }
  42. private LocationDbo getLocation()
  43. {
  44. String locationPseudoMail = getIntent().getStringExtra("LOCATION_PSEUDO_MAIL");
  45. List<LocationDbo> locations = CamotionBusiness.getLocations(this);
  46. for (LocationDbo location : locations) {
  47. if (location.getPseudoMail().equals(locationPseudoMail)) {
  48. return location;
  49. }
  50. }
  51. return null;
  52. }
  53. private void askConfirmation(final CommandDbo command, final LocationDbo location)
  54. {
  55. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  56. builder.setMessage(this.getString(R.string.command_confirm, command.getName()))
  57. .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
  58. @Override
  59. public void onClick(DialogInterface dialog, int which) {
  60. login(command, location);
  61. }
  62. })
  63. .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
  64. @Override
  65. public void onClick(DialogInterface dialog, int which) {
  66. setResult(RESULT_CANCELED);
  67. finish();
  68. }
  69. })
  70. .setOnCancelListener(new DialogInterface.OnCancelListener() {
  71. @Override
  72. public void onCancel(DialogInterface dialog) {
  73. setResult(RESULT_CANCELED);
  74. finish();
  75. }
  76. })
  77. .show();
  78. }
  79. private void login(final CommandDbo command, final LocationDbo location)
  80. {
  81. LoginDialog dlg = new LoginDialog(this);
  82. dlg.login(location).then(new LuPromise.LuConsumer<LuLoginDbo>() {
  83. @Override
  84. public void execute(LuLoginDbo loginDbo) {
  85. CommandExecDialog.showLoadingDialog(location.getConfig(loginDbo), CommandExecActivity.this, command)
  86. .then(new LuPromise.LuConsumer<Boolean>() {
  87. @Override
  88. public void execute(Boolean data) {
  89. setResult(RESULT_OK);
  90. finish();
  91. }
  92. }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
  93. @Override
  94. public void execute(LuPromise.LuPromiseError error) {
  95. setResult(RESULT_CANCELED);
  96. finish();
  97. }
  98. });
  99. }
  100. }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
  101. @Override
  102. public void execute(LuPromise.LuPromiseError error) {
  103. Toast.makeText(CommandExecActivity.this, error.toString(), Toast.LENGTH_LONG).show();
  104. setResult(RESULT_CANCELED);
  105. finish();
  106. }
  107. });
  108. }
  109. }