]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - client/AndroidClients/LightingController/src/com/lightingcontroller/sceneSelect.java
add support for control bridge FW
[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / sceneSelect.java
1 /**************************************************************************************************
2   Filename:       sceneSelect.java
3   Revised:        $$
4   Revision:       $$
6   Description:    ZLL UI
8   Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions
13    are met:
15      Redistributions of source code must retain the above copyright
16      notice, this list of conditions and the following disclaimer.
18      Redistributions in binary form must reproduce the above copyright
19      notice, this list of conditions and the following disclaimer in the
20      documentation and/or other materials provided with the
21      distribution.
23      Neither the name of Texas Instruments Incorporated nor the names of
24      its contributors may be used to endorse or promote products derived
25      from this software without specific prior written permission.
27    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 **************************************************************************************************/
40 package com.lightingcontroller;
42 import java.util.ArrayList;
43 import java.util.List;
45 import com.lightingcontroller.R;
46 import com.lightingcontroller.Zigbee.ZigbeeAssistant;
47 import com.lightingcontroller.Zigbee.ZigbeeGroup;
48 import com.lightingcontroller.Zigbee.ZigbeeScene;
50 import android.app.Activity;
51 import android.app.AlertDialog;
52 import android.app.ProgressDialog;
53 import android.content.DialogInterface;
54 import android.os.AsyncTask;
55 import android.os.Bundle;
56 import android.view.View;
57 import android.widget.AdapterView;
58 import android.widget.ArrayAdapter;
59 import android.widget.EditText;
60 import android.widget.Spinner;
61 import android.widget.AdapterView.OnItemSelectedListener;
63 public class sceneSelect extends Activity {
65         private static int currGroup = 0;
66         private static Spinner groupSpinner;
67         private static ArrayAdapter<String> groupSpinnerAdapter;
68         
69         private static int currScene;
70         private Spinner sceneSpinner;
71         private ArrayAdapter<String> sceneSpinnerAdapter;
72         
73         ProgressDialog bar;
74         
75     @Override
76     public void onCreate(Bundle savedInstanceState) {
77         super.onCreate(savedInstanceState);
78         this.overridePendingTransition(R.anim.animation_enter_l, R.anim.animation_leave_r);  
79                 
80         setContentView(R.layout.sceneview);        
81           
82         List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();
83         List<String> groupNameList = new ArrayList<String>();
84         for (int i = 0 ; i < groupList.size() ; i++)
85         {
86                 groupNameList.add(groupList.get(i).getGroupName());        
87         }                 
88         groupSpinner = (Spinner) findViewById(R.id.groupSpinner);
89         groupSpinnerAdapter = new ArrayAdapter<String>(this,
90                         android.R.layout.simple_spinner_item, groupNameList);
91         groupSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
92         groupSpinner.setAdapter(groupSpinnerAdapter);
93                 
94         addItemsOnGroupSpinner();
95         addListenerOnGroupSpinnerItemSelection();  
97         List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();
98         List<String> sceneNameList = new ArrayList<String>();
99         for (int i = 0 ; i < sceneList.size() ; i++)
100         {
101                 sceneNameList.add(sceneList.get(i).getSceneName());        
102         }                 
103         sceneSpinner = (Spinner) findViewById(R.id.sceneSelectSpinner);
104         sceneSpinnerAdapter = new ArrayAdapter<String>(this,
105                         android.R.layout.simple_spinner_item, sceneNameList);
106         sceneSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
107         sceneSpinner.setAdapter(sceneSpinnerAdapter);        
109         addItemsOnSceneSpinner();
110         addListenerOnSceneSpinnerItemSelection();       
111     }    
113     // add items into spinner dynamically
114     public void addItemsOnGroupSpinner() {          
115         List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();
116         groupSpinnerAdapter.clear();
117         for (int i = 0 ; i < groupList.size() ; i++)
118         {               
119                 groupSpinnerAdapter.add(groupList.get(i).getGroupName());
120         }
121     }
122    
123     public void addListenerOnGroupSpinnerItemSelection() {
124         groupSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
125             @Override
126             public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
127                 currGroup = position;
128                 currScene = 0;
129                 //change the scene spinner to reflect the new group 
130                 addItemsOnSceneSpinner();
131           }
132          
133           @Override
134           public void onNothingSelected(AdapterView<?> arg0) {
135                 // TODO Auto-generated method stub
136                 currGroup = -1;  
137           }
138         });
139     }
140     
141     // add items into spinner dynamically
142     public void addItemsOnSceneSpinner() {          
143         List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();
144         List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();
145         sceneSpinnerAdapter.clear();
146         if(currGroup != -1)
147         {               
148                 for (int i = 0 ; i < sceneList.size() ; i++)
149                 {    
150                         if(groupList.get(currGroup).getGroupId() == sceneList.get(i).getGroupId())
151                         {
152                                 sceneSpinnerAdapter.add(sceneList.get(i).getSceneName());
153                         }
154                 }
155         }
156     }
157    
158     public void addListenerOnSceneSpinnerItemSelection() {
159         sceneSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
160             @Override
161             public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
162                 currScene = position;
163           }
164          
165           @Override
166           public void onNothingSelected(AdapterView<?> arg0) {
167                 // TODO Auto-generated method stub
168                 currScene = -1;  
169           }
170         });
171     }    
174     public void newSceneButton(View view) {
176         final EditText t = new EditText(this);
178         new AlertDialog.Builder(this)
179                 .setTitle("Create Scene")
180                 .setMessage("Enter the new scene name")
181                 .setView(t)
182                 .setPositiveButton("OK",          
183                 new DialogInterface.OnClickListener()
184                 {                       
185                         public void onClick(DialogInterface dialoginterface,int i){     
186                                 List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();                              
187                                 ZigbeeAssistant.newScene(t.getText().toString(), groupList.get(currGroup).getGroupId() );                                                       
188                         //set selection to the new group, which will be last as this is a new one                                               
189                         currScene = sceneSpinnerAdapter.getCount();                                                                             
190                         sceneSpinner.setSelection(currScene);
191                         //update the spinner
192                         addItemsOnSceneSpinner();                                                                                   
193                         }               
194                 })                              
195                 .setNegativeButton("Cancel", null)
196                 .show();                
197         }                 
199     public void sceneStoreButton(View view) {
200         
201         if(currGroup != -1)
202         {
203                 AlertDialog show = new AlertDialog.Builder(sceneSelect.this)
204                         .setTitle("Store Scene to group")
205                         .setMessage("Add Scene Selected Light to Group " + currGroup)
206                         .setPositiveButton("OK",             
207                         new DialogInterface.OnClickListener()
208                         {                       
209                                 public void onClick(DialogInterface dialoginterface,int i){     
210                                         List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();
211                                         List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();                                                                      
212                                         ZigbeeAssistant.storeScene(sceneList.get(currScene).getSceneName(), groupList.get(currGroup).getGroupId() );                                            
213                                 }               
214                         })                              
215                         .setNegativeButton("Cancel", null)
216                         .show();                                
217         }
218         else
219         {
220                 AlertDialog show = new AlertDialog.Builder(sceneSelect.this)
221                         .setTitle("Store Scene to group")
222                         .setMessage("No scene selected ")
223                         .setPositiveButton("OK",             
224                         new DialogInterface.OnClickListener()
225                         {                       
226                                 public void onClick(DialogInterface dialoginterface,int i){                                     
227                                 }               
228                         })                              
229                         .show();                                
230         }               
232     }     
234     public void sceneRestoreButton(View view) {
235                 List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();      
236                 List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();      
237                 ZigbeeAssistant.recallScene(sceneList.get(currScene).getSceneName(), groupList.get(currGroup).getGroupId() );
238     }