From 666932739682630b8b88b1748b3e052e77de0ffe Mon Sep 17 00:00:00 2001 From: Anesu Date: Fri, 1 Jan 2016 17:22:18 -0600 Subject: [PATCH 1/3] [gci2015] added switches from database --- app/build.gradle | 4 +- .../java/com/score/homez/db/SwitchesDB.java | 101 ++++++++++++++++++ .../java/com/score/homez/ui/HomeActivity.java | 21 ++-- .../com/score/homez/ui/SwitchAdapter.java | 97 +++++++++++++++++ .../java/com/score/homez/utils/Switch.java | 44 ++++++++ app/src/main/res/layout/activity_main.xml | 96 +---------------- app/src/main/res/layout/single_toggle.xml | 28 +++++ senzc/build.gradle | 4 +- 8 files changed, 287 insertions(+), 108 deletions(-) create mode 100644 app/src/main/java/com/score/homez/db/SwitchesDB.java create mode 100644 app/src/main/java/com/score/homez/ui/SwitchAdapter.java create mode 100644 app/src/main/java/com/score/homez/utils/Switch.java create mode 100644 app/src/main/res/layout/single_toggle.xml diff --git a/app/build.gradle b/app/build.gradle index d8227c4..f17840b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 21 - buildToolsVersion "21.0.0" + buildToolsVersion "21.1.2" defaultConfig { applicationId "com.score.homez" @@ -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:21+' compile project(':senzc') } 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..f5c0cab --- /dev/null +++ b/app/src/main/java/com/score/homez/db/SwitchesDB.java @@ -0,0 +1,101 @@ +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.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; + + Cursor cursor; + + private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY, " + + "TEXT," + NAME + " TEXT," + STATUS + " TEXT);"; + + private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; + + 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 addSwitch(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 ArrayList getAllSwitches() + { + String SELECT_ALL = "SELECT * FROM " + TABLE_NAME; + SQLiteDatabase db = this.getReadableDatabase(); + ArrayList switches = new ArrayList<>(); + Cursor cursor = db.rawQuery(SELECT_ALL, null); + this.cursor = cursor; + if(cursor != null) + { + if(cursor.moveToFirst()) + { + do + { + String name = cursor.getString(NAME_INDEX); + int id = cursor.getInt(ID_INDEX); + int status = Integer.parseInt(cursor.getString(STATUS_INDEX)); + Switch aSwitch = new Switch(name, id, status); + switches.add(aSwitch); + } + while(cursor.moveToNext()); + } + } + else + { + return null; + } + + return switches; + } + + public void toggleSwitch(int id, 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}); + } + +} + 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..532728f 100644 --- a/app/src/main/java/com/score/homez/ui/HomeActivity.java +++ b/app/src/main/java/com/score/homez/ui/HomeActivity.java @@ -22,12 +22,14 @@ import android.view.ViewGroup; import android.view.Window; import android.widget.Button; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; 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.NetworkUtil; import com.score.senz.ISenzService; @@ -48,6 +50,9 @@ public class HomeActivity extends Activity implements View.OnClickListener { private String lastSwitch; private String lastStatus; + private ListView list; + SwitchesDB db; + // use to track share timeout private SenzCountDownTimer senzCountDownTimer; private boolean isResponseReceivedPut; @@ -94,6 +99,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + db = new SwitchesDB(this); registerReceiver(senzMessageReceiver, new IntentFilter("com.score.senzc.DATA")); senzCountDownTimer = new SenzCountDownTimer(16000, 5000); @@ -192,19 +198,10 @@ public void onClick(View tb) { * 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, db.getAllSwitches()); + list.setAdapter(adapter); } /** 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..f0a1227 --- /dev/null +++ b/app/src/main/java/com/score/homez/ui/SwitchAdapter.java @@ -0,0 +1,97 @@ +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; + int prev = -1; + public SwitchAdapter(Context context, int resource, ArrayList switches) { + super(context, resource); + this.context = context; + this.resource = resource; + db = new SwitchesDB(context); + this.switches = db.getAllSwitches(); + 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(position, 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/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_main.xml b/app/src/main/res/layout/activity_main.xml index e033ab7..5d731e4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,97 +5,9 @@ android:background="#e4e4e4" android:orientation="vertical" > - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/layout/single_toggle.xml b/app/src/main/res/layout/single_toggle.xml new file mode 100644 index 0000000..6498022 --- /dev/null +++ b/app/src/main/res/layout/single_toggle.xml @@ -0,0 +1,28 @@ + + + + + + + + \ No newline at end of file diff --git a/senzc/build.gradle b/senzc/build.gradle index f749f28..a93a5ca 100644 --- a/senzc/build.gradle +++ b/senzc/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' android { compileSdkVersion 21 - buildToolsVersion "21.0.0" + buildToolsVersion "21.1.2" defaultConfig { minSdkVersion 14 @@ -21,5 +21,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:21.0.3' + compile 'com.android.support:appcompat-v7:21+' } From 7fe39da52657f4726ff05da97f1b59f56cc17cde Mon Sep 17 00:00:00 2001 From: Anesu Date: Mon, 4 Jan 2016 16:19:28 -0600 Subject: [PATCH 2/3] gci2015 updated modes --- .../java/com/score/homez/db/SwitchesDB.java | 15 ++++ .../java/com/score/homez/ui/HomeActivity.java | 90 ++++++++++--------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/score/homez/db/SwitchesDB.java b/app/src/main/java/com/score/homez/db/SwitchesDB.java index f5c0cab..b6967cd 100644 --- a/app/src/main/java/com/score/homez/db/SwitchesDB.java +++ b/app/src/main/java/com/score/homez/db/SwitchesDB.java @@ -97,5 +97,20 @@ public void toggleSwitch(int id, String name, int newState) db.update(TABLE_NAME, content, NAME + "=?", new String[]{name}); } + public void clearDB() + { + SQLiteDatabase db = this.getReadableDatabase(); + db.delete(TABLE_NAME, null, null); + } + + public int getCount() + { + String SELECT_ALL = "SELECT * FROM " + TABLE_NAME; + SQLiteDatabase db = this.getReadableDatabase(); + ArrayList switches = new ArrayList<>(); + Cursor cursor = db.rawQuery(SELECT_ALL, null); + return cursor.getCount(); + } + } 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 532728f..0ea4fda 100644 --- a/app/src/main/java/com/score/homez/ui/HomeActivity.java +++ b/app/src/main/java/com/score/homez/ui/HomeActivity.java @@ -32,6 +32,7 @@ import com.score.homez.db.SwitchesDB; import com.score.homez.utils.ActivityUtils; 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; @@ -42,13 +43,13 @@ 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 ListView list; SwitchesDB db; @@ -100,7 +101,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); db = new SwitchesDB(this); - + initSimulationDB(); registerReceiver(senzMessageReceiver, new IntentFilter("com.score.senzc.DATA")); senzCountDownTimer = new SenzCountDownTimer(16000, 5000); isResponseReceivedPut = true; @@ -110,8 +111,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(); @@ -126,9 +127,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(); } } @@ -149,7 +149,7 @@ protected void onDestroy() { @Override public void onClick(View tb) { - if(NetworkUtil.isAvailableNetwork(this)) { + if (NetworkUtil.isAvailableNetwork(this)) { if (dbSource.getSwitches().size() > 0) { @@ -189,12 +189,13 @@ 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() { @@ -242,7 +243,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"); } @@ -261,15 +262,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; } } } @@ -343,46 +344,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); } } @@ -403,7 +403,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()); @@ -424,12 +424,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",""); @@ -447,4 +447,14 @@ private void get() { e.printStackTrace(); } } + + public void initSimulationDB() { + //check to see if database is already populated + if(db.getCount() == 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(sw, 0); + } + } + } } From 6fd5232890e976cecb00d387545c71d7478c7208 Mon Sep 17 00:00:00 2001 From: Anesu Date: Tue, 12 Jan 2016 18:09:52 -0600 Subject: [PATCH 3/3] Added menu items --- app/build.gradle | 8 +- app/src/main/AndroidManifest.xml | 21 ++- .../java/com/score/homez/db/SwitchesDB.java | 109 +++++++------ .../main/java/com/score/homez/ui/AddHome.java | 126 +++++++++++++++ .../java/com/score/homez/ui/HelpPage.java | 100 ++++++++++++ .../java/com/score/homez/ui/HomeActivity.java | 93 ++++++++++- .../main/java/com/score/homez/ui/Homes.java | 122 +++++++++++++++ .../main/java/com/score/homez/ui/Modes.java | 146 ++++++++++++++++++ .../com/score/homez/ui/SwitchAdapter.java | 14 +- .../score/homez/utils/HomesListAdapter.java | 51 ++++++ app/src/main/res/layout/activity_add_home.xml | 26 ++++ .../main/res/layout/activity_help_page.xml | 12 ++ app/src/main/res/layout/activity_homes.xml | 10 ++ app/src/main/res/layout/activity_modes.xml | 101 ++++++++++++ app/src/main/res/layout/home_name_dialog.xml | 13 ++ app/src/main/res/layout/home_picker.xml | 24 +++ app/src/main/res/layout/home_row.xml | 36 +++++ app/src/main/res/menu/menu_main.xml | 29 +++- app/src/main/res/menu/menu_modes.xml | 4 + app/src/main/res/mipmap-xhdpi/ic_add.png | Bin 0 -> 108 bytes app/src/main/res/mipmap-xhdpi/ic_clear.png | Bin 0 -> 235 bytes app/src/main/res/values/strings.xml | 4 + senzc/build.gradle | 8 +- 23 files changed, 986 insertions(+), 71 deletions(-) create mode 100644 app/src/main/java/com/score/homez/ui/AddHome.java create mode 100644 app/src/main/java/com/score/homez/ui/HelpPage.java create mode 100644 app/src/main/java/com/score/homez/ui/Homes.java create mode 100644 app/src/main/java/com/score/homez/ui/Modes.java create mode 100644 app/src/main/java/com/score/homez/utils/HomesListAdapter.java create mode 100644 app/src/main/res/layout/activity_add_home.xml create mode 100644 app/src/main/res/layout/activity_help_page.xml create mode 100644 app/src/main/res/layout/activity_homes.xml create mode 100644 app/src/main/res/layout/activity_modes.xml create mode 100644 app/src/main/res/layout/home_name_dialog.xml create mode 100644 app/src/main/res/layout/home_picker.xml create mode 100644 app/src/main/res/layout/home_row.xml create mode 100644 app/src/main/res/menu/menu_modes.xml create mode 100644 app/src/main/res/mipmap-xhdpi/ic_add.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_clear.png diff --git a/app/build.gradle b/app/build.gradle index f17840b..6e7b75a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 21 - buildToolsVersion "21.1.2" + 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+' + 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 index b6967cd..d5a4e04 100644 --- a/app/src/main/java/com/score/homez/db/SwitchesDB.java +++ b/app/src/main/java/com/score/homez/db/SwitchesDB.java @@ -9,6 +9,7 @@ import com.score.homez.utils.Switch; +import java.lang.reflect.Array; import java.util.ArrayList; /** @@ -16,8 +17,8 @@ */ 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 DB_NAME = "Switches.db"; + private static final String TABLE_NAME = "Switches"; private static final String ID = "_id"; private static final String NAME = "name"; @@ -27,89 +28,109 @@ public class SwitchesDB extends SQLiteOpenHelper { private static final int NAME_INDEX = 2; private static final int STATUS_INDEX = 3; - Cursor cursor; - - private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY, " + - "TEXT," + NAME + " TEXT," + STATUS + " TEXT);"; - - private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; - public SwitchesDB(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { - db.execSQL(CREATE_TABLE); + + //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); - onCreate(db); } - public void addSwitch(String name, int status) - { + 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); + 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 SELECT_ALL = "SELECT * FROM " + TABLE_NAME; + 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); - this.cursor = cursor; - if(cursor != null) - { - if(cursor.moveToFirst()) - { - do - { - String name = cursor.getString(NAME_INDEX); + 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(name, id, status); + Switch aSwitch = new Switch(home_name, id, status); switches.add(aSwitch); } - while(cursor.moveToNext()); + while (cursor.moveToNext()); } - } - else - { + } else { return null; } return switches; } - public void toggleSwitch(int id, String name, int newState) - { + 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}); + content.put(STATUS, newState + ""); + db.update(table_name, content, NAME + "=?", new String[]{name}); } - public void clearDB() - { + public void clearDB(String name) { SQLiteDatabase db = this.getReadableDatabase(); - db.delete(TABLE_NAME, null, null); + db.delete(name, null, null); } - public int getCount() - { - String SELECT_ALL = "SELECT * FROM " + TABLE_NAME; - SQLiteDatabase db = this.getReadableDatabase(); - ArrayList switches = new ArrayList<>(); - Cursor cursor = db.rawQuery(SELECT_ALL, null); - return cursor.getCount(); + 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 0ea4fda..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,23 +16,33 @@ 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; @@ -39,6 +51,7 @@ import com.score.senzc.pojos.User; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -50,6 +63,7 @@ public class HomeActivity extends Activity implements View.OnClickListener { //put message variables private String lastSwitch; private String lastStatus; + private String table_name; private ListView list; SwitchesDB db; @@ -59,6 +73,9 @@ public class HomeActivity extends Activity implements View.OnClickListener { private boolean isResponseReceivedPut; private boolean isResponseReceivedGet; + ListView homes_list; + ImageButton add; + // we use custom font here private Typeface typeface; @@ -100,6 +117,7 @@ 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")); @@ -194,6 +212,7 @@ public void onClick(View tb) { } } + /** * } * Initialize UI components @@ -201,7 +220,7 @@ public void onClick(View tb) { private void initUi() { list = (ListView) findViewById(R.id.list_view); - SwitchAdapter adapter = new SwitchAdapter(this, R.layout.single_toggle, db.getAllSwitches()); + SwitchAdapter adapter = new SwitchAdapter(this, R.layout.single_toggle, table_name); list.setAdapter(adapter); } @@ -211,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("_"," ")); } /** @@ -448,13 +465,77 @@ private void get() { } } + 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() == 0) { + 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(sw, 0); + 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 index f0a1227..869de78 100644 --- a/app/src/main/java/com/score/homez/ui/SwitchAdapter.java +++ b/app/src/main/java/com/score/homez/ui/SwitchAdapter.java @@ -31,13 +31,21 @@ public class SwitchAdapter extends ArrayAdapter { ArrayList switches; SwitchesDB db; Typeface typeface; + String table; int prev = -1; - public SwitchAdapter(Context context, int resource, ArrayList switches) { + public SwitchAdapter(Context context, int resource,String table) { super(context, resource); this.context = context; + this.table = table; this.resource = resource; db = new SwitchesDB(context); - this.switches = db.getAllSwitches(); + 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"); } @@ -66,7 +74,7 @@ public View getView(final int position, View convertView, ViewGroup parent) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { toggle.setChecked(isChecked); setTitle(toggle.isChecked(), title, name); - db.toggleSwitch(position, name, isChecked==true ? 1 : 0); + db.toggleSwitch(table, name, isChecked==true ? 1 : 0); Log.i("state_changed", name + " has been toggled"); } }); 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/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 @@ + + + + + +