diff --git a/app/build.gradle b/app/build.gradle index d8227c4..6e7b75a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 21 - buildToolsVersion "21.0.0" + compileSdkVersion 23 + buildToolsVersion "23.0.2" defaultConfig { applicationId "com.score.homez" minSdkVersion 17 - targetSdkVersion 21 + targetSdkVersion 23 versionCode 1 versionName "1.0" } @@ -22,6 +22,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:21.0.3' + compile 'com.android.support:appcompat-v7:23+' compile project(':senzc') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c0d8dce..eb74dee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,10 +19,10 @@ android:windowSoftInputMode="stateHidden"> + - - + android:windowSoftInputMode="stateHidden" /> - - - - + + + + diff --git a/app/src/main/java/com/score/homez/db/SwitchesDB.java b/app/src/main/java/com/score/homez/db/SwitchesDB.java new file mode 100644 index 0000000..d5a4e04 --- /dev/null +++ b/app/src/main/java/com/score/homez/db/SwitchesDB.java @@ -0,0 +1,137 @@ +package com.score.homez.db; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import com.score.homez.utils.Switch; + +import java.lang.reflect.Array; +import java.util.ArrayList; + +/** + * Created by Anesu on 1/1/2016. + */ +public class SwitchesDB extends SQLiteOpenHelper { + private static final int DB_VERSION = 1; + private static final String DB_NAME = "Switches.db"; + private static final String TABLE_NAME = "Switches"; + + private static final String ID = "_id"; + private static final String NAME = "name"; + private static final String STATUS = "status"; + + private static final int ID_INDEX = 1; + private static final int NAME_INDEX = 2; + private static final int STATUS_INDEX = 3; + + public SwitchesDB(Context context) { + super(context, DB_NAME, null, DB_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + + //db.execSQL(CREATE_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + //db.execSQL(DROP_TABLE); + //onCreate(db); + } + + public void deleteHomeTable(String name) { + final String DROP_TABLE = "DROP TABLE IF EXISTS " + name; + SQLiteDatabase db = this.getReadableDatabase(); + db.execSQL(DROP_TABLE); + } + + public void addSwitch(String table_name, String name, int status) { + SQLiteDatabase db = this.getReadableDatabase(); + + ContentValues values = new ContentValues(); + values.put(NAME, name); + values.put(STATUS, status); + db.insert(table_name, null, values); + } + + public void createHomeTable(String name) { + final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + name + "(" + ID + " INTEGER PRIMARY KEY, " + + "TEXT," + NAME + " TEXT," + STATUS + " TEXT);"; + + SQLiteDatabase db = this.getReadableDatabase(); + db.execSQL(CREATE_TABLE); + } + + public ArrayList getAllHomes() { + ArrayList homes = new ArrayList<>(); + String GET_TABLES = "SELECT name FROM sqlite_master WHERE type='table'"; + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.rawQuery(GET_TABLES, null); + + if (cursor.moveToFirst()) { + while (!cursor.isAfterLast()) { + String name = cursor.getString(cursor.getColumnIndex("name")); + if (!name.equals("android_metadata")) { + homes.add(name); + } + cursor.moveToNext(); + } + } + return homes; + } + + public ArrayList getAllSwitches(String name) { + String SELECT_ALL = "SELECT * FROM " + name; + SQLiteDatabase db = this.getReadableDatabase(); + ArrayList switches = new ArrayList<>(); + Cursor cursor = db.rawQuery(SELECT_ALL, null); + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + String home_name = cursor.getString(NAME_INDEX); + int id = cursor.getInt(ID_INDEX); + int status = Integer.parseInt(cursor.getString(STATUS_INDEX)); + Switch aSwitch = new Switch(home_name, id, status); + switches.add(aSwitch); + } + while (cursor.moveToNext()); + } + } else { + return null; + } + + return switches; + } + + public void toggleSwitch(String table_name, String name, int newState) { + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues content = new ContentValues(); + content.put(STATUS, newState + ""); + db.update(table_name, content, NAME + "=?", new String[]{name}); + } + + public void clearDB(String name) { + SQLiteDatabase db = this.getReadableDatabase(); + db.delete(name, null, null); + } + + public int getCount(String name) { + try { + String SELECT_ALL = "SELECT * FROM " + name; + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.rawQuery(SELECT_ALL, null); + return cursor.getCount(); + }catch (Exception e) + { + this.createHomeTable(name); + return this.getCount(name); + } + } + +} + diff --git a/app/src/main/java/com/score/homez/ui/AddHome.java b/app/src/main/java/com/score/homez/ui/AddHome.java new file mode 100644 index 0000000..9804457 --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/AddHome.java @@ -0,0 +1,126 @@ +package com.score.homez.ui; + +import android.content.Intent; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import com.score.homez.R; +import com.score.homez.db.SwitchesDB; + +public class AddHome extends ActionBarActivity implements View.OnClickListener{ + + Button create_btn; + EditText name; + String table; + String table_name; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_add_home); + + getIntentData(); + initUi(); + } + + private void initUi(){ + create_btn = (Button) findViewById(R.id.create); + name = (EditText) findViewById(R.id.home_name); + create_btn.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + if(v.getId() == R.id.create){ + table = name.getText().toString().replace(" ", "_"); + SwitchesDB db = new SwitchesDB(this); + db.createHomeTable(table); + Toast.makeText(this, table.replace("_", " ") + " has been created", Toast.LENGTH_LONG).show(); + toHome(); + } + } + + private void toHome() + { + Intent intent = new Intent(AddHome.this, HomeActivity.class); + intent.putExtra("home", table_name); + startActivity(intent); + } + + public void viewHomes() { + Intent intent = new Intent(AddHome.this, Homes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewModes() { + Intent intent = new Intent(AddHome.this, Modes.class); + intent.putExtra("home",table_name); + finish();; + startActivity(intent); + } + + public void viewHelp() { + Intent intent = new Intent(AddHome.this, HelpPage.class); + finish(); + startActivity(intent); + } + private void getIntentData() { + Intent intent = getIntent(); + String table_name = intent.getStringExtra("home"); + + if (table_name != null) { + this.table_name = table_name; + } else { + this.table_name = "Main_Home"; + } + } + + @Override + public void onBackPressed(){ + Intent intent = new Intent(AddHome.this, HomeActivity.class); + intent.putExtra("home", table_name); + startActivity(intent); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } else if (id == android.R.id.home) { + onBackPressed(); + }else if(id == R.id.action_help){ + viewHelp(); + } + else if(id == R.id.action_homes){ + viewHomes(); + } + else if(id == R.id.action_modes){ + viewModes(); + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/com/score/homez/ui/HelpPage.java b/app/src/main/java/com/score/homez/ui/HelpPage.java new file mode 100644 index 0000000..2cc6146 --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/HelpPage.java @@ -0,0 +1,100 @@ +package com.score.homez.ui; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.Menu; +import android.view.MenuItem; + +import com.score.homez.R; + +public class HelpPage extends ActionBarActivity { + + String table_name; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_help_page); + + getIntentData(); + setupActionBar(); + } + + private void setupActionBar() { + getSupportActionBar().setTitle("Help"); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + + @Override + public void onBackPressed(){ + Intent intent = new Intent(HelpPage.this, HomeActivity.class); + intent.putExtra("home", table_name); + startActivity(intent); + } + + public void viewHomes() { + Intent intent = new Intent(HelpPage.this, Homes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewModes() { + Intent intent = new Intent(HelpPage.this, Modes.class); + intent.putExtra("home",table_name); + finish();; + startActivity(intent); + } + + public void viewHelp() { + Intent intent = new Intent(HelpPage.this, HelpPage.class); + finish(); + startActivity(intent); + } + private void getIntentData() { + Intent intent = getIntent(); + String table_name = intent.getStringExtra("home"); + + if (table_name != null) { + this.table_name = table_name; + } else { + this.table_name = "Main_Home"; + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } else if (id == android.R.id.home) { + onBackPressed(); + }else if(id == R.id.action_help){ + viewHelp(); + } + else if(id == R.id.action_homes){ + viewHomes(); + } + else if(id == R.id.action_modes){ + viewModes(); + }else if(id == android.R.id.home){ + onBackPressed(); + } + + return super.onOptionsItemSelected(item); + } + +} diff --git a/app/src/main/java/com/score/homez/ui/HomeActivity.java b/app/src/main/java/com/score/homez/ui/HomeActivity.java index dd0712b..a215ba4 100644 --- a/app/src/main/java/com/score/homez/ui/HomeActivity.java +++ b/app/src/main/java/com/score/homez/ui/HomeActivity.java @@ -1,10 +1,12 @@ package com.score.homez.ui; import android.app.Activity; +import android.app.AlertDialog; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; @@ -14,45 +16,66 @@ import android.os.CountDownTimer; import android.os.IBinder; import android.os.RemoteException; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.text.Spanned; import android.util.Log; import android.view.Gravity; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; +import android.widget.AdapterView; import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.ImageView; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; +import android.widget.Toolbar; import com.score.homez.R; import com.score.homez.db.DBSource; +import com.score.homez.db.SwitchesDB; import com.score.homez.utils.ActivityUtils; +import com.score.homez.utils.HomesListAdapter; import com.score.homez.utils.NetworkUtil; +import com.score.homez.utils.Switch; import com.score.senz.ISenzService; import com.score.senzc.enums.SenzTypeEnum; import com.score.senzc.pojos.Senz; import com.score.senzc.pojos.User; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; -public class HomeActivity extends Activity implements View.OnClickListener { +public class HomeActivity extends Activity implements View.OnClickListener { private static final String TAG = HomeActivity.class.getName(); //put message variables private String lastSwitch; - private String lastStatus; + private String lastStatus; + private String table_name; + + private ListView list; + SwitchesDB db; // use to track share timeout private SenzCountDownTimer senzCountDownTimer; private boolean isResponseReceivedPut; private boolean isResponseReceivedGet; + ListView homes_list; + ImageButton add; + // we use custom font here private Typeface typeface; @@ -94,7 +117,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - + getIntentData(); + db = new SwitchesDB(this); + initSimulationDB(); registerReceiver(senzMessageReceiver, new IntentFilter("com.score.senzc.DATA")); senzCountDownTimer = new SenzCountDownTimer(16000, 5000); isResponseReceivedPut = true; @@ -104,8 +129,8 @@ protected void onCreate(Bundle savedInstanceState) { setupActionBar(); bindSenzService(); dbSource = new DBSource(getApplicationContext()); - if(NetworkUtil.isAvailableNetwork(this)){ - if(dbSource.getSwitches().size()>0) { + if (NetworkUtil.isAvailableNetwork(this)) { + if (dbSource.getSwitches().size() > 0) { isResponseReceivedGet = false; senzCountDownTimer.start(); @@ -120,9 +145,8 @@ protected void onCreate(Bundle savedInstanceState) { toast.show(); } - } - else { - Toast.makeText(this,"No Network Connection Available",Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(this, "No Network Connection Available", Toast.LENGTH_LONG).show(); } } @@ -143,7 +167,7 @@ protected void onDestroy() { @Override public void onClick(View tb) { - if(NetworkUtil.isAvailableNetwork(this)) { + if (NetworkUtil.isAvailableNetwork(this)) { if (dbSource.getSwitches().size() > 0) { @@ -183,28 +207,21 @@ public void onClick(View tb) { toast.show(); } - } - else{ - Toast.makeText(this,"No Network Conection Available",Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(this, "No Network Conection Available", Toast.LENGTH_LONG).show(); } } - /** } + + + /** + * } * Initialize UI components */ private void initUi() { - typeface = Typeface.createFromAsset(getAssets(), "fonts/vegur_2.otf"); - - nightModeText = (TextView) findViewById(R.id.text_night_mode); - visitorModeText = (TextView) findViewById(R.id.text_visitor_mode); - - nightModeButton = (ToggleButton) findViewById(R.id.switch_night_mode); - visitorModeButton = (ToggleButton) findViewById(R.id.switch_visitor_mode); - nightModeButton.setOnClickListener(this); - visitorModeButton.setOnClickListener(this); - - nightModeText.setTypeface(typeface, Typeface.BOLD); - visitorModeText.setTypeface(typeface, Typeface.BOLD); + list = (ListView) findViewById(R.id.list_view); + SwitchAdapter adapter = new SwitchAdapter(this, R.layout.single_toggle, table_name); + list.setAdapter(adapter); } /** @@ -213,14 +230,12 @@ private void initUi() { private void setupActionBar() { // enable ActionBar app icon to behave as action to toggle nav drawer //getActionBar().setDisplayHomeAsUpEnabled(true); - getActionBar().setHomeButtonEnabled(true); - int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); TextView yourTextView = (TextView) findViewById(titleId); yourTextView.setTextColor(getResources().getColor(R.color.white)); yourTextView.setTypeface(typeface); - getActionBar().setTitle("Switch board"); + getActionBar().setTitle(table_name.replace("_"," ")); } /** @@ -245,7 +260,7 @@ public SenzCountDownTimer(long millisInFuture, long countDownInterval) { public void onTick(long millisUntilFinished) { // if response not received yet, resend share if (!isResponseReceivedPut) { - ActivityUtils.showProgressDialog(HomeActivity.this,"Please wait ..."); + ActivityUtils.showProgressDialog(HomeActivity.this, "Please wait ..."); put(); Log.d(TAG, "Put Response not received yet"); } @@ -264,15 +279,15 @@ public void onFinish() { if (!isResponseReceivedPut) {//ToDo generalize String senzclient - Homep String message = "Seems we couldn't reach the " + "" + "Homep" + "" + " at this moment"; displayInformationMessageDialog("#PUT Fail", message); - isResponseReceivedPut=true; - nightModeButton.setChecked(1==dbSource.getStatus("s1")); - visitorModeButton.setChecked(1==dbSource.getStatus("s2")); + isResponseReceivedPut = true; + nightModeButton.setChecked(1 == dbSource.getStatus("s1")); + visitorModeButton.setChecked(1 == dbSource.getStatus("s2")); } if (!isResponseReceivedGet) { String message = "Seems we couldn't reach the " + "" + "Homep" + "" + " at this moment"; displayInformationMessageDialog("#GET Fail", message); - isResponseReceivedGet=true; + isResponseReceivedGet = true; } } } @@ -346,46 +361,45 @@ private void handleMessage(Intent intent) { if (msg != null && msg.equalsIgnoreCase("PutDone")) { Log.d(TAG, "DATA #msg PutDone Recieved"); isResponseReceivedPut = true; - for(Map.Entry entry : senz.getAttributes().entrySet()) { + for (Map.Entry entry : senz.getAttributes().entrySet()) { String key = entry.getKey(); int value; - if(key.equals("s1")){ + if (key.equals("s1")) { value = Integer.parseInt(entry.getValue()); - dbSource.setStatus(key,value); - nightModeButton.setChecked(1==dbSource.getStatus(key)); - if(1==value) nightModeText.setText(night_on); + dbSource.setStatus(key, value); + nightModeButton.setChecked(1 == dbSource.getStatus(key)); + if (1 == value) nightModeText.setText(night_on); else nightModeText.setText(night_off); } - if(key.equals("s2")){ + if (key.equals("s2")) { value = Integer.parseInt(entry.getValue()); - dbSource.setStatus(key,value); - visitorModeButton.setChecked(1==value); - if(1==value) visitorModeText.setText(visitor_on); + dbSource.setStatus(key, value); + visitorModeButton.setChecked(1 == value); + if (1 == value) visitorModeText.setText(visitor_on); else visitorModeText.setText(visitor_off); } } - } - else if (msg != null && msg.equalsIgnoreCase("GetResponse")) { + } else if (msg != null && msg.equalsIgnoreCase("GetResponse")) { isResponseReceivedGet = true; Toast.makeText(this.getBaseContext(), "Status Received", Toast.LENGTH_SHORT).show(); - for(Map.Entry entry : senz.getAttributes().entrySet()) { + for (Map.Entry entry : senz.getAttributes().entrySet()) { String key = entry.getKey(); int value; - if(key.equals("s1")){ + if (key.equals("s1")) { //Log.d(TAG, "Get Response === key ; vale ==== "+key +" : "+entry.getValue()); value = Integer.parseInt(entry.getValue()); - dbSource.setStatus(key,value); - nightModeButton.setChecked(1==value); - if(1==value) nightModeText.setText(night_on); + dbSource.setStatus(key, value); + nightModeButton.setChecked(1 == value); + if (1 == value) nightModeText.setText(night_on); else nightModeText.setText(night_off); } - if(key.equals("s2")){ + if (key.equals("s2")) { //Log.d(TAG, "Get response === key ; vale ===== "+key +" : "+entry.getValue()); value = Integer.parseInt(entry.getValue()); dbSource.setStatus(key, value); visitorModeButton.setChecked(1 == value); - if(1==value) visitorModeText.setText(visitor_on); + if (1 == value) visitorModeText.setText(visitor_on); else visitorModeText.setText(visitor_off); } } @@ -406,7 +420,7 @@ private void put() { try { // create senz attributes HashMap senzAttributes = new HashMap<>(); - Log.d(TAG, "put ============ "+lastSwitch+" : "+lastStatus); + Log.d(TAG, "put ============ " + lastSwitch + " : " + lastStatus); senzAttributes.put(lastSwitch, lastStatus); senzAttributes.put("time", ((Long) (System.currentTimeMillis() / 1000)).toString()); @@ -427,12 +441,12 @@ private void put() { private void get() { try { - ArrayList data= dbSource.getSwitches(); + ArrayList data = dbSource.getSwitches(); // create senz attributes HashMap senzAttributes = new HashMap<>(); - for (String sw: data){ - senzAttributes.put(sw,""); + for (String sw : data) { + senzAttributes.put(sw, ""); } Log.d(TAG, "get ============ attributes : " + senzAttributes); //senzAttributes.put("all",""); @@ -450,4 +464,78 @@ private void get() { e.printStackTrace(); } } + + private void getIntentData() { + Intent intent = getIntent(); + String table_name = intent.getStringExtra("home"); + + if (table_name != null) { + this.table_name = table_name; + } else { + this.table_name = "Main_Home"; + } + + getActionBar().setTitle("Switch Board for " + table_name); + } + + public void initSimulationDB() { + db.deleteHomeTable("Switches"); + //check to see if database is already populated + if (db.getCount(table_name) == 0) { + String[] switches = new String[]{"Night Mode", "Visitor Mode", "Alarm", "Lights", "Heating", "Cooling", "Lock Doors", "Garage Door", "Security Cameras"}; + for (String sw : switches) { + db.addSwitch(table_name, sw, 0); + } + } + } + + public void viewHomes() { + Intent intent = new Intent(HomeActivity.this, Homes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewModes() { + Intent intent = new Intent(HomeActivity.this, Modes.class); + intent.putExtra("home",table_name); + finish();; + startActivity(intent); + } + + public void viewHelp() { + Intent intent = new Intent(HomeActivity.this, HelpPage.class); + intent.putExtra("home",table_name); + finish(); + startActivity(intent); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } else if (id == R.id.action_modes) { + viewModes(); + } else if (id == R.id.action_help) { + viewHelp(); + } else if (id == R.id.action_homes) { + viewHomes(); + } + + + return super.onOptionsItemSelected(item); + } } diff --git a/app/src/main/java/com/score/homez/ui/Homes.java b/app/src/main/java/com/score/homez/ui/Homes.java new file mode 100644 index 0000000..fec025f --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/Homes.java @@ -0,0 +1,122 @@ +package com.score.homez.ui; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ImageButton; +import android.widget.ListView; + +import com.score.homez.R; +import com.score.homez.db.SwitchesDB; +import com.score.homez.utils.HomesListAdapter; + +public class Homes extends ActionBarActivity implements View.OnClickListener{ + + ListView homes_list; + ImageButton add; + SwitchesDB db; + String table_name; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_homes); + + setupActionBar(); + initUi(); + } + + private void initUi(){ + db = new SwitchesDB(this); + homes_list = (ListView) findViewById(R.id.pick_home); + add = (ImageButton) findViewById(R.id.add); + add.setOnClickListener(this); + HomesListAdapter adapter = new HomesListAdapter(this, R.layout.home_row, db.getAllHomes()); + homes_list.setAdapter(adapter); + homes_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Intent intent = new Intent(Homes.this, HomeActivity.class); + finish(); + intent.putExtra("home", db.getAllHomes().get(position)); + startActivity(intent); + } + }); + + } + + private void setupActionBar() { + getSupportActionBar().setTitle("All Homes"); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + + @Override + public void onClick(View v) { + Intent intent = new Intent(Homes.this, AddHome.class); + startActivity(intent); + } + + public void viewHomes() { + Intent intent = new Intent(Homes.this, Homes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewModes() { + Intent intent = new Intent(Homes.this, Modes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewHelp() { + Intent intent = new Intent(Homes.this, HelpPage.class); + finish(); + startActivity(intent); + } + + @Override + public void onBackPressed(){ + Intent intent = new Intent(Homes.this, HomeActivity.class); + intent.putExtra("home", table_name); + startActivity(intent); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } else if (id == android.R.id.home) { + onBackPressed(); + }else if(id == R.id.action_help){ + viewHelp(); + } + else if(id == R.id.action_homes){ + viewHomes(); + } + else if(id == R.id.action_modes){ + viewModes(); + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/com/score/homez/ui/Modes.java b/app/src/main/java/com/score/homez/ui/Modes.java new file mode 100644 index 0000000..21c2646 --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/Modes.java @@ -0,0 +1,146 @@ +package com.score.homez.ui; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.text.Html; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.CompoundButton; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.ToggleButton; + +import com.score.homez.R; +import com.score.homez.db.SwitchesDB; +import com.score.homez.utils.Switch; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Modes extends ActionBarActivity implements ToggleButton.OnCheckedChangeListener { + + ToggleButton visitor, night; + TextView night_txt, visitor_txt; + String table_name; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_modes); + + getIntentData(); + setupActionBar(); + setupModes(); + } + + private void setupActionBar() { + getSupportActionBar().setHomeButtonEnabled(true); + getSupportActionBar().setTitle(table_name.replace("_", " ") + " Modes"); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + + private void getIntentData() { + Intent intent = getIntent(); + String table_name = intent.getStringExtra("home"); + + if (table_name != null) { + this.table_name = table_name; + } else { + this.table_name = "Main_Home"; + } + } + + + private void setupModes() { + visitor = (ToggleButton) findViewById(R.id.switch_visitor_mode); + night = (ToggleButton) findViewById(R.id.switch_night_mode); + + visitor_txt = (TextView) findViewById(R.id.text_visitor_mode); + night_txt = (TextView) findViewById(R.id.text_night_mode); + visitor.setOnCheckedChangeListener(this); + night.setOnCheckedChangeListener(this); + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (buttonView.getId() == R.id.switch_visitor_mode) { + if (isChecked == false) { + String indicator = " [OFF]"; + visitor_txt.setText(Html.fromHtml("Visitor Mode" + indicator)); + } else { + String indicator = " [ON]"; + visitor_txt.setText(Html.fromHtml("Visitor Mode" + indicator)); + } + } else if (buttonView.getId() == R.id.switch_night_mode) { + if (isChecked == false) { + String indicator = " [OFF]"; + night_txt.setText(Html.fromHtml("Night Mode" + indicator)); + } else { + String indicator = " [ON]"; + night_txt.setText(Html.fromHtml("Night Mode" + indicator)); + } + } + } + public void viewHomes() { + Intent intent = new Intent(Modes.this, Homes.class); + intent.putExtra("home", table_name); + finish(); + startActivity(intent); + } + + public void viewModes() { + Intent intent = new Intent(Modes.this, Modes.class); + intent.putExtra("home",table_name); + finish();; + startActivity(intent); + } + + public void viewHelp() { + Intent intent = new Intent(Modes.this, HelpPage.class); + finish(); + startActivity(intent); + } + + @Override + public void onBackPressed(){ + Intent intent = new Intent(Modes.this, HomeActivity.class); + intent.putExtra("home", table_name); + startActivity(intent); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } else if (id == android.R.id.home) { + onBackPressed(); + }else if(id == R.id.action_help){ + viewHelp(); + } + else if(id == R.id.action_homes){ + viewHomes(); + } + else if(id == R.id.action_modes){ + viewModes(); + } + + + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/com/score/homez/ui/SwitchAdapter.java b/app/src/main/java/com/score/homez/ui/SwitchAdapter.java new file mode 100644 index 0000000..869de78 --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/SwitchAdapter.java @@ -0,0 +1,105 @@ +package com.score.homez.ui; + +import android.animation.ObjectAnimator; +import android.content.Context; +import android.graphics.Color; +import android.graphics.Typeface; +import android.text.Html; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.CompoundButton; +import android.widget.TextView; +import android.widget.ToggleButton; + +import com.score.homez.R; +import com.score.homez.db.SwitchesDB; +import com.score.homez.utils.Switch; + +import org.w3c.dom.Text; + +import java.util.ArrayList; + +/** + * Created by Anesu on 1/1/2016. + */ +public class SwitchAdapter extends ArrayAdapter { + Context context; + int resource; + ArrayList switches; + SwitchesDB db; + Typeface typeface; + String table; + int prev = -1; + public SwitchAdapter(Context context, int resource,String table) { + super(context, resource); + this.context = context; + this.table = table; + this.resource = resource; + db = new SwitchesDB(context); + switches = new ArrayList<>(); + for(Switch aSwitch : db.getAllSwitches(table)) + { + if(!aSwitch.getSwitchName().equals("Night Mode") && !aSwitch.getSwitchName().equals("Visitor Mode")){ + switches.add(aSwitch); + } + } + typeface = Typeface.createFromAsset(context.getAssets(), "fonts/vegur_2.otf"); + } + + @Override + public int getCount() { + return switches.size(); + } + + @Override + public View getView(final int position, View convertView, ViewGroup parent) { + if(convertView == null) { + convertView = LayoutInflater.from(context).inflate(resource, parent, false); + } + + final String name = switches.get(position).getSwitchName(); + int status = switches.get(position).getStatus(); + final TextView title = (TextView) convertView.findViewById(R.id.name); + title.setTypeface(typeface, Typeface.BOLD); + final ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle); + toggle.setChecked(status==1); + + setTitle(toggle.isChecked(), title, name); + + toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + toggle.setChecked(isChecked); + setTitle(toggle.isChecked(), title, name); + db.toggleSwitch(table, name, isChecked==true ? 1 : 0); + Log.i("state_changed", name + " has been toggled"); + } + }); + + ObjectAnimator anim = ObjectAnimator.ofFloat(convertView, "translationY", prev > position ? -250 : 250, 0); + + anim.setDuration(400); + anim.start(); + + prev = position; + + return convertView; + } + private void setTitle(boolean isChecked, TextView title,String name) + { + if(isChecked == false) + { + String indicator = " [OFF]"; + title.setText(Html.fromHtml(name + indicator)); + } + else + { + String indicator = " [ON]"; + title.setText(Html.fromHtml(name + indicator)); + } + } + +} diff --git a/app/src/main/java/com/score/homez/utils/HomesListAdapter.java b/app/src/main/java/com/score/homez/utils/HomesListAdapter.java new file mode 100644 index 0000000..e621001 --- /dev/null +++ b/app/src/main/java/com/score/homez/utils/HomesListAdapter.java @@ -0,0 +1,51 @@ +package com.score.homez.utils; + +import android.content.Context; +import android.content.Intent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageButton; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.score.homez.R; +import com.score.homez.ui.HomeActivity; + +import java.util.ArrayList; + +/** + * Created by Anesu on 1/11/2016. + */ +public class HomesListAdapter extends ArrayAdapter { + Context context; + ArrayList home_names; + + public HomesListAdapter(Context context, int resource, ArrayList home_names) { + super(context, resource); + this.context = context; + this.home_names = home_names; + } + + @Override + public int getCount() { + return home_names.size(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if(convertView == null) + { + LayoutInflater inflater = LayoutInflater.from(context); + convertView = inflater.inflate(R.layout.home_row, parent, false); + } + + TextView name = (TextView) convertView.findViewById(R.id.home_name); + ImageButton delete = (ImageButton) convertView.findViewById(R.id.delete); + + name.setText(home_names.get(position).replace("_", " ")); + + return convertView; + } +} diff --git a/app/src/main/java/com/score/homez/utils/Switch.java b/app/src/main/java/com/score/homez/utils/Switch.java new file mode 100644 index 0000000..af4f6c5 --- /dev/null +++ b/app/src/main/java/com/score/homez/utils/Switch.java @@ -0,0 +1,44 @@ +package com.score.homez.utils; + +/** + * Created by Anesu on 1/1/2016. + */ +public class Switch +{ + private String switchName; + private int switchId; + //status of 0 indicates off. status of 1 indicates on + private int status; + + public Switch(String switchName, int switchId, int status) + { + this.switchId = switchId; + this.switchName = switchName; + this.status = status; + } + + public void setSwitchName(String name) + { + this.switchName = name; + } + public void setSwitchId(int id) + { + this.switchId = id; + } + public void setStatus(int status) + { + this.status = status; + } + public String getSwitchName() + { + return switchName; + } + public int getSwitchId() + { + return switchId; + } + public int getStatus() + { + return status; + } +} diff --git a/app/src/main/res/layout/activity_add_home.xml b/app/src/main/res/layout/activity_add_home.xml new file mode 100644 index 0000000..fae276b --- /dev/null +++ b/app/src/main/res/layout/activity_add_home.xml @@ -0,0 +1,26 @@ + + + + + +