diff --git a/.idea/misc.xml b/.idea/misc.xml
index 7158618..cca2cda 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/Battery.java b/app/src/main/java/com/kasilov/andrew/solidrobot/Battery.java
index d22ed7a..936ddcf 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/Battery.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/Battery.java
@@ -1,23 +1,73 @@
package com.kasilov.andrew.solidrobot;
-public abstract class Battery {
+import android.os.AsyncTask;
- int chargeLevel = 0;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryCharging;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryStatus;
+import com.kasilov.andrew.solidrobot.utils.LogUtil;
- abstract void charge();
+import java.util.concurrent.TimeUnit;
- protected void turnOnIndicator(){}
+abstract class Battery implements IBatteryCharging, IBatteryStatus {
- protected void turnOffIndicator() { }
+ protected IBatteryStatus iBatteryStatus;
+ protected BatteryCharging batteryCharging;
+ private boolean isChargeable = true;
+ private boolean charged = false;
- public abstract void setCharged(boolean b);
+ Battery() {
+ this.batteryCharging = new BatteryCharging();
+ }
- public abstract boolean isPlugged();
+ @Override
+ public void onBatteryCharged() {
+ this.setCharged(true);
+ this.setChargeable(false);
+ this.iBatteryStatus.onBatteryCharged();
+ }
- public abstract boolean isCharged();
+ public abstract int getChargeLevel();
- public abstract boolean isIndicatorTurnedOn();
+ public boolean isChargeable() {
+ return isChargeable;
+ }
- public abstract void setPlugged(boolean b);
+ private void setChargeable(boolean chargeable) {
+ isChargeable = chargeable;
+ }
+
+ public boolean isCharged() {
+ return charged;
+ }
+
+ protected void setCharged(boolean charged) {
+ this.charged = charged;
+ }
+
+ abstract void notifyBatteriesChargingIsFinished(Integer chargeLevel);
+
+ protected class BatteryCharging extends AsyncTask {
+
+ @Override
+ protected Integer doInBackground(Integer... integers) {
+ LogUtil.makeLog("Charging started");
+ int chargeLevel = 0;
+ while (chargeLevel < 100) {
+ chargeLevel += integers[0];
+ try {
+ TimeUnit.MILLISECONDS.sleep(500);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ return chargeLevel;
+ }
+
+ @Override
+ protected void onPostExecute(Integer integer) {
+ Battery.this.notifyBatteriesChargingIsFinished(integer);
+ Battery.this.onBatteryCharged();
+ }
+ }
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/ChargingDevice.java b/app/src/main/java/com/kasilov/andrew/solidrobot/ChargingDevice.java
index f6485c8..2ff5916 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/ChargingDevice.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/ChargingDevice.java
@@ -1,13 +1,4 @@
package com.kasilov.andrew.solidrobot;
public class ChargingDevice {
- private boolean PLuggedIn;
-
- public void setPLuggedIn(boolean PLuggedIn) {
- this.PLuggedIn = PLuggedIn;
- }
-
- public boolean isPluggedIn() {
- return PLuggedIn;
- }
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/MainActivity.java b/app/src/main/java/com/kasilov/andrew/solidrobot/MainActivity.java
index c3f2bea..e445416 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/MainActivity.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/MainActivity.java
@@ -3,193 +3,197 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
+import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
-public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
+import com.kasilov.andrew.solidrobot.interfaces.IErrorState;
+import com.kasilov.andrew.solidrobot.interfaces.IViewControl;
+public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener, IViewControl {
- private static final String LOG = "ROBOT_LOG";
- Robot robot;
- private NewBattery newBattery;
- private TextView robotIndicator, battery_plugged, battery_discharged, charging, moving, reachedDestination;
- private int alreadyMoved = 170;
- private boolean destinationReached = false;
- ImageView imageView;
- private OldBattery oldBattery;
- private ChargingDevice chargingDevice;
+ private TextView tv_robot_power;
+ private TextView tv_battery_inject_state;
+ private TextView tv_battery_charge_state;
+ private TextView tv_charging_state;
+ private TextView tv_robot_movement_state;
+ private ImageView iv_battery_indicator;
+ private ToggleButton btn_robot_power;
+ private ToggleButton btn_plug_charging_device;
+ private Presenter presenter;
+
+ private IErrorState iErrorState = new IErrorState() {
+ @Override
+ public void onError(String message) {
+ Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
+ }
+ };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imageView);
- robotIndicator = (TextView) findViewById(R.id.tv_indicator_robot_on);
- battery_plugged = (TextView) findViewById(R.id.tv_indicator_battery_plugged_in);
- battery_discharged = (TextView) findViewById(R.id.tv_indicator_battery_discharged);
- charging = (TextView) findViewById(R.id.tv_indicator_charging);
- moving = (TextView) findViewById(R.id.tv_indicator_moving);
- reachedDestination = (TextView) findViewById(R.id.tv_indicator_reached_the_destination);
- ToggleButton btn_robot_power = (ToggleButton) findViewById(R.id.switch_turn_on_off);
- ToggleButton btn_battery_plugged = (ToggleButton) findViewById(R.id.switch_battery_plugged);
- ToggleButton btn_plug_charging_device = (ToggleButton) findViewById(R.id.switch_plug_chrging_device);
- ToggleButton btn_old_type_battery = (ToggleButton) findViewById(R.id.toggleButton);
- btn_old_type_battery.setOnCheckedChangeListener(this);
+ initializeViews();
+ presenter = new Presenter(this, iErrorState);
+ }
+
+
+ private void initializeViews() {
+ iv_battery_indicator = (ImageView) findViewById(R.id.imv_battery_indicator);
+ tv_robot_power = (TextView) findViewById(R.id.tv_indicator_robot_on);
+ tv_battery_inject_state = (TextView) findViewById(R.id.tv_indicator_battery_plugged_in);
+ tv_battery_charge_state = (TextView) findViewById(R.id.tv_indicator_battery_discharged);
+ tv_charging_state = (TextView) findViewById(R.id.tv_indicator_charging);
+ tv_robot_movement_state = (TextView) findViewById(R.id.tv_indicator_moving);
+ btn_robot_power = (ToggleButton) findViewById(R.id.btn_robot_power);
+ Button btn_new_type_battery_injection = (Button) findViewById(R.id.btn_new_type_battery_injection);
+ btn_plug_charging_device = (ToggleButton) findViewById(R.id.btn_charging_device_injection);
+ Button btn_old_type_battery_injection = (Button) findViewById(R.id.btn_old_type_battery_injection);
+ Button btn_plug_out_battery = (Button) findViewById(R.id.btn_plug_out_battery);
+ btn_plug_out_battery.setOnClickListener(this);
+ btn_old_type_battery_injection.setOnClickListener(this);
btn_robot_power.setOnCheckedChangeListener(this);
- btn_battery_plugged.setOnCheckedChangeListener(this);
+ btn_new_type_battery_injection.setOnClickListener(this);
btn_plug_charging_device.setOnCheckedChangeListener(this);
- robot = new Robot();
- chargingDevice = new ChargingDevice();
- newBattery = new NewBattery();
- oldBattery = new OldBattery();
- (findViewById(R.id.btn_move)).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (robot.turnedOn) {
- if (!robot.getChargingDevice().isPluggedIn()) {
- robot.getBattery().setCharged(false);
- battery_discharged.setText("Battery discharged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorRed));
- imageView.setImageResource(R.drawable.red_circle);
- if (robot.getBattery().isIndicatorTurnedOn()) {
- imageView.setImageResource(R.drawable.red_circle);
- try {
- robot.getBattery().turnOffIndicator();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- alreadyMoved -= robot.getBattery().chargeLevel;
- if (alreadyMoved < 0) {
- destinationReached = true;
- }else
- {
- Toast.makeText(MainActivity.this, "Insert another battery", Toast.LENGTH_LONG).show();
- }
- if (destinationReached) {
- reachedDestination.setText("Destination reached");
- reachedDestination.setTextColor(getResources().getColor(R.color.colorGreen));
- Toast.makeText(getApplicationContext(), "Destination reached. Battery level " + Math.abs(alreadyMoved), Toast.LENGTH_LONG).show();
- }
- } else {
- Toast.makeText(getApplicationContext(), "Charging device is plugged in", Toast.LENGTH_LONG).show();
- }
- } else {
- Toast.makeText(getApplicationContext(), "Robot is turned off", Toast.LENGTH_LONG).show();
- }
- }
- });
-
+ Button btn_move = (Button) findViewById(R.id.btn_move);
+ btn_move.setOnClickListener(this);
}
-
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
- case R.id.switch_turn_on_off:
- if (robot.getBattery().isPlugged()) {
- if (robot.getBattery().isCharged()) {
- if (isChecked) {
- robot.turnedOn = true;
- robotIndicator.setText("Robot turned on");
- robotIndicator.setTextColor(getResources().getColor(R.color.colorGreen));
- } else {
- robot.turnedOn = false;
- robotIndicator.setText("Robot turned off");
- robotIndicator.setTextColor(getResources().getColor(R.color.colorRed));
- }
- } else {
- compoundButton.setChecked(false);
- Toast.makeText(this, "Battery discharged", Toast.LENGTH_LONG).show();
- }
-
- } else {
- compoundButton.setChecked(false);
- Toast.makeText(this, "Battery is not plugged in", Toast.LENGTH_LONG).show();
- }
+ case R.id.btn_robot_power:
+ if (isChecked) this.presenter.turnOnRobot();
+ else this.presenter.turnOffRobot();
+ break;
+ case R.id.btn_charging_device_injection:
+ if (isChecked) this.presenter.plugInChargingDevice();
+ else this.presenter.plugOutChargingDevice();
break;
- case R.id.switch_plug_chrging_device:
- if (robot.getBattery().isPlugged()) {
- if (isChecked) {
- robot.pluginchargingDevice(chargingDevice);
- robot.getChargingDevice().setPLuggedIn(true);
- charging.setText("Charging");
- charging.setTextColor(getResources().getColor(R.color.colorGreen));
- robot.getBattery().setCharged(true);
- try {
- charging.setText("Charging");
- Thread.sleep(4000);
- robot.getBattery().chargeLevel = 100;
- battery_discharged.setText("Battery is charged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorGreen));
- if (!robot.getBattery().isIndicatorTurnedOn()) {
- imageView.setImageResource(R.drawable.green_circle);
- try {
- robot.getBattery().turnOnIndicator();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- } else {
- robot.getChargingDevice().setPLuggedIn(false);
- charging.setText("Not charging");
- charging.setTextColor(getResources().getColor(R.color.colorRed));
- }
- } else {
- compoundButton.setChecked(false);
- Toast.makeText(this, "Battery is plugged out", Toast.LENGTH_LONG).show();
- }
+ }
+ }
+
+
+ @Override
+ public void onClick(View view) {
+ switch (view.getId()) {
+ case R.id.btn_new_type_battery_injection:
+ this.presenter.plugInNewTypeBattery();
+ break;
+ case R.id.btn_old_type_battery_injection:
+ this.presenter.plugInOldTypeBattery();
break;
- case R.id.switch_battery_plugged:
- if (isChecked) {
- robot.plugInNewTypeBattery(newBattery);
- battery_plugged.setText("New type battery plugged in");
- battery_plugged.setTextColor(getResources().getColor(R.color.colorGreen));
- robot.getBattery().setPlugged(true);
- if (robot.getBattery().isCharged()) {
- battery_discharged.setText("New type battery charged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorGreen));
- robot.getBattery().setCharged(true);
- } else {
- battery_discharged.setText("New type battery discharged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorRed));
- robot.getBattery().setCharged(false);
- }
- } else {
- battery_plugged.setText("New type battery plugged out");
- battery_plugged.setTextColor(getResources().getColor(R.color.colorRed));
- robot.getBattery().setPlugged(false);
- }
+ case R.id.btn_plug_out_battery:
+ this.presenter.plugOutBattery();
break;
- case R.id.toggleButton:
- if (isChecked) {
- robot.plugInoldTypeBattery(oldBattery);
- battery_plugged.setText("Old type battery plugged in");
- battery_plugged.setTextColor(getResources().getColor(R.color.colorGreen));
- robot.getBattery().setPlugged(true);
- if (robot.getBattery().isCharged()) {
- battery_discharged.setText("Old type battery charged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorGreen));
- robot.getBattery().setCharged(true);
- } else {
- battery_discharged.setText("Old type battery discharged");
- battery_discharged.setTextColor(getResources().getColor(R.color.colorRed));
- robot.getBattery().setCharged(false);
- }
- } else {
- battery_plugged.setText("Old type battery plugged out");
- battery_plugged.setTextColor(getResources().getColor(R.color.colorRed));
- robot.getBattery().setPlugged(false);
- }
+ case R.id.btn_move:
+ this.presenter.move();
break;
}
}
+ @Override
+ public void switchOffPowerButton() {
+ this.btn_robot_power.setChecked(false);
+ }
+
+ @Override
+ public void onRobotTurnedOn() {
+ this.tv_robot_power.setText("Robot is turned on");
+ this.tv_robot_power.setTextColor(this.getColorGreen());
+ }
+
+ @Override
+ public void onRobotTurnedOff() {
+ this.tv_robot_power.setText("Robot is turned off");
+ this.tv_robot_power.setTextColor(this.getColorRed());
+ switchOffPowerButton();
+ }
+
+ @Override
+ public void turnOffBatteryIndicator() {
+ this.iv_battery_indicator.setImageResource(R.drawable.red_circle);
+ }
+
+ @Override
+ public void turnOnBatteryIndicator() {
+ this.iv_battery_indicator.setImageResource(R.drawable.green_circle);
+ }
+
+ @Override
+ public void onBatteryChargingStarted() {
+ this.tv_battery_charge_state.setText("Battery is charging...");
+ this.tv_battery_charge_state.setTextColor(this.getColorYellow());
+ }
+
+ @Override
+ public void onBatteryCharged() {
+ this.tv_battery_charge_state.setText("Battery charged");
+ this.tv_battery_charge_state.setTextColor(this.getColorGreen());
+ }
+
+ @Override
+ public void onBatteryDischarged(int distanceToTravel) {
+ this.tv_battery_charge_state.setText("Battery discharged");
+ this.tv_battery_charge_state.setTextColor(this.getColorRed());
+ Toast.makeText(this, "Battery discharged. " + distanceToTravel + "m left to ride.", Toast.LENGTH_LONG).show();
+ }
+
+ @Override
+ public void onChargingDevicePluggedIn() {
+ this.tv_charging_state.setText("Charging device is plugged in");
+ this.tv_charging_state.setTextColor(getColorGreen());
+ }
+
+ @Override
+ public void onChargingDevicePluggedOut() {
+ this.btn_plug_charging_device.setChecked(false);
+ this.tv_charging_state.setText("Charging device is not plugged in");
+ this.tv_charging_state.setTextColor(getColorRed());
+ }
+
+ @Override
+ public void onOldTypeBatteryPluggedIn() {
+ this.turnOffBatteryIndicator();
+ this.tv_battery_inject_state.setText("Old type battery plugged in");
+ this.tv_battery_inject_state.setTextColor(this.getColorGreen());
+ }
+
+ @Override
+ public void onNewTypeBatteryPluggedIn() {
+ this.tv_battery_inject_state.setText("New type battery plugged in");
+ this.tv_battery_inject_state.setTextColor(this.getColorGreen());
+ }
+
+ @Override
+ public void onBatteryPluggedOut() {
+ this.turnOffBatteryIndicator();
+ this.tv_battery_charge_state.setText("Battery discharged");
+ this.tv_battery_charge_state.setTextColor(getColorRed());
+ this.tv_battery_inject_state.setText("Battery plugged out");
+ this.tv_battery_inject_state.setTextColor(getColorRed());
+ }
+
+ @Override
+ public void onDestinationReached(int batteryChargeLevel) {
+ this.tv_robot_movement_state.setText("Destination is reached");
+ this.tv_robot_movement_state.setTextColor(getColorGreen());
+ Toast.makeText(this, "Destination is reached. " + batteryChargeLevel + "% left in the battery.", Toast.LENGTH_LONG).show();
+ }
+
+ private int getColorGreen() {
+ return getResources().getColor(R.color.colorGreen);
+ }
+
+ private int getColorRed() {
+ return getResources().getColor(R.color.colorRed);
+ }
+
+ private int getColorYellow() {
+ return getResources().getColor(R.color.colorYellow);
+ }
+
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/NewBattery.java b/app/src/main/java/com/kasilov/andrew/solidrobot/NewBattery.java
index d343eab..6766d33 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/NewBattery.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/NewBattery.java
@@ -1,37 +1,36 @@
package com.kasilov.andrew.solidrobot;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryIndicator;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryStatus;
+
public class NewBattery extends Battery {
- public boolean isIndicatorTurnedOn() {
- return indicatorTurnedOn;
- }
- private boolean indicatorTurnedOn;
+ private IBatteryIndicator iBatteryIndicator;
+ private int chargeLevel = 0;
- private boolean plugged= false;
- private boolean charged = false;
- public void setPlugged(boolean plugged) {
- this.plugged = plugged;
- }
- public void setCharged(boolean charged) {
- this.charged = charged;
+ public NewBattery(IBatteryIndicator iBatteryIndicator, IBatteryStatus iBatteryStatus) {
+ this.iBatteryIndicator = iBatteryIndicator;
+ this.iBatteryStatus = iBatteryStatus;
+ this.setCharged(false);
}
- public boolean isCharged() {
- return charged;
+ @Override
+ public void charge() {
+ this.iBatteryIndicator.turnOffIndicator();
+ new BatteryCharging().execute(10);
}
- public boolean isPlugged() {
- return plugged;
- }
- @Override
- void charge() {
+ @Override
+ public int getChargeLevel() {
+ return this.chargeLevel;
}
@Override
- protected void turnOnIndicator() {
- super.turnOnIndicator();
+ void notifyBatteriesChargingIsFinished(Integer chargeLevel) {
+ this.chargeLevel = chargeLevel;
+ this.iBatteryIndicator.turnOnIndicator();
}
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/OldBattery.java b/app/src/main/java/com/kasilov/andrew/solidrobot/OldBattery.java
index a005df3..7c4a310 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/OldBattery.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/OldBattery.java
@@ -1,45 +1,32 @@
package com.kasilov.andrew.solidrobot;
-public class OldBattery extends Battery {
-
-
- private boolean plugged = false;
- private boolean charged = false;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryStatus;
- public void setPlugged(boolean plugged) {
- this.plugged = plugged;
- }
+public class OldBattery extends Battery {
- public void setCharged(boolean charged) {
- this.charged = charged;
- }
- public boolean isCharged() {
- return charged;
- }
+ private int chargeLevel = 0;
- public boolean isPlugged() {
- return plugged;
- }
- public boolean isIndicatorTurnedOn() {
- return indicatorTurnedOn;
+ public OldBattery(IBatteryStatus iBatteryStatus) {
+ this.iBatteryStatus = iBatteryStatus;
+ this.setCharged(false);
}
- private boolean indicatorTurnedOn;
-
@Override
- void charge() {
-
+ public void charge() {
+ new BatteryCharging().execute(10);
}
@Override
- protected void turnOnIndicator() {
- throw new UnsupportedOperationException("It is not supported with this battery");
+ public int getChargeLevel() {
+ return this.chargeLevel;
}
+
@Override
- protected void turnOffIndicator() {
- throw new UnsupportedOperationException("It is not supported with this battery");
+ void notifyBatteriesChargingIsFinished(Integer chargeLevel) {
+ this.chargeLevel = chargeLevel;
}
+
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/Presenter.java b/app/src/main/java/com/kasilov/andrew/solidrobot/Presenter.java
new file mode 100644
index 0000000..5d7d090
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/Presenter.java
@@ -0,0 +1,127 @@
+package com.kasilov.andrew.solidrobot;
+
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryIndicator;
+import com.kasilov.andrew.solidrobot.interfaces.IBatteryStatus;
+import com.kasilov.andrew.solidrobot.interfaces.IErrorState;
+import com.kasilov.andrew.solidrobot.interfaces.IRobotMovement;
+import com.kasilov.andrew.solidrobot.interfaces.IViewControl;
+
+public class Presenter {
+
+ private final NewBattery newBattery;
+ private final OldBattery oldBattery;
+ private final IViewControl viewControl;
+ private final IErrorState iErrorState;
+ private final ChargingDevice chargingDevice;
+ private Robot robot;
+
+ private IBatteryIndicator iBatteryIndicator = new IBatteryIndicator() {
+ @Override
+ public void turnOnIndicator() {
+ Presenter.this.viewControl.turnOnBatteryIndicator();
+ }
+
+ @Override
+ public void turnOffIndicator() {
+ Presenter.this.viewControl.turnOffBatteryIndicator();
+
+ }
+ };
+
+ private IBatteryStatus iBatteryStatus = new IBatteryStatus() {
+ @Override
+ public void onBatteryCharged() {
+ Presenter.this.viewControl.onBatteryCharged();
+ }
+ };
+ private IRobotMovement iRobotMovement = new IRobotMovement() {
+ @Override
+ public void onDestinationReached(int batteryChargeLevel) {
+ Presenter.this.viewControl.onDestinationReached(batteryChargeLevel);
+ }
+
+ @Override
+ public void onBatteryDischarged(int distanceToTravel) {
+ Presenter.this.viewControl.onBatteryDischarged(distanceToTravel);
+ Presenter.this.viewControl.onRobotTurnedOff();
+ }
+ };
+
+ public Presenter(IViewControl viewControl, IErrorState iErrorState) {
+ this.viewControl = viewControl;
+ this.iErrorState = iErrorState;
+ this.newBattery = new NewBattery(iBatteryIndicator, iBatteryStatus);
+ this.oldBattery = new OldBattery(iBatteryStatus);
+ this.robot = new Robot(iRobotMovement);
+ this.chargingDevice = new ChargingDevice();
+ }
+
+ public void turnOnRobot() {
+ if (!this.robot.isTurnedOn()) {
+ try {
+ this.robot.turnOn();
+ this.viewControl.onRobotTurnedOn();
+ } catch (NullPointerException | IllegalStateException e) {
+ this.viewControl.switchOffPowerButton();
+ this.iErrorState.onError(e.getLocalizedMessage());
+ }
+ }
+ }
+
+ public void turnOffRobot() {
+ this.robot.turnOff();
+ this.viewControl.onRobotTurnedOff();
+ }
+
+ public void plugInNewTypeBattery() {
+ this.robot.plugInBattery(this.newBattery);
+ this.viewControl.onNewTypeBatteryPluggedIn();
+ }
+
+
+ public void plugInOldTypeBattery() {
+ this.robot.plugInBattery(this.oldBattery);
+ this.viewControl.onOldTypeBatteryPluggedIn();
+ }
+
+ public void plugOutBattery() {
+ this.robot.plugOutBattery();
+ this.robot.turnOff();
+ this.viewControl.onRobotTurnedOff();
+ this.viewControl.onBatteryPluggedOut();
+ }
+
+ public void plugInChargingDevice() {
+ this.robot.plugInChargingDevice(this.chargingDevice);
+ this.viewControl.onChargingDevicePluggedIn();
+ this.chargeBattery();
+ }
+
+
+ public void plugOutChargingDevice() {
+ this.robot.plugOutChargingDevice();
+ this.viewControl.onChargingDevicePluggedOut();
+ }
+
+ private void chargeBattery() {
+ if (this.robot.getBattery() != null) {
+ this.viewControl.onBatteryChargingStarted();
+ this.robot.getBattery().charge();
+ } else {
+ this.iErrorState.onError("Nothing to charge! Plug in the battery.");
+ this.viewControl.onChargingDevicePluggedOut();
+ }
+ }
+
+ public void move() {
+ if (!this.robot.isTurnedOn()) {
+ this.iErrorState.onError("Robot is turned off. Turn on the robot.");
+ return;
+ }
+ if (this.robot.isChargingDevicePluggedIn()) {
+ this.iErrorState.onError("Charging device is plugged in. Plug out");
+ return;
+ }
+ this.robot.move();
+ }
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/Robot.java b/app/src/main/java/com/kasilov/andrew/solidrobot/Robot.java
index 3fa0218..d10858c 100644
--- a/app/src/main/java/com/kasilov/andrew/solidrobot/Robot.java
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/Robot.java
@@ -1,31 +1,97 @@
package com.kasilov.andrew.solidrobot;
+import android.os.AsyncTask;
+
+import com.kasilov.andrew.solidrobot.interfaces.IRobotMovement;
+
public class Robot {
+ private final IRobotMovement iRobotMovement;
private Battery battery;
- public boolean turnedOn;
+ private boolean turnedOn;
private ChargingDevice chargingDevice;
+ private int distanceToTravel = 27;
+ private int batteryChargeLevel;
- public void plugInoldTypeBattery(Battery battery) {
- this.battery = battery;
+ public Robot(IRobotMovement iRobotMovement) {
+ this.iRobotMovement = iRobotMovement;
}
+ public void turnOn() {
+ if (this.battery == null) {
+ throw new NullPointerException("Battery is missed. Plug in the battery first.");
+ }
- public Battery getBattery() {
- return battery;
+ if (!this.battery.isCharged()) {
+ throw new IllegalStateException("Battery is not charged. Connect charging device to charge the battery.");
+ }
+
+ this.turnedOn = true;
+ }
+
+ public void turnOff() {
+ this.turnedOn = false;
+ }
+
+ public boolean isTurnedOn() {
+ return turnedOn;
}
+ public void plugInBattery(Battery battery) {
+ this.battery = battery;
+ }
+
+ public void plugOutBattery() {
+ this.battery = null;
+ }
- public void pluginchargingDevice(ChargingDevice chargingDevice) {
+ public void plugInChargingDevice(ChargingDevice chargingDevice) {
this.chargingDevice = chargingDevice;
}
+ public void plugOutChargingDevice() {
+ this.chargingDevice = null;
+ }
+
+ public Battery getBattery() {
+ return battery;
+ }
+
+ public void move() {
+ new RobotMovement().execute(this.distanceToTravel, this.battery.getChargeLevel());
+ }
+
+
+ private void onMovementFinished() {
+ if (batteryChargeLevel == 0) {
+ this.iRobotMovement.onBatteryDischarged(distanceToTravel);
+ } else if (distanceToTravel == 0) {
+ this.iRobotMovement.onDestinationReached(batteryChargeLevel);
+ }
+ }
- public ChargingDevice getChargingDevice() {
- return chargingDevice;
+ boolean isChargingDevicePluggedIn() {
+ return this.chargingDevice != null;
}
- public void plugInNewTypeBattery(NewBattery newBattery) {
- this.battery = newBattery;
+ private class RobotMovement extends AsyncTask {
+ @Override
+ protected Integer doInBackground(Integer... integers) {
+ int distanceToTravel = integers[0];
+ int chargeLevel = integers[1];
+ while (distanceToTravel > 0 && chargeLevel > 0) {
+ distanceToTravel--;
+ chargeLevel = chargeLevel - 5;
+ }
+ Robot.this.distanceToTravel = distanceToTravel;
+ Robot.this.batteryChargeLevel = chargeLevel;
+ return integers[0];
+ }
+
+ @Override
+ protected void onPostExecute(Integer integer) {
+ Robot.this.onMovementFinished();
+ }
}
+
}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryCharging.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryCharging.java
new file mode 100644
index 0000000..570c849
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryCharging.java
@@ -0,0 +1,6 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IBatteryCharging {
+
+ void charge();
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryIndicator.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryIndicator.java
new file mode 100644
index 0000000..079fdc4
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryIndicator.java
@@ -0,0 +1,7 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IBatteryIndicator {
+ void turnOnIndicator();
+
+ void turnOffIndicator();
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryStatus.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryStatus.java
new file mode 100644
index 0000000..b19809b
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IBatteryStatus.java
@@ -0,0 +1,5 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IBatteryStatus {
+ void onBatteryCharged();
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IErrorState.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IErrorState.java
new file mode 100644
index 0000000..350fc40
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IErrorState.java
@@ -0,0 +1,5 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IErrorState {
+ void onError(String message);
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IRobotMovement.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IRobotMovement.java
new file mode 100644
index 0000000..faec4bb
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IRobotMovement.java
@@ -0,0 +1,7 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IRobotMovement {
+ void onBatteryDischarged(int distanceToTravel);
+
+ void onDestinationReached(int batteryChargeLevel);
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IViewControl.java b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IViewControl.java
new file mode 100644
index 0000000..4bbe04e
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/interfaces/IViewControl.java
@@ -0,0 +1,31 @@
+package com.kasilov.andrew.solidrobot.interfaces;
+
+public interface IViewControl {
+ void switchOffPowerButton();
+
+ void onRobotTurnedOn();
+
+ void onRobotTurnedOff();
+
+ void turnOnBatteryIndicator();
+
+ void turnOffBatteryIndicator();
+
+ void onBatteryChargingStarted();
+
+ void onBatteryCharged();
+
+ void onChargingDevicePluggedOut();
+
+ void onNewTypeBatteryPluggedIn();
+
+ void onOldTypeBatteryPluggedIn();
+
+ void onBatteryPluggedOut();
+
+ void onBatteryDischarged(int distanceToTravel);
+
+ void onDestinationReached(int batteryChargeLevel);
+
+ void onChargingDevicePluggedIn();
+}
diff --git a/app/src/main/java/com/kasilov/andrew/solidrobot/utils/LogUtil.java b/app/src/main/java/com/kasilov/andrew/solidrobot/utils/LogUtil.java
new file mode 100644
index 0000000..698004c
--- /dev/null
+++ b/app/src/main/java/com/kasilov/andrew/solidrobot/utils/LogUtil.java
@@ -0,0 +1,26 @@
+package com.kasilov.andrew.solidrobot.utils;
+
+import android.util.Log;
+
+public class LogUtil {
+
+ public static void makeLog(Object logMessage) {
+ Log.d("Seed LOG", "=================================================================\n"
+ + logMessage + "\n"
+ + "=================================================================");
+ }
+
+ public static void makeLog(String tag, String logMessage) {
+ Log.d(tag, "=================================================================\n"
+ + logMessage + "\n"
+ + "=================================================================");
+ }
+
+ public static void makeLog(Object o, String logMessage) {
+ Log.d(o.getClass().getName(), "=================================================================\n"
+ + logMessage + "\n"
+ + "=================================================================");
+ }
+
+
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index ac46258..f75c0fb 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -29,7 +29,7 @@
android:layout_marginTop="8dp"
android:drawablePadding="8dp"
android:gravity="center"
- android:text="Battery plugged out"
+ android:text="@string/battery_plugged_out"
android:textColor="@color/colorRed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_indicator_robot_on" />
@@ -42,7 +42,7 @@
android:layout_marginTop="8dp"
android:drawablePadding="8dp"
android:gravity="center"
- android:text="Not charging"
+ android:text="@string/not_charging"
android:textColor="@color/colorRed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_indicator_battery_discharged" />
@@ -55,23 +55,12 @@
android:layout_marginTop="8dp"
android:drawablePadding="8dp"
android:gravity="center"
- android:text="Not moving"
+ android:text="@string/not_moving"
android:textColor="@color/colorRed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_indicator_charging" />
-
+ /
+ app:layout_constraintTop_toBottomOf="@+id/tv_indicator_moving" />
-
+ app:layout_constraintRight_toLeftOf="@+id/guideline"
+ app:layout_constraintTop_toBottomOf="@+id/btn_robot_power" />
+ app:layout_constraintTop_toBottomOf="@+id/btn_plug_out_battery" />
+ app:layout_constraintTop_toBottomOf="@+id/btn_charging_device_injection" />
-
+
+
+ app:layout_constraintTop_toBottomOf="@+id/btn_old_type_battery_injection" />
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index f367674..02c5289 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -5,4 +5,5 @@
#FF4081
#FF60FF40
#FF860722
+ #FFE9E228
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 1aa4781..fe1a859 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,3 +1,21 @@
SOLIDRobot
+ Battery plugged out
+ Not charging
+ Not moving
+ The destination is not reached
+ Battery discharged
+ Turn On robot
+ Turn off robot
+ Plug in new type battery
+ Plug out new type battery
+ Plug in charging device
+ Plug out charging device
+ plug in old type battery
+ plug out old type battery
+ Move
+ New type battery
+ Robot power
+ Charging device
+ Old type battery
diff --git a/build.gradle b/build.gradle
index a3330d4..53f4fad 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.2.0'
+ classpath 'com.android.tools.build:gradle:2.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files