Skip to content

Instantly share code, notes, and snippets.

@sidishere
Created December 24, 2019 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidishere/a9879571352b9338141ec5de32f3ad78 to your computer and use it in GitHub Desktop.
Save sidishere/a9879571352b9338141ec5de32f3ad78 to your computer and use it in GitHub Desktop.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
final DatabaseReference smartAC = myRef.child("smartAC").child("status");
final DatabaseReference smartBulb = myRef.child("smartBulb").child("status");
final DatabaseReference smartFan = myRef.child("smartFan").child("status");
Switch s1, s2, s3, s4;
TextView textView1, textView2, textView3, masterSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Switch) findViewById(R.id.button1);
s2 = (Switch) findViewById(R.id.button2);
s3 = (Switch) findViewById(R.id.button3);
s4 = (Switch) findViewById(R.id.button4);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
masterSwitch = (TextView) findViewById(R.id.master_switch);   //Text to indicate whether master switch on or off
smartAC.addValueEventListener(new ValueEventListener() {
boolean firstTime=true;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d("file", "Value is: " + value);
textView1.setText("smartAC " + value);
if(firstTime) {
if (value.equals( "ON")) {
s4.setChecked(true);                //The order in which s4 and s1 are checked to true is necessary to be maintained in this same way
s1.setChecked(true);
firstTime = false;
} else {
s1.setChecked(false);
firstTime = false;
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("file", "Failed to read value.", error.toException());
}
});
smartBulb.addValueEventListener(new ValueEventListener() {
boolean firstTime=true;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d("file", "Value is: " + value);
textView2.setText("smartBulb " + value);
if(firstTime) {
if (value.equals( "ON")) {
s4.setChecked(true);                //The order in which s4 and s2 are checked to true is necessary to be maintained in this same way
s2.setChecked(true);
firstTime = false;
} else {
s2.setChecked(false);
firstTime = false;
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("file", "Failed to read value.", error.toException());
}
});
smartFan.addValueEventListener(new ValueEventListener() {
boolean firstTime=true;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d("file", "Value is: " + value);
textView3.setText("smartFan " + value);
if(firstTime) {
if (value.equals( "ON")) {
s4.setChecked(true);                //The order in which s4 and s3 are checked to true is necessary to be maintained in this same way
s3.setChecked(true);
firstTime = false;
} else {
s3.setChecked(false);
firstTime = false;
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("file", "Failed to read value.", error.toException());
}
});
s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (s4.isChecked())
if (s1.isChecked())
smartAC.setValue(("ON"));
else
smartAC.setValue("OFF");
else
if(s1.isChecked()){
s1.setChecked(false);
Toast.makeText(getApplicationContext(),"Enable master switch",Toast.LENGTH_SHORT).show();
}
}
});
s2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (s4.isChecked())
if (s2.isChecked())
smartBulb.setValue(("ON"));
else
smartBulb.setValue("OFF");
else
if(s2.isChecked()){
s2.setChecked(false);
Toast.makeText(getApplicationContext(),"Enable master switch",Toast.LENGTH_SHORT).show();
}
}
});
s3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (s4.isChecked())
if (s3.isChecked())
smartFan.setValue(("ON"));
else
smartFan.setValue("OFF");
else
if(s3.isChecked()){
s3.setChecked(false);
Toast.makeText(getApplicationContext(),"Enable master switch",Toast.LENGTH_SHORT).show();
}
}
});
s4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!s4.isChecked()) {
s1.setChecked(false);
s2.setChecked(false);
s3.setChecked(false);
smartAC.setValue("OFF");
smartBulb.setValue("OFF");
smartFan.setValue("OFF");
masterSwitch.setText("Master Switch OFF");
}
else{
masterSwitch.setText("Master Switch ON");
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment