Imported Upstream version 0.9.1
[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.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;
44
45 /*
46  * Activity for handling user's selection on UI for GroupApis.
47  * & for updating UI.
48  */
49 public class GroupApiActivity extends Activity {
50
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;
58
59     private static EditText         logs;
60     private static String           logMessage;
61
62     // For Scheduled ActionSet
63     public static Context           mcontext;
64     public static Calendar          scheduleTime;
65
66     @Override
67     protected void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         setContentView(R.layout.groupapis);
70
71         groupApiActivityObj = this;
72         mcontext = this;
73
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);
79
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)");
84
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");
89
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");
94
95         groupApisList.add("6. Get ActionSet(ALLBULBOFF)");
96         groupApisList.add("7. Delete ActionSet(ALLBULBOFF)");
97         groupApisList.add("8. Find BookMark to Observe");
98
99         // handler for updating the UI i.e. MessageLog (TextBox) & ListView
100         mHandler = new Handler() {
101             @Override
102             public void handleMessage(Message msg) {
103                 switch (msg.what) {
104                     case 0:
105                         groupApis = new ArrayAdapter<String>(
106                                 groupApiActivityObj,
107                                 android.R.layout.simple_list_item_1,
108                                 groupApisList);
109                         list.setAdapter(groupApis);
110                         list.bringToFront();
111                         break;
112                     case 1:
113                         logs.setText("");
114                         logs.setText(logMessage);
115                 }
116             }
117         };
118         setHandler(mHandler);
119
120         // find group Button Listener
121         findGroup.setOnClickListener(new OnClickListener() {
122             @Override
123             public void onClick(View v) {
124                 groupClientObj.findGroup();
125             }
126         });
127
128         // Listener for item clicked by the user on the UI
129         list.setOnItemClickListener(new OnItemClickListener() {
130             @Override
131             public void onItemClick(AdapterView<?> parent, View view,
132                     int position, long id) {
133                 if (position == 0) {
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();
158                 }
159             }
160         });
161
162         // creating group and find light resources
163         groupClientObj.createGroup();
164         groupClientObj.findLightResources();
165     }
166
167     public void showDateAndTimeDialog() {
168         // for scheduled actionSet
169         scheduleTime = Calendar.getInstance();
170
171         final Dialog dialog = new Dialog(mcontext);
172         dialog.setContentView(R.layout.custom_dialog);
173         dialog.setTitle("Choose date and time for Secheduling");
174
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);
179
180         dialog.setCancelable(false);
181         dialog.show();
182         tp.setOnTimeChangedListener(new OnTimeChangedListener() {
183             @Override
184             public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
185                 scheduleTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
186                 scheduleTime.set(Calendar.MINUTE, minute);
187             }
188         });
189         dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
190                 new OnDateChangedListener() {
191                     @Override
192                     public void onDateChanged(DatePicker arg0, int arg1,
193                             int arg2, int arg3) {
194                         scheduleTime.set(arg1, arg2, arg3);
195                     }
196                 });
197         ok.setOnClickListener(new OnClickListener() {
198             @Override
199             public void onClick(View v) {
200                 dialog.dismiss();
201
202                 // Calculate the time difference in delay
203                 Calendar localTime = Calendar.getInstance();
204                 if (scheduleTime.compareTo(localTime) != 1) {
205                     groupApiActivityObj
206                             .displayToastMessage("Invalid set time!");
207                     return;
208                 }
209
210                 long delay = scheduleTime.getTimeInMillis()
211                         - localTime.getTimeInMillis();
212                 delay /= 1000;
213
214                 groupClientObj.createScheduledActionSetBulbOff(delay);
215             }
216         });
217         cancel.setOnClickListener(new OnClickListener() {
218             @Override
219             public void onClick(View v) {
220                 dialog.dismiss();
221             }
222         });
223     }
224
225     public static void setMessageLog(String message) {
226         logMessage = message;
227     }
228
229     @Override
230     public void onBackPressed() {
231         super.onBackPressed();
232         // unregister the resource and set callback listener to null
233         groupClientObj.leaveGroup();
234     }
235
236     // for update UI these functions will be called from GroupClient Class
237     public static GroupApiActivity getGroupApiActivityObj() {
238         return groupApiActivityObj;
239     }
240
241     public Handler getHandler() {
242         return mHandler;
243     }
244
245     public void setHandler(Handler mHandler) {
246         GroupApiActivity.mHandler = mHandler;
247     }
248
249     public void displayToastMessage(String message) {
250         Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
251         toast.show();
252     }
253 }