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] "
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.groupapis);
76 groupApiActivityObj = this;
79 groupClientObj = new GroupClient();
80 groupApisList = new ArrayList<String>();
81 list = (ListView) findViewById(R.id.groupaApiList);
82 findGroup = (Button) findViewById(R.id.button1);
83 logs = (EditText) findViewById(R.id.EditText);
85 // adding the item to list that will be displayed on the UI.
86 groupApisList.add("1. Create ActionSet (ALLBULBON & ALLBULBOFF)");
87 groupApisList.add("2. Execute ActionSet (ALLBULBON)");
88 groupApisList.add("3. Execute ActionSet (AllBULBOFF)");
90 // Recursive GroupAction
91 groupApisList.add("4. Create ActionSet (Recursive_ALLBULBON)");
92 groupApisList.add(" 4.1 Execute ActionSet");
93 groupApisList.add(" 4.2 Cancel ActionSet");
95 // scheduled GroupAction
96 groupApisList.add("5. Create ActionSet (Scheduled_ALLBULBOFF)");
97 groupApisList.add(" 5.1 Execute ActionSet");
98 groupApisList.add(" 5.2 Cancel ActionSet");
100 groupApisList.add("6. Get ActionSet(ALLBULBOFF)");
101 groupApisList.add("7. Delete ActionSet(ALLBULBOFF)");
102 groupApisList.add("8. Find BookMark to Observe");
104 // handler for updating the UI i.e. MessageLog (TextBox) & ListView
105 mHandler = new Handler() {
107 public void handleMessage(Message msg) {
110 groupApis = new ArrayAdapter<String>(
112 android.R.layout.simple_list_item_1,
114 list.setAdapter(groupApis);
119 logs.setText(logMessage);
120 Log.i(LOG_TAG, logMessage);
124 setHandler(mHandler);
126 // find group Button Listener
127 findGroup.setOnClickListener(new OnClickListener() {
129 public void onClick(View v) {
130 groupClientObj.findGroup();
134 // Listener for item clicked by the user on the UI
135 list.setOnItemClickListener(new OnItemClickListener() {
137 public void onItemClick(AdapterView<?> parent, View view,
138 int position, long id) {
140 groupClientObj.createActionSetBulbOn();
141 groupClientObj.createActionSetBulbOff();
142 } else if (position == 1) {
143 groupClientObj.executeActionSetBulbOn(0);
144 } else if (position == 2) {
145 groupClientObj.executeActionSetBulbOff(0);
146 } else if (position == 3) {
147 groupClientObj.createRecursiveActionSetBulbOn();
148 } else if (position == 4) {
149 groupClientObj.executeRecursiveActionSetBulbOn(0);
150 } else if (position == 5) {
151 groupClientObj.cancelRecursiveActionSetBulbOn();
152 } else if (position == 6) {
153 showDateAndTimeDialog();
154 } else if (position == 7) {
155 groupClientObj.executeScheduledActionSetBulbOff(0);
156 } else if (position == 8) {
157 groupClientObj.cancelScheduledActionSetBulbOff();
158 } else if (position == 9) {
159 groupClientObj.getActionSetBulbOff();
160 } else if (position == 10) {
161 groupClientObj.deleteActionSetBulbOff();
162 } else if (position == 11) {
163 groupClientObj.findBookMarkResources();
168 // creating group and find light resources
169 groupClientObj.createGroup();
170 groupClientObj.findLightResources();
173 public void showDateAndTimeDialog() {
174 // for scheduled actionSet
175 scheduleTime = Calendar.getInstance();
177 final Dialog dialog = new Dialog(mcontext);
178 dialog.setContentView(R.layout.custom_dialog);
179 dialog.setTitle("Choose date and time for Secheduling");
181 TimePicker tp = (TimePicker) dialog.findViewById(R.id.timePicker1);
182 DatePicker dp = (DatePicker) dialog.findViewById(R.id.datePicker1);
183 Button ok = (Button) dialog.findViewById(R.id.ok);
184 Button cancel = (Button) dialog.findViewById(R.id.cancel);
186 dialog.setCancelable(false);
188 tp.setOnTimeChangedListener(new OnTimeChangedListener() {
190 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
191 scheduleTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
192 scheduleTime.set(Calendar.MINUTE, minute);
195 dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
196 new OnDateChangedListener() {
198 public void onDateChanged(DatePicker arg0, int arg1,
199 int arg2, int arg3) {
200 scheduleTime.set(arg1, arg2, arg3);
203 ok.setOnClickListener(new OnClickListener() {
205 public void onClick(View v) {
208 // Calculate the time difference in delay
209 Calendar localTime = Calendar.getInstance();
210 if (scheduleTime.compareTo(localTime) != 1) {
212 .displayToastMessage("Invalid set time!");
216 long delay = scheduleTime.getTimeInMillis()
217 - localTime.getTimeInMillis();
220 groupClientObj.createScheduledActionSetBulbOff(delay);
223 cancel.setOnClickListener(new OnClickListener() {
225 public void onClick(View v) {
231 public static void setMessageLog(String message) {
232 logMessage = message;
236 public void onBackPressed() {
237 super.onBackPressed();
238 // unregister the resource and set callback listener to null
239 groupClientObj.leaveGroup();
242 // for update UI these functions will be called from GroupClient Class
243 public static GroupApiActivity getGroupApiActivityObj() {
244 return groupApiActivityObj;
247 public Handler getHandler() {
251 public void setHandler(Handler mHandler) {
252 GroupApiActivity.mHandler = mHandler;
255 public void displayToastMessage(String message) {
256 Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
258 Log.i(LOG_TAG, message);