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.

AddCommandShortcutActivity.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.rthoni.camotion.ui;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.Toast;
  6. import com.crashlytics.android.Crashlytics;
  7. import com.luticate.auth.dbo.LuFullLoginDbo;
  8. import com.luticate.utils.business.LuPromise;
  9. import com.luticate.utils.business.LuRequest;
  10. import com.rthoni.camotion.R;
  11. import com.rthoni.camotion.dbo.CommandDbo;
  12. import com.rthoni.camotion.dbo.LocationDbo;
  13. import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
  14. import com.rthoni.camotion.ui.dialogs.LoginDialog;
  15. import com.rthoni.camotion.ui.fragments.CommandsFragment;
  16. import io.fabric.sdk.android.Fabric;
  17. public class AddCommandShortcutActivity extends Activity implements CommandsFragment.OnCommandClickedInterface {
  18. private LocationDbo _currentLocation = null;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. Fabric.with(this, new Crashlytics());
  23. LuRequest.init(this);
  24. setContentView(R.layout.content_main);
  25. selectLocation();
  26. }
  27. private void selectLocation()
  28. {
  29. LocationPickerDialog.getLocation(this)
  30. .then(new LuPromise.LuConsumer<LocationDbo>() {
  31. @Override
  32. public void execute(LocationDbo location) {
  33. login(location);
  34. }
  35. }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
  36. @Override
  37. public void execute(LuPromise.LuPromiseError data) {
  38. setResult(RESULT_CANCELED);
  39. finish();
  40. }
  41. });
  42. }
  43. private void login(final LocationDbo location)
  44. {
  45. LoginDialog dlg = new LoginDialog(this);
  46. dlg.loginFull(location).then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
  47. @Override
  48. public void execute(LuFullLoginDbo loginDbo) {
  49. _currentLocation = location;
  50. showCommands(loginDbo);
  51. }
  52. }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
  53. @Override
  54. public void execute(LuPromise.LuPromiseError error) {
  55. selectLocation();
  56. Toast.makeText(AddCommandShortcutActivity.this, error.toString(), Toast.LENGTH_LONG).show();
  57. }
  58. });
  59. }
  60. private void showCommands(final LuFullLoginDbo loginDbo)
  61. {
  62. CommandsFragment fragment = new CommandsFragment();
  63. Bundle args = new Bundle();
  64. args.putString("CAMOTION_LOGIN_DBO", loginDbo.toString());
  65. args.putString("CAMOTION_LOCATION_DBO", _currentLocation.toString());
  66. fragment.setArguments(args);
  67. getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
  68. }
  69. @Override
  70. public void onCommandClicked(CommandDbo command) {
  71. Intent shortcutIntent = new Intent(this, CommandExecActivity.class);
  72. shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", _currentLocation.getPseudoMail());
  73. shortcutIntent.putExtra("COMMAND", command.toString());
  74. Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg);
  75. Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  76. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  77. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, command.getName());
  78. intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
  79. setResult(RESULT_OK, intent);
  80. finish();
  81. }
  82. }