1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 package com.tm.sample;
22 import java.util.ArrayList;
23 import java.util.Calendar;
25 import android.app.Activity;
26 import android.app.Dialog;
27 import android.content.Context;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.Message;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.AdapterView;
34 import android.widget.AdapterView.OnItemClickListener;
35 import android.widget.ArrayAdapter;
36 import android.widget.Button;
37 import android.widget.DatePicker;
38 import android.widget.DatePicker.OnDateChangedListener;
39 import android.widget.EditText;
40 import android.widget.ListView;
41 import android.widget.TimePicker;
42 import android.widget.TimePicker.OnTimeChangedListener;
43 import android.widget.Toast;
46 * Activity for handling user's selection on UI for GroupApis.
49 public class GroupApiActivity extends Activity {
51 private ListView list;
52 private Button findGroup;
53 private ArrayAdapter<String> groupApis;
54 private ArrayList<String> groupApisList;
55 private static GroupApiActivity groupApiActivityObj;
56 private GroupClient groupClientObj;
57 private static Handler mHandler;
59 private static EditText logs;
60 private static String logMessage;
62 // For Scheduled ActionSet
63 public static Context mcontext;
64 public static Calendar scheduleTime;
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 setContentView(R.layout.groupapis);
71 groupApiActivityObj = this;
74 groupClientObj = new GroupClient();
75 groupApisList = new ArrayList<String>();
76 list = (ListView) findViewById(R.id.groupaApiList);
77 findGroup = (Button) findViewById(R.id.button1);
78 logs = (EditText) findViewById(R.id.EditText);
80 // adding the item to list that will be displayed on the UI.
81 groupApisList.add("1. Create ActionSet (ALLBULBON & ALLBULBOFF)");
82 groupApisList.add("2. Execute ActionSet (ALLBULBON)");
83 groupApisList.add("3. Execute ActionSet (AllBULBOFF)");
85 // Recursive GroupAction
86 groupApisList.add("4. Create ActionSet (Recursive_ALLBULBON)");
87 groupApisList.add(" 4.1 Execute ActionSet");
88 groupApisList.add(" 4.2 Cancel ActionSet");
90 // scheduled GroupAction
91 groupApisList.add("5. Create ActionSet (Scheduled_ALLBULBOFF)");
92 groupApisList.add(" 5.1 Execute ActionSet");
93 groupApisList.add(" 5.2 Cancel ActionSet");
95 groupApisList.add("6. Get ActionSet(ALLBULBOFF)");
96 groupApisList.add("7. Delete ActionSet(ALLBULBOFF)");
97 groupApisList.add("8. Find BookMark to Observe");
99 // handler for updating the UI i.e. MessageLog (TextBox) & ListView
100 mHandler = new Handler() {
102 public void handleMessage(Message msg) {
105 groupApis = new ArrayAdapter<String>(
107 android.R.layout.simple_list_item_1,
109 list.setAdapter(groupApis);
114 logs.setText(logMessage);
118 setHandler(mHandler);
120 // find group Button Listener
121 findGroup.setOnClickListener(new OnClickListener() {
123 public void onClick(View v) {
124 groupClientObj.findGroup();
128 // Listener for item clicked by the user on the UI
129 list.setOnItemClickListener(new OnItemClickListener() {
131 public void onItemClick(AdapterView<?> parent, View view,
132 int position, long id) {
134 groupClientObj.createActionSetBulbOn();
135 groupClientObj.createActionSetBulbOff();
136 } else if (position == 1) {
137 groupClientObj.executeActionSetBulbOn(0);
138 } else if (position == 2) {
139 groupClientObj.executeActionSetBulbOff(0);
140 } else if (position == 3) {
141 groupClientObj.createRecursiveActionSetBulbOn();
142 } else if (position == 4) {
143 groupClientObj.executeRecursiveActionSetBulbOn(0);
144 } else if (position == 5) {
145 groupClientObj.cancelRecursiveActionSetBulbOn();
146 } else if (position == 6) {
147 showDateAndTimeDialog();
148 } else if (position == 7) {
149 groupClientObj.executeScheduledActionSetBulbOff(0);
150 } else if (position == 8) {
151 groupClientObj.cancelScheduledActionSetBulbOff();
152 } else if (position == 9) {
153 groupClientObj.getActionSetBulbOff();
154 } else if (position == 10) {
155 groupClientObj.deleteActionSetBulbOff();
156 } else if (position == 11) {
157 groupClientObj.findBookMarkResources();
162 // creating group and find light resources
163 groupClientObj.createGroup();
164 groupClientObj.findLightResources();
167 public void showDateAndTimeDialog() {
168 // for scheduled actionSet
169 scheduleTime = Calendar.getInstance();
171 final Dialog dialog = new Dialog(mcontext);
172 dialog.setContentView(R.layout.custom_dialog);
173 dialog.setTitle("Choose date and time for Secheduling");
175 TimePicker tp = (TimePicker) dialog.findViewById(R.id.timePicker1);
176 DatePicker dp = (DatePicker) dialog.findViewById(R.id.datePicker1);
177 Button ok = (Button) dialog.findViewById(R.id.ok);
178 Button cancel = (Button) dialog.findViewById(R.id.cancel);
180 dialog.setCancelable(false);
182 tp.setOnTimeChangedListener(new OnTimeChangedListener() {
184 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
185 scheduleTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
186 scheduleTime.set(Calendar.MINUTE, minute);
189 dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
190 new OnDateChangedListener() {
192 public void onDateChanged(DatePicker arg0, int arg1,
193 int arg2, int arg3) {
194 scheduleTime.set(arg1, arg2, arg3);
197 ok.setOnClickListener(new OnClickListener() {
199 public void onClick(View v) {
202 // Calculate the time difference in delay
203 Calendar localTime = Calendar.getInstance();
204 if (scheduleTime.compareTo(localTime) != 1) {
206 .displayToastMessage("Invalid set time!");
210 long delay = scheduleTime.getTimeInMillis()
211 - localTime.getTimeInMillis();
214 groupClientObj.createScheduledActionSetBulbOff(delay);
217 cancel.setOnClickListener(new OnClickListener() {
219 public void onClick(View v) {
225 public static void setMessageLog(String message) {
226 logMessage = message;
230 public void onBackPressed() {
231 super.onBackPressed();
232 // unregister the resource and set callback listener to null
233 groupClientObj.leaveGroup();
236 // for update UI these functions will be called from GroupClient Class
237 public static GroupApiActivity getGroupApiActivityObj() {
238 return groupApiActivityObj;
241 public Handler getHandler() {
245 public void setHandler(Handler mHandler) {
246 GroupApiActivity.mHandler = mHandler;
249 public void displayToastMessage(String message) {
250 Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);