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.util.Log;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.widget.AdapterView;
35 import android.widget.AdapterView.OnItemClickListener;
36 import android.widget.ArrayAdapter;
37 import android.widget.Button;
38 import android.widget.DatePicker;
39 import android.widget.DatePicker.OnDateChangedListener;
40 import android.widget.EditText;
41 import android.widget.ListView;
42 import android.widget.TimePicker;
43 import android.widget.TimePicker.OnTimeChangedListener;
44 import android.widget.Toast;
47 * Activity for handling user's selection on UI for GroupApis.
50 public class GroupApiActivity extends Activity {
52 private ListView list;
53 private Button findGroup;
54 private ArrayAdapter<String> groupApis;
55 private ArrayList<String> groupApisList;
56 private static GroupApiActivity groupApiActivityObj;
57 private GroupClient groupClientObj;
58 private static Handler mHandler;
60 private static EditText logs;
61 private static String logMessage;
63 // For Scheduled ActionSet
64 public static Context mcontext;
65 public static Calendar scheduleTime;
67 private final String LOG_TAG = "[TMSample] " + this.getClass()
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73 setContentView(R.layout.groupapis);
75 groupApiActivityObj = this;
78 groupClientObj = new GroupClient();
79 groupApisList = new ArrayList<String>();
80 list = (ListView) findViewById(R.id.groupaApiList);
81 findGroup = (Button) findViewById(R.id.button1);
82 logs = (EditText) findViewById(R.id.EditText);
84 // adding the item to list that will be displayed on the UI.
85 groupApisList.add("1. Create ActionSet (ALLBULBON & ALLBULBOFF)");
86 groupApisList.add("2. Execute ActionSet (ALLBULBON)");
87 groupApisList.add("3. Execute ActionSet (AllBULBOFF)");
89 // Recursive GroupAction
90 groupApisList.add("4. Create ActionSet (Recursive_ALLBULBON)");
91 groupApisList.add(" 4.1 Execute ActionSet");
92 groupApisList.add(" 4.2 Cancel ActionSet");
94 // scheduled GroupAction
95 groupApisList.add("5. Create ActionSet (Scheduled_ALLBULBOFF)");
96 groupApisList.add(" 5.1 Execute ActionSet");
97 groupApisList.add(" 5.2 Cancel ActionSet");
99 groupApisList.add("6. Get ActionSet(ALLBULBOFF)");
100 groupApisList.add("7. Delete ActionSet(ALLBULBOFF)");
101 groupApisList.add("8. Find BookMark to Observe");
103 // handler for updating the UI i.e. MessageLog (TextBox) & ListView
104 mHandler = new Handler() {
106 public void handleMessage(Message msg) {
109 groupApis = new ArrayAdapter<String>(
111 android.R.layout.simple_list_item_1,
113 list.setAdapter(groupApis);
118 logs.setText(logMessage);
119 Log.i(LOG_TAG, logMessage);
123 setHandler(mHandler);
125 // find group Button Listener
126 findGroup.setOnClickListener(new OnClickListener() {
128 public void onClick(View v) {
129 groupClientObj.findGroup();
133 // Listener for item clicked by the user on the UI
134 list.setOnItemClickListener(new OnItemClickListener() {
136 public void onItemClick(AdapterView<?> parent, View view,
137 int position, long id) {
139 groupClientObj.createActionSetBulbOn();
140 groupClientObj.createActionSetBulbOff();
141 } else if (position == 1) {
142 groupClientObj.executeActionSetBulbOn(0);
143 } else if (position == 2) {
144 groupClientObj.executeActionSetBulbOff(0);
145 } else if (position == 3) {
146 groupClientObj.createRecursiveActionSetBulbOn();
147 } else if (position == 4) {
148 groupClientObj.executeRecursiveActionSetBulbOn(0);
149 } else if (position == 5) {
150 groupClientObj.cancelRecursiveActionSetBulbOn();
151 } else if (position == 6) {
152 showDateAndTimeDialog();
153 } else if (position == 7) {
154 groupClientObj.executeScheduledActionSetBulbOff(0);
155 } else if (position == 8) {
156 groupClientObj.cancelScheduledActionSetBulbOff();
157 } else if (position == 9) {
158 groupClientObj.getActionSetBulbOff();
159 } else if (position == 10) {
160 groupClientObj.deleteActionSetBulbOff();
161 } else if (position == 11) {
162 groupClientObj.findBookMarkResources();
167 // creating group and find light resources
168 groupClientObj.createGroup();
169 groupClientObj.findLightResources();
172 public void showDateAndTimeDialog() {
173 // for scheduled actionSet
174 scheduleTime = Calendar.getInstance();
176 final Dialog dialog = new Dialog(mcontext);
177 dialog.setContentView(R.layout.custom_dialog);
178 dialog.setTitle("Choose date and time for Secheduling");
180 TimePicker tp = (TimePicker) dialog.findViewById(R.id.timePicker1);
181 DatePicker dp = (DatePicker) dialog.findViewById(R.id.datePicker1);
182 Button ok = (Button) dialog.findViewById(R.id.ok);
183 Button cancel = (Button) dialog.findViewById(R.id.cancel);
185 dialog.setCancelable(false);
187 tp.setOnTimeChangedListener(new OnTimeChangedListener() {
189 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
190 scheduleTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
191 scheduleTime.set(Calendar.MINUTE, minute);
194 dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
195 new OnDateChangedListener() {
197 public void onDateChanged(DatePicker arg0, int arg1,
198 int arg2, int arg3) {
199 scheduleTime.set(arg1, arg2, arg3);
202 ok.setOnClickListener(new OnClickListener() {
204 public void onClick(View v) {
207 // Calculate the time difference in delay
208 Calendar localTime = Calendar.getInstance();
209 if (scheduleTime.compareTo(localTime) != 1) {
211 .displayToastMessage("Invalid set time!");
215 long delay = scheduleTime.getTimeInMillis()
216 - localTime.getTimeInMillis();
219 groupClientObj.createScheduledActionSetBulbOff(delay);
222 cancel.setOnClickListener(new OnClickListener() {
224 public void onClick(View v) {
230 public static void setMessageLog(String message) {
231 logMessage = message;
235 public void onBackPressed() {
236 super.onBackPressed();
237 // unregister the resource and set callback listener to null
238 groupClientObj.leaveGroup();
241 // for update UI these functions will be called from GroupClient Class
242 public static GroupApiActivity getGroupApiActivityObj() {
243 return groupApiActivityObj;
246 public Handler getHandler() {
250 public void setHandler(Handler mHandler) {
251 GroupApiActivity.mHandler = mHandler;
254 public void displayToastMessage(String message) {
255 Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
257 Log.i(LOG_TAG, message);