Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / Sample / src / com / tm / sample / GroupApiActivity.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20 package com.tm.sample;
21
22 import java.util.ArrayList;
23 import java.util.Calendar;
24
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;
45
46 /*
47  * Activity for handling user's selection on UI for GroupApis.
48  * & for updating UI.
49  */
50 public class GroupApiActivity extends Activity {
51
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;
59
60     private static EditText         logs;
61     private static String           logMessage;
62
63     // For Scheduled ActionSet
64     public static Context           mcontext;
65     public static Calendar          scheduleTime;
66     
67     private final String            LOG_TAG = "[TMSample] " + this.getClass()
68                                                                .getSimpleName();;
69
70     @Override
71     protected void onCreate(Bundle savedInstanceState) {
72         super.onCreate(savedInstanceState);
73         setContentView(R.layout.groupapis);
74
75         groupApiActivityObj = this;
76         mcontext = this;
77
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);
83
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)");
88
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");
93
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");
98
99         groupApisList.add("6. Get ActionSet(ALLBULBOFF)");
100         groupApisList.add("7. Delete ActionSet(ALLBULBOFF)");
101         groupApisList.add("8. Find BookMark to Observe");
102
103         // handler for updating the UI i.e. MessageLog (TextBox) & ListView
104         mHandler = new Handler() {
105             @Override
106             public void handleMessage(Message msg) {
107                 switch (msg.what) {
108                     case 0:
109                         groupApis = new ArrayAdapter<String>(
110                                 groupApiActivityObj,
111                                 android.R.layout.simple_list_item_1,
112                                 groupApisList);
113                         list.setAdapter(groupApis);
114                         list.bringToFront();
115                         break;
116                     case 1:
117                         logs.setText("");
118                         logs.setText(logMessage);
119                         Log.i(LOG_TAG, logMessage);
120                 }
121             }
122         };
123         setHandler(mHandler);
124
125         // find group Button Listener
126         findGroup.setOnClickListener(new OnClickListener() {
127             @Override
128             public void onClick(View v) {
129                 groupClientObj.findGroup();
130             }
131         });
132
133         // Listener for item clicked by the user on the UI
134         list.setOnItemClickListener(new OnItemClickListener() {
135             @Override
136             public void onItemClick(AdapterView<?> parent, View view,
137                     int position, long id) {
138                 if (position == 0) {
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();
163                 }
164             }
165         });
166
167         // creating group and find light resources
168         groupClientObj.createGroup();
169         groupClientObj.findLightResources();
170     }
171
172     public void showDateAndTimeDialog() {
173         // for scheduled actionSet
174         scheduleTime = Calendar.getInstance();
175
176         final Dialog dialog = new Dialog(mcontext);
177         dialog.setContentView(R.layout.custom_dialog);
178         dialog.setTitle("Choose date and time for Secheduling");
179
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);
184
185         dialog.setCancelable(false);
186         dialog.show();
187         tp.setOnTimeChangedListener(new OnTimeChangedListener() {
188             @Override
189             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
190                 scheduleTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
191                 scheduleTime.set(Calendar.MINUTE, minute);
192             }
193         });
194         dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
195                 new OnDateChangedListener() {
196                     @Override
197                     public void onDateChanged(DatePicker arg0, int arg1,
198                             int arg2, int arg3) {
199                         scheduleTime.set(arg1, arg2, arg3);
200                     }
201                 });
202         ok.setOnClickListener(new OnClickListener() {
203             @Override
204             public void onClick(View v) {
205                 dialog.dismiss();
206
207                 // Calculate the time difference in delay
208                 Calendar localTime = Calendar.getInstance();
209                 if (scheduleTime.compareTo(localTime) != 1) {
210                     groupApiActivityObj
211                             .displayToastMessage("Invalid set time!");
212                     return;
213                 }
214
215                 long delay = scheduleTime.getTimeInMillis()
216                         - localTime.getTimeInMillis();
217                 delay /= 1000;
218
219                 groupClientObj.createScheduledActionSetBulbOff(delay);
220             }
221         });
222         cancel.setOnClickListener(new OnClickListener() {
223             @Override
224             public void onClick(View v) {
225                 dialog.dismiss();
226             }
227         });
228     }
229
230     public static void setMessageLog(String message) {
231         logMessage = message;
232     }
233
234     @Override
235     public void onBackPressed() {
236         super.onBackPressed();
237         // unregister the resource and set callback listener to null
238         groupClientObj.leaveGroup();
239     }
240
241     // for update UI these functions will be called from GroupClient Class
242     public static GroupApiActivity getGroupApiActivityObj() {
243         return groupApiActivityObj;
244     }
245
246     public Handler getHandler() {
247         return mHandler;
248     }
249
250     public void setHandler(Handler mHandler) {
251         GroupApiActivity.mHandler = mHandler;
252     }
253
254     public void displayToastMessage(String message) {
255         Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
256         toast.show();
257         Log.i(LOG_TAG, message);
258     }
259 }