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