Skip to content

Commit

Permalink
Whole activity addCustomTask is working good now :)
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed Jun 21, 2019
1 parent 80a1a11 commit 9623ad7
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package dev.kamilklecha.autoswitchmobile;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.DateFormatSymbols;
import java.util.Calendar;

public class AddCustomTask extends AppCompatActivity {

int hourT, hourDT, minuteT, minuteDT;
int yearDT, monthDT, dayDT;

ConstraintLayout LayoutTime;
ConstraintLayout LayoutDateTime;
ConstraintLayout LayoutCustom;
Expand All @@ -23,9 +32,25 @@ public class AddCustomTask extends AppCompatActivity {
RadioButton MainRadioTime;
RadioButton MainRadioDateTime;

Spinner ActionSpinner;

RadioButton CertainRGSeconds;
RadioButton CertainRGMinutes;
RadioButton CertainRGHours;
RadioButton CertainRGCustom;

EditText CertainCustomAmount;
Spinner SecondsSpinner;
Spinner MinutesSpinner;
Spinner HoursSpinner;
Spinner CustomUnitspinner;

TextView LTView;
TextView LDTView;

Button btnLTUpdate;
Button btnLDTUpdate;
Button btnAddTask;
Button btnCancel;

@Override
Expand All @@ -41,15 +66,40 @@ protected void onCreate(Bundle savedInstanceState) {
MainRadioTime = (RadioButton) findViewById(R.id.addCustom_MainRadio_AtCertainTime);
MainRadioDateTime = (RadioButton) findViewById(R.id.addCustom_MainRadio_DateTime);

ActionSpinner = (Spinner) findViewById(R.id.addCustom_ActionSpinner);

CertainRGSeconds = (RadioButton) findViewById(R.id.addCustom_LCT_RG_AfterSeconds);
CertainRGMinutes = (RadioButton) findViewById(R.id.addCustom_LCT_RG_AfterMinutes);
CertainRGHours = (RadioButton) findViewById(R.id.addCustom_LCT_RG_AfterHours);
CertainRGCustom = (RadioButton) findViewById(R.id.addCustom_LCT_RG_Custom);

CertainCustomAmount = (EditText) findViewById(R.id.addCustom_LCT_CustomEditText);
SecondsSpinner = (Spinner) findViewById(R.id.addCustom_LCT_SecondsSpinner);
MinutesSpinner = (Spinner) findViewById(R.id.addCustom_LCT_MinutesSpinner);
HoursSpinner = (Spinner) findViewById(R.id.addCustom_LCT_HoursSpinner);
CustomUnitspinner = (Spinner) findViewById(R.id.addCustom_LCT_CustomSpinner);

LTView = (TextView) findViewById(R.id.addCustom_LT_SettedTime);
LDTView = (TextView) findViewById(R.id.addCustom_LDT_SettedDateTime);

btnLTUpdate = (Button) findViewById(R.id.addCustom_LT_Update);
btnLDTUpdate = (Button) findViewById(R.id.addCustom_LDT_Update);
btnAddTask = (Button) findViewById(R.id.addCustom_AddTask);
btnCancel = (Button) findViewById(R.id.addCustom_Cancel);

disableEnableControls(false, LayoutTime);
disableEnableControls(false, LayoutDateTime);
disableEnableControls(false, LayoutCustom);

Calendar c = Calendar.getInstance();
minuteT = minuteDT = c.get(Calendar.MINUTE);
hourT = hourDT = c.get(Calendar.HOUR_OF_DAY);
dayDT = c.get(Calendar.DAY_OF_MONTH);
monthDT = c.get(Calendar.MONTH);
yearDT = c.get(Calendar.YEAR);
LTView.setText(hourT + ":" + minuteT);
LDTView.setText(dayDT + " " + new DateFormatSymbols().getMonths()[monthDT] + " " + yearDT + " " + hourDT + ":" + minuteDT);

MainRadioCertainTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -82,19 +132,124 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddCustomTask.this, new TimePickerDialog.OnTimeSetListener() {
Calendar currentTime = Calendar.getInstance();
hourT = currentTime.get(Calendar.HOUR_OF_DAY);
minuteT = currentTime.get(Calendar.MINUTE);

TimePickerDialog timePicker;
timePicker = new TimePickerDialog(AddCustomTask.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
LTView.setText( selectedHour + ":" + selectedMinute);
hourT = selectedHour;
minuteT = selectedMinute;
LTView.setText(selectedHour + ":" + selectedMinute);
}
}, hourT, minuteT, true);//Yes 24 hour time
timePicker.setTitle("Select Time");
timePicker.show();
}
});

btnLDTUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar currentTime = Calendar.getInstance();
yearDT = currentTime.get(Calendar.YEAR);
monthDT = currentTime.get(Calendar.MONTH);
dayDT = currentTime.get(Calendar.DAY_OF_MONTH);
hourDT = currentTime.get(Calendar.HOUR_OF_DAY);
minuteDT = currentTime.get(Calendar.MINUTE);

DatePickerDialog datePicker;
datePicker = new DatePickerDialog(AddCustomTask.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
yearDT = year;
monthDT = month;
dayDT = dayOfMonth;
LDTView.setText(dayOfMonth + " " + new DateFormatSymbols().getMonths()[month] + " " + year);

TimePickerDialog timePicker;
timePicker = new TimePickerDialog(AddCustomTask.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
hourDT = selectedHour;
minuteDT = selectedMinute;
LDTView.setText(LDTView.getText().toString() + " " + selectedHour + ":" + selectedMinute);
}
}, hourDT, minuteDT, true);//Yes 24 hour time
timePicker.setTitle("Select Time");
timePicker.show();
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}, yearDT, monthDT, dayDT);
datePicker.setTitle("Select Date");
datePicker.show();
}
});

btnAddTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Packet p = new Packet();
p.Exec = "AddTask";
p.TimeMode = "Postponed";
switch (ActionSpinner.getSelectedItemPosition()) {
case 0: p.Action = "Shutdown"; break;
case 1: p.Action = "Reboot"; break;
case 2: p.Action = "Sleep"; break;
case 3: p.Action = "Hibernate"; break;
case 4: p.Action = "Logout"; break;
case 5: p.Action = "Block"; break;
case 6: p.Action = "MonitorOff"; break;
}
if(MainRadioCertainTime.isChecked()) {
boolean err = false;
if(CertainRGSeconds.isChecked()) p.seconds = Integer.parseInt(SecondsSpinner.getSelectedItem().toString());
else if(CertainRGMinutes.isChecked()) p.seconds = Integer.parseInt(MinutesSpinner.getSelectedItem().toString()) * 60;
else if(CertainRGHours.isChecked()) p.seconds = Integer.parseInt(HoursSpinner.getSelectedItem().toString()) * 60 * 60;
else if(CertainRGCustom.isChecked()) {
int amount = 0;
try {
amount = Integer.parseInt(CertainCustomAmount.getText().toString());
}
catch (Exception e) {
Toast.makeText(AddCustomTask.this, "Custom time value cannot be empty!", Toast.LENGTH_LONG);
}
int index = CustomUnitspinner.getSelectedItemPosition();
switch(index) {
case 0: p.seconds = amount; break;
case 1: p.seconds = amount * 60; break;
case 2: p.seconds = amount * 60 * 60; break;
case 3: p.seconds = amount * 60 * 60 * 24; break;
case 4: p.seconds = amount * 60 * 60 * 24 * 31; break;
case 5: p.seconds = amount * 60 * 60 * 24 * 365; break;
}
}
else {
Toast.makeText(AddCustomTask.this, "You didn't chose any certain option!", Toast.LENGTH_LONG).show();
err = true;
}

if(p.seconds != null && p.seconds != 0) {
Hub.cui.sendMsg(p);
Toast.makeText(AddCustomTask.this, "Task added", Toast.LENGTH_LONG).show();
finish();
}
else if(!err) Toast.makeText(AddCustomTask.this, "Selected value must NOT be 0!", Toast.LENGTH_LONG).show();
}
else if(MainRadioTime.isChecked()) {
p.dt.set(1900,1,1, hourT, minuteT);
Hub.cui.sendMsg(p);
Toast.makeText(AddCustomTask.this, "Task added", Toast.LENGTH_LONG).show();
finish();
}
else if(MainRadioDateTime.isChecked()) {
p.dt.set(yearDT, monthDT, dayDT, hourDT, minuteDT);
Hub.cui.sendMsg(p);
Toast.makeText(AddCustomTask.this, "Task added", Toast.LENGTH_LONG).show();
finish();
}
else Toast.makeText(AddCustomTask.this, "You didn't chose when the task should be executed!", Toast.LENGTH_LONG).show();
}
});

Expand All @@ -106,12 +261,12 @@ public void onClick(View v) {
});
}

private void disableEnableControls(boolean enable, ViewGroup vg){
for (int i = 0; i < vg.getChildCount(); i++){
private void disableEnableControls(boolean enable, ViewGroup vg) {
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
child.setEnabled(enable);
if (child instanceof ViewGroup){
disableEnableControls(enable, (ViewGroup)child);
if (child instanceof ViewGroup) {
disableEnableControls(enable, (ViewGroup) child);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addr = getIntent().getExtras().getString("Addr");

Hub.cui = this;

setContentView(R.layout.activity_communicator_ui);

TextView tv = (TextView) findViewById(R.id.commUI_Addr);
Expand Down Expand Up @@ -165,7 +167,7 @@ private void OpenSettings() {

}

private void sendMsg(Packet p) {
public void sendMsg(Packet p) {
Gson gson = new Gson();
String json = gson.toJson(p);
Hub.sc.sendMessage(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public class Hub {
public static SocketCommunicator sc;
public static CommunicatorUI cui;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package dev.kamilklecha.autoswitchmobile;

import java.io.Serializable;
import java.sql.Time;
import java.util.Date;
import java.util.Calendar;


public class Packet implements Serializable {
public
String Exec;
String Action;
String TimeMode;
Integer seconds;
Time time;
Date date;
Calendar dt = Calendar.getInstance();
Boolean force;
}
9 changes: 5 additions & 4 deletions Android/app/src/main/res/layout/activity_add_custom_task.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:text="Action"
android:text="Action:"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
Expand All @@ -53,7 +53,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginEnd="16dp"
android:entries="@array/DropdownActionsList"
android:spinnerMode="dialog"
app:layout_constraintEnd_toEndOf="parent"
Expand Down Expand Up @@ -257,6 +257,7 @@
android:layout_height="0dp"
android:ems="10"
android:inputType="numberDecimal"
android:maxLength="5"
android:textAlignment="textEnd"
android:textSize="11sp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down Expand Up @@ -346,7 +347,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="At certain date ant time:"
android:text="At certain date and time:"
android:textAlignment="center"
app:layout_constraintEnd_toStartOf="@+id/addCustom_LDT_SettedDateTime"
app:layout_constraintHorizontal_bias="1.0"
Expand Down

0 comments on commit 9623ad7

Please sign in to comment.