package com.rthoni.camotion.ui; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; import com.crashlytics.android.Crashlytics; import com.luticate.auth.dbo.LuFullLoginDbo; import com.luticate.utils.business.LuPromise; import com.luticate.utils.business.LuRequest; import com.rthoni.camotion.R; import com.rthoni.camotion.dbo.CommandDbo; import com.rthoni.camotion.dbo.LocationDbo; import com.rthoni.camotion.ui.dialogs.LocationPickerDialog; import com.rthoni.camotion.ui.dialogs.LoginDialog; import com.rthoni.camotion.ui.fragments.CommandsFragment; import io.fabric.sdk.android.Fabric; public class AddCommandShortcutActivity extends Activity implements CommandsFragment.OnCommandClickedInterface { private LocationDbo _currentLocation = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fabric.with(this, new Crashlytics()); LuRequest.init(this); setContentView(R.layout.content_main); selectLocation(); } private void selectLocation() { LocationPickerDialog.getLocation(this) .then(new LuPromise.LuConsumer() { @Override public void execute(LocationDbo location) { login(location); } }, new LuPromise.LuConsumer() { @Override public void execute(LuPromise.LuPromiseError data) { setResult(RESULT_CANCELED); finish(); } }); } private void login(final LocationDbo location) { LoginDialog dlg = new LoginDialog(this); dlg.loginFull(location).then(new LuPromise.LuConsumer() { @Override public void execute(LuFullLoginDbo loginDbo) { _currentLocation = location; showCommands(loginDbo); } }, new LuPromise.LuConsumer() { @Override public void execute(LuPromise.LuPromiseError error) { selectLocation(); Toast.makeText(AddCommandShortcutActivity.this, error.toString(), Toast.LENGTH_LONG).show(); } }); } private void showCommands(final LuFullLoginDbo loginDbo) { CommandsFragment fragment = new CommandsFragment(); Bundle args = new Bundle(); args.putString("CAMOTION_LOGIN_DBO", loginDbo.toString()); args.putString("CAMOTION_LOCATION_DBO", _currentLocation.toString()); fragment.setArguments(args); getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); } @Override public void onCommandClicked(CommandDbo command) { Intent shortcutIntent = new Intent(this, CommandExecActivity.class); shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", _currentLocation.getPseudoMail()); shortcutIntent.putExtra("COMMAND", command.toString()); Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg); Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, command.getName()); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); setResult(RESULT_OK, intent); finish(); } }